Commit c1f44a98 authored by Marco Mariani's avatar Marco Mariani

Custom operators: fallback to < and = when others are missing

parent 06c4c331
......@@ -255,6 +255,9 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value,
if (value.ne !== undefined) {
return value.ne(comparison_value);
}
if (value.eq !== undefined) {
return !value.eq(comparison_value);
}
if (
convertStringToRegExp(
comparison_value.toString(),
......@@ -311,6 +314,9 @@ SimpleQuery.prototype["<="] = function (object_value, comparison_value) {
if (value.le !== undefined) {
return value.le(comparison_value);
}
if (value.lt !== undefined && value.eq !== undefined) {
return value.lt(comparison_value) || value.eq(comparison_value);
}
return value <= comparison_value;
};
......@@ -335,6 +341,9 @@ SimpleQuery.prototype[">"] = function (object_value, comparison_value) {
if (value.gt !== undefined) {
return value.gt(comparison_value);
}
if (value.lt !== undefined && value.eq !== undefined) {
return !(value.lt(comparison_value) || value.eq(comparison_value));
}
return value > comparison_value;
};
......@@ -359,6 +368,9 @@ SimpleQuery.prototype[">="] = function (object_value, comparison_value) {
if (value.ge !== undefined) {
return value.ge(comparison_value);
}
if (value.lt !== undefined) {
return !value.lt(comparison_value);
}
return value >= comparison_value;
};
......
/*jslint indent: 2, maxlen: 120, nomen: true, vars: true */
/*global define, exports, require, module, complex_queries, jiodate, window, test, ok,
equal, deepEqual, sinon */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(require('complex_queries'), require('jiodate'));
}
module(complex_queries, jiodate);
}(['complex_queries', 'jiodate', 'qunit'], function (complex_queries, jiodate) {
"use strict";
module('Custom keys: fallback to < and = for missing operators');
test('Stock comparison operators with year precision', function () {
var doc_list, docList = function () {
return [
{'identifier': 'twenty ten', 'date': '2010-03-04T08:52:13.746Z'},
{'identifier': 'twenty eleven', 'date': '2011-03-04T08:52:13.746Z'},
{'identifier': 'twenty twelve', 'date': '2012-03-04T08:52:13.746Z'}
];
}, key_schema = {
key_set: {
date: {
read_from: 'date',
cast_to: jiodate.JIODate
}
}
};
jiodate.JIODate.prototype.ne = undefined;
jiodate.JIODate.prototype.le = undefined;
jiodate.JIODate.prototype.gt = undefined;
jiodate.JIODate.prototype.ge = undefined;
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
], 'Match with "date = 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '!=',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date != 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '<',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'}
], 'Match with "date < 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '<=',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
], 'Match with "date <= 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '>',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date > 2011" (query tree form)');
doc_list = docList();
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '>=',
value: '2011'
}, key_schema).exec(doc_list);
deepEqual(doc_list, [
{'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)');
});
}));
......@@ -27,6 +27,8 @@
<script src="../lib/moment/moment-2.5.0.js"></script>
<script src="../src/jio.date/jiodate.js"></script>
<script src="queries/jiodate.tests.js"></script>
<script src="queries/jiodate.key.tests.js"></script>
<script src="queries/keys.eq-lt.tests.js"></script>
<script src="../src/jio.storage/localstorage.js"></script>
<script src="queries/localstorage-keys.tests.js"></script>
......
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