-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
98 lines (79 loc) · 3.69 KB
/
index.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
const Database = require("./index");
const dbName = 'test';
const collectionName = 'new';
const items = ['one', 'two', 'three'].map(val => ({val}));
describe(`Database '${dbName}'`, () => {
it('should create and access db', async () => {
await Database.access(dbName, collectionName);
});
it('should be able to access db again', async () => {
await Database.access(dbName, collectionName);
});
it('should list active databases', () => {
expect(Database.getActiveDBs()).toEqual([dbName]);
});
it('should insert items in order', async () => {
const testDB = await Database.access(dbName, collectionName);
expect(await testDB.insert(...items)).toBe(items.length);
const allItems = await testDB.getAll();
expect(allItems.map(({val}) => val)).toEqual(items.map(({val}) => val));
});
it('should get last item', async () => {
const testDB = await Database.access(dbName, collectionName);
const {val} = await testDB.getLastItem();
expect(val).toEqual(items[items.length - 1].val);
});
it(`should update item: ${JSON.stringify(items[0])}`, async () => {
const testDB = await Database.access(dbName, collectionName);
const filterFunc = ({val}) => val === items[0].val;
const [{_id: keyOfItem}] = await testDB.getAll(filterFunc);
const update = 'newVal';
await testDB.update(keyOfItem, {update});
const newVar = await testDB.getKey(keyOfItem);
const {update: result} = newVar;
expect(result).toEqual(update);
});
it(`should access an existing db and add an item`, async () => {
await Database.closeAllDBs();
const testDB = await Database.access(dbName, collectionName);
const newItem = {val: 'four'};
await testDB.insert(newItem);
const allItems = await testDB.getAll();
expect(allItems.map(({val}) => val)).toEqual(items.concat([newItem]).map(({val}) => val));
const {val} = await testDB.getLastItem();
expect(val).toEqual(newItem.val);
});
it(`should delete item: ${JSON.stringify(items[1])}`, async () => {
const testDB = await Database.access(dbName, collectionName);
const filterFunc = ({val}) => val === items[1].val;
expect(await testDB.deleteBy(filterFunc)).toBe(1);
expect(await testDB.getAll(filterFunc)).toHaveLength(0);
});
it(`should delete last item and have a new last item`, async () => {
const testDB = await Database.access(dbName, collectionName);
await testDB.deleteKey(testDB.latestKey);
expect(await testDB.getLastItem()).toBeDefined();
});
it(`should be able to handle empty operations`, async () => {
const testDB = await Database.access(dbName, collectionName);
expect(await testDB.insert()).toBe(0);
const allItems = await testDB.getAll();
expect(allItems.length).toBeGreaterThan(0);
expect(await testDB.deleteBy(() => false)).toBe(0);
expect(await testDB.deleteBy()).toBe(allItems.length);
});
it('should keep different collections separate', async () => {
const testDB1 = await Database.access(dbName, collectionName + 1);
const testDB2 = await Database.access(dbName, collectionName + 2);
await testDB1.insert(items[0]);
await testDB2.insert(items[0]);
const allItems1 = await testDB1.getAll();
const allItems2 = await testDB2.getAll();
expect(allItems1).toHaveLength(1);
expect(allItems2).toHaveLength(1);
expect(allItems1[0]._id).toEqual(allItems2[0]._id)
});
afterAll(async () => {
(await Database.access(dbName, collectionName))._level.clear();
});
});