Blame view

node_modules/loopback-datasource-juggler/test/kvao/keys.suite.js 3.2 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
  'use strict';
  
  var bdd = require('../helpers/bdd-if');
  var helpers = require('./_helpers');
  var Promise = require('bluebird');
  var should = require('should');
  
  module.exports = function(dataSourceFactory, connectorCapabilities) {
    var canIterateKeys = connectorCapabilities.canIterateKeys !== false;
  
    bdd.describeIf(canIterateKeys, 'keys', function() {
      var CacheItem;
      beforeEach(function unpackContext() {
        CacheItem = helpers.givenCacheItem(dataSourceFactory);
        CacheItem.sortedKeys = function(filter, options) {
          return this.keys(filter, options).then(function(keys) {
            keys.sort();
            return keys;
          });
        };
      });
  
      it('returns all keys - Callback API', function(done) {
        helpers.givenKeys(CacheItem, ['key1', 'key2'], function(err) {
          if (err) return done(err);
          CacheItem.keys(function(err, keys) {
            if (err) return done(err);
            keys.sort();
            should(keys).eql(['key1', 'key2']);
            done();
          });
        });
      });
  
      it('returns all keys - Promise API', function() {
        return helpers.givenKeys(CacheItem, ['key1', 'key2'])
          .then(function() {
            return CacheItem.keys();
          })
          .then(function(keys) {
            keys.sort();
            should(keys).eql(['key1', 'key2']);
          });
      });
  
      it('returns keys of the given model only', function() {
        var AnotherModel = CacheItem.dataSource.createModel('AnotherModel');
        return helpers.givenKeys(CacheItem, ['key1', 'key2'])
          .then(function() {
            return helpers.givenKeys(AnotherModel, ['otherKey1', 'otherKey2']);
          })
          .then(function() {
            return CacheItem.sortedKeys();
          })
          .then(function(keys) {
            should(keys).eql(['key1', 'key2']);
          });
      });
  
      var largeKeySets = connectorCapabilities.canIterateLargeKeySets !== false;
      bdd.itIf(largeKeySets, 'handles large key set', function() {
        var expectedKeys = [];
        for (var ix = 0; ix < 1000; ix++)
          expectedKeys.push('key-' + ix);
        expectedKeys.sort();
  
        return helpers.givenKeys(CacheItem, expectedKeys)
          .then(function() {
            return CacheItem.sortedKeys();
          })
          .then(function(keys) {
            should(keys).eql(expectedKeys);
          });
      });
  
      context('with "filter.match"', function() {
        beforeEach(function createTestData() {
          return helpers.givenKeys(CacheItem, [
            'hallo',
            'hello',
            'hxllo',
            'hllo',
            'heeello',
            'foo',
            'bar',
          ]);
        });
  
        it('supports "?" operator', function() {
          return CacheItem.sortedKeys({match: 'h?llo'}).then(function(keys) {
            should(keys).eql(['hallo', 'hello', 'hxllo']);
          });
        });
  
        it('supports "*" operator', function() {
          return CacheItem.sortedKeys({match: 'h*llo'}).then(function(keys) {
            should(keys).eql(['hallo', 'heeello', 'hello', 'hllo', 'hxllo']);
          });
        });
  
        it('handles no matches found', function() {
          return CacheItem.sortedKeys({match: 'not-found'})
            .then(function(keys) {
              should(keys).eql([]);
            });
        });
      });
    });
  };