Commit aed2d3c3 authored by Marco Mariani's avatar Marco Mariani

added key schema support to complex queries, fixed and added tests

parent 0b8f4cd2
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* @param {String} spec.key The metadata key * @param {String} spec.key The metadata key
* @param {String} spec.value The value of the metadata to compare * @param {String} spec.value The value of the metadata to compare
*/ */
function ComplexQuery(spec) { function ComplexQuery(spec, key_schema) {
Query.call(this); Query.call(this);
/** /**
...@@ -38,7 +38,7 @@ function ComplexQuery(spec) { ...@@ -38,7 +38,7 @@ function ComplexQuery(spec) {
/*jslint unparam: true*/ /*jslint unparam: true*/
this.query_list = this.query_list.map( this.query_list = this.query_list.map(
// decorate the map to avoid sending the index as key_schema argument // decorate the map to avoid sending the index as key_schema argument
function (o, i) { return QueryFactory.create(o); } function (o, i) { return QueryFactory.create(o, key_schema); }
); );
/*jslint unparam: false*/ /*jslint unparam: false*/
......
...@@ -168,6 +168,54 @@ ...@@ -168,6 +168,54 @@
}); });
test('Test key schema + complex queries', function () {
var doc_list, docList = function () {
return [
{'identifier': '10', 'number': '10'},
{'identifier': '19', 'number': '19'},
{'identifier': '100', 'number': '100'}
];
}, key_schema = {
types: {
intType: function (value) {
if (typeof value === 'string') {
return parseInt(value, 10);
}
return value;
}
},
keys: {
number: {
readFrom: 'number',
castTo: 'intType'
}
}
};
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'complex',
operator: 'OR',
query_list: [{
type: 'simple',
key: 'number',
operator: '<',
value: '19'
}, {
type: 'simple',
key: 'number',
operator: '=',
value: '19'
}]
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'identifier': '10', 'number': '10'},
{'identifier': '19', 'number': '19'}
], 'Key schema should be propagated from complex to simple queries');
});
test('Key Schema with translation lookup', function () { test('Key Schema with translation lookup', function () {
var doc_list, docList = function () { var doc_list, docList = function () {
return [ return [
......
...@@ -357,13 +357,13 @@ ...@@ -357,13 +357,13 @@
readFrom: 'number', readFrom: 'number',
castTo: intType castTo: intType
}, },
operator: '>', operator: '=',
value: '19' value: '19'
}] }]
}).exec(doc_list); }).exec(doc_list);
deepEqual(doc_list, [ deepEqual(doc_list, [
{'identifier': '10', 'number': '10'}, {'identifier': '10', 'number': '10'},
{'identifier': '100', 'number': '100'} {'identifier': '19', 'number': '19'}
], 'Custom keys should also work within compound queries'); ], 'Custom keys should also work within compound queries');
}); });
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment