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) { ...@@ -3,39 +3,8 @@ define(function(require) {
3 var _ = require('underscore'); 3 var _ = require('underscore');
4 var Backbone = require('backbone'); 4 var Backbone = require('backbone');
5 var Facets = require('solr/model/Facets'); 5 var Facets = require('solr/model/Facets');
6 6 var Util = require('solr/model/Util');
7 function setData(obj, key, value) { 7 var SolrSpecData = require('solr/model/Solr.spec.data');
8 return obj[key] = value;
9 }
10
11 function getData(obj, key) {
12 return obj[key];
13 }
14
15 function createSolrResponse(has) {
16 var solrData = {};
17 var facetCounts = setData(solrData, 'facet_counts', {});
18 var facetIntervals = setData(facetCounts, 'facet_intervals', {});
19 var facetRanges = setData(facetCounts, 'facet_ranges', {});
20 var facetFields = setData(facetCounts, 'facet_fields', {});
21 var facetQueries = setData(facetCounts, 'facet_queries', {});
22 if (has.stats) {
23 solrData.stats = {};
24 setData(solrData.stats, 'stats_fields', {});
25 }
26 return solrData;
27 }
28
29 function makeSolrResponseMixedCase(solrData) {
30 var facetCounts = getData(solrData, 'facet_counts');
31 return {
32 facetCounts: facetCounts,
33 facetRanges: getData(facetCounts, 'facet_ranges'),
34 facetFields: getData(facetCounts, 'facet_fields'),
35 facetQueries: getData(facetCounts, 'facet_queries'),
36 statsFields: getData(solrData.stats, 'stats_fields'),
37 };
38 }
39 8
40 describe('Facets', function() { 9 describe('Facets', function() {
41 it('loads', function() { 10 it('loads', function() {
...@@ -54,50 +23,8 @@ define(function(require) { ...@@ -54,50 +23,8 @@ define(function(require) {
54 origNow = _.now; 23 origNow = _.now;
55 _.now = function() { return new Date().getTime(); }; 24 _.now = function() { return new Date().getTime(); };
56 spyOn(Backbone, 'ajax'); 25 spyOn(Backbone, 'ajax');
57 var facets = {
58 model: new Facets.Facet({
59 formName: 'model',
60 label: 'Model',
61 }, {
62 queryField: 'model_s',
63 facetType: 'field',
64 bins: {
65 misc: ['saturn', 'jeep'],
66 startsWithB: ['buick'],
67 },
68 }),
69 year: new Facets.Facet({
70 formName: 'year',
71 label: 'Year',
72 }, {
73 queryField: 'year_d',
74 comparator: Facets.Sort.date,
75 facetType: 'year',
76 facetStats: true,
77 }),
78 dateOfBirth: new Facets.Facet({
79 formName: 'dateOfBirth',
80 label: 'Birthday',
81 }, {
82 queryField: 'dateOfBirth_d',
83 comparator: Facets.Sort.date,
84 facetType: 'year',
85 facetStats: true,
86 other: 'before',
87 start: 'NOW/YEAR-25YEAR',
88 }),
89 price: new Facets.Facet({
90 formName: 'price',
91 label: 'Price',
92 }, {
93 queryField: 'price_i',
94 comparator: Facets.Sort.number,
95 facetType: 'field',
96 facetStats: true,
97 }),
98 };
99 onItemChange = jasmine.createSpy('onItemChange'); 26 onItemChange = jasmine.createSpy('onItemChange');
100 f = new Facets(facets, {search: {url: '/'}}); 27 f = new Facets(SolrSpecData.facetsBuilder(), {search: {url: '/'}});
101 f.on('item-change', onItemChange); 28 f.on('item-change', onItemChange);
102 }); 29 });
103 afterEach(function() { 30 afterEach(function() {
...@@ -113,95 +40,45 @@ define(function(require) { ...@@ -113,95 +40,45 @@ define(function(require) {
113 beforeEach(function() { 40 beforeEach(function() {
114 formData.push(f.getFacetFormData()); 41 formData.push(f.getFacetFormData());
115 }); 42 });
116 function expectItem(item, key, value, checked) {
117 expect(item.get('key')).toEqual(key);
118 expect(item.get('value')).toEqual(value);
119 expect(item.get('checked')).toEqual(checked);
120 }
121 43
122 describe('facets', function() { 44 describe('facets', function() {
123 var modelFacet, modelItems; 45 var modelFacet, modelItems;
124 var yearFacet, yearItems; 46 var yearFacet, yearItems;
125 var dobFacet, dobItems; 47 var dobFacet, dobItems;
126 var priceFacet, priceItems; 48 var valueFacet, valueItems;
127 beforeEach(function() { 49 beforeEach(function() {
128 var solrResponse = createSolrResponse({stats: true}); 50 f.applyFacetResults(SolrSpecData.responses.sequential[0]);
129 var solrData = makeSolrResponseMixedCase(solrResponse);
130 //console.log('solrData', JSON.stringify(solrResponse));
131 solrData.facetRanges.year = {
132 counts: [
133 '2000-01-01T00:00:00.000Z', 1,
134 '2001-01-01T00:00:00.000Z', 4,
135 '2002-01-01T00:00:00.000Z', 0,
136 '2003-01-01T00:00:00.000Z', 3,
137 '2004-01-01T00:00:00.000Z', 0,
138 '2005-01-01T00:00:00.000Z', 0,
139 '2006-01-01T00:00:00.000Z', 0,
140 '2007-01-01T00:00:00.000Z', 0,
141 '2008-01-01T00:00:00.000Z', 7,
142 '2009-01-01T00:00:00.000Z', 0,
143 '2010-01-01T00:00:00.000Z', 1,
144 ],
145 };
146 solrData.facetRanges.dateOfBirth = {
147 counts: [
148 '1974-01-01T00:00:00.000Z', 1,
149 '1972-01-01T00:00:00.000Z', 1,
150 '1970-01-01T00:00:00.000Z', 1,
151 ],
152 before: 5,
153 };
154 solrData.facetFields.model = [
155 'ford', 23,
156 'chevy', 18,
157 'toyota', 5,
158 'saturn', 7, // keep here to test the bin/exclude feature
159 'jeep', 19, // keep here to test the bin/exclude feature
160 'buick', 0, // keep here to test the bin/exclude feature
161 'dodge', 0,
162 ];
163 solrData.facetFields.price = [
164 '23456', 1,
165 '5678', 2,
166 '1234', 3,
167 ];
168 solrData.facetQueries['model:misc'] = 26;
169 solrData.facetQueries['model:startsWithB'] = 0;
170 solrData.statsFields.year = {min: '2000', max: '2010'};
171 solrData.statsFields.dateOfBirth = {min: '2000', max: '2010'};
172 solrData.statsFields.price = {min: '1234', max: '23456'};
173 f.applyFacetResults(solrResponse);
174 modelFacet = f.get('model'); 51 modelFacet = f.get('model');
175 modelItems = modelFacet.get('items'); 52 modelItems = modelFacet.get('items');
176 yearFacet = f.get('year'); 53 yearFacet = f.get('year');
177 yearItems = yearFacet.get('items'); 54 yearItems = yearFacet.get('items');
178 dobFacet = f.get('dateOfBirth'); 55 dobFacet = f.get('dateOfBirth');
179 dobItems = dobFacet.get('items'); 56 dobItems = dobFacet.get('items');
180 priceFacet = f.get('price'); 57 valueFacet = f.get('value');
181 priceItems = priceFacet.get('items'); 58 valueItems = valueFacet.get('items');
182 }); 59 });
183 it('initial', function() { 60 it('initial', function() {
184 //console.log(JSON.stringify(yearItems)); 61 //console.log(JSON.stringify(yearItems));
185 expect(yearItems.length).toBe(5); 62 expect(yearItems.length).toBe(5);
186 expectItem(yearItems.at(0), '2000-01-01T00:00:00.000Z', 1, false); 63 SolrSpecData.expectItem(yearItems.at(0), '2010-01-01T00:00:00.000Z', 1, false);
187 expectItem(yearItems.at(1), '2001-01-01T00:00:00.000Z', 4, false); 64 SolrSpecData.expectItem(yearItems.at(1), '2008-01-01T00:00:00.000Z', 7, false);
188 expectItem(yearItems.at(2), '2003-01-01T00:00:00.000Z', 3, false); 65 SolrSpecData.expectItem(yearItems.at(2), '2003-01-01T00:00:00.000Z', 3, false);
189 expectItem(yearItems.at(3), '2008-01-01T00:00:00.000Z', 7, false); 66 SolrSpecData.expectItem(yearItems.at(3), '2001-01-01T00:00:00.000Z', 4, false);
190 expectItem(yearItems.at(4), '2010-01-01T00:00:00.000Z', 1, false); 67 SolrSpecData.expectItem(yearItems.at(4), '2000-01-01T00:00:00.000Z', 1, false);
191 expect(modelItems.length).toBe(4); 68 expect(modelItems.length).toBe(4);
192 expectItem(modelItems.at(0), 'chevy', 18, false); 69 SolrSpecData.expectItem(modelItems.at(0), 'chevy', 18, false);
193 expectItem(modelItems.at(1), 'ford', 23, false); 70 SolrSpecData.expectItem(modelItems.at(1), 'ford', 23, false);
194 expectItem(modelItems.at(2), 'misc', 26, false); 71 SolrSpecData.expectItem(modelItems.at(2), 'misc', 26, false);
195 expectItem(modelItems.at(3), 'toyota', 5, false); 72 SolrSpecData.expectItem(modelItems.at(3), 'toyota', 5, false);
196 expect(dobItems.length).toBe(4); 73 expect(dobItems.length).toBe(4);
197 expectItem(dobItems.at(0), 'before', 5, false); 74 SolrSpecData.expectItem(dobItems.at(0), 'before', 5, false);
198 expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false); 75 SolrSpecData.expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false);
199 expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false); 76 SolrSpecData.expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false);
200 expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false); 77 SolrSpecData.expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false);
201 expect(priceItems.length).toBe(3); 78 expect(valueItems.length).toBe(3);
202 expectItem(priceItems.at(0), '1234', 3, false); 79 SolrSpecData.expectItem(valueItems.at(0), '1234', 3, false);
203 expectItem(priceItems.at(1), '5678', 2, false); 80 SolrSpecData.expectItem(valueItems.at(1), '5678', 2, false);
204 expectItem(priceItems.at(2), '23456', 1, false); 81 SolrSpecData.expectItem(valueItems.at(2), '23456', 1, false);
205 expect(Backbone.ajax).not.toHaveBeenCalled(); 82 expect(Backbone.ajax).not.toHaveBeenCalled();
206 expect(onItemChange).not.toHaveBeenCalled(); 83 expect(onItemChange).not.toHaveBeenCalled();
207 }); 84 });
...@@ -211,8 +88,8 @@ define(function(require) { ...@@ -211,8 +88,8 @@ define(function(require) {
211 modelItems.get('toyota').set('checked', true); 88 modelItems.get('toyota').set('checked', true);
212 yearItems.get('2003-01-01T00:00:00.000Z').set('checked', true); 89 yearItems.get('2003-01-01T00:00:00.000Z').set('checked', true);
213 dobItems.get('before').set('checked', true); 90 dobItems.get('before').set('checked', true);
214 priceFacet.set('queryMin', 1234); 91 valueFacet.set('queryMin', 1234);
215 priceFacet.set('queryMax', 6000); 92 valueFacet.set('queryMax', 6000);
216 }); 93 });
217 it('result', function() { 94 it('result', function() {
218 expect(Backbone.ajax).not.toHaveBeenCalled(); 95 expect(Backbone.ajax).not.toHaveBeenCalled();
...@@ -225,10 +102,10 @@ define(function(require) { ...@@ -225,10 +102,10 @@ define(function(require) {
225 expect(facetFormData.stats).toBe(true); 102 expect(facetFormData.stats).toBe(true);
226 expect(facetFormData['facet.missing']).toBe(true); 103 expect(facetFormData['facet.missing']).toBe(true);
227 expect(facetFormData.fq).toEqual([ 104 expect(facetFormData.fq).toEqual([
228 "{!tag=model}model_s:(\"ford\" OR \"toyota\")", 105 '{!tag=model}model_s:("ford" OR "toyota")',
229 "{!tag=year}year_d:([2003-01-01T00:00:00.000Z TO 2004-01-01T00:00:00.000Z])", 106 '{!tag=year}year_dt:(2003-01-01T00:00:00.000Z)',
230 "{!tag=dateOfBirth}dateOfBirth_d:([* TO NOW/YEAR-25YEAR-1SECOND])", 107 '{!tag=dateOfBirth}dateOfBirth_dt:([* TO NOW/YEAR-25YEAR-1SECOND])',
231 "{!tag=price}price_i:([1234 TO 6000])" 108 '{!tag=value}value_d:([1234 TO 6000])',
232 ]); 109 ]);
233 }); 110 });
234 }); 111 });
...@@ -260,19 +137,9 @@ define(function(require) { ...@@ -260,19 +137,9 @@ define(function(require) {
260 describe('result', function() { 137 describe('result', function() {
261 var wantedSuggestions; 138 var wantedSuggestions;
262 beforeEach(function() { 139 beforeEach(function() {
263 var solrResponse = createSolrResponse({stats: true});
264 var solrData = makeSolrResponseMixedCase(solrResponse);
265 //console.log('solrData', JSON.stringify(solrData));
266 solrData.facetFields['suggestions:model'] = [
267 'toyota', 5,
268 'ford', 23,
269 'chevy', 18,
270 'nissan', 0,
271 ];
272
273 var ajaxData = Backbone.ajax.calls.argsFor(0); 140 var ajaxData = Backbone.ajax.calls.argsFor(0);
274 var arg0 = ajaxData[0]; 141 var arg0 = ajaxData[0];
275 arg0.success(solrResponse, arg0); 142 arg0.success(SolrSpecData.responses.suggestions[0], arg0);
276 wantedSuggestions = [ 143 wantedSuggestions = [
277 {key: 'toyota', value: 5}, 144 {key: 'toyota', value: 5},
278 {key: 'ford', value: 23}, 145 {key: 'ford', value: 23},
......
1 define(function(require) {
2 'use strict';
3 var _ = require('underscore');
4 var Util = require('./Util');
5 var Facets = require('./Facets');
6 var Solr = require('./Solr');
7
8 function createSolrResponse(has) {
9 var solrData = {};
10 var facetCounts = Util.setField(solrData, 'facet_counts', {});
11 var facetIntervals = Util.setField(facetCounts, 'facet_intervals', {});
12 var facetRanges = Util.setField(facetCounts, 'facet_ranges', {});
13 var facetFields = Util.setField(facetCounts, 'facet_fields', {});
14 var facetQueries = Util.setField(facetCounts, 'facet_queries', {});
15 if (has.stats) {
16 solrData.stats = {};
17 Util.setField(solrData.stats, 'stats_fields', {});
18 }
19 return solrData;
20 }
21
22 function makeSolrResponseMixedCase(solrData) {
23 var facetCounts = Util.getField(solrData, 'facet_counts');
24 return {
25 facetCounts: facetCounts,
26 facetRanges: Util.getField(facetCounts, 'facet_ranges'),
27 facetFields: Util.getField(facetCounts, 'facet_fields'),
28 facetQueries: Util.getField(facetCounts, 'facet_queries'),
29 statsFields: Util.getField(solrData.stats, 'stats_fields'),
30 };
31 }
32
33 function createStaticSolrResponse(has, toMerge) {
34 var solrResponse = createSolrResponse({stats: true});
35 var solrData = makeSolrResponseMixedCase(solrResponse);
36 _.each(solrData, function(value, key) {
37 _.extend(value, toMerge[key]);
38 });
39 return solrResponse;
40 }
41
42 function expectItem(item, key, value, checked) {
43 expect(item.get('key')).toEqual(key);
44 expect(item.get('value')).toEqual(value);
45 expect(item.get('checked')).toEqual(checked);
46 }
47
48 var facetsBuilder = function facetsBuilder() {
49 return {
50 model: new Facets.Facet({
51 formName: 'model',
52 label: 'Model',
53 },
54 {
55 queryField: 'model_s',
56 facetType: 'field',
57 bins: {
58 misc: ['saturn', 'jeep'],
59 startsWithB: ['buick'],
60 },
61 }),
62 year: new Facets.Facet({
63 formName: 'year',
64 orderByDirection: 'desc',
65 label: 'Year',
66 },
67 {
68 queryField: 'year_dt',
69 comparator: 'date',
70 facetType: 'year-field',
71 facetStats: true,
72 formatter: function(item) {
73 var value = item.get('key');
74 if (value === 'before') {
75 return 'Older';
76 }
77 if (value === 'after') {
78 return 'Newer';
79 }
80 var year = new Date(value).getUTCFullYear();
81 return year + ' - ' + (year + 2);
82 },
83 }),
84 dateOfBirth: new Facets.Facet({
85 formName: 'dateOfBirth',
86 label: 'Birthday',
87 }, {
88 queryField: 'dateOfBirth_dt',
89 comparator: 'date',
90 facetType: 'year',
91 facetStats: true,
92 other: 'before',
93 start: 'NOW/YEAR-25YEAR',
94 }),
95 value: new Facets.Facet({
96 formName: 'value',
97 label: 'Value',
98 }, {
99 queryField: 'value_d',
100 comparator: 'number',
101 facetType: 'field',
102 facetStats: true,
103 formatter: function(item) {
104 var value = parseInt(item.get('key'));
105 return '$' + value + ' - $' + (value + 1000);
106 },
107 }),
108 };
109 };
110
111 var responses = {
112 sequential: [
113 createStaticSolrResponse({stats: true}, {
114 facetRanges: {
115 dateOfBirth: {
116 counts: [
117 '1974-01-01T00:00:00.000Z', 1,
118 '1972-01-01T00:00:00.000Z', 1,
119 '1970-01-01T00:00:00.000Z', 1,
120 ],
121 before: 5,
122 },
123 },
124 facetFields: {
125 model: [
126 'ford', 23,
127 'chevy', 18,
128 'toyota', 5,
129 'saturn', 7, // keep here to test the bin/exclude feature
130 'jeep', 19, // keep here to test the bin/exclude feature
131 'buick', 0, // keep here to test the bin/exclude feature
132 'dodge', 0,
133 ],
134 value: [
135 '23456', 1,
136 '5678', 2,
137 '1234', 3,
138 ],
139 year: [
140 '2000-01-01T00:00:00.000Z', 1,
141 '2001-01-01T00:00:00.000Z', 4,
142 '2002-01-01T00:00:00.000Z', 0,
143 '2003-01-01T00:00:00.000Z', 3,
144 '2004-01-01T00:00:00.000Z', 0,
145 '2005-01-01T00:00:00.000Z', 0,
146 '2006-01-01T00:00:00.000Z', 0,
147 '2007-01-01T00:00:00.000Z', 0,
148 '2008-01-01T00:00:00.000Z', 7,
149 '2009-01-01T00:00:00.000Z', 0,
150 '2010-01-01T00:00:00.000Z', 1,
151 ],
152 },
153 facetQueries: {
154 'model:misc': 26,
155 'model:startsWithB': 0,
156 },
157 statsFields: {
158 year: {min: '2000', max: '2010'},
159 dateOfBirth: {min: '2000', max: '2010'},
160 value: {min: '1234', max: '23456'},
161 },
162 }),
163 ],
164 suggestions: [
165 createStaticSolrResponse({stats: true}, {
166 facetFields: {
167 'suggestions:model': [
168 'toyota', 5,
169 'ford', 23,
170 'chevy', 18,
171 'nissan', 0,
172 ],
173 },
174 }),
175 ],
176 };
177
178 var testSearchBuilder = function testSearchBuilder() {
179 var TestSearch = Solr.extend({}, {
180 selectUrl: '/solr/query/select',
181 orderingItems: [
182 {value: 'value_d asc', label: 'Value Ascending'},
183 {value: 'year_dt asc', label: 'Year Ascending'},
184 ],
185 returnFields: [
186 'model_s',
187 'year_dt',
188 'value_d',
189 'dateOfBirth_dt',
190 ],
191 facets: facetsBuilder(),
192 });
193 return TestSearch;
194 };
195 return {
196 testSearchBuilder: testSearchBuilder,
197 expectItem: expectItem,
198 facetsBuilder: facetsBuilder,
199 responses: responses,
200 };
201 });
1 define(function(require) {
2 'use strict';
3 var _ = require('underscore');
4 var Backbone = require('backbone');
5 var Solr = require('solr/model/Solr');
6 var SolrTestData = require('solr/model/Solr.spec.data');
7
8 describe('Solr', function() {
9 it('loads', function() {
10 expect(Solr).toBeDefined();
11 });
12
13 describe('construct', function() {
14 var s;
15 var origNow;
16 var facets;
17 var modelFacet, modelItems;
18 beforeEach(function() {
19 jasmine.clock().install();
20 jasmine.clock().mockDate();
21 origNow = _.now;
22 _.now = function() { return new Date().getTime(); };
23 spyOn(Backbone, 'ajax');
24 s = new (SolrTestData.testSearchBuilder())();
25 facets = s.get('facets');
26 modelFacet = facets.get('model');
27 modelItems = modelFacet.get('items');
28 });
29 afterEach(function() {
30 jasmine.clock().uninstall();
31 _.now = origNow;
32 });
33 it('initial values', function() {
34 expect(Backbone.ajax).not.toHaveBeenCalled();
35 expect(modelItems.length).toBe(0);
36 });
37 describe('initial debounce', function() {
38 beforeEach(function() {
39 jasmine.clock().tick(300);
40 });
41 it('foo', function() {
42 expect(Backbone.ajax).not.toHaveBeenCalled();
43 });
44 });
45 describe('click facet', function() {
46 beforeEach(function() {
47 });
48 it('not searched', function() {
49 });
50 });
51 });
52 });
53 });
...@@ -18,6 +18,9 @@ define(function(require) { ...@@ -18,6 +18,9 @@ define(function(require) {
18 getField: function getField(obj, key) { 18 getField: function getField(obj, key) {
19 return obj[key]; 19 return obj[key];
20 }, 20 },
21 setField: function getField(obj, key, value) {
22 return obj[key] = value;
23 },
21 getItemKeyAccessor: function getItemKeyAccessor(item) { 24 getItemKeyAccessor: function getItemKeyAccessor(item) {
22 return item.get('key'); 25 return item.get('key');
23 }, 26 },
......