key-jiodate.tests.js 5.17 KB
Newer Older
Marco Mariani's avatar
Marco Mariani committed
1 2
/*jslint indent: 2, maxlen: 120, nomen: true, vars: true */
/*global define, exports, require, module, complex_queries, jiodate, window, test, ok,
Marco Mariani's avatar
Marco Mariani committed
3
  equal, deepEqual, sinon, start, stop, RSVP */
Marco Mariani's avatar
Marco Mariani committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17

// 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";

18
  module('Custom Key Queries with JIODate');
Marco Mariani's avatar
Marco Mariani committed
19

Marco Mariani's avatar
Marco Mariani committed
20 21 22 23
  var noop = function () {
    return; // use with RSVP.all
  };

Marco Mariani's avatar
Marco Mariani committed
24
  test('Stock comparison operators with year precision', function () {
Marco Mariani's avatar
Marco Mariani committed
25
    var docList = function () {
Marco Mariani's avatar
Marco Mariani committed
26 27 28 29 30 31 32 33 34 35 36 37
      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
        }
      }
Marco Mariani's avatar
Marco Mariani committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    }, query_list = [], promise = [];

    stop();

    promise.push(
      complex_queries.QueryFactory.create({
        type: 'simple',
        key: 'date',
        value: '2011'
      }, key_schema).
        exec(docList()).
        then(function (dl) {
          deepEqual(dl, [
            {'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
          ], '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': '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)');
        })
    );

    promise.push(
      complex_queries.QueryFactory.create({
        type: 'simple',
        key: 'date',
        operator: '<',
        value: '2011'
      }, key_schema).
        exec(docList()).
        then(function (dl) {
          deepEqual(dl, [
            {'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'}
          ], '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': '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)');
        })
    );

    promise.push(
      complex_queries.QueryFactory.create({
        type: 'simple',
        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)');
        })
    );
Marco Mariani's avatar
Marco Mariani committed
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    query_list = [
      [
        'date: < "2011" OR date: "2012-03"',
        [
          {'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
          {'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
        ]
      ],
      [
        'date: >= "2011-01" AND date: != "2012-03-04T08:52:13.746Z"',
        [
          {'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
        ]
      ]
    ];

    query_list.forEach(function (o) {
      var qs = o[0], expected = o[1];
Marco Mariani's avatar
Marco Mariani committed
152 153 154 155 156 157 158
      promise.push(
        complex_queries.QueryFactory.create(qs, key_schema).
          exec(docList()).
          then(function (dl) {
            deepEqual(dl, expected, "Match with '" + qs + "' (parsed query string)");
          })
      );
159 160 161 162 163 164 165 166 167 168
    });

    query_list = [
      'date < "2011"',
      'date <= "2011"',
      'date > "2011"',
      'date >= "2011"'
    ];

    query_list.forEach(function (qs) {
Marco Mariani's avatar
Marco Mariani committed
169 170 171 172 173 174 175 176
      promise.push(
        complex_queries.QueryFactory.create(qs, key_schema).
          exec(docList()).
          then(function (dl) {
            deepEqual(dl, [
            ], "Match with an invalid parsed string " + qs + " should return empty list but not raise errors");
          })
      );
177
    });
Marco Mariani's avatar
Marco Mariani committed
178

Marco Mariani's avatar
Marco Mariani committed
179
    RSVP.all(promise).then(noop).always(start);
Marco Mariani's avatar
Marco Mariani committed
180 181 182
  });

}));