This repository was archived by the owner on Mar 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcrudSpec.js
More file actions
138 lines (105 loc) · 3.19 KB
/
Copy pathcrudSpec.js
File metadata and controls
138 lines (105 loc) · 3.19 KB
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
136
137
138
const assert = require('assert');
const ElasticItems = require('./../src/index');
const movies_config = require('./fixtures/movies_config.json');
const movies_schema = require('./fixtures/movies_schema.json');
const movies = require('./fixtures/movies.json');
const HOST = process.env.HOST || 'http://localhost:9200';
const INDEX = 'test';
const elasticitems = ElasticItems({
host: HOST,
index: INDEX,
type: INDEX,
}, movies_config);
const { Client } = require('@opensearch-project/opensearch');
const elastic = new Client({
node: HOST
});
describe('should make crud operations', function() {
before(async function() {
await elastic.indices.delete({
index: INDEX,
ignore_unavailable: true,
});
// Create index with schema
await elastic.indices.create({
index: INDEX,
body: movies_schema
});
// Bulk import using native helpers
const bulkBody = movies.flatMap(doc => [
{ index: { _index: INDEX, _id: doc.id } },
doc
]);
await elastic.bulk({
refresh: true,
body: bulkBody
});
});
it('adds new item', async function() {
const v = await elasticitems.add({
id: 'shawshank',
permalink: 'shawshank',
name: 'The Shawshank Redemption',
tags: ['drama', 'escape', 'prison']
}, {
refresh: true
});
assert.equal('created', v.result);
});
it('should get item', async function() {
const v = await elasticitems.get('shawshank');
assert.equal('shawshank', v.id);
assert.equal('The Shawshank Redemption', v.name);
});
it('should get item by key', async function() {
const v = await elasticitems.getBy('permalink', 'shawshank');
assert.equal('shawshank', v.id);
assert.equal('The Shawshank Redemption', v.name);
});
it('should not get not existing item', async function() {
try {
await elasticitems.get('matrix');
} catch (e) {
assert.equal('Response Error', e.message);
assert.equal(404, e.statusCode);
}
});
it('should make partial update item', async function() {
let v = await elasticitems.partialUpdate('shawshank', {
year: 1995
});
assert.equal('updated', v.result);
assert.equal(2, v._version);
v = await elasticitems.partialUpdate('shawshank', {
year: 1994
});
assert.equal('updated', v.result);
assert.equal(3, v._version);
});
it('should make partial update item with fields out of schema', async function() {
const v = await elasticitems.partialUpdate('shawshank', {
a1: 'xyz',
a2: 1,
a3: new Date()
});
assert.equal('updated', v.result);
});
it('should get item', async function() {
const v = await elasticitems.get('shawshank');
assert.equal('shawshank', v.id);
assert.equal(1994, v.year);
assert.equal('The Shawshank Redemption', v.name);
});
it('should delete item', async function() {
const v = await elasticitems.delete('shawshank');
assert.equal('deleted', v.result);
});
it('should not get not existing item', async function() {
try {
await elasticitems.get('shawshank');
} catch (e) {
assert.equal('Response Error', e.message);
assert.equal(404, e.statusCode);
}
});
});