Commit 1ebfd26d authored by Marco Mariani's avatar Marco Mariani

async key+query tests

parent 44a20b19
/*jslint indent: 2, maxlen: 120, nomen: true, vars: true */ /*jslint indent: 2, maxlen: 120, nomen: true, vars: true */
/*global define, exports, require, module, complex_queries, jiodate, window, test, ok, /*global define, exports, require, module, complex_queries, jiodate, window, test, ok,
equal, deepEqual, sinon */ equal, deepEqual, sinon, start, stop, RSVP */
// define([module_name], [dependencies], module); // define([module_name], [dependencies], module);
(function (dependencies, module) { (function (dependencies, module) {
...@@ -17,8 +17,12 @@ ...@@ -17,8 +17,12 @@
module('Custom Key Queries with JIODate'); module('Custom Key Queries with JIODate');
var noop = function () {
return; // use with RSVP.all
};
test('Stock comparison operators with year precision', function () { test('Stock comparison operators with year precision', function () {
var doc_list, docList = function () { var docList = function () {
return [ return [
{'identifier': 'twenty ten', 'date': '2010-03-04T08:52:13.746Z'}, {'identifier': 'twenty ten', 'date': '2010-03-04T08:52:13.746Z'},
{'identifier': 'twenty eleven', 'date': '2011-03-04T08:52:13.746Z'}, {'identifier': 'twenty eleven', 'date': '2011-03-04T08:52:13.746Z'},
...@@ -31,75 +35,101 @@ ...@@ -31,75 +35,101 @@
cast_to: jiodate.JIODate cast_to: jiodate.JIODate
} }
} }
}, query_list = null; }, query_list = [], promise = [];
doc_list = docList(); stop();
complex_queries.QueryFactory.create({
type: 'simple', promise.push(
key: 'date', complex_queries.QueryFactory.create({
value: '2011' type: 'simple',
}, key_schema).exec(doc_list); key: 'date',
deepEqual(doc_list, [ value: '2011'
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'} }, key_schema).
], 'Match with "date = 2011" (query tree form)'); exec(docList()).
then(function (dl) {
doc_list = docList(); deepEqual(dl, [
complex_queries.QueryFactory.create({ {'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
type: 'simple', ], 'Match with "date = 2011" (query tree form)');
key: 'date', })
operator: '!=', );
value: '2011'
}, key_schema).exec(doc_list); promise.push(
deepEqual(doc_list, [ complex_queries.QueryFactory.create({
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'}, type: 'simple',
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'} key: 'date',
], 'Match with "date != 2011" (query tree form)'); operator: '!=',
value: '2011'
doc_list = docList(); }, key_schema).
complex_queries.QueryFactory.create({ exec(docList()).
type: 'simple', then(function (dl) {
key: 'date', deepEqual(dl, [
operator: '<', {'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
value: '2011' {'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
}, key_schema).exec(doc_list); ], 'Match with "date != 2011" (query tree form)');
deepEqual(doc_list, [ })
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'} );
], 'Match with "date < 2011" (query tree form)');
promise.push(
doc_list = docList(); complex_queries.QueryFactory.create({
complex_queries.QueryFactory.create({ type: 'simple',
type: 'simple', key: 'date',
key: 'date', operator: '<',
operator: '<=', value: '2011'
value: '2011' }, key_schema).
}, key_schema).exec(doc_list); exec(docList()).
deepEqual(doc_list, [ then(function (dl) {
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'}, deepEqual(dl, [
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'} {'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'}
], 'Match with "date <= 2011" (query tree form)'); ], 'Match with "date < 2011" (query tree form)');
})
doc_list = docList(); );
complex_queries.QueryFactory.create({
type: 'simple', promise.push(
key: 'date', complex_queries.QueryFactory.create({
operator: '>', type: 'simple',
value: '2011' key: 'date',
}, key_schema).exec(doc_list); operator: '<=',
deepEqual(doc_list, [ value: '2011'
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'} }, key_schema).
], 'Match with "date > 2011" (query tree form)'); exec(docList()).
then(function (dl) {
doc_list = docList(); deepEqual(dl, [
complex_queries.QueryFactory.create({ {'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
type: 'simple', {'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
key: 'date', ], 'Match with "date <= 2011" (query tree form)');
operator: '>=', })
value: '2011' );
}, key_schema).exec(doc_list);
deepEqual(doc_list, [ promise.push(
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}, complex_queries.QueryFactory.create({
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'} type: 'simple',
], 'Match with "date >= 2011" (query tree form)'); key: 'date',
operator: '>',
value: '2011'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date > 2011" (query tree form)');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '>=',
value: '2011'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date >= 2011" (query tree form)');
})
);
query_list = [ query_list = [
[ [
...@@ -119,9 +149,13 @@ ...@@ -119,9 +149,13 @@
query_list.forEach(function (o) { query_list.forEach(function (o) {
var qs = o[0], expected = o[1]; var qs = o[0], expected = o[1];
doc_list = docList(); promise.push(
complex_queries.QueryFactory.create(qs, key_schema).exec(doc_list); complex_queries.QueryFactory.create(qs, key_schema).
deepEqual(doc_list, expected, "Match with '" + qs + "' (parsed query string)"); exec(docList()).
then(function (dl) {
deepEqual(dl, expected, "Match with '" + qs + "' (parsed query string)");
})
);
}); });
query_list = [ query_list = [
...@@ -132,13 +166,17 @@ ...@@ -132,13 +166,17 @@
]; ];
query_list.forEach(function (qs) { query_list.forEach(function (qs) {
doc_list = docList(); promise.push(
complex_queries.QueryFactory.create(qs, key_schema).exec(doc_list); complex_queries.QueryFactory.create(qs, key_schema).
deepEqual(doc_list, [ exec(docList()).
], "Match with an invalid parsed string " + qs + " should return empty list but not raise errors"); then(function (dl) {
deepEqual(dl, [
], "Match with an invalid parsed string " + qs + " should return empty list but not raise errors");
})
);
}); });
RSVP.all(promise).then(noop).always(start);
}); });
})); }));
...@@ -21,54 +21,6 @@ ...@@ -21,54 +21,6 @@
local_storage.clear(); local_storage.clear();
/**
* all(promises): Promise
*
* Produces a promise that is resolved when all the given promises are
* fulfilled. The resolved value is an array of each of the answers of the
* given promises.
*
* @param {Array} promises The promises to use
* @return {Promise} A new promise
*/
function all(promises) {
var results = [], i, count = 0;
function cancel() {
var j;
for (j = 0; j < promises.length; j += 1) {
if (typeof promises[j].cancel === 'function') {
promises[j].cancel();
}
}
}
return new RSVP.Promise(function (resolve, reject, notify) {
/*jslint unparam: true */
function succeed(j) {
return function (answer) {
results[j] = answer;
count += 1;
if (count !== promises.length) {
return;
}
resolve(results);
};
}
function notified(j) {
return function (answer) {
notify({
"promise": promises[j],
"index": j,
"notified": answer
});
};
}
for (i = 0; i < promises.length; i += 1) {
promises[i].then(succeed(i), succeed(i), notified(i));
}
}, cancel);
}
var key_schema = { var key_schema = {
cast_lookup: { cast_lookup: {
dateType: function (obj) { dateType: function (obj) {
...@@ -105,7 +57,7 @@ ...@@ -105,7 +57,7 @@
o.date_b = new Date(); o.date_b = new Date();
// put some document before listing them // put some document before listing them
all([ RSVP.all([
jio.put({ jio.put({
"_id": "a", "_id": "a",
"title": "one", "title": "one",
......
/*jslint indent: 2, maxlen: 100, nomen: true, vars: true */ /*jslint indent: 2, maxlen: 100, nomen: true, vars: true */
/*global define, exports, require, module, complex_queries, window, test, ok, /*global define, exports, require, module, complex_queries, window, test, ok,
deepEqual, sinon */ deepEqual, sinon, start, stop, RSVP */
// define([module_name], [dependencies], module); // define([module_name], [dependencies], module);
(function (dependencies, module) { (function (dependencies, module) {
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
module('Custom Key Queries with Schema'); module('Custom Key Queries with Schema');
var noop = function () {
return; // use with RSVP.all
};
var translationEqualityMatcher = function (data) { var translationEqualityMatcher = function (data) {
return function (object_value, value) { return function (object_value, value) {
value = data[value]; value = data[value];
...@@ -24,7 +28,6 @@ ...@@ -24,7 +28,6 @@
}; };
}; };
/*jslint unparam: true*/ /*jslint unparam: true*/
var key_schema = { var key_schema = {
cast_lookup: { cast_lookup: {
...@@ -90,29 +93,37 @@ ...@@ -90,29 +93,37 @@
test('Keys defined in a Schema can be used like metadata', function () { test('Keys defined in a Schema can be used like metadata', function () {
var doc_list, docList = function () { var docList = function () {
return [ return [
{'identifier': 'a'}, {'identifier': 'a'},
{'identifier': 'A'}, {'identifier': 'A'},
{'identifier': 'b'} {'identifier': 'b'}
]; ];
}; }, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'case_insensitive_identifier',
value: 'A'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': 'a'},
{'identifier': 'A'}
], 'Key Schema: case_insensitive_identifier');
})
);
doc_list = docList(); RSVP.all(promise).then(noop).always(start);
complex_queries.QueryFactory.create({
type: 'simple',
key: 'case_insensitive_identifier',
value: 'A'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'identifier': 'a'},
{'identifier': 'A'}
], 'Key Schema: case_insensitive_identifier');
}); });
test('Standard date keys', function () { test('Standard date keys', function () {
var doc_list, docList = function () { var docList = function () {
return [ return [
{'identifier': 'a', 'date': '2013-01-01'}, {'identifier': 'a', 'date': '2013-01-01'},
{'identifier': 'b', 'date': '2013-02-01'}, {'identifier': 'b', 'date': '2013-02-01'},
...@@ -121,54 +132,58 @@ ...@@ -121,54 +132,58 @@
{'identifier': 'c', 'date': '2013-03-03'}, {'identifier': 'c', 'date': '2013-03-03'},
{'identifier': 'd', 'date': '2013-04-04'} {'identifier': 'd', 'date': '2013-04-04'}
]; ];
}; }, promise = [];
doc_list = docList(); stop();
complex_queries.QueryFactory.create({
type: 'simple', promise.push(
key: 'date_day', complex_queries.QueryFactory.create({
value: '2013-02-02' type: 'simple',
}, key_schema).exec(doc_list); key: 'date_day',
deepEqual(doc_list, [ value: '2013-02-02'
{'identifier': 'bb', 'date': '2013-02-02'} }, key_schema).
], 'Key Schema: same_day'); exec(docList()).
then(function (dl) {
deepEqual(dl, [
doc_list = docList(); {'identifier': 'bb', 'date': '2013-02-02'}
complex_queries.QueryFactory.create({ ], 'Key Schema: same_day');
type: 'simple', })
key: 'date_month', );
value: '2013-02-10'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{
'date': '2013-02-01',
'identifier': 'b'
},
{
'date': '2013-02-02',
'identifier': 'bb'
},
{
'date': '2013-02-03',
'identifier': 'bbb'
}
], 'Key Schema: date_month');
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date_month',
value: '2013-02-10'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2013-02-01', 'identifier': 'b'},
{'date': '2013-02-02', 'identifier': 'bb'},
{'date': '2013-02-03', 'identifier': 'bbb'}
], 'Key Schema: date_month');
})
);
doc_list = docList(); promise.push(
complex_queries.QueryFactory.create({ complex_queries.QueryFactory.create({
type: 'simple', type: 'simple',
key: 'date_year', key: 'date_year',
value: '2013-02-10' value: '2013-02-10'
}, key_schema).exec(doc_list); }, key_schema).
deepEqual(doc_list.length, 6, 'Key Schema: date_year'); exec(docList()).
then(function (dl) {
deepEqual(dl.length, 6, 'Key Schema: date_year');
})
);
RSVP.all(promise).then(noop).always(start);
}); });
test('Test key schema + complex queries', function () { test('Test key schema + complex queries', function () {
var doc_list, docList = function () { var docList = function () {
return [ return [
{'identifier': '10', 'number': '10'}, {'identifier': '10', 'number': '10'},
{'identifier': '19', 'number': '19'}, {'identifier': '19', 'number': '19'},
...@@ -189,61 +204,77 @@ ...@@ -189,61 +204,77 @@
cast_to: 'intType' cast_to: 'intType'
} }
} }
}; }, promise = [];
doc_list = docList(); stop();
complex_queries.QueryFactory.create({
type: 'complex', promise.push(
operator: 'OR', complex_queries.QueryFactory.create({
query_list: [{ type: 'complex',
type: 'simple', operator: 'OR',
key: 'number', query_list: [{
operator: '<', type: 'simple',
value: '19' key: 'number',
}, { operator: '<',
type: 'simple', value: '19'
key: 'number', }, {
operator: '=', type: 'simple',
value: '19' key: 'number',
}] operator: '=',
}, key_schema).exec(doc_list); value: '19'
deepEqual(doc_list, [ }]
{'identifier': '10', 'number': '10'}, }, key_schema).
{'identifier': '19', 'number': '19'} exec(docList()).
], 'Key schema should be propagated from complex to simple queries'); then(function (dl) {
deepEqual(dl, [
{'identifier': '10', 'number': '10'},
{'identifier': '19', 'number': '19'}
], 'Key schema should be propagated from complex to simple queries');
})
);
RSVP.all(promise).then(noop).always(start);
}); });
test('Key Schema with translation lookup', function () { test('Key Schema with translation lookup', function () {
var doc_list, docList = function () { var docList = function () {
return [ return [
{'identifier': '1', 'state': 'open'}, {'identifier': '1', 'state': 'open'},
{'identifier': '2', 'state': 'closed'} {'identifier': '2', 'state': 'closed'}
]; ];
}; }, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'translated_state',
value: 'ouvert'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '1', 'state': 'open'}
], 'Key Schema: It should be possible to look for a translated string');
})
);
doc_list = docList(); promise.push(
complex_queries.QueryFactory.create({ complex_queries.QueryFactory.create({
type: 'simple', type: 'simple',
key: 'translated_state', key: 'translated_state',
value: 'ouvert' operator: '=',
}, key_schema).exec(doc_list); value: 'ouvert'
deepEqual(doc_list, [ }, key_schema).
{'identifier': '1', 'state': 'open'} exec(docList()).
], 'Key Schema: It should be possible to look for a translated string'); then(function (dl) {
deepEqual(dl, [
{'identifier': '1', 'state': 'open'}
doc_list = docList(); ], 'Key Schema: It should be possible to look for a translated string with operator =');
complex_queries.QueryFactory.create({ })
type: 'simple', );
key: 'translated_state',
operator: '=',
value: 'ouvert'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'identifier': '1', 'state': 'open'}
], 'Key Schema: It should be possible to look for a translated string with operator =');
// XXX not implemented yet // XXX not implemented yet
...@@ -258,6 +289,7 @@ ...@@ -258,6 +289,7 @@
// {'identifier': '2', 'state': 'closed'} // {'identifier': '2', 'state': 'closed'}
// ], 'Key Schema: It should be possible to look for a translated string with operator !='); // ], 'Key Schema: It should be possible to look for a translated string with operator !=');
RSVP.all(promise).then(noop).always(start);
}); });
})); }));
This diff is collapsed.
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