-
Notifications
You must be signed in to change notification settings - Fork 2
feat(cache): Add internal cache to stomer #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| function Cache(options) { | ||
| this.options = options; | ||
| }; | ||
|
|
||
| Cache.prototype.get = function(key) { | ||
| return null; | ||
| }; | ||
|
|
||
| Cache.prototype.set = function(key, _) { | ||
| return false; | ||
| }; | ||
|
|
||
| Cache.prototype.delete = function(key) { | ||
| return false; | ||
| }; | ||
|
|
||
| module.exports = Cache; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| var Cache = require('../../lib/cache'); | ||
| var util = require('util'); | ||
|
|
||
| function MyCache() { | ||
| Cache.call(this); | ||
| this._internal = new Map(); | ||
| }; | ||
|
|
||
| util.inherits(MyCache, Cache); | ||
|
|
||
| MyCache.prototype.get = function(key) { | ||
| return this._internal.get(key); | ||
| }; | ||
|
|
||
| MyCache.prototype.set = function(key, obj) { | ||
| return this._internal.set(key, obj); | ||
| }; | ||
|
|
||
| MyCache.prototype.delete = function(key) { | ||
| return this._internal.delete(key); | ||
| }; | ||
|
|
||
| module.exports = MyCache; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| var chai = require('chai'); | ||
| var sinon = require('sinon'); | ||
| var Store = require('../lib/store'); | ||
| var MyCache = require('./mock/mycache'); | ||
| chai.should(); | ||
|
|
||
| var sandbox = sinon.sandbox.create(); | ||
|
|
@@ -62,7 +63,7 @@ describe('Store Tests', function() { | |
| var fakeObj = { pk: '1234'}; | ||
| var createStub = sandbox.stub(store, '_set').returns(Promise.resolve()); | ||
|
|
||
| var model = store.define('myModel', { pk: 'String' }); | ||
| var model = store.define('myModel', { pk: { type: 'String', primaryKey: true } }); | ||
| store.create('myModel', fakeObj).then(function() { | ||
| createStub.called.should.be.true; | ||
| createStub.calledWith(model, fakeObj, 'create').should.be.true; | ||
|
|
@@ -74,7 +75,7 @@ describe('Store Tests', function() { | |
| var fakeObj = { pk: '1234'}; | ||
| var updateSpy = sandbox.stub(store, '_set').returns(Promise.resolve()); | ||
|
|
||
| var model = store.define('myModel', { pk: 'String' }); | ||
| var model = store.define('myModel', { pk: { type: 'String', primaryKey: true } }); | ||
| store.update('myModel', fakeObj).then(function() { | ||
| updateSpy.called.should.be.true; | ||
| updateSpy.calledWith(model, fakeObj, 'update').should.be.true; | ||
|
|
@@ -93,5 +94,108 @@ describe('Store Tests', function() { | |
| done(); | ||
| }).catch(done); | ||
| }); | ||
|
|
||
| it('Store.prototype._filter() should return a Promise rejected', function(done) { | ||
| store._filter().catch(function(err) { | ||
| err.message.should.equal('Store.prototype._filter(model, query) is not implemented'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('Store.prototype._get() should return a Promise rejected', function(done) { | ||
| store._get().catch(function(err) { | ||
| err.message.should.equal('Store.prototype._get(model, pk) is not implemented'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('Store.prototype._set() should return a Promise rejected', function(done) { | ||
| store._set().catch(function(err) { | ||
| err.message.should.equal('Store.prototype._set(model, obj, operation) is not implemented'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('Store.prototype._delete() should return a Promise rejected', function(done) { | ||
| store._delete().catch(function(err) { | ||
| err.message.should.equal('Store.prototype._delete(query) is not implemented'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Cache Integration should', function() { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Medium] Can we put the cache test in a seprate file (to be consistent with the project structure)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I am actually testing the integration of cache in store and not the actual cache implementation as it does not exist. I think it make more sense for this to be here.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool |
||
|
|
||
| var cache; | ||
|
|
||
| beforeEach(function() { | ||
| cache = new MyCache(); | ||
| }); | ||
|
|
||
| it('have cache objext', function(done) { | ||
| var store = new Store(); | ||
| store.cache.should.equal(false); | ||
|
|
||
| var store = new Store(cache); | ||
| store.cache.should.equal(cache); | ||
| done(); | ||
| }); | ||
|
|
||
| it('return from cache when get', function(done) { | ||
| var store = new Store(cache); | ||
|
|
||
| var pk = '1234'; | ||
| var instance = Promise.resolve({ foo: 'bar' }); | ||
| var getStub = sandbox.stub(store, '_get').returns(instance); | ||
|
|
||
| var model = store.define('myModel', {}); | ||
|
|
||
| store.get('myModel', pk).then(function(expected) { | ||
| store.get('myModel', pk).then(function(actual) { | ||
| (getStub.calledOnce == true).should.equal(true); | ||
| (getStub.calledWith(model, pk) == true).should.equal(true); | ||
| actual.should.equal(expected); | ||
| done(); | ||
| }).catch(done); | ||
| }).catch(done); | ||
| }); | ||
|
|
||
| it('set to cache when create', function(done) { | ||
| var obj = { pk: '1234' }; | ||
| var resolved = Promise.resolve(obj); | ||
|
|
||
| var store = new Store(cache); | ||
| var model = store.define('myModel', { pk: { type: 'String', primaryKey: true } }); | ||
|
|
||
| var createStub = sandbox.stub(store, '_set').returns(resolved); | ||
| var getStub = sandbox.stub(store, '_get').returns(resolved); | ||
|
|
||
| store.create('myModel', obj).then(function(actual) { | ||
| store.get('myModel', obj.pk).then(function(expected) { | ||
| getStub.called.should.equal(false); | ||
| expected.should.equal(actual); | ||
| done(); | ||
| }).catch(done); | ||
| }).catch(done); | ||
| }); | ||
|
|
||
| it('set to cache when update', function(done) { | ||
| var obj = { pk: '1234' }; | ||
| var resolved = Promise.resolve(obj); | ||
|
|
||
| var store = new Store(cache); | ||
| var model = store.define('myModel', { pk: { type: 'String', primaryKey: true } }); | ||
|
|
||
| var createStub = sandbox.stub(store, '_set').returns(resolved); | ||
| var getStub = sandbox.stub(store, '_get').returns(resolved); | ||
|
|
||
| store.update('myModel', obj).then(function(actual) { | ||
| store.get('myModel', obj.pk).then(function(expected) { | ||
| getStub.called.should.equal(false); | ||
| expected.should.equal(actual); | ||
| done(); | ||
| }).catch(done); | ||
| }).catch(done); | ||
| }); | ||
| }); | ||
|
|
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍