-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwallet-store.test.js
140 lines (116 loc) · 4.38 KB
/
wallet-store.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
135
136
137
138
139
140
// Copyright 2024 Tether Operations Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict'
const test = require('brittle')
const WalletStoreHyperbee = require('../src/wallet-store-hyperbee')
const path = require('path')
const fs = require('fs')
test('WalletStoreHyperbee - Basic operations', async (t) => {
const store = new WalletStoreHyperbee()
await store.init()
t.teardown(async () => {
await store.close()
})
// Test put and get
await store.put('key1', 'value1')
const value = await store.get('key1')
t.is(value, 'value1', 'Get should return the correct value')
// Test delete
await store.delete('key1')
const deletedValue = await store.get('key1')
t.is(deletedValue, null, 'Deleted key should return null')
// Test put with object
const objValue = { name: 'John', age: 30 }
await store.put('key2', objValue)
const retrievedObj = await store.get('key2')
t.alike(retrievedObj, objValue, 'Retrieved object should match the original')
// Test has
const hasKey2 = await store.has('key2')
t.ok(hasKey2, 'Has should return true for existing key')
const hasNonExistent = await store.has('nonexistent')
t.is(hasNonExistent, null, 'Has should return null for non-existent key')
// Test some
let foundValue = false
await store.some((key, value) => {
if (key === 'key2') {
foundValue = true
return true
}
return false
})
t.ok(foundValue, 'Some should find the existing key')
// Test entries
const entries = []
await store.entries((key, value) => {
entries.push({ key, value })
})
t.is(entries.length, 1, 'Entries should return the correct number of items')
t.alike(entries[0], { key: 'key2', value: objValue }, 'Entry should match the stored data')
// Test clear
await store.clear()
const clearedValue = await store.get('key2')
t.is(clearedValue, null, 'Clear should remove all entries')
})
test('WalletStoreHyperbee - New instance', async (t) => {
const mainStore = new WalletStoreHyperbee()
await mainStore.init()
t.teardown(async () => {
await mainStore.close()
})
const instance1 = mainStore.newInstance({ name: 'instance1' })
await instance1.init()
const instance2 = mainStore.newInstance({ name: 'instance2' })
await instance2.init()
t.not(instance1, instance2, 'Instances should be different')
t.is(instance1.name, 'instance1', 'Instance name should be correctly set')
t.is(instance2.name, 'instance2', 'Instance name should be correctly set')
await instance1.put('key', 'value1')
await instance2.put('key', 'value2')
const value1 = await instance1.get('key')
const value2 = await instance2.get('key')
t.is(value1, 'value1', 'Instance 1 should have correct value')
t.is(value2, 'value2', 'Instance 2 should have correct value')
})
const tmpDir = './data'
test('should throw when creating multiple instances with same path and lock', async (t) => {
const config = {
store_path: path.join(tmpDir, 'db1'),
lock: true
}
const i1 = new WalletStoreHyperbee(config)
await i1.init()
await t.exception(async () => {
const i2 = new WalletStoreHyperbee(config)
await i2.init()
}, /ELOCKED/, 'should throw')
const i1a = i1.newInstance({ name: 'subdb' })
t.ok(i1a._store_config.lock === true, 'sub db is locked')
fs.rmSync(tmpDir, { recursive: true, force: true })
})
test('should not when creating multiple instances with same path and lock', async (t) => {
const config = {
store_path: path.join(tmpDir, 'db1'),
lock: false
}
const i1 = new WalletStoreHyperbee(config)
await i1.init()
await i1.put('hello', 'world')
const i2 = new WalletStoreHyperbee(config)
await i2.init()
const res = await i2.get('hello')
t.ok(res === 'world', 'read and write from different instances')
const i1a = i1.newInstance({ name: 'subdb' })
t.ok(i1a._store_config.lock === false, 'sub db is not locked')
fs.rmSync(tmpDir, { recursive: true, force: true })
})