b3e51cf0 by Adam Heath

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

of Solr spec.
1 parent b32c60af
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');
},
......