-
Notifications
You must be signed in to change notification settings - Fork 3
/
arango-datasource.test.js
102 lines (82 loc) · 2.97 KB
/
arango-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
const { ArangoDataSource } = require('.');
const { aql } = require('arangojs');
const hash = require('object-hash');
describe('ArangDataSource', () => {
test('it queries the db (cache miss)', async () => {
const result = { results: 'output' };
const db = createDb(result);
const config = createConfig();
const query = aql`DOCUMENT()`;
const datasource = new ArangoDataSource(db);
datasource.initialize(config);
const output = await datasource.query(query);
expect(output).toEqual(result);
expect(db.query).toHaveBeenCalledTimes(1);
expect(config.cache.set).toHaveBeenCalledTimes(1);
expect(config.cache.set).toHaveBeenCalledWith(hash(query), result);
});
test('it queries the db (cache hit)', async () => {
const result = { results: 'output' };
const query = aql`DOCUMENT()`;
const key = hash(query);
const db = createDb(result);
const config = createConfig({
[key]: result,
});
const datasource = new ArangoDataSource(db);
datasource.initialize(config);
const output = await datasource.query(query);
expect(output).toEqual(result);
expect(db.query).toHaveBeenCalledTimes(0);
expect(config.cache.set).toHaveBeenCalledTimes(0);
expect(config.cache.get).toHaveBeenCalledTimes(1);
expect(config.cache.get).toHaveReturnedWith(Promise.resolve(result));
});
test('it caches a successful query', async () => {
const result = { results: 'output' };
const db = createDb(result);
const config = createConfig();
const query = aql`DOCUMENT()`;
const datasource = new ArangoDataSource(db);
datasource.initialize(config);
for (let i = 1; i <= 3; i++) {
const output = await datasource.query(query);
expect(output).toEqual(result);
expect(db.query).toHaveBeenCalledTimes(1);
expect(config.cache.get).toHaveBeenCalledTimes(i);
expect(config.cache.get).toHaveReturnedWith(Promise.resolve(result));
expect(config.cache.set).toHaveBeenCalledTimes(1);
expect(config.cache.set).toHaveBeenCalledWith(hash(query), result);
}
});
test('it ignores the cache', async () => {
const result = { results: 'output' };
const db = createDb(result);
const config = createConfig();
const query = aql`DOCUMENT()`;
const datasource = new ArangoDataSource(db);
datasource.initialize(config);
const output = await datasource.query(query, { useCache: false });
expect(output).toEqual(result);
expect(db.query).toHaveBeenCalledTimes(1);
expect(config.cache.set).toHaveBeenCalledTimes(0);
expect(config.cache.get).toHaveBeenCalledTimes(0);
});
});
function createDb(queryResults) {
return {
query: jest.fn().mockResolvedValue({
all: () => queryResults,
}),
};
}
function createConfig(cache = {}) {
return {
cache: {
get: jest.fn().mockImplementation(async (key) => cache[key]),
set: jest
.fn()
.mockImplementation(async (key, result) => (cache[key] = result)),
},
};
}