This repository was archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathElementRXDBAdapter.spec.js
More file actions
109 lines (95 loc) · 2.9 KB
/
ElementRXDBAdapter.spec.js
File metadata and controls
109 lines (95 loc) · 2.9 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
const ElementRXDBAdapter = require('./ElementRXDBAdapter');
let db;
const dbName = 'elementrxdb';
beforeAll(async () => {
db = new ElementRXDBAdapter({
name: dbName,
adapter: 'memory',
});
});
afterAll(async () => {
await db.close();
});
describe('ElementRXDBAdapter', () => {
const id1 = 'id1';
const id2 = 'id2';
const id3 = 'id3';
const type1 = 'type1';
const type2 = 'type2';
const type3 = 'type3';
const anchorFileHash1 = 'anchorFileHash1';
const anchorFileHash2 = 'anchorFileHash2';
describe('constructor', () => {
it('should set dbName', () => {
expect(db.name).toBe(dbName);
});
it('should set adapter', () => {
expect(db.adapter).toBeDefined();
});
});
describe('write', () => {
it('should create a record', async () => {
const record = await db.write(id1, {
anchorFileHash: anchorFileHash1,
});
expect(record.id).toBe(id1);
expect(record.anchorFileHash).toBe(anchorFileHash1);
});
it('overwrite the record if same id is specified', async () => {
const record = await db.write(id1, {
anchorFileHash: anchorFileHash2,
type: type1,
});
expect(record.id).toBe(id1);
expect(record.anchorFileHash).toBe(anchorFileHash2);
expect(record.type).toBe(type1);
});
});
describe('read', () => {
it('should return the record if it exists', async () => {
const record = await db.read(id1);
expect(record.id).toBe(id1);
expect(record.anchorFileHash).toBe(anchorFileHash2);
expect(record.type).toBe(type1);
});
it('should return null if record does not exist', async () => {
const record = await db.read(id2);
expect(record).toBe(null);
});
});
describe('readCollection', () => {
beforeAll(async () => {
// Create a new record with the same type
await db.write(id2, {
anchorFileHash: anchorFileHash1,
type: type1,
});
// Create a new record with a different type
await db.write(id3, {
anchorFileHash: anchorFileHash1,
type: type2,
});
});
it('should return only records of the given type', async () => {
const record = await db.readCollection(type1);
expect(record.length).toBe(2);
expect(record[0].id).toBe(id1);
expect(record[1].id).toBe(id2);
});
it('should return an empty array if no record has the given type', async () => {
const record = await db.readCollection(type3);
expect(record).toEqual([]);
});
});
describe('deleteDB', () => {
it('should remove all records', async () => {
expect(await db.read(id1)).toBeDefined();
expect(await db.read(id2)).toBeDefined();
expect(await db.read(id3)).toBeDefined();
await db.deleteDB();
expect(await db.read(id1)).toBe(null);
expect(await db.read(id2)).toBe(null);
expect(await db.read(id3)).toBe(null);
});
});
});