|
| 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 | + }); |
| 13 | + |
| 14 | + it('returns all keys - Callback API', function(done) { |
| 15 | + helpers.givenKeys(CacheItem, ['key1', 'key2'], function(err) { |
| 16 | + if (err) return done(err); |
| 17 | + CacheItem.keys(function(err, keys) { |
| 18 | + if (err) return done(err); |
| 19 | + keys.sort(); |
| 20 | + should(keys).eql(['key1', 'key2']); |
| 21 | + done(); |
| 22 | + }); |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns all keys - Promise API', function() { |
| 27 | + return helpers.givenKeys(CacheItem, ['key1', 'key2']) |
| 28 | + .then(function() { |
| 29 | + return CacheItem.keys(); |
| 30 | + }) |
| 31 | + .then(function(keys) { |
| 32 | + keys.sort(); |
| 33 | + should(keys).eql(['key1', 'key2']); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it('returns keys of the given model only', function() { |
| 38 | + var AnotherModel = CacheItem.dataSource.createModel('AnotherModel'); |
| 39 | + return helpers.givenKeys(CacheItem, ['key1', 'key2']) |
| 40 | + .then(function() { |
| 41 | + return helpers.givenKeys(AnotherModel, ['otherKey1', 'otherKey2']); |
| 42 | + }) |
| 43 | + .then(function() { |
| 44 | + return CacheItem.keys(); |
| 45 | + }) |
| 46 | + .then(function(keys) { |
| 47 | + keys.sort(); |
| 48 | + should(keys).eql(['key1', 'key2']); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('handles large key set', function() { |
| 53 | + var expectedKeys = []; |
| 54 | + for (var ix = 0; ix < 1000; ix++) |
| 55 | + expectedKeys.push('key-' + ix); |
| 56 | + |
| 57 | + return helpers.givenKeys(CacheItem, expectedKeys) |
| 58 | + .then(function() { |
| 59 | + return CacheItem.keys(); |
| 60 | + }) |
| 61 | + .then(function(keys) { |
| 62 | + keys.sort(); |
| 63 | + expectedKeys.sort(); |
| 64 | + should(keys).eql(expectedKeys); |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | +}; |
0 commit comments