d91e8f81 by Adam Heath

Much work towards more coverage.

1 parent 628b2310
......@@ -37,6 +37,10 @@ define(function(require) {
};
},
initialize: function(data, options) {
options = _.defaults({}, options, {
gap: 1,
sortKeyExtractor: Util.getItemKeyAccessor,
});
this.set('all', true);
this.on('change:queryMax change:queryMin', function() {
this.set('hasQuery', this.get('queryMin') !== undefined && this.get('queryMax') !== undefined);
......@@ -123,9 +127,6 @@ define(function(require) {
}, self);
})(this);
var sortKeyExtractor = options.sortKeyExtractor;
if (!sortKeyExtractor) {
sortKeyExtractor = Util.getItemKeyAccessor;
}
var formatter = options.formatter;
if (!formatter) {
var labelMap = options.labelMap;
......@@ -154,21 +155,21 @@ define(function(require) {
comparator = wrapComparatorForAllBeforeAfter(comparator, ['all', 'before'].indexOf(options.other) !== -1, ['all', 'after'].indexOf(options.other) !== -1);
break;
}
var facet = this;
comparator = (function(comparator) {
return function facetItemValue(a, b) {
var keyA = sortKeyExtractor(a);
var keyB = sortKeyExtractor(b);
switch (facet.get('orderByDirection')) {
case 'desc':
var tmp = keyA;
keyA = keyB;
keyB = tmp;
break;
}
return comparator.call(facet, keyA, keyB);
};
})(comparator);
var facet = this;
if (this.get('orderByDirection') === 'desc') {
comparator = (function(comparator) {
return function reverseSort(a, b) {
return -comparator.call(facet, a, b);
}
})(comparator);
}
var items = this.get('items');
items.comparator = comparator;
items.sort();
......@@ -191,7 +192,7 @@ define(function(require) {
case 'year':
this.rangeStart = options.start;
this.rangeEnd = options.end;
this.rangeGap = '+' + (options.gap ? options.gap : 1) + 'YEAR';
this.rangeGap = options.gap;
break;
case 'range':
this.rangeStart = options.start;
......
......@@ -174,6 +174,7 @@ define(function(require) {
var queryField = facet.queryField;
var facetField = facet.facetField ? facet.facetField : queryField;
var type = facet.facetType;
var solrType = type;
var valueFormatter;
var facetOptions = {};
var quoteFormatter = function(value) {
......@@ -184,7 +185,7 @@ define(function(require) {
};
switch (type) {
case 'year':
type = 'range';
solrType = 'range';
quoteFormatter = function(value) {
return value.toISOString();
};
......@@ -192,11 +193,12 @@ define(function(require) {
var key = item.get('key');
var fromDate, toDate;
if (key === 'before') {
return '[* TO ' + facet.rangeStart + '-1SECOND]';
return '[* TO ' + facet.rangeStart + '-1MILLISECOND]';
}
fromDate = new Date(key);
toDate = new Date(key);
toDate.setUTCFullYear(toDate.getUTCFullYear() + 1);
toDate.setUTCFullYear(toDate.getUTCFullYear() + facet.rangeGap);
toDate.setTime(toDate.getTime() - 1);
return rangeFormatter(fromDate, toDate);
};
if (facet.other) {
......@@ -211,7 +213,7 @@ define(function(require) {
};
break;
case 'year-field':
type = 'field';
solrType = 'field';
quoteFormatter = function(value) {
return new Date(value).toISOString();
};
......@@ -238,6 +240,11 @@ define(function(require) {
break;
}
switch (type) {
case 'year':
facetOptions['facet.range.start'] = facet.rangeStart;
facetOptions['facet.range.end'] = facet.rangeEnd;
facetOptions['facet.range.gap'] = '+' + facet.rangeGap + 'YEAR';
break;
case 'range':
facetOptions['facet.range.start'] = facet.rangeStart;
facetOptions['facet.range.end'] = facet.rangeEnd;
......@@ -279,7 +286,7 @@ define(function(require) {
facetOptionList.push(key + '=' + value);
}
});
result['facet.' + type].push('{!' + facetOptionList.join(' ') + '}' + queryField);
result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField);
if (facet.facetStats) {
result['stats.field'].push('{!' + facetOptionList.join(' ') + '}' + queryField);
}
......@@ -292,7 +299,7 @@ define(function(require) {
facetOptionList.push(key + '=' + value);
}
});
result['facet.' + type].push('{!' + facetOptionList.join(' ') + '}' + queryField);
result['facet.' + solrType].push('{!' + facetOptionList.join(' ') + '}' + queryField);
}
}, this));
return result;
......
......@@ -46,6 +46,8 @@ define(function(require) {
var yearFacet, yearItems;
var dobFacet, dobItems;
var valueFacet, valueItems;
var categoryFacet, categoryItems;
var odometerFacet, odometerItems;
beforeEach(function() {
f.applyFacetResults(SolrSpecData.responses.sequential[0]);
modelFacet = f.get('model');
......@@ -56,15 +58,17 @@ define(function(require) {
dobItems = dobFacet.get('items');
valueFacet = f.get('value');
valueItems = valueFacet.get('items');
categoryFacet = f.get('category');
categoryItems = categoryFacet.get('items');
odometerFacet = f.get('odometer');
odometerItems = odometerFacet.get('items');
});
it('initial', function() {
//console.log(JSON.stringify(yearItems));
expect(yearItems.length).toBe(5);
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(yearItems.length).toBe(3);
SolrSpecData.expectItem(yearItems.at(0), '2000-01-01T00:00:00.000Z', 1, false);
SolrSpecData.expectItem(yearItems.at(1), '2004-01-01T00:00:00.000Z', 3, false);
SolrSpecData.expectItem(yearItems.at(2), '2010-01-01T00:00:00.000Z', 1, false);
expect(modelItems.length).toBe(4);
SolrSpecData.expectItem(modelItems.at(0), 'chevy', 18, false);
SolrSpecData.expectItem(modelItems.at(1), 'ford', 23, false);
......@@ -75,10 +79,22 @@ define(function(require) {
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(valueItems.length).toBe(7);
SolrSpecData.expectItem(valueItems.at(0), '1234', 1, false);
SolrSpecData.expectItem(valueItems.at(1), '2345', 2, false);
SolrSpecData.expectItem(valueItems.at(2), '3456', 3, false);
SolrSpecData.expectItem(valueItems.at(3), '4567', 4, false);
SolrSpecData.expectItem(valueItems.at(4), '5678', 5, false);
SolrSpecData.expectItem(valueItems.at(5), '6789', 6, false);
SolrSpecData.expectItem(valueItems.at(6), '7890', 7, false);
expect(categoryItems.length).toBe(3);
SolrSpecData.expectItem(categoryItems.at(0), '10000:First Category', 5, false);
SolrSpecData.expectItem(categoryItems.at(1), '20000:Second Category', 42, false);
SolrSpecData.expectItem(categoryItems.at(2), '30000:Third Category', 75, false);
expect(odometerItems.length).toBe(3);
SolrSpecData.expectItem(odometerItems.at(0), '0', 27, false);
SolrSpecData.expectItem(odometerItems.at(1), '50000', 73, false);
SolrSpecData.expectItem(odometerItems.at(2), '150000', 96, false);
expect(Backbone.ajax).not.toHaveBeenCalled();
expect(onItemChange).not.toHaveBeenCalled();
});
......@@ -86,26 +102,53 @@ define(function(require) {
beforeEach(function() {
modelItems.get('ford').set('checked', true);
modelItems.get('toyota').set('checked', true);
yearItems.get('2003-01-01T00:00:00.000Z').set('checked', true);
yearItems.get('2004-01-01T00:00:00.000Z').set('checked', true);
dobItems.get('before').set('checked', true);
dobItems.get('1972-01-01T00:00:00.000Z').set('checked', true);
valueFacet.set('queryMin', 1234);
valueFacet.set('queryMax', 6000);
categoryItems.get('20000:Second Category').set('checked', true);
odometerItems.get('50000').set('checked', true);
});
it('result', function() {
expect(Backbone.ajax).not.toHaveBeenCalled();
expect(onItemChange).toHaveBeenCalled();
expect(onItemChange.calls.count()).toEqual(6);
expect(onItemChange.calls.count()).toEqual(9);
console.log(JSON.stringify(onItemChange.calls.allArgs(), null, 1));
var facetFormData = f.getFacetFormData();
console.log(JSON.stringify(facetFormData, null, 1));
expect(facetFormData.facet).toBe(true);
expect(facetFormData.stats).toBe(true);
expect(facetFormData['facet.missing']).toBe(true);
expect(facetFormData['facet.field']).toEqual([
'{!key=model ex=model}model_s',
'{!key=year ex=year}year_dt',
'{!key=value ex=value}value_d',
'{!key=bodyType tag=bodyType}bodyType_s',
'{!key=category ex=category}category_and_name_s',
]);
expect(facetFormData['facet.range']).toEqual([
'{!facet.range.other=before facet.range.start=NOW/YEAR-25YEAR facet.range.gap=+2YEAR key=dateOfBirth ex=dateOfBirth}dateOfBirth_dt',
'{!facet.range.gap=5000 key=odometer ex=odometer}odometer_i',
]);
expect(facetFormData['facet.query']).toEqual([
'{!key=model:misc tag=model:misc}(model_s:saturn OR model_s:jeep)',
'{!key=model:startsWithB tag=model:startsWithB}(model_s:buick)',
'{!key=year:ancient tag=year:ancient}(year_dt:\'1950-01-01T00:00:00.000Z\' OR year_dt:\'1952-01-01T00:00:00.000Z\')',
]);
expect(facetFormData['stats.field']).toEqual([
'{!key=year ex=year}year_dt',
'{!facet.range.other=before facet.range.start=NOW/YEAR-25YEAR facet.range.gap=+2YEAR key=dateOfBirth ex=dateOfBirth}dateOfBirth_dt',
'{!key=value ex=value}value_d',
'{!facet.range.gap=5000 key=odometer ex=odometer}odometer_i',
]);
expect(facetFormData.fq).toEqual([
'{!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=year}year_dt:(2004-01-01T00:00:00.000Z)',
'{!tag=dateOfBirth}dateOfBirth_dt:([* TO NOW/YEAR-25YEAR-1MILLISECOND] OR [1972-01-01T00:00:00.000Z TO 1973-12-31T23:59:59.999Z])',
'{!tag=value}value_d:([1234 TO 6000])',
'{!tag=category}category_id_s:(20000)',
'{!tag=odometer}odometer_i:([50000 TO 55000])',
]);
});
});
......