|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var helpers = require('./_helpers'); |
| 4 | +var Promise = require('bluebird'); |
| 5 | +var should = require('should'); |
| 6 | + |
| 7 | +module.exports = function(dataSourceFactory, connectorCapabilities) { |
| 8 | + describe('keys', function() { |
| 9 | + var CacheItem; |
| 10 | + beforeEach(function unpackContext() { |
| 11 | + CacheItem = helpers.givenCacheItem(dataSourceFactory); |
| 12 | + CacheItem.sortedKeys = function(filter, options) { |
| 13 | + return this.keys(filter, options).then(function(keys) { |
| 14 | + keys.sort(); |
| 15 | + return keys; |
| 16 | + }); |
| 17 | + }; |
| 18 | + }); |
| 19 | + |
| 20 | + it('returns all keys - Callback API', function(done) { |
| 21 | + helpers.givenKeys(CacheItem, ['key1', 'key2'], function(err) { |
| 22 | + if (err) return done(err); |
| 23 | + CacheItem.keys(function(err, keys) { |
| 24 | + if (err) return done(err); |
| 25 | + keys.sort(); |
| 26 | + should(keys).eql(['key1', 'key2']); |
| 27 | + done(); |
| 28 | + }); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns all keys - Promise API', function() { |
| 33 | + return helpers.givenKeys(CacheItem, ['key1', 'key2']) |
| 34 | + .then(function() { |
| 35 | + return CacheItem.keys(); |
| 36 | + }) |
| 37 | + .then(function(keys) { |
| 38 | + keys.sort(); |
| 39 | + should(keys).eql(['key1', 'key2']); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns keys of the given model only', function() { |
| 44 | + var AnotherModel = CacheItem.dataSource.createModel('AnotherModel'); |
| 45 | + return helpers.givenKeys(CacheItem, ['key1', 'key2']) |
| 46 | + .then(function() { |
| 47 | + return helpers.givenKeys(AnotherModel, ['otherKey1', 'otherKey2']); |
| 48 | + }) |
| 49 | + .then(function() { |
| 50 | + return CacheItem.sortedKeys(); |
| 51 | + }) |
| 52 | + .then(function(keys) { |
| 53 | + should(keys).eql(['key1', 'key2']); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('handles large key set', function() { |
| 58 | + var expectedKeys = []; |
| 59 | + for (var ix = 0; ix < 1000; ix++) |
| 60 | + expectedKeys.push('key-' + ix); |
| 61 | + expectedKeys.sort(); |
| 62 | + |
| 63 | + return helpers.givenKeys(CacheItem, expectedKeys) |
| 64 | + .then(function() { |
| 65 | + return CacheItem.sortedKeys(); |
| 66 | + }) |
| 67 | + .then(function(keys) { |
| 68 | + should(keys).eql(expectedKeys); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + context('with "filter.match"', function() { |
| 73 | + beforeEach(function createTestData() { |
| 74 | + return helpers.givenKeys(CacheItem, [ |
| 75 | + 'hallo', |
| 76 | + 'hello', |
| 77 | + 'hxllo', |
| 78 | + 'hllo', |
| 79 | + 'heeello', |
| 80 | + 'foo', |
| 81 | + 'bar', |
| 82 | + ]); |
| 83 | + }); |
| 84 | + |
| 85 | + it('supports "?" operator', function() { |
| 86 | + return CacheItem.sortedKeys({ match: 'h?llo' }).then(function(keys) { |
| 87 | + should(keys).eql(['hallo', 'hello', 'hxllo']); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('supports "*" operator', function() { |
| 92 | + return CacheItem.sortedKeys({ match: 'h*llo' }).then(function(keys) { |
| 93 | + should(keys).eql(['hallo', 'heeello', 'hello', 'hllo', 'hxllo']); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('handles no matches found', function() { |
| 98 | + return CacheItem.sortedKeys({ match: 'not-found' }) |
| 99 | + .then(function(keys) { |
| 100 | + should(keys).eql([]); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}; |
0 commit comments