-
Notifications
You must be signed in to change notification settings - Fork 742
/
Copy pathmongo.test.js
118 lines (100 loc) · 3.22 KB
/
mongo.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* Copyright 2017 The boardgame.io Authors
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
import { Mongo } from './mongo';
import * as MongoDB from 'mongo-mock';
test('Mongo', async () => {
const mockClient = MongoDB.MongoClient;
const db = new Mongo({ mockClient, url: 'a' });
await db.connect();
// Must return undefined when no game exists.
let state = await db.get('gameID');
expect(state).toEqual(undefined);
// Create game.
await db.set('gameID', { a: 1 });
// Cache hits.
{
// Must return created game.
state = await db.get('gameID');
expect(state).toMatchObject({ a: 1 });
// Must return true if game exists
const has = await db.has('gameID');
expect(has).toBe(true);
}
// Cache misses.
{
// Must return created game.
db.cache.reset();
state = await db.get('gameID');
expect(state).toMatchObject({ a: 1 });
// Must return true if game exists
db.cache.reset();
const has = await db.has('gameID');
expect(has).toBe(true);
}
// Cache size.
{
const db = new Mongo({ mockClient, cacheSize: 1, url: 'a' });
await db.connect();
await db.set('gameID', { a: 1 });
await db.set('another', { b: 1 });
state = await db.get('gameID');
// Check that it came from Mongo and not the cache.
expect(state._id).toBeDefined();
}
{
const db = new Mongo({ url: 'a', dbname: 'test' });
expect(db.client).toBeDefined();
expect(db.dbname).toBe('test');
}
});
test('Mongo - race conditions', async () => {
const mockClient = MongoDB.MongoClient;
const db = new Mongo({ mockClient, url: 'a' });
await db.connect();
// Out of order set()'s.
await db.set('gameID', { _stateID: 1 });
await db.set('gameID', { _stateID: 0 });
expect(await db.get('gameID')).toEqual({ _stateID: 1 });
// Do not override cache on get() if it is fresher than Mongo.
await db.set('gameID', { _stateID: 0 });
db.cache.set('gameID', { _stateID: 1 });
await db.get('gameID');
expect(db.cache.get('gameID')).toEqual({ _stateID: 1 });
// Override if it is staler than Mongo.
await db.set('gameID', { _stateID: 1 });
db.cache.reset();
expect(await db.get('gameID')).toMatchObject({ _stateID: 1 });
expect(db.cache.get('gameID')).toMatchObject({ _stateID: 1 });
});
test('Mongo - list', async () => {
const mockClient = MongoDB.MongoClient;
const db = new Mongo({ mockClient, url: 'a' });
await db.connect();
// Insert 3 entries
await db.set('gameID_0', { a: 0 });
await db.set('gameID_2', { a: 2 });
await db.set('gameID_1', { a: 1 });
const ids = await db.list();
expect(ids).toContain('gameID_0');
expect(ids).toContain('gameID_1');
expect(ids).toContain('gameID_2');
});
test('Mongo - remove', async () => {
const mockClient = MongoDB.MongoClient;
const db = new Mongo({ mockClient, url: 'a' });
await db.connect();
// Insert 2 entries
await db.set('gameID_0', { a: 0 });
await db.set('gameID_1', { a: 1 });
// Remove 1
await db.remove('gameID_1');
expect(await db.has('gameID_0')).toBe(true);
expect(await db.has('gameID_1')).toBe(false);
// Shall not throw error
await db.remove('gameID_1');
});