b3e51cf0 by Adam Heath

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

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