b3e51cf0 by Adam Heath

A bit of refactoring on the static solr response data; also, the start

of Solr spec.
1 parent b32c60af
......@@ -3,39 +3,8 @@ define(function(require) {
var _ = require('underscore');
var Backbone = require('backbone');
var Facets = require('solr/model/Facets');
function setData(obj, key, value) {
return obj[key] = value;
}
function getData(obj, key) {
return obj[key];
}
function createSolrResponse(has) {
var solrData = {};
var facetCounts = setData(solrData, 'facet_counts', {});
var facetIntervals = setData(facetCounts, 'facet_intervals', {});
var facetRanges = setData(facetCounts, 'facet_ranges', {});
var facetFields = setData(facetCounts, 'facet_fields', {});
var facetQueries = setData(facetCounts, 'facet_queries', {});
if (has.stats) {
solrData.stats = {};
setData(solrData.stats, 'stats_fields', {});
}
return solrData;
}
function makeSolrResponseMixedCase(solrData) {
var facetCounts = getData(solrData, 'facet_counts');
return {
facetCounts: facetCounts,
facetRanges: getData(facetCounts, 'facet_ranges'),
facetFields: getData(facetCounts, 'facet_fields'),
facetQueries: getData(facetCounts, 'facet_queries'),
statsFields: getData(solrData.stats, 'stats_fields'),
};
}
var Util = require('solr/model/Util');
var SolrSpecData = require('solr/model/Solr.spec.data');
describe('Facets', function() {
it('loads', function() {
......@@ -54,50 +23,8 @@ define(function(require) {
origNow = _.now;
_.now = function() { return new Date().getTime(); };
spyOn(Backbone, 'ajax');
var facets = {
model: new Facets.Facet({
formName: 'model',
label: 'Model',
}, {
queryField: 'model_s',
facetType: 'field',
bins: {
misc: ['saturn', 'jeep'],
startsWithB: ['buick'],
},
}),
year: new Facets.Facet({
formName: 'year',
label: 'Year',
}, {
queryField: 'year_d',
comparator: Facets.Sort.date,
facetType: 'year',
facetStats: true,
}),
dateOfBirth: new Facets.Facet({
formName: 'dateOfBirth',
label: 'Birthday',
}, {
queryField: 'dateOfBirth_d',
comparator: Facets.Sort.date,
facetType: 'year',
facetStats: true,
other: 'before',
start: 'NOW/YEAR-25YEAR',
}),
price: new Facets.Facet({
formName: 'price',
label: 'Price',
}, {
queryField: 'price_i',
comparator: Facets.Sort.number,
facetType: 'field',
facetStats: true,
}),
};
onItemChange = jasmine.createSpy('onItemChange');
f = new Facets(facets, {search: {url: '/'}});
f = new Facets(SolrSpecData.facetsBuilder(), {search: {url: '/'}});
f.on('item-change', onItemChange);
});
afterEach(function() {
......@@ -113,95 +40,45 @@ define(function(require) {
beforeEach(function() {
formData.push(f.getFacetFormData());
});
function expectItem(item, key, value, checked) {
expect(item.get('key')).toEqual(key);
expect(item.get('value')).toEqual(value);
expect(item.get('checked')).toEqual(checked);
}
describe('facets', function() {
var modelFacet, modelItems;
var yearFacet, yearItems;
var dobFacet, dobItems;
var priceFacet, priceItems;
var valueFacet, valueItems;
beforeEach(function() {
var solrResponse = createSolrResponse({stats: true});
var solrData = makeSolrResponseMixedCase(solrResponse);
//console.log('solrData', JSON.stringify(solrResponse));
solrData.facetRanges.year = {
counts: [
'2000-01-01T00:00:00.000Z', 1,
'2001-01-01T00:00:00.000Z', 4,
'2002-01-01T00:00:00.000Z', 0,
'2003-01-01T00:00:00.000Z', 3,
'2004-01-01T00:00:00.000Z', 0,
'2005-01-01T00:00:00.000Z', 0,
'2006-01-01T00:00:00.000Z', 0,
'2007-01-01T00:00:00.000Z', 0,
'2008-01-01T00:00:00.000Z', 7,
'2009-01-01T00:00:00.000Z', 0,
'2010-01-01T00:00:00.000Z', 1,
],
};
solrData.facetRanges.dateOfBirth = {
counts: [
'1974-01-01T00:00:00.000Z', 1,
'1972-01-01T00:00:00.000Z', 1,
'1970-01-01T00:00:00.000Z', 1,
],
before: 5,
};
solrData.facetFields.model = [
'ford', 23,
'chevy', 18,
'toyota', 5,
'saturn', 7, // keep here to test the bin/exclude feature
'jeep', 19, // keep here to test the bin/exclude feature
'buick', 0, // keep here to test the bin/exclude feature
'dodge', 0,
];
solrData.facetFields.price = [
'23456', 1,
'5678', 2,
'1234', 3,
];
solrData.facetQueries['model:misc'] = 26;
solrData.facetQueries['model:startsWithB'] = 0;
solrData.statsFields.year = {min: '2000', max: '2010'};
solrData.statsFields.dateOfBirth = {min: '2000', max: '2010'};
solrData.statsFields.price = {min: '1234', max: '23456'};
f.applyFacetResults(solrResponse);
f.applyFacetResults(SolrSpecData.responses.sequential[0]);
modelFacet = f.get('model');
modelItems = modelFacet.get('items');
yearFacet = f.get('year');
yearItems = yearFacet.get('items');
dobFacet = f.get('dateOfBirth');
dobItems = dobFacet.get('items');
priceFacet = f.get('price');
priceItems = priceFacet.get('items');
valueFacet = f.get('value');
valueItems = valueFacet.get('items');
});
it('initial', function() {
//console.log(JSON.stringify(yearItems));
expect(yearItems.length).toBe(5);
expectItem(yearItems.at(0), '2000-01-01T00:00:00.000Z', 1, false);
expectItem(yearItems.at(1), '2001-01-01T00:00:00.000Z', 4, false);
expectItem(yearItems.at(2), '2003-01-01T00:00:00.000Z', 3, false);
expectItem(yearItems.at(3), '2008-01-01T00:00:00.000Z', 7, false);
expectItem(yearItems.at(4), '2010-01-01T00:00:00.000Z', 1, false);
SolrSpecData.expectItem(yearItems.at(0), '2010-01-01T00:00:00.000Z', 1, false);
SolrSpecData.expectItem(yearItems.at(1), '2008-01-01T00:00:00.000Z', 7, false);
SolrSpecData.expectItem(yearItems.at(2), '2003-01-01T00:00:00.000Z', 3, false);
SolrSpecData.expectItem(yearItems.at(3), '2001-01-01T00:00:00.000Z', 4, false);
SolrSpecData.expectItem(yearItems.at(4), '2000-01-01T00:00:00.000Z', 1, false);
expect(modelItems.length).toBe(4);
expectItem(modelItems.at(0), 'chevy', 18, false);
expectItem(modelItems.at(1), 'ford', 23, false);
expectItem(modelItems.at(2), 'misc', 26, false);
expectItem(modelItems.at(3), 'toyota', 5, false);
SolrSpecData.expectItem(modelItems.at(0), 'chevy', 18, false);
SolrSpecData.expectItem(modelItems.at(1), 'ford', 23, false);
SolrSpecData.expectItem(modelItems.at(2), 'misc', 26, false);
SolrSpecData.expectItem(modelItems.at(3), 'toyota', 5, false);
expect(dobItems.length).toBe(4);
expectItem(dobItems.at(0), 'before', 5, false);
expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false);
expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false);
expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false);
expect(priceItems.length).toBe(3);
expectItem(priceItems.at(0), '1234', 3, false);
expectItem(priceItems.at(1), '5678', 2, false);
expectItem(priceItems.at(2), '23456', 1, false);
SolrSpecData.expectItem(dobItems.at(0), 'before', 5, false);
SolrSpecData.expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false);
SolrSpecData.expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false);
SolrSpecData.expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false);
expect(valueItems.length).toBe(3);
SolrSpecData.expectItem(valueItems.at(0), '1234', 3, false);
SolrSpecData.expectItem(valueItems.at(1), '5678', 2, false);
SolrSpecData.expectItem(valueItems.at(2), '23456', 1, false);
expect(Backbone.ajax).not.toHaveBeenCalled();
expect(onItemChange).not.toHaveBeenCalled();
});
......@@ -211,8 +88,8 @@ define(function(require) {
modelItems.get('toyota').set('checked', true);
yearItems.get('2003-01-01T00:00:00.000Z').set('checked', true);
dobItems.get('before').set('checked', true);
priceFacet.set('queryMin', 1234);
priceFacet.set('queryMax', 6000);
valueFacet.set('queryMin', 1234);
valueFacet.set('queryMax', 6000);
});
it('result', function() {
expect(Backbone.ajax).not.toHaveBeenCalled();
......@@ -225,10 +102,10 @@ define(function(require) {
expect(facetFormData.stats).toBe(true);
expect(facetFormData['facet.missing']).toBe(true);
expect(facetFormData.fq).toEqual([
"{!tag=model}model_s:(\"ford\" OR \"toyota\")",
"{!tag=year}year_d:([2003-01-01T00:00:00.000Z TO 2004-01-01T00:00:00.000Z])",
"{!tag=dateOfBirth}dateOfBirth_d:([* TO NOW/YEAR-25YEAR-1SECOND])",
"{!tag=price}price_i:([1234 TO 6000])"
'{!tag=model}model_s:("ford" OR "toyota")',
'{!tag=year}year_dt:(2003-01-01T00:00:00.000Z)',
'{!tag=dateOfBirth}dateOfBirth_dt:([* TO NOW/YEAR-25YEAR-1SECOND])',
'{!tag=value}value_d:([1234 TO 6000])',
]);
});
});
......@@ -260,19 +137,9 @@ define(function(require) {
describe('result', function() {
var wantedSuggestions;
beforeEach(function() {
var solrResponse = createSolrResponse({stats: true});
var solrData = makeSolrResponseMixedCase(solrResponse);
//console.log('solrData', JSON.stringify(solrData));
solrData.facetFields['suggestions:model'] = [
'toyota', 5,
'ford', 23,
'chevy', 18,
'nissan', 0,
];
var ajaxData = Backbone.ajax.calls.argsFor(0);
var arg0 = ajaxData[0];
arg0.success(solrResponse, arg0);
arg0.success(SolrSpecData.responses.suggestions[0], arg0);
wantedSuggestions = [
{key: 'toyota', value: 5},
{key: 'ford', value: 23},
......
define(function(require) {
'use strict';
var _ = require('underscore');
var Util = require('./Util');
var Facets = require('./Facets');
var Solr = require('./Solr');
function createSolrResponse(has) {
var solrData = {};
var facetCounts = Util.setField(solrData, 'facet_counts', {});
var facetIntervals = Util.setField(facetCounts, 'facet_intervals', {});
var facetRanges = Util.setField(facetCounts, 'facet_ranges', {});
var facetFields = Util.setField(facetCounts, 'facet_fields', {});
var facetQueries = Util.setField(facetCounts, 'facet_queries', {});
if (has.stats) {
solrData.stats = {};
Util.setField(solrData.stats, 'stats_fields', {});
}
return solrData;
}
function makeSolrResponseMixedCase(solrData) {
var facetCounts = Util.getField(solrData, 'facet_counts');
return {
facetCounts: facetCounts,
facetRanges: Util.getField(facetCounts, 'facet_ranges'),
facetFields: Util.getField(facetCounts, 'facet_fields'),
facetQueries: Util.getField(facetCounts, 'facet_queries'),
statsFields: Util.getField(solrData.stats, 'stats_fields'),
};
}
function createStaticSolrResponse(has, toMerge) {
var solrResponse = createSolrResponse({stats: true});
var solrData = makeSolrResponseMixedCase(solrResponse);
_.each(solrData, function(value, key) {
_.extend(value, toMerge[key]);
});
return solrResponse;
}
function expectItem(item, key, value, checked) {
expect(item.get('key')).toEqual(key);
expect(item.get('value')).toEqual(value);
expect(item.get('checked')).toEqual(checked);
}
var facetsBuilder = function facetsBuilder() {
return {
model: new Facets.Facet({
formName: 'model',
label: 'Model',
},
{
queryField: 'model_s',
facetType: 'field',
bins: {
misc: ['saturn', 'jeep'],
startsWithB: ['buick'],
},
}),
year: new Facets.Facet({
formName: 'year',
orderByDirection: 'desc',
label: 'Year',
},
{
queryField: 'year_dt',
comparator: 'date',
facetType: 'year-field',
facetStats: true,
formatter: function(item) {
var value = item.get('key');
if (value === 'before') {
return 'Older';
}
if (value === 'after') {
return 'Newer';
}
var year = new Date(value).getUTCFullYear();
return year + ' - ' + (year + 2);
},
}),
dateOfBirth: new Facets.Facet({
formName: 'dateOfBirth',
label: 'Birthday',
}, {
queryField: 'dateOfBirth_dt',
comparator: 'date',
facetType: 'year',
facetStats: true,
other: 'before',
start: 'NOW/YEAR-25YEAR',
}),
value: new Facets.Facet({
formName: 'value',
label: 'Value',
}, {
queryField: 'value_d',
comparator: 'number',
facetType: 'field',
facetStats: true,
formatter: function(item) {
var value = parseInt(item.get('key'));
return '$' + value + ' - $' + (value + 1000);
},
}),
};
};
var responses = {
sequential: [
createStaticSolrResponse({stats: true}, {
facetRanges: {
dateOfBirth: {
counts: [
'1974-01-01T00:00:00.000Z', 1,
'1972-01-01T00:00:00.000Z', 1,
'1970-01-01T00:00:00.000Z', 1,
],
before: 5,
},
},
facetFields: {
model: [
'ford', 23,
'chevy', 18,
'toyota', 5,
'saturn', 7, // keep here to test the bin/exclude feature
'jeep', 19, // keep here to test the bin/exclude feature
'buick', 0, // keep here to test the bin/exclude feature
'dodge', 0,
],
value: [
'23456', 1,
'5678', 2,
'1234', 3,
],
year: [
'2000-01-01T00:00:00.000Z', 1,
'2001-01-01T00:00:00.000Z', 4,
'2002-01-01T00:00:00.000Z', 0,
'2003-01-01T00:00:00.000Z', 3,
'2004-01-01T00:00:00.000Z', 0,
'2005-01-01T00:00:00.000Z', 0,
'2006-01-01T00:00:00.000Z', 0,
'2007-01-01T00:00:00.000Z', 0,
'2008-01-01T00:00:00.000Z', 7,
'2009-01-01T00:00:00.000Z', 0,
'2010-01-01T00:00:00.000Z', 1,
],
},
facetQueries: {
'model:misc': 26,
'model:startsWithB': 0,
},
statsFields: {
year: {min: '2000', max: '2010'},
dateOfBirth: {min: '2000', max: '2010'},
value: {min: '1234', max: '23456'},
},
}),
],
suggestions: [
createStaticSolrResponse({stats: true}, {
facetFields: {
'suggestions:model': [
'toyota', 5,
'ford', 23,
'chevy', 18,
'nissan', 0,
],
},
}),
],
};
var testSearchBuilder = function testSearchBuilder() {
var TestSearch = Solr.extend({}, {
selectUrl: '/solr/query/select',
orderingItems: [
{value: 'value_d asc', label: 'Value Ascending'},
{value: 'year_dt asc', label: 'Year Ascending'},
],
returnFields: [
'model_s',
'year_dt',
'value_d',
'dateOfBirth_dt',
],
facets: facetsBuilder(),
});
return TestSearch;
};
return {
testSearchBuilder: testSearchBuilder,
expectItem: expectItem,
facetsBuilder: facetsBuilder,
responses: responses,
};
});
define(function(require) {
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var Solr = require('solr/model/Solr');
var SolrTestData = require('solr/model/Solr.spec.data');
describe('Solr', function() {
it('loads', function() {
expect(Solr).toBeDefined();
});
describe('construct', function() {
var s;
var origNow;
var facets;
var modelFacet, modelItems;
beforeEach(function() {
jasmine.clock().install();
jasmine.clock().mockDate();
origNow = _.now;
_.now = function() { return new Date().getTime(); };
spyOn(Backbone, 'ajax');
s = new (SolrTestData.testSearchBuilder())();
facets = s.get('facets');
modelFacet = facets.get('model');
modelItems = modelFacet.get('items');
});
afterEach(function() {
jasmine.clock().uninstall();
_.now = origNow;
});
it('initial values', function() {
expect(Backbone.ajax).not.toHaveBeenCalled();
expect(modelItems.length).toBe(0);
});
describe('initial debounce', function() {
beforeEach(function() {
jasmine.clock().tick(300);
});
it('foo', function() {
expect(Backbone.ajax).not.toHaveBeenCalled();
});
});
describe('click facet', function() {
beforeEach(function() {
});
it('not searched', function() {
});
});
});
});
});
......@@ -18,6 +18,9 @@ define(function(require) {
getField: function getField(obj, key) {
return obj[key];
},
setField: function getField(obj, key, value) {
return obj[key] = value;
},
getItemKeyAccessor: function getItemKeyAccessor(item) {
return item.get('key');
},
......