Skip to content

Commit c9eb788

Browse files
author
Albo Vieira
committed
fix unit tests
1 parent 68cd5d7 commit c9eb788

File tree

7 files changed

+139
-141
lines changed

7 files changed

+139
-141
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ sudo apt-get install memcached
6767

6868
```javascript
6969
const client = Cache.create({
70-
host: 'localhost' // if any host was informed, it will set localhost as default
70+
host: 'localhost:11211' // if any host was informed, it will set localhost as default
7171
provider: 'memcached',
7272
ttl: 2000 // a defautt TTL (miliseconds)
7373
});

dist/models/cache-contract.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default interface CacheContract {
2-
get(key: string): any;
3-
has(key: string): any;
4-
delete(key: string): any;
5-
add<T>(key: string, object: T, ttl?: number | string): any;
2+
get<T>(key: string): Promise<T>;
3+
has(key: string): Promise<boolean>;
4+
delete(key: string): Promise<boolean>;
5+
add<T>(key: string, object: T, ttl?: number | string): Promise<boolean>;
66
}

docker-compose.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
version: '3'
22
services:
3+
memcached:
4+
image: memcached
5+
ports:
6+
- '11211:11211'
37
redis:
48
image: 'redis'
59
environment:

tests/base-test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { expect } from 'chai';
2+
import CacheContract from '../src/models/cache-contract';
3+
4+
async function shouldGet(client: CacheContract) {
5+
const done = await client.add('hashKey', { name: 'Albo' });
6+
expect(done).to.be.equal(true);
7+
const result = await client.get('hashKey');
8+
expect(result).to.be.deep.equal({ name: 'Albo' });
9+
}
10+
11+
async function shouldPersist(client: CacheContract) {
12+
const done = await client.add('hashKey', { name: 'Albo' });
13+
expect(done).to.be.equal(true);
14+
const result = await client.get('hashKey');
15+
expect(result).to.be.deep.equal({ name: 'Albo' });
16+
}
17+
18+
async function shouldCheck(client: CacheContract) {
19+
const done = await client.add('teste', { name: 'guarda ai pf' });
20+
expect(done).to.be.equal(true);
21+
const has = await client.has('teste');
22+
expect(has).to.be.equal(has);
23+
}
24+
25+
async function shouldDelete(client: CacheContract) {
26+
const done = await client.add('teste', { name: 'guarda ai pf' });
27+
expect(done).to.be.equal(true);
28+
const deleted = await client.delete('teste');
29+
expect(deleted).to.be.equal(true);
30+
}
31+
32+
async function shouldKeepForATime(client: CacheContract) {
33+
const done = await client.add('hashKey', { name: 'Albo' }, 50);
34+
expect(done).to.be.equal(true);
35+
36+
let result;
37+
const promise = new Promise(resolve => {
38+
setTimeout(async () => {
39+
result = await client.get('hashKey');
40+
resolve(result);
41+
}, 40);
42+
});
43+
result = await promise;
44+
expect(result).to.be.deep.equal({ name: 'Albo' });
45+
}
46+
47+
async function shouldAddTTLByStringFormat(client: CacheContract) {
48+
const done = await client.add('hashKey', { name: 'Albo' }, '2s');
49+
expect(done).to.be.equal(true);
50+
51+
let result;
52+
const promise = new Promise(resolve => {
53+
setTimeout(async () => {
54+
result = await client.get('hashKey');
55+
resolve(result);
56+
}, 900);
57+
});
58+
result = await promise;
59+
expect(result).to.be.deep.equal({ name: 'Albo' });
60+
}
61+
62+
export {
63+
shouldGet,
64+
shouldCheck,
65+
shouldDelete,
66+
shouldAddTTLByStringFormat,
67+
shouldKeepForATime,
68+
shouldPersist
69+
};

tests/in-memory.spec.ts

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import { expect } from 'chai';
33
import { Cache } from './dependencies';
44
import CacheContract from '../src/models/cache-contract';
55
import { ProvidersEnum } from '../src/models/provider';
6+
import {
7+
shouldGet,
8+
shouldPersist,
9+
shouldCheck,
10+
shouldDelete,
11+
shouldKeepForATime,
12+
shouldAddTTLByStringFormat
13+
} from './base-test';
614

715
describe('InMemory', () => {
816
let client: CacheContract;
@@ -15,60 +23,27 @@ describe('InMemory', () => {
1523
});
1624
});
1725

18-
it('Should get from memory', async () => {
19-
const done = await client.add('hashKey', { name: 'Albo' });
20-
expect(done).to.be.equal(true);
21-
const result = await client.get('hashKey');
22-
expect(result).to.be.deep.equal({ name: 'Albo' });
26+
it('Should get', async () => {
27+
shouldGet(client);
2328
});
2429

25-
it('Should persist in memory', async () => {
26-
const done = await client.add('hashKey', { name: 'Albo' });
27-
expect(done).to.be.equal(true);
28-
const result = await client.get('hashKey');
29-
expect(result).to.be.deep.equal({ name: 'Albo' });
30+
it('Should persist', async () => {
31+
shouldPersist(client);
3032
});
3133

32-
it('Should check on redis', async () => {
33-
const done = await client.add('teste', { name: 'guarda ai pf' });
34-
expect(done).to.be.equal(true);
35-
const has = await client.has('teste');
36-
expect(has).to.be.equal(has);
34+
it('Should check if key exist', async () => {
35+
shouldCheck(client);
3736
});
3837

39-
it('Should delete on redis', async () => {
40-
const done = await client.add('teste', { name: 'guarda ai pf' });
41-
expect(done).to.be.equal(true);
42-
const deleted = await client.delete('teste');
43-
expect(deleted).to.be.equal(true);
38+
it('Should delete', async () => {
39+
shouldDelete(client);
4440
});
4541

46-
it('Should keep in memory for the given time', async () => {
47-
const done = await client.add('hashKey', { name: 'Albo' }, 50);
48-
expect(done).to.be.equal(true);
49-
50-
let result;
51-
const promise = new Promise(resolve => {
52-
setTimeout(async () => {
53-
result = await client.get('hashKey');
54-
resolve(result);
55-
}, 40);
56-
});
57-
result = await promise;
58-
expect(result).to.be.deep.equal({ name: 'Albo' });
42+
it('Should keep for a given time', async () => {
43+
shouldKeepForATime(client);
5944
});
6045

61-
it('Should add ttl by string format in memorycache', async () => {
62-
const done = await client.add('hashKey', { name: 'Albo' }, '1s');
63-
expect(done).to.be.equal(true);
64-
let result;
65-
const promise = new Promise(resolve => {
66-
setTimeout(async () => {
67-
result = await client.get('hashKey');
68-
resolve(result);
69-
}, 900);
70-
});
71-
result = await promise;
72-
expect(result).to.be.deep.equal({ name: 'Albo' });
46+
it('Should add ttl by string format', async () => {
47+
shouldAddTTLByStringFormat(client);
7348
});
7449
});

tests/memcached.spec.ts

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import { expect } from 'chai';
33
import { Cache } from './dependencies';
44
import { ProvidersEnum } from '../src/models/provider';
55
import CacheContract from '../src/models/cache-contract';
6+
import {
7+
shouldGet,
8+
shouldPersist,
9+
shouldCheck,
10+
shouldDelete,
11+
shouldKeepForATime,
12+
shouldAddTTLByStringFormat
13+
} from './base-test';
614

715
describe('MemCached', () => {
816
let client: CacheContract;
@@ -15,61 +23,27 @@ describe('MemCached', () => {
1523
});
1624
});
1725

18-
it('Should get from memCached', async () => {
19-
const done = await client.add('hashKey', { name: 'Albo' });
20-
expect(done).to.be.equal(true);
21-
const result = await client.get('hashKey');
22-
expect(result).to.be.deep.equal({ name: 'Albo' });
26+
it('Should get', async () => {
27+
shouldGet(client);
2328
});
2429

25-
it('Should persist in memCached', async () => {
26-
const done = await client.add('hashKey', { name: 'Albo' });
27-
expect(done).to.be.equal(true);
28-
const result = await client.get('hashKey');
29-
expect(result).to.be.deep.equal({ name: 'Albo' });
30+
it('Should persist', async () => {
31+
shouldPersist(client);
3032
});
3133

32-
it('Should check on redis', async () => {
33-
const done = await client.add('teste', { name: 'guarda ai pf' });
34-
expect(done).to.be.equal(true);
35-
const has = await client.has('teste');
36-
expect(has).to.be.equal(has);
34+
it('Should check if key exist', async () => {
35+
shouldCheck(client);
3736
});
3837

39-
it('Should delete on redis', async () => {
40-
const done = await client.add('teste', { name: 'guarda ai pf' });
41-
expect(done).to.be.equal(true);
42-
const deleted = await client.delete('teste');
43-
expect(deleted).to.be.equal(true);
38+
it('Should delete', async () => {
39+
shouldDelete(client);
4440
});
4541

46-
it('Should keep in memory for the given time', async () => {
47-
const done = await client.add('hashKey', { name: 'Albo' }, 50);
48-
expect(done).to.be.equal(true);
49-
50-
let result;
51-
const promise = new Promise(resolve => {
52-
setTimeout(async () => {
53-
result = await client.get('hashKey');
54-
resolve(result);
55-
}, 40);
56-
});
57-
result = await promise;
58-
expect(result).to.be.deep.equal({ name: 'Albo' });
42+
it('Should keep for a given time', async () => {
43+
shouldKeepForATime(client);
5944
});
6045

61-
it('Should add ttl by string format in memCached', async () => {
62-
const done = await client.add('hashKey', { name: 'Albo' }, '2s');
63-
expect(done).to.be.equal(true);
64-
65-
let result;
66-
const promise = new Promise(resolve => {
67-
setTimeout(async () => {
68-
result = await client.get('hashKey');
69-
resolve(result);
70-
}, 900);
71-
});
72-
result = await promise;
73-
expect(result).to.be.deep.equal({ name: 'Albo' });
46+
it('Should add ttl by string format', async () => {
47+
shouldAddTTLByStringFormat(client);
7448
});
7549
});

tests/redis.spec.ts

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
import { before, describe, it } from 'mocha';
1+
import { before, beforeEach, describe, it } from 'mocha';
22
import { expect } from 'chai';
33
import { Cache } from './dependencies';
44
import CacheContract from '../src/models/cache-contract';
55
import { ProvidersEnum } from '../src/models/provider';
6+
import {
7+
shouldGet,
8+
shouldPersist,
9+
shouldCheck,
10+
shouldDelete,
11+
shouldKeepForATime,
12+
shouldAddTTLByStringFormat
13+
} from './base-test';
614

715
describe('Redis', () => {
816
let client: CacheContract;
@@ -22,59 +30,27 @@ describe('Redis', () => {
2230
});
2331
});
2432

25-
it('Should get from redis', async () => {
26-
const done = await client.add('teste', { name: 'guarda ai pf' });
27-
expect(done).to.be.equal(true);
28-
const result = await client.get('teste');
29-
expect(result).to.be.deep.equal({ name: 'guarda ai pf' });
33+
it('Should get', async () => {
34+
shouldGet(client);
3035
});
3136

32-
it('Should persist on redis', async () => {
33-
const done = await client.add('teste', { name: 'guarda ai pf' });
34-
expect(done).to.be.equal(true);
37+
it('Should persist', async () => {
38+
shouldPersist(client);
3539
});
3640

37-
it('Should check on redis', async () => {
38-
const done = await client.add('teste', { name: 'guarda ai pf' });
39-
expect(done).to.be.equal(true);
40-
const has = await client.has('teste');
41-
expect(has).to.be.equal(has);
41+
it('Should check if key exist', async () => {
42+
shouldCheck(client);
4243
});
4344

44-
it('Should delete on redis', async () => {
45-
const done = await client.add('teste', { name: 'guarda ai pf' });
46-
expect(done).to.be.equal(true);
47-
const deleted = await client.delete('teste');
48-
expect(deleted).to.be.equal(true);
45+
it('Should delete', async () => {
46+
shouldDelete(client);
4947
});
5048

51-
it('Should keep in redis for the given time', async () => {
52-
const done = await client.add('hashKey', { name: 'Albo' }, 50);
53-
expect(done).to.be.equal(true);
54-
55-
let result;
56-
const promise = new Promise(resolve => {
57-
setTimeout(async () => {
58-
result = await client.get('hashKey');
59-
resolve(result);
60-
}, 40);
61-
});
62-
result = await promise;
63-
expect(result).to.be.deep.equal({ name: 'Albo' });
49+
it('Should keep for a given time', async () => {
50+
shouldKeepForATime(client);
6451
});
6552

66-
it('Should add ttl by string format in redis', async () => {
67-
const done = await client.add('hashKey', { name: 'Albo' }, '400');
68-
expect(done).to.be.equal(true);
69-
70-
let result;
71-
const promise = new Promise(resolve => {
72-
setTimeout(async () => {
73-
result = await client.get('hashKey');
74-
resolve(result);
75-
}, 300);
76-
});
77-
result = await promise;
78-
expect(result).to.be.deep.equal({ name: 'Albo' });
53+
it('Should add ttl by string format', async () => {
54+
shouldAddTTLByStringFormat(client);
7955
});
8056
});

0 commit comments

Comments
 (0)