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;
......
......@@ -26,13 +26,6 @@ define(function(require) {
this.builders.push(builder);
return builder;
},
next: function next() {
var next = this.createNextHandler();
this.addBuilder(function() {
next.build();
});
return next;
},
end: function end() {
return this.parent;
},
......@@ -53,14 +46,9 @@ define(function(require) {
},
build: function build() {
var self = this;
var description = this.description();
if (description) {
describe(this.description(), function() {
BaseBuilder.__super__.build.call(self, build);
});
} else {
describe(this.description(), function() {
BaseBuilder.__super__.build.call(self, build);
}
});
},
});
var FacetBaseBuilder = BaseBuilder.extend({
......@@ -72,49 +60,53 @@ define(function(require) {
data
);
},
description: function() {
return null;
},
facetBuilder: function facetBuilder() {
return this.parent.facetBuilder();
},
createNextHandler: function createNextHandler() {
return new FacetBaseBuilder(this);
validateFormatting: function validateFormatting(wantedFormatting) {
this.addBuilder(function(builder) {
var f, items;
beforeEach(function() {
var facetBuilder = builder.facetBuilder();
f = facetBuilder.get('f');
items = facetBuilder.get('items');
});
it('formatting', function() {
_.each(wantedFormatting, function(wantedValue, key) {
var item = items.get(key);
expect(f.formatter(item)).toEqual(wantedValue);
});
});
});
return this;
},
createExpects: function createExpects(all, orderedItems, optionalHasQuery) {
var wantedCheckedKeysLength = 0;
var wantedCheckedKeys = {};
var wantedChecks = {};
var wantedItemOrder = [];
optionalHasQuery = optionalHasQuery || {};
function scanKey(key, wanted) {
wantedChecks[key] = wanted;
if (wanted) {
wantedCheckedKeys[key] = true;
wantedCheckedKeysLength++;
}
wantedItemOrder.push(key);
}
var facetBuilder = this.facetBuilder();
var ascItemOrder = _.clone(facetBuilder.ascItemOrder);
if (facetBuilder.hasAfter) {
ascItemOrder.push('after');
} else if (facetBuilder.hasAfter !== undefined) {
orderedItems.pop();
}
if (facetBuilder.hasBefore) {
ascItemOrder.unshift('before');
} else if (facetBuilder.hasBefore !== undefined) {
orderedItems.shift();
}
for (var i = 0; i < orderedItems.length; i++) {
scanKey(ascItemOrder[i], orderedItems[i]);
}
var item2 = ascItemOrder[2];
this.addBuilder(function(builder) {
var f, items;
var wantedCheckedKeysLength = 0;
var wantedCheckedKeys = {};
var wantedChecks = {};
function scanKey(key, wanted) {
wantedChecks[key] = wanted;
if (wanted) {
wantedCheckedKeys[key] = true;
wantedCheckedKeysLength++;
}
}
var facetBuilder = builder.facetBuilder();
var ascItemOrder = facetBuilder.getAscItemOrder();
if (facetBuilder.hasAfter === null) {
orderedItems.pop();
}
if (facetBuilder.hasBefore === null) {
orderedItems.shift();
}
for (var i = 0; i < orderedItems.length; i++) {
scanKey(ascItemOrder[i], orderedItems[i]);
}
var item2 = ascItemOrder[2];
beforeEach(function() {
var facetBuilder = builder.facetBuilder();
f = facetBuilder.get('f');
items = facetBuilder.get('items');
});
......@@ -124,25 +116,11 @@ define(function(require) {
it('items', function() {
var gotChecks = {};
var gotOrder = [];
items.each(function(item) {
var key = item.get('key');
gotChecks[key] = item.get('checked');
gotOrder.push(key);
});
expect(gotChecks).toEqual(wantedChecks);
expect(gotOrder).toEqual(wantedItemOrder);
f.set('orderByDirection', 'desc');
wantedItemOrder.reverse();
gotOrder = [];
items.each(function(item) {
var key = item.get('key');
gotOrder.push(key);
});
expect(gotOrder).toEqual(wantedItemOrder);
});
it('checkedKeys.length', function() {
expect(f.get('checkedKeys').length).toBe(wantedCheckedKeysLength);
});
it('checkedKeys', function() {
var got = {};
......@@ -152,9 +130,9 @@ define(function(require) {
expect(got).toEqual(wantedCheckedKeys);
});
it('remove not supported', function() {
expect(items.length).toBe(wantedItemOrder.length);
expect(items.length).toBe(ascItemOrder.length);
items.remove(item2);
expect(items.length).toBe(wantedItemOrder.length);
expect(items.length).toBe(ascItemOrder.length);
});
if (optionalHasQuery.min) {
if (optionalHasQuery.max) {
......@@ -259,7 +237,31 @@ define(function(require) {
expect(f).not.toBe(undefined);
expect(f).not.toBe(null);
});
it('order', function() {
var gotOrder = [];
var facetBuilder = builder.facetBuilder();
var f = facetBuilder.get('f');
var items = f.get('items');
items.each(function(item) {
gotOrder.push(item.get('key'));
});
var wantedItemOrder = _.clone(facetBuilder.getAscItemOrder());
expect(gotOrder).toEqual(wantedItemOrder);
f.set('orderByDirection', 'desc');
wantedItemOrder.reverse();
gotOrder = [];
items.each(function(item) {
gotOrder.push(item.get('key'));
});
expect(gotOrder).toEqual(wantedItemOrder);
});
});
if (typeof nameOrBuilder === 'string') {
var facetInfo = SolrSpecData.extractFacetData(nameOrBuilder);
this.createPojoItems(facetInfo.items);
this.setAscItemOrder(facetInfo.order);
this.setBeforeAfter(facetInfo.beforeItem, facetInfo.afterItem);
}
},
facetBuilder: function facetBuilder() {
return this;
......@@ -268,6 +270,16 @@ define(function(require) {
this.ascItemOrder = ascItemOrder;
return this;
},
getAscItemOrder: function getAscItemOrder() {
var ascItemOrder = _.clone(this.ascItemOrder);
if (this.hasAfter) {
ascItemOrder.push('after');
}
if (this.hasBefore) {
ascItemOrder.unshift('before');
}
return ascItemOrder;
},
setBeforeAfter: function setBeforeAfter(hasBefore, hasAfter) {
this.hasBefore = hasBefore;
this.hasAfter = hasAfter;
......@@ -277,14 +289,14 @@ define(function(require) {
this.addBuilder(function(builder) {
beforeEach(function() {
var newItems = [];
if (builder.hasBefore) {
newItems.push(new Facet.Item(builder.hasBefore));
if (builder.hasAfter) {
newItems.push(new Facet.Item(builder.hasAfter));
}
for (var i = 0; i < pojoItems.length; i++) {
newItems.push(new Facet.Item(pojoItems[i]));
}
if (builder.hasAfter) {
newItems.push(new Facet.Item(builder.hasAfter));
if (builder.hasBefore) {
newItems.push(new Facet.Item(builder.hasBefore));
}
builder.get('items').set(newItems);
});
......@@ -300,13 +312,18 @@ define(function(require) {
return new FacetBuilder(nameOrBuilder, description);
}
it('unsupported facet type', function() {
expect(function() {
new Facet(null, {facetType: 'foobar'});
}).toThrow(new Error('Unsupported facet type: foobar'));
});
setFacet('model')
.createPojoItems([
{key: 'toyota', value: 5},
{key: 'misc', value: 26},
{key: 'ford', value: 23},
{key: 'chevy', value: 18},
]).setAscItemOrder(['chevy', 'ford', 'misc', 'toyota'])
.validateFormatting({
'toyota': 'Toyota',
'misc': 'Miscellaneous',
'ford': 'Ford',
'chevy': 'chevy',
})
.createExpects(true, [false, false, false, false])
.setChecked('chevy', true)
.createExpects(false, [true, false, false, false])
......@@ -332,16 +349,15 @@ define(function(require) {
.endAll()
.build();
setFacet('value')
.createPojoItems([
{key: '7890', value: 7},
{key: '6789', value: 6},
{key: '5678', value: 5},
{key: '4567', value: 4},
{key: '3456', value: 3},
{key: '2345', value: 2},
{key: '1234', value: 1},
])
.setAscItemOrder(['1234', '2345', '3456', '4567', '5678', '6789', '7890'])
.validateFormatting({
'1234': '$1234 - $2234',
'2345': '$2345 - $3345',
'3456': '$3456 - $4456',
'4567': '$4567 - $5567',
'5678': '$5678 - $6678',
'6789': '$6789 - $7789',
'7890': '$7890 - $8890',
})
.setChecked('5678', true)
.createExpects(false, [false, false, false, false, true, false, false])
.setChecked('queryMin', '1234')
......@@ -367,43 +383,21 @@ define(function(require) {
.build();
setFacet('year')
.createPojoItems([
{key: '2010-01-01T00:00:00.000Z', value: 1},
{key: '2009-01-01T00:00:00.000Z', value: 0},
{key: '2008-01-01T00:00:00.000Z', value: 7},
{key: '2007-01-01T00:00:00.000Z', value: 0},
{key: '2006-01-01T00:00:00.000Z', value: 0},
{key: '2005-01-01T00:00:00.000Z', value: 0},
{key: '2004-01-01T00:00:00.000Z', value: 0},
{key: '2003-01-01T00:00:00.000Z', value: 3},
{key: '2002-01-01T00:00:00.000Z', value: 0},
{key: '2001-01-01T00:00:00.000Z', value: 4},
{key: '2000-01-01T00:00:00.000Z', value: 1},
])
.setAscItemOrder([
'2000-01-01T00:00:00.000Z',
'2001-01-01T00:00:00.000Z',
'2003-01-01T00:00:00.000Z',
'2008-01-01T00:00:00.000Z',
'2010-01-01T00:00:00.000Z',
])
.setChecked('2003-01-01T00:00:00.000Z', true)
.validateFormatting({
'2000-01-01T00:00:00.000Z': '2000',
'2004-01-01T00:00:00.000Z': '2004',
'2010-01-01T00:00:00.000Z': '2010',
})
.setChecked('2004-01-01T00:00:00.000Z', true)
.endAll()
.build();
setFacet('dateOfBirth')
.createPojoItems([
{key: '1974-01-01T00:00:00.000Z', value: 1},
{key: '1972-01-01T00:00:00.000Z', value: 1},
{key: '1970-01-01T00:00:00.000Z', value: 1},
])
.setAscItemOrder([
'1970-01-01T00:00:00.000Z',
'1972-01-01T00:00:00.000Z',
'1974-01-01T00:00:00.000Z',
])
.setBeforeAfter({key: 'before', value: 5}, undefined)
.validateFormatting({
'before': 'Older',
'1970-01-01T00:00:00.000Z': '1970 - 1972',
'1972-01-01T00:00:00.000Z': '1972 - 1974',
'1974-01-01T00:00:00.000Z': '1974 - 1976',
})
.setChecked('1972-01-01T00:00:00.000Z', true)
.createExpects(false, [false, false, true, false])
.setChecked('before', true)
......@@ -418,6 +412,32 @@ define(function(require) {
.endAll()
.build();
setFacet('bodyType')
.validateFormatting({
'aluminum': 'aluminum',
'carbonComposite': 'carbonComposite',
'steel': 'steel',
})
.endAll()
.build();
setFacet('category')
.validateFormatting({
'10000:First Category': 'First Category',
'20000:Second Category': 'Second Category',
'30000:Third Category': 'Third Category',
})
.endAll()
.build();
setFacet('odometer')
.validateFormatting({
'0': '0 - 5000',
'50000': '50000 - 55000',
'150000': '150000 - 155000',
})
.endAll()
.build();
function rangeSupportHelper(hasBefore, hasAfter) {
var description = 'type[year]' + (hasBefore ? ' hasBefore' : '') + (hasAfter ? ' hasAfter' : '');
......@@ -426,6 +446,16 @@ define(function(require) {
queryField: 'dateOfBirth_dt',
facetType: 'year',
facetStats: true,
formatter: function(item) {
var value = item.get('key');
if (value === 'before') {
return 'Before';
}
if (value === 'after') {
return 'Newer';
}
return value;
},
};
if (hasBefore) {
if (hasAfter) {
......@@ -438,7 +468,7 @@ define(function(require) {
}
return new Facet(null, facetOptions);
}, description)
.createPojoItems([
.createPojoItems([
{key: '1974-01-01T00:00:00.000Z', value: 1},
{key: '1972-01-01T00:00:00.000Z', value: 1},
{key: '1970-01-01T00:00:00.000Z', value: 1},
......@@ -449,11 +479,25 @@ define(function(require) {
'1974-01-01T00:00:00.000Z',
])
.setBeforeAfter(hasBefore, hasAfter)
;
var validFormatting = {
'1970-01-01T00:00:00.000Z': '1970-01-01T00:00:00.000Z',
'1972-01-01T00:00:00.000Z': '1972-01-01T00:00:00.000Z',
'1974-01-01T00:00:00.000Z': '1974-01-01T00:00:00.000Z',
};
if (hasBefore) {
validFormatting.before = 'Before';
}
if (hasAfter) {
validFormatting.after = 'Newer';
}
builder.validateFormatting(validFormatting);
var l1 = builder
.setChecked('1972-01-01T00:00:00.000Z', true)
.createExpects(false, [false, false, true, false, false])
;
if (hasBefore) {
builder.setChecked('before', true)
l1.setChecked('before', true)
.createExpects(false, [true, false, true, false, false])
.setChecked('checkedKeys', ['before', '1974-01-01T00:00:00.000Z'])
.createExpects(false, [true, false, false, true, false])
......@@ -462,7 +506,7 @@ define(function(require) {
;
}
if (hasAfter) {
builder.setChecked('after', true)
l1.setChecked('after', true)
.createExpects(false, [false, false, true, false, true])
.setChecked('checkedKeys', ['after', '1974-01-01T00:00:00.000Z'])
.createExpects(false, [false, false, false, true, true])
......
......@@ -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])',
]);
});
});
......
......@@ -5,6 +5,7 @@ define(function(require) {
var Facet = require('./Facet');
var Facets = require('./Facets');
var Solr = require('./Solr');
var Sort = require('./Sort');
function createSolrResponse(has) {
var solrData = {};
......@@ -49,63 +50,172 @@ define(function(require) {
expect(item.get('checked')).toEqual(checked);
}
function categorySplitter(item, handler) {
var key = item.get('key');
if (!key) {
return key;
}
var parts = key.split(':', 2);
if (parts.length === 1) {
return key;
} else {
return handler(parts);
}
}
function categoryNameExtractor(item) {
return categorySplitter(item, function(parts) {
return parts[1];
});
}
function categoryIdExtractor(item) {
return categorySplitter(item, function(parts) {
return parts[0];
});
}
var definedFacets = {
model: [
{
model: {
facetData: {
formName: 'model',
label: 'Model',
}, {
},
facetOptions: {
queryField: 'model_s',
facetType: 'field',
bins: {
misc: ['saturn', 'jeep'],
startsWithB: ['buick'],
},
labelMap: {
FORD: 'Ford',
TOYOTA: 'Toyota',
MISC: 'Miscellaneous',
},
},
],
year: [
{
facetItems: [
{key: 'toyota', value: 5},
{key: 'misc', value: 26},
{key: 'ford', value: 23},
{key: 'chevy', value: 18},
],
facetItemOrder: [
'chevy',
'ford',
'misc',
'toyota',
],
searchData: {
facetFields: [
'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,
],
facetQueries: {
'model:misc': 26,
'model:startsWithB': 0,
},
},
},
year: {
facetData: {
formName: 'year',
orderByDirection: 'desc',
label: 'Year',
}, {
},
facetOptions: {
queryField: 'year_dt',
comparator: 'date',
facetType: 'year-field',
facetStats: true,
gap: 2,
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);
return '' + new Date(item.get('key')).getUTCFullYear();
},
bins: {
ancient: ['1950-01-01T00:00:00.000Z', '1952-01-01T00:00:00.000Z'],
},
},
],
dateOfBirth: [
{
facetItems: [
{key: '2010-01-01T00:00:00.000Z', value: 1},
{key: '2004-01-01T00:00:00.000Z', value: 3},
{key: '2000-01-01T00:00:00.000Z', value: 1},
],
facetItemOrder: [
'2000-01-01T00:00:00.000Z',
'2004-01-01T00:00:00.000Z',
'2010-01-01T00:00:00.000Z',
],
searchData: {
facetFields: [
'2000-01-01T00:00:00.000Z', 1,
'2002-01-01T00:00:00.000Z', 0,
'2004-01-01T00:00:00.000Z', 3,
'2006-01-01T00:00:00.000Z', 0,
'2008-01-01T00:00:00.000Z', 0,
'2010-01-01T00:00:00.000Z', 1,
],
facetQueries: {
'year:ancient': 0,
},
statsFields: {min: '2000-01-01T00:00:00.000Z', max: '2010-01-01T00:00:00.000Z'},
},
},
dateOfBirth: {
facetData: {
formName: 'dateOfBirth',
label: 'Birthday',
}, {
},
facetOptions: {
queryField: 'dateOfBirth_dt',
comparator: 'date',
facetType: 'year',
facetStats: true,
other: 'before',
start: 'NOW/YEAR-25YEAR',
gap: 2,
formatter: function(item) {
var value = item.get('key');
if (value === 'before') {
return 'Older';
}
var year = new Date(value).getUTCFullYear();
return year + ' - ' + (year + 2);
},
},
],
value: [
{
facetItems: [
{key: '1974-01-01T00:00:00.000Z', value: 1},
{key: '1972-01-01T00:00:00.000Z', value: 1},
{key: '1970-01-01T00:00:00.000Z', value: 1},
],
facetItemOrder: [
'1970-01-01T00:00:00.000Z',
'1972-01-01T00:00:00.000Z',
'1974-01-01T00:00:00.000Z',
],
facetItemBefore: {key: 'before', value: 5},
searchData: {
facetRanges: {
counts: [
'1974-01-01T00:00:00.000Z', 1,
'1972-01-01T00:00:00.000Z', 1,
'1970-01-01T00:00:00.000Z', 1,
],
before: 5,
},
statsFields: {min: '2000', max: '2010'},
},
},
value: {
facetData: {
formName: 'value',
label: 'Value',
}, {
},
facetOptions: {
queryField: 'value_d',
comparator: 'number',
comparator: Sort.number,
facetType: 'field',
facetStats: true,
formatter: function(item) {
......@@ -113,42 +223,175 @@ define(function(require) {
return '$' + value + ' - $' + (value + 1000);
},
},
],
bodyType: [
{
facetItems: [
{key: '7890', value: 7},
{key: '6789', value: 6},
{key: '5678', value: 5},
{key: '4567', value: 4},
{key: '3456', value: 3},
{key: '2345', value: 2},
{key: '1234', value: 1},
],
facetItemOrder: [
'1234',
'2345',
'3456',
'4567',
'5678',
'6789',
'7890',
],
searchData: {
facetFields: [
'7890', 7,
'6789', 6,
'5678', 5,
'4567', 4,
'3456', 3,
'2345', 2,
'1234', 1,
],
statsFields: {min: '1234', max: '7890'},
},
},
bodyType: {
facetData: {
label: 'Type'
}, {
},
facetOptions: {
queryField: 'bodyType_s',
facetType: 'field',
labelMap: {
_labelMap: {
aluminum: 'Aluminum',
plastic: 'Plastic',
carbonComposite: 'Carbon Composite',
steel: 'Steel',
},
},
],
facetItems: [
{key: 'steel', value: 2},
{key: 'carbonComposite', value: 66},
{key: 'aluminum', value: 14},
],
facetItemOrder: [
'aluminum',
'carbonComposite',
'steel',
],
searchData: {
facetFields: [
'aluminum', 14,
'carbonComposite', 66,
'steel', 2,
],
},
},
category: {
facetData: {
formName: 'category',
label: 'Category',
},
facetOptions: {
queryField: 'category_and_name_s',
facetField: 'category_id_s',
facetType: 'field',
formatter: categoryNameExtractor,
queryValue: categoryIdExtractor,
sortKeyExtractor: categoryNameExtractor,
},
facetItems: [
{key: '30000:Third Category', value: 75},
{key: '20000:Second Category', value: 42},
{key: '10000:First Category', value: 5},
],
facetItemOrder: [
'10000:First Category',
'20000:Second Category',
'30000:Third Category',
],
searchData: {
facetFields: [
'10000:First Category', 5,
'20000:Second Category', 42,
'30000:Third Category', 75,
],
},
},
odometer: {
facetData: {
label: 'Type'
},
facetOptions: {
queryField: 'odometer_i',
comparator: 'number',
facetType: 'range',
facetStats: true,
gap: 5000,
formatter: function(item) {
var value = parseInt(item.get('key'));
return value + ' - ' + (value + 5000);
},
},
facetItems: [
{key: '150000', value: 96},
{key: '50000', value: 73},
{key: '0', value: 27},
],
facetItemOrder: [
'0',
'50000',
'150000',
],
searchData: {
facetRanges: {
counts: [
'0', 27,
'50000', 73,
'150000', 96,
],
},
statsFields: {min: '0', max: '154123'},
},
},
};
var facetsBuilder = function facetsBuilder(facetName) {
if (facetName) {
return new Facet(definedFacets[facetName][0], definedFacets[facetName][1]);
function facetsBuilder(wantedFacetName) {
if (wantedFacetName) {
return new Facet(definedFacets[wantedFacetName].facetData, definedFacets[wantedFacetName].facetOptions);
}
var result = {};
_.each(_.keys(definedFacets), function(facetName) {
result[facetName] = new Facet(definedFacets[facetName][0], definedFacets[facetName][1]);
result[facetName] = new Facet(definedFacets[facetName].facetData, definedFacets[facetName].facetOptions);
});
return result;
};
}
function createSolrDataMerger(modelData) {
function extractFacetData(wantedFacetName) {
var facetInfo = definedFacets[wantedFacetName];
return {
items: facetInfo.facetItems,
order: facetInfo.facetItemOrder,
beforeItem: facetInfo.facetItemBefore,
afterItem: facetInfo.facetItemAfter,
};
}
function createSolrDataMerger() {
var wantedFacetNames;
if (arguments.length > 0) {
wantedFacetNames = arguments;
} else {
wantedFacetNames = _.keys(definedFacets);
}
return function(solrData) {
_.each(solrData, function(solrValue, solrKey) {
_.each(modelData, function(modelObj, modelKey) {
var modelValue = modelObj[solrKey];
_.each(_.pick(definedFacets, wantedFacetNames), function(facetObj, facetName) {
var modelValue = facetObj.searchData[solrKey];
if (modelValue) {
if (solrKey === 'facetQueries') {
_.extend(solrValue, modelValue);
} else {
solrValue[modelKey] = modelValue;
solrValue[facetName] = modelValue;
}
}
});
......@@ -156,69 +399,9 @@ define(function(require) {
};
}
var defaultModelData = {
dateOfBirth: {
facetRanges: {
counts: [
'1974-01-01T00:00:00.000Z', 1,
'1972-01-01T00:00:00.000Z', 1,
'1970-01-01T00:00:00.000Z', 1,
],
before: 5,
},
statsFields: {min: '2000', max: '2010'},
},
bodyType: {
facetFields: [
'aluminum', 14,
'carbonComposite', 66,
'steel', 2,
],
},
model: {
facetFields: [
'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,
],
facetQueries: {
'model:misc': 26,
'model:startsWithB': 0,
},
},
value: {
facetFields: [
'23456', 1,
'5678', 2,
'1234', 3,
],
statsFields: {min: '1234', max: '23456'},
},
year: {
facetFields: [
'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,
],
statsFields: {min: '2000-01-01T00:00:00.000Z', max: '2010-01-01T00:00:00.000Z'},
},
};
var responses = {
sequential: [
createStaticSolrResponse({stats: true}, createSolrDataMerger(defaultModelData)),
createStaticSolrResponse({stats: true}, createSolrDataMerger()),
],
suggestions: [
createStaticSolrResponse({stats: true}, {
......@@ -246,6 +429,8 @@ define(function(require) {
'year_dt',
'value_d',
'dateOfBirth_dt',
'category_id_s',
'category_and_name_s',
],
facets: facetsBuilder(),
});
......@@ -256,5 +441,6 @@ define(function(require) {
expectItem: expectItem,
facetsBuilder: facetsBuilder,
responses: responses,
extractFacetData: extractFacetData,
};
});
......