d91e8f81 by Adam Heath

Much work towards more coverage.

1 parent 628b2310
...@@ -37,6 +37,10 @@ define(function(require) { ...@@ -37,6 +37,10 @@ define(function(require) {
37 }; 37 };
38 }, 38 },
39 initialize: function(data, options) { 39 initialize: function(data, options) {
40 options = _.defaults({}, options, {
41 gap: 1,
42 sortKeyExtractor: Util.getItemKeyAccessor,
43 });
40 this.set('all', true); 44 this.set('all', true);
41 this.on('change:queryMax change:queryMin', function() { 45 this.on('change:queryMax change:queryMin', function() {
42 this.set('hasQuery', this.get('queryMin') !== undefined && this.get('queryMax') !== undefined); 46 this.set('hasQuery', this.get('queryMin') !== undefined && this.get('queryMax') !== undefined);
...@@ -123,9 +127,6 @@ define(function(require) { ...@@ -123,9 +127,6 @@ define(function(require) {
123 }, self); 127 }, self);
124 })(this); 128 })(this);
125 var sortKeyExtractor = options.sortKeyExtractor; 129 var sortKeyExtractor = options.sortKeyExtractor;
126 if (!sortKeyExtractor) {
127 sortKeyExtractor = Util.getItemKeyAccessor;
128 }
129 var formatter = options.formatter; 130 var formatter = options.formatter;
130 if (!formatter) { 131 if (!formatter) {
131 var labelMap = options.labelMap; 132 var labelMap = options.labelMap;
...@@ -154,21 +155,21 @@ define(function(require) { ...@@ -154,21 +155,21 @@ define(function(require) {
154 comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1); 155 comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
155 break; 156 break;
156 } 157 }
157 var facet = this;
158 comparator = (function(comparator) { 158 comparator = (function(comparator) {
159 return function facetItemValue(a, b) { 159 return function facetItemValue(a, b) {
160 var keyA = sortKeyExtractor(a); 160 var keyA = sortKeyExtractor(a);
161 var keyB = sortKeyExtractor(b); 161 var keyB = sortKeyExtractor(b);
162 switch (facet.get('orderByDirection')) {
163 case 'desc':
164 var tmp = keyA;
165 keyA = keyB;
166 keyB = tmp;
167 break;
168 }
169 return comparator.call(facet, keyA, keyB); 162 return comparator.call(facet, keyA, keyB);
170 }; 163 };
171 })(comparator); 164 })(comparator);
165 var facet = this;
166 if (this.get('orderByDirection') === 'desc') {
167 comparator = (function(comparator) {
168 return function reverseSort(a, b) {
169 return -comparator.call(facet, a, b);
170 }
171 })(comparator);
172 }
172 var items = this.get('items'); 173 var items = this.get('items');
173 items.comparator = comparator; 174 items.comparator = comparator;
174 items.sort(); 175 items.sort();
...@@ -191,7 +192,7 @@ define(function(require) { ...@@ -191,7 +192,7 @@ define(function(require) {
191 case 'year': 192 case 'year':
192 this.rangeStart = options.start; 193 this.rangeStart = options.start;
193 this.rangeEnd = options.end; 194 this.rangeEnd = options.end;
194 this.rangeGap = '+' + (options.gap ? options.gap : 1) + 'YEAR'; 195 this.rangeGap = options.gap;
195 break; 196 break;
196 case 'range': 197 case 'range':
197 this.rangeStart = options.start; 198 this.rangeStart = options.start;
......
...@@ -174,6 +174,7 @@ define(function(require) { ...@@ -174,6 +174,7 @@ define(function(require) {
174 var queryField = facet.queryField; 174 var queryField = facet.queryField;
175 var facetField = facet.facetField ? facet.facetField : queryField; 175 var facetField = facet.facetField ? facet.facetField : queryField;
176 var type = facet.facetType; 176 var type = facet.facetType;
177 var solrType = type;
177 var valueFormatter; 178 var valueFormatter;
178 var facetOptions = {}; 179 var facetOptions = {};
179 var quoteFormatter = function(value) { 180 var quoteFormatter = function(value) {
...@@ -184,7 +185,7 @@ define(function(require) { ...@@ -184,7 +185,7 @@ define(function(require) {
184 }; 185 };
185 switch (type) { 186 switch (type) {
186 case 'year': 187 case 'year':
187 type = 'range'; 188 solrType = 'range';
188 quoteFormatter = function(value) { 189 quoteFormatter = function(value) {
189 return value.toISOString(); 190 return value.toISOString();
190 }; 191 };
...@@ -192,11 +193,12 @@ define(function(require) { ...@@ -192,11 +193,12 @@ define(function(require) {
192 var key = item.get('key'); 193 var key = item.get('key');
193 var fromDate, toDate; 194 var fromDate, toDate;
194 if (key === 'before') { 195 if (key === 'before') {
195 return '[* TO ' + facet.rangeStart + '-1SECOND]'; 196 return '[* TO ' + facet.rangeStart + '-1MILLISECOND]';
196 } 197 }
197 fromDate = new Date(key); 198 fromDate = new Date(key);
198 toDate = new Date(key); 199 toDate = new Date(key);
199 toDate.setUTCFullYear(toDate.getUTCFullYear() + 1); 200 toDate.setUTCFullYear(toDate.getUTCFullYear() + facet.rangeGap);
201 toDate.setTime(toDate.getTime() - 1);
200 return rangeFormatter(fromDate, toDate); 202 return rangeFormatter(fromDate, toDate);
201 }; 203 };
202 if (facet.other) { 204 if (facet.other) {
...@@ -211,7 +213,7 @@ define(function(require) { ...@@ -211,7 +213,7 @@ define(function(require) {
211 }; 213 };
212 break; 214 break;
213 case 'year-field': 215 case 'year-field':
214 type = 'field'; 216 solrType = 'field';
215 quoteFormatter = function(value) { 217 quoteFormatter = function(value) {
216 return new Date(value).toISOString(); 218 return new Date(value).toISOString();
217 }; 219 };
...@@ -238,6 +240,11 @@ define(function(require) { ...@@ -238,6 +240,11 @@ define(function(require) {
238 break; 240 break;
239 } 241 }
240 switch (type) { 242 switch (type) {
243 case 'year':
244 facetOptions['facet.range.start'] = facet.rangeStart;
245 facetOptions['facet.range.end'] = facet.rangeEnd;
246 facetOptions['facet.range.gap'] = '+' + facet.rangeGap + 'YEAR';
247 break;
241 case 'range': 248 case 'range':
242 facetOptions['facet.range.start'] = facet.rangeStart; 249 facetOptions['facet.range.start'] = facet.rangeStart;
243 facetOptions['facet.range.end'] = facet.rangeEnd; 250 facetOptions['facet.range.end'] = facet.rangeEnd;
...@@ -279,7 +286,7 @@ define(function(require) { ...@@ -279,7 +286,7 @@ define(function(require) {
279 facetOptionList.push(key + '=' + value); 286 facetOptionList.push(key + '=' + value);
280 } 287 }
281 }); 288 });
282 result['facet.' + type].push('{!' + facetOptionList.join(' ') + '}' + queryField); 289 result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField);
283 if (facet.facetStats) { 290 if (facet.facetStats) {
284 result['stats.field'].push('{!' + facetOptionList.join(' ') + '}' + queryField); 291 result['stats.field'].push('{!' + facetOptionList.join(' ') + '}' + queryField);
285 } 292 }
...@@ -292,7 +299,7 @@ define(function(require) { ...@@ -292,7 +299,7 @@ define(function(require) {
292 facetOptionList.push(key + '=' + value); 299 facetOptionList.push(key + '=' + value);
293 } 300 }
294 }); 301 });
295 result['facet.' + type].push('{!' + facetOptionList.join(' ') + '}' + queryField); 302 result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField);
296 } 303 }
297 }, this)); 304 }, this));
298 return result; 305 return result;
......
...@@ -46,6 +46,8 @@ define(function(require) { ...@@ -46,6 +46,8 @@ define(function(require) {
46 var yearFacet, yearItems; 46 var yearFacet, yearItems;
47 var dobFacet, dobItems; 47 var dobFacet, dobItems;
48 var valueFacet, valueItems; 48 var valueFacet, valueItems;
49 var categoryFacet, categoryItems;
50 var odometerFacet, odometerItems;
49 beforeEach(function() { 51 beforeEach(function() {
50 f.applyFacetResults(SolrSpecData.responses.sequential[0]); 52 f.applyFacetResults(SolrSpecData.responses.sequential[0]);
51 modelFacet = f.get('model'); 53 modelFacet = f.get('model');
...@@ -56,15 +58,17 @@ define(function(require) { ...@@ -56,15 +58,17 @@ define(function(require) {
56 dobItems = dobFacet.get('items'); 58 dobItems = dobFacet.get('items');
57 valueFacet = f.get('value'); 59 valueFacet = f.get('value');
58 valueItems = valueFacet.get('items'); 60 valueItems = valueFacet.get('items');
61 categoryFacet = f.get('category');
62 categoryItems = categoryFacet.get('items');
63 odometerFacet = f.get('odometer');
64 odometerItems = odometerFacet.get('items');
59 }); 65 });
60 it('initial', function() { 66 it('initial', function() {
61 //console.log(JSON.stringify(yearItems)); 67 //console.log(JSON.stringify(yearItems));
62 expect(yearItems.length).toBe(5); 68 expect(yearItems.length).toBe(3);
63 SolrSpecData.expectItem(yearItems.at(0), '2010-01-01T00:00:00.000Z', 1, false); 69 SolrSpecData.expectItem(yearItems.at(0), '2000-01-01T00:00:00.000Z', 1, false);
64 SolrSpecData.expectItem(yearItems.at(1), '2008-01-01T00:00:00.000Z', 7, false); 70 SolrSpecData.expectItem(yearItems.at(1), '2004-01-01T00:00:00.000Z', 3, false);
65 SolrSpecData.expectItem(yearItems.at(2), '2003-01-01T00:00:00.000Z', 3, false); 71 SolrSpecData.expectItem(yearItems.at(2), '2010-01-01T00:00:00.000Z', 1, false);
66 SolrSpecData.expectItem(yearItems.at(3), '2001-01-01T00:00:00.000Z', 4, false);
67 SolrSpecData.expectItem(yearItems.at(4), '2000-01-01T00:00:00.000Z', 1, false);
68 expect(modelItems.length).toBe(4); 72 expect(modelItems.length).toBe(4);
69 SolrSpecData.expectItem(modelItems.at(0), 'chevy', 18, false); 73 SolrSpecData.expectItem(modelItems.at(0), 'chevy', 18, false);
70 SolrSpecData.expectItem(modelItems.at(1), 'ford', 23, false); 74 SolrSpecData.expectItem(modelItems.at(1), 'ford', 23, false);
...@@ -75,10 +79,22 @@ define(function(require) { ...@@ -75,10 +79,22 @@ define(function(require) {
75 SolrSpecData.expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false); 79 SolrSpecData.expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false);
76 SolrSpecData.expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false); 80 SolrSpecData.expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false);
77 SolrSpecData.expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false); 81 SolrSpecData.expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false);
78 expect(valueItems.length).toBe(3); 82 expect(valueItems.length).toBe(7);
79 SolrSpecData.expectItem(valueItems.at(0), '1234', 3, false); 83 SolrSpecData.expectItem(valueItems.at(0), '1234', 1, false);
80 SolrSpecData.expectItem(valueItems.at(1), '5678', 2, false); 84 SolrSpecData.expectItem(valueItems.at(1), '2345', 2, false);
81 SolrSpecData.expectItem(valueItems.at(2), '23456', 1, false); 85 SolrSpecData.expectItem(valueItems.at(2), '3456', 3, false);
86 SolrSpecData.expectItem(valueItems.at(3), '4567', 4, false);
87 SolrSpecData.expectItem(valueItems.at(4), '5678', 5, false);
88 SolrSpecData.expectItem(valueItems.at(5), '6789', 6, false);
89 SolrSpecData.expectItem(valueItems.at(6), '7890', 7, false);
90 expect(categoryItems.length).toBe(3);
91 SolrSpecData.expectItem(categoryItems.at(0), '10000:First Category', 5, false);
92 SolrSpecData.expectItem(categoryItems.at(1), '20000:Second Category', 42, false);
93 SolrSpecData.expectItem(categoryItems.at(2), '30000:Third Category', 75, false);
94 expect(odometerItems.length).toBe(3);
95 SolrSpecData.expectItem(odometerItems.at(0), '0', 27, false);
96 SolrSpecData.expectItem(odometerItems.at(1), '50000', 73, false);
97 SolrSpecData.expectItem(odometerItems.at(2), '150000', 96, false);
82 expect(Backbone.ajax).not.toHaveBeenCalled(); 98 expect(Backbone.ajax).not.toHaveBeenCalled();
83 expect(onItemChange).not.toHaveBeenCalled(); 99 expect(onItemChange).not.toHaveBeenCalled();
84 }); 100 });
...@@ -86,26 +102,53 @@ define(function(require) { ...@@ -86,26 +102,53 @@ define(function(require) {
86 beforeEach(function() { 102 beforeEach(function() {
87 modelItems.get('ford').set('checked', true); 103 modelItems.get('ford').set('checked', true);
88 modelItems.get('toyota').set('checked', true); 104 modelItems.get('toyota').set('checked', true);
89 yearItems.get('2003-01-01T00:00:00.000Z').set('checked', true); 105 yearItems.get('2004-01-01T00:00:00.000Z').set('checked', true);
90 dobItems.get('before').set('checked', true); 106 dobItems.get('before').set('checked', true);
107 dobItems.get('1972-01-01T00:00:00.000Z').set('checked', true);
91 valueFacet.set('queryMin', 1234); 108 valueFacet.set('queryMin', 1234);
92 valueFacet.set('queryMax', 6000); 109 valueFacet.set('queryMax', 6000);
110 categoryItems.get('20000:Second Category').set('checked', true);
111 odometerItems.get('50000').set('checked', true);
93 }); 112 });
94 it('result', function() { 113 it('result', function() {
95 expect(Backbone.ajax).not.toHaveBeenCalled(); 114 expect(Backbone.ajax).not.toHaveBeenCalled();
96 expect(onItemChange).toHaveBeenCalled(); 115 expect(onItemChange).toHaveBeenCalled();
97 expect(onItemChange.calls.count()).toEqual(6); 116 expect(onItemChange.calls.count()).toEqual(9);
98 console.log(JSON.stringify(onItemChange.calls.allArgs(), null, 1)); 117 console.log(JSON.stringify(onItemChange.calls.allArgs(), null, 1));
99 var facetFormData = f.getFacetFormData(); 118 var facetFormData = f.getFacetFormData();
100 console.log(JSON.stringify(facetFormData, null, 1)); 119 console.log(JSON.stringify(facetFormData, null, 1));
101 expect(facetFormData.facet).toBe(true); 120 expect(facetFormData.facet).toBe(true);
102 expect(facetFormData.stats).toBe(true); 121 expect(facetFormData.stats).toBe(true);
103 expect(facetFormData['facet.missing']).toBe(true); 122 expect(facetFormData['facet.missing']).toBe(true);
123 expect(facetFormData['facet.field']).toEqual([
124 '{!key=model ex=model}model_s',
125 '{!key=year ex=year}year_dt',
126 '{!key=value ex=value}value_d',
127 '{!key=bodyType tag=bodyType}bodyType_s',
128 '{!key=category ex=category}category_and_name_s',
129 ]);
130 expect(facetFormData['facet.range']).toEqual([
131 '{!facet.range.other=before facet.range.start=NOW/YEAR-25YEAR facet.range.gap=+2YEAR key=dateOfBirth ex=dateOfBirth}dateOfBirth_dt',
132 '{!facet.range.gap=5000 key=odometer ex=odometer}odometer_i',
133 ]);
134 expect(facetFormData['facet.query']).toEqual([
135 '{!key=model:misc tag=model:misc}(model_s:saturn OR model_s:jeep)',
136 '{!key=model:startsWithB tag=model:startsWithB}(model_s:buick)',
137 '{!key=year:ancient tag=year:ancient}(year_dt:\'1950-01-01T00:00:00.000Z\' OR year_dt:\'1952-01-01T00:00:00.000Z\')',
138 ]);
139 expect(facetFormData['stats.field']).toEqual([
140 '{!key=year ex=year}year_dt',
141 '{!facet.range.other=before facet.range.start=NOW/YEAR-25YEAR facet.range.gap=+2YEAR key=dateOfBirth ex=dateOfBirth}dateOfBirth_dt',
142 '{!key=value ex=value}value_d',
143 '{!facet.range.gap=5000 key=odometer ex=odometer}odometer_i',
144 ]);
104 expect(facetFormData.fq).toEqual([ 145 expect(facetFormData.fq).toEqual([
105 '{!tag=model}model_s:("ford" OR "toyota")', 146 '{!tag=model}model_s:("ford" OR "toyota")',
106 '{!tag=year}year_dt:(2003-01-01T00:00:00.000Z)', 147 '{!tag=year}year_dt:(2004-01-01T00:00:00.000Z)',
107 '{!tag=dateOfBirth}dateOfBirth_dt:([* TO NOW/YEAR-25YEAR-1SECOND])', 148 '{!tag=dateOfBirth}dateOfBirth_dt:([* TO NOW/YEAR-25YEAR-1MILLISECOND] OR [1972-01-01T00:00:00.000Z TO 1973-12-31T23:59:59.999Z])',
108 '{!tag=value}value_d:([1234 TO 6000])', 149 '{!tag=value}value_d:([1234 TO 6000])',
150 '{!tag=category}category_id_s:(20000)',
151 '{!tag=odometer}odometer_i:([50000 TO 55000])',
109 ]); 152 ]);
110 }); 153 });
111 }); 154 });
......