-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve code in factories and in SlidingExpiration add unit tests
- Loading branch information
1 parent
7c0c11d
commit 20437a4
Showing
15 changed files
with
691 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { expect } from 'chai'; | ||
import * as sinon from 'sinon'; | ||
|
||
import { ClassCacheProvider } from '../../../lib/cache/cacheProvider/ClassCacheProvider'; | ||
import { CacheFactory } from '../../../lib/cache/caches/factory'; | ||
|
||
describe('@cache ClassCacheProvider', () => { | ||
|
||
let cacheFactoryStub: sinon.SinonStubbedInstance<CacheFactory>; | ||
let service: ClassCacheProvider; | ||
|
||
beforeEach(() => { | ||
cacheFactoryStub = sinon.createStubInstance(CacheFactory); | ||
|
||
service = new ClassCacheProvider(cacheFactoryStub as any); | ||
}); | ||
|
||
describe('constructor', () => { | ||
|
||
it('should create', () => expect(service).to.be.instanceOf(ClassCacheProvider)); | ||
|
||
it('should init cache property to null', () => expect(service['cache']).to.be.null); | ||
|
||
}); | ||
|
||
describe('get', () => { | ||
|
||
it('should call CacheFactory.create to create an instance at first call', () => { | ||
const cacheInstance = {} as any; | ||
cacheFactoryStub.create.returns(cacheInstance); | ||
|
||
expect(service.get()).to.be.equals(cacheInstance); | ||
expect(cacheFactoryStub.create.calledOnce).to.be.true; | ||
expect(service['cache']).to.be.equals(cacheInstance); | ||
}); | ||
|
||
it('should return existent instance of cache if is not first call', () => { | ||
const response = service['cache'] = {} as any; | ||
|
||
expect(cacheFactoryStub.create.called).to.be.false; | ||
expect(service.get()).to.be.equals(response); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { expect } from 'chai'; | ||
import * as sinon from 'sinon'; | ||
|
||
import { InstanceCacheProvider } from '../../../lib/cache/cacheProvider/InstanceCacheProvider'; | ||
import { CacheFactory } from '../../../lib/cache/caches/factory'; | ||
|
||
describe('@cache InstanceCacheProvider', () => { | ||
|
||
let cacheFactoryStub: sinon.SinonStubbedInstance<CacheFactory>; | ||
let service: InstanceCacheProvider; | ||
|
||
beforeEach(() => { | ||
cacheFactoryStub = sinon.createStubInstance(CacheFactory); | ||
|
||
service = new InstanceCacheProvider(cacheFactoryStub as any); | ||
}); | ||
|
||
describe('constructor', () => { | ||
|
||
it('should create', () => expect(service).to.be.instanceOf(InstanceCacheProvider)); | ||
|
||
it('should init instancesCaches property', () => { | ||
expect(service['instanceCaches']).to.be.instanceOf(WeakMap); | ||
}); | ||
|
||
}); | ||
|
||
describe('get', () => { | ||
|
||
it('should create new cache instance if was called first time with this instance', () => { | ||
const result = {} as any; | ||
const instance = {} as any; | ||
cacheFactoryStub.create.returns(result); | ||
|
||
expect(service.get(instance)).to.be.equals(result); | ||
expect(service['instanceCaches'].get(instance)).to.be.equals(result); | ||
expect(cacheFactoryStub.create.calledOnce).to.be.true; | ||
}); | ||
|
||
it('should return already created cache for current isntance', () => { | ||
const result = {} as any; | ||
const instance = {} as any; | ||
service['instanceCaches'].set(instance, result); | ||
|
||
expect(service.get(instance)).to.be.equals(result); | ||
expect(cacheFactoryStub.create.called).to.be.false; | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { expect } from 'chai'; | ||
|
||
import { ClassCacheProvider } from '../../../lib/cache/cacheProvider/ClassCacheProvider'; | ||
import { CacheProviderFactory } from '../../../lib/cache/cacheProvider/factory'; | ||
import { InstanceCacheProvider } from '../../../lib/cache/cacheProvider/InstanceCacheProvider'; | ||
|
||
describe('@cache CacheProviderFactory', () => { | ||
|
||
describe('constructor', () => { | ||
|
||
it('should create', () => { | ||
expect(new CacheProviderFactory('class', undefined)).to.be.instanceOf(CacheProviderFactory); | ||
}); | ||
|
||
}); | ||
|
||
describe('create', () => { | ||
|
||
it('should create instanceof ClassCacheProvider if scope is "class"', () => { | ||
const instance = new CacheProviderFactory('class', undefined); | ||
expect(instance.create()).to.be.instanceOf(ClassCacheProvider); | ||
}); | ||
|
||
it('should create instanceof InstanceCacheProvider if scope is "instance"', () => { | ||
const instance = new CacheProviderFactory('instance', undefined); | ||
expect(instance.create()).to.be.instanceOf(InstanceCacheProvider); | ||
}); | ||
|
||
it('should throw error if scope options is not a valid one', () => { | ||
const scope = '123' as any; | ||
const message = `@cache invalid scope option: ${scope}.`; | ||
|
||
const instance = new CacheProviderFactory(scope, undefined); | ||
|
||
expect(() => instance.create()).to.throw(message); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { expect } from 'chai'; | ||
import * as sinon from 'sinon'; | ||
|
||
import { Cache } from '../../../lib/cache/caches/Cache'; | ||
import { AbsoluteExpiration } from '../../../lib/cache/expirations/AbsoluteExpiration'; | ||
import { Expiration } from '../../../lib/cache/expirations/Expiration'; | ||
import { MemoryStorage } from '../../../lib/cache/storages/MemoryStorage'; | ||
import { Storage } from '../../../lib/cache/storages/Storage'; | ||
import { HashService } from '../../../lib/utils/hash'; | ||
|
||
describe('@cache Cache', () => { | ||
|
||
let hashStub: sinon.SinonStubbedInstance<HashService>; | ||
let storageStub: sinon.SinonStubbedInstance<Storage>; | ||
let expirationStub: sinon.SinonStubbedInstance<Expiration>; | ||
let service: Cache; | ||
|
||
beforeEach(() => { | ||
hashStub = sinon.createStubInstance(HashService); | ||
storageStub = sinon.createStubInstance(MemoryStorage); | ||
expirationStub = sinon.createStubInstance(AbsoluteExpiration); | ||
|
||
service = new Cache(storageStub, expirationStub, hashStub); | ||
}); | ||
|
||
describe('constructor', () => { | ||
|
||
it('should create', () => expect(service).to.be.instanceOf(Cache)); | ||
|
||
}); | ||
|
||
describe('set', () => { | ||
|
||
it('should call hash.calculate to obtain arguments hash', async () => { | ||
await service.set(['key'], 'value'); | ||
|
||
expect(hashStub.calculate.calledOnce).to.be.true; | ||
}); | ||
|
||
it('should call storage.set to store given hashed key and data', async () => { | ||
const key = 'key'; | ||
hashStub.calculate.returns(key); | ||
|
||
await service.set(['key'], 'value'); | ||
|
||
expect(storageStub.set.calledOnce).to.be.true; | ||
expect(storageStub.set.calledWithExactly(key, 'value')).to.be.true; | ||
}); | ||
|
||
it('should call expiration.add to make cache expirable', async () => { | ||
const key = 'key'; | ||
hashStub.calculate.returns(key); | ||
|
||
await service.set(['key'], 'value'); | ||
|
||
expect(expirationStub.add.calledOnce).to.be.true; | ||
expect(expirationStub.add.calledWith(key, sinon.match.func)).to.be.true; | ||
}); | ||
|
||
describe('function passed to expiration', () => { | ||
|
||
it('should call storage.delete', async () => { | ||
const key = 'key'; | ||
hashStub.calculate.returns(key); | ||
|
||
await service.set(['key'], 'value'); | ||
|
||
const callback = expirationStub.add.firstCall.args[1]; | ||
|
||
await callback(key); | ||
|
||
expect(storageStub.delete.calledOnce).to.be.true; | ||
expect(storageStub.delete.calledWith(key)).to.be.true; | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
describe('has', () => { | ||
|
||
it('should call hash.calculate to obtain arguments hash', async () => { | ||
await service.has(['key']); | ||
|
||
expect(hashStub.calculate.calledOnce).to.be.true; | ||
}); | ||
|
||
it('should call storage has to check if key exists', async () => { | ||
const key = 'key'; | ||
hashStub.calculate.returns(key); | ||
|
||
await service.has(['key']); | ||
|
||
expect(storageStub.has.calledOnce).to.be.true; | ||
expect(storageStub.has.calledWith(key)).to.be.true; | ||
}); | ||
|
||
}); | ||
|
||
describe('get', () => { | ||
|
||
it('should call hash.calculate to obtain arguments hash', async () => { | ||
await service.get(['key']); | ||
|
||
expect(hashStub.calculate.calledOnce).to.be.true; | ||
}); | ||
|
||
it('should call expiration.add to update cache expiration', async () => { | ||
const key = 'key'; | ||
hashStub.calculate.returns(key); | ||
|
||
await service.get(['key']); | ||
|
||
expect(expirationStub.add.calledOnce).to.be.true; | ||
expect(expirationStub.add.calledWith(key, sinon.match.func)).to.be.true; | ||
}); | ||
|
||
it('should call storage.get to obtain cached value', async () => { | ||
const key = 'key'; | ||
hashStub.calculate.returns(key); | ||
|
||
await service.get(['key']); | ||
|
||
expect(storageStub.get.calledOnce).to.be.true; | ||
expect(storageStub.get.calledWith(key)).to.be.true; | ||
}); | ||
|
||
describe('function passed to expiration', () => { | ||
|
||
it('should call storage.delete', async () => { | ||
const key = 'key'; | ||
hashStub.calculate.returns(key); | ||
|
||
await service.get(['key']); | ||
|
||
const callback = expirationStub.add.firstCall.args[1]; | ||
|
||
await callback(key); | ||
|
||
expect(storageStub.delete.calledOnce).to.be.true; | ||
expect(storageStub.delete.calledWith(key)).to.be.true; | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
|
||
}); |
Oops, something went wrong.