-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcache.test.js
134 lines (100 loc) · 4.23 KB
/
cache.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
const cachedDocument = require('../lib/cached-document');
const stub = require('sinon').stub;
const test = require('tape');
const cache = require('../bin/cache.js');
const elastic = require('./helpers/mock-database')();
const archiveTree = require('../lib/archive-tree');
test('Cache Error when starting', async function (t) {
t.plan(2);
const cacheStart = stub(cache, 'start').rejects({ code: 'ECONNREFUSED' });
let data;
let error;
try {
data = await cachedDocument(elastic, 'smga-documents-110000013', 'smga-documents-110000003');
} catch (err) {
error = err;
}
t.ok(error, 'Error returned from cache start');
t.notOk(data, 'no data');
cacheStart.restore();
});
test('Get fonds data', async function (t) {
t.plan(1);
const cacheStart = stub(cache, 'start').resolves();
const cacheGet = stub(cache, 'get').resolves();
const elasticGet = stub(elastic, 'get').resolves({ body: { _source: { level: { value: 'fonds' }, summary: { title: 'doc' }, identifier: [{ value: 'BAB' }] } } });
const data = await cachedDocument(elastic, 'smga-documents-110000013', 'smga-documents-110000003');
t.ok(data, 'data from elasticsearch');
cacheStart.restore();
cacheGet.restore();
elasticGet.restore();
});
test('Get child document data', async function (t) {
t.plan(1);
const cacheStart = stub(cache, 'start').resolves();
const cacheGet = stub(cache, 'get').resolves();
const cacheSet = stub(cache, 'set').resolves();
const elasticGet = stub(elastic, 'get').resolves({ body: { _source: { level: { value: 'document' }, fonds: [{ '@admin': { uid: 'smga-documents-110000003' }, summary: { title: 'doc' } }], summary: { title: 'doc' }, identifier: [{ value: 'BAB' }] } } });
const archiveTreeSort = stub(archiveTree, 'sortChildren').callsFake(function (data) {
return {};
});
const data = await cachedDocument(elastic, 'smga-documents-110000013', 'smga-documents-110000003');
t.ok(data, 'data from elasticsearch');
cacheStart.restore();
cacheGet.restore();
cacheSet.restore();
elasticGet.restore();
archiveTreeSort.restore();
});
test('Get single document data', async function (t) {
t.plan(1);
const cacheStart = stub(cache, 'start').resolves();
const cacheGet = stub(cache, 'get').resolves();
const cacheSet = stub(cache, 'set').resolves();
const elasticGet = stub(elastic, 'get').resolves({ body: { _source: { level: { value: 'document' }, summary: { title: 'doc' }, identifier: [{ value: 'BAB' }] } } });
const archiveTreeSort = stub(archiveTree, 'sortChildren').callsFake(function (data) {
return {};
});
const data = await cachedDocument(elastic, 'smga-documents-110000013', 'smga-documents-110000003');
t.ok(data, 'data from elasticsearch');
cacheStart.restore();
cacheGet.restore();
cacheSet.restore();
elasticGet.restore();
archiveTreeSort.restore();
});
test('Get single document data', async function (t) {
t.plan(2);
const cacheStart = stub(cache, 'start').resolves();
const cacheGet = stub(cache, 'get').resolves();
const cacheSet = stub(cache, 'set').resolves();
const elasticGet = stub(elastic, 'get').resolves({ body: { _source: { level: { value: 'document' }, fonds: [{ '@admin': { uid: 'smga-documents-110000003' }, summary: { title: 'doc' } }], summary: { title: 'doc' }, identifier: [{ value: 'BAB' }] } } });
const elasticSearch = stub(elastic, 'search').rejects(new Error());
const archiveTreeSort = stub(archiveTree, 'sortChildren').callsFake(function (data) {
return {};
});
let data;
let error;
try {
data = await cachedDocument(elastic, 'smga-documents-110000013', 'smga-documents-110000003');
} catch (err) {
error = err;
}
t.ok(error, 'Error from elasticsearch');
t.notOk(data, 'no data');
cacheStart.restore();
cacheGet.restore();
cacheSet.restore();
elasticGet.restore();
elasticSearch.restore();
archiveTreeSort.restore();
});
test('Get single document data', async function (t) {
t.plan(1);
const cacheStart = stub(cache, 'start').resolves();
const cacheGet = stub(cache, 'get').resolves({ item: '123' });
const data = await cachedDocument(elastic, 'smga-documents-110000013', 'smga-documents-110000003');
t.equal(data, '123', 'cached item is returned correctly');
cacheStart.restore();
cacheGet.restore();
});