-
Notifications
You must be signed in to change notification settings - Fork 3
/
arango-document-datasource.test.js
135 lines (107 loc) · 3.68 KB
/
arango-document-datasource.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const { ArangoDocumentDataSource } = require('.');
describe('ArangDocumentDataSource', () => {
test('it batches key loads', async () => {
const results = [{ _id: '123' }, { _id: '456' }];
const db = createDb(results);
const datasource = new ArangoDocumentDataSource(db);
datasource.initialize();
const docs = await Promise.all([
datasource.get('123'),
datasource.get('456'),
]);
expect(docs).toEqual(results);
expect(db.query).toHaveBeenCalledTimes(1);
});
test('it caches key loads', async () => {
const results = [{ _id: '123' }];
const db = createDb(results);
const datasource = new ArangoDocumentDataSource(db);
datasource.initialize();
for (let i = 0; i < 3; i++) {
const doc = await datasource.get('123');
expect(db.query).toHaveBeenCalledTimes(1);
expect(doc).toEqual(results[0]);
}
});
test('it returns docs for several keys', async () => {
const results = [{ _id: '123' }, { _id: '456' }];
const db = createDb(results);
const datasource = new ArangoDocumentDataSource(db);
datasource.initialize();
const docs = await datasource.getMany(['456', '123']);
expect(docs).toEqual(results.reverse());
expect(db.query).toHaveBeenCalledTimes(1);
});
describe(`exists`, () => {
test(`it returns true if a document exists`, async () => {
const results = ['123'];
const db = createExistsDb(results);
const datasource = new ArangoDocumentDataSource(db);
datasource.initialize();
const exists = await datasource.exists('123');
expect(exists).toBe(true);
});
test(`it caches the results`, async () => {
const results = ['123'];
const db = createExistsDb(results);
const datasource = new ArangoDocumentDataSource(db);
datasource.initialize();
await datasource.exists('123');
await datasource.exists('123');
expect(db.query).toHaveBeenCalledTimes(1);
});
test(`it batches the calls`, async () => {
const results = ['123'];
const db = createExistsDb(results);
const datasource = new ArangoDocumentDataSource(db);
datasource.initialize();
await Promise.all([datasource.exists('123'), datasource.exists('123')]);
expect(db.query).toHaveBeenCalledTimes(1);
});
});
describe(`manyExist`, () => {
test(`it returns a list of document existence`, async () => {
const results = ['123'];
const db = createExistsDb(results);
const datasource = new ArangoDocumentDataSource(db);
datasource.initialize();
const exists = await datasource.manyExist(['123', '456']);
expect(exists[0]).toBe(true);
expect(exists[1]).toBe(false);
});
test(`it caches the results`, async () => {
const results = ['123'];
const db = createExistsDb(results);
const datasource = new ArangoDocumentDataSource(db);
datasource.initialize();
await datasource.manyExist(['123']);
await datasource.manyExist(['123']);
expect(db.query).toHaveBeenCalledTimes(1);
});
test(`it batches the calls`, async () => {
const results = ['123'];
const db = createExistsDb(results);
const datasource = new ArangoDocumentDataSource(db);
datasource.initialize();
await Promise.all([
datasource.manyExist(['123']),
datasource.manyExist(['123']),
]);
expect(db.query).toHaveBeenCalledTimes(1);
});
});
});
function createDb(queryResults) {
return {
query: jest.fn().mockResolvedValue({
all: () => [queryResults],
}),
};
}
function createExistsDb(queryResults) {
return {
query: jest.fn().mockResolvedValue({
all: () => queryResults,
}),
};
}