Skip to content

Commit 20cd31a

Browse files
committed
chore: updated tests
1 parent f18cdf5 commit 20cd31a

File tree

3 files changed

+231
-74
lines changed

3 files changed

+231
-74
lines changed

test/client.js

Lines changed: 98 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ const tap = require('tap')
33
const ImmudbClient = require('../lib/client')
44
const types = require('../lib/types')
55

6-
const unix = Math.floor(Date.now()/1000)
6+
const IMMUDB_HOST = process.env.IMMUDB_HOST || '127.0.0.1'
7+
const IMMUDB_PORT = process.env.IMMUDB_TEST_PORT || 56789
8+
const IMMUDB_USER = process.env.IMMUDB_USER || 'immudb'
9+
const IMMUDB_PWD = process.env.IMMUDB_PWD || 'immudb'
710

811
const setup = (options, t, done) => {
912
ImmudbClient(options, (err, cl) => {
@@ -14,31 +17,39 @@ const setup = (options, t, done) => {
1417

1518
tap.test('database management', (t) => {
1619
const options = {
17-
address: '127.0.0.1:56789',
20+
address: `${IMMUDB_HOST}:${IMMUDB_PORT}`,
1821
}
1922
setup(options, t, async function(cl) {
2023
try {
21-
const rand = '' + Math.floor(Math.random()
22-
* Math.floor(100000))
23-
24-
let req = { username: 'immudb', password: 'immudb' }
24+
// test: login using the specified username and password
25+
let req = { username: IMMUDB_USER, password: IMMUDB_PWD}
2526
let res = await cl.login(req)
2627
t.type(res.token, 'string')
2728

28-
await cl.createDatabase({ database: 'db1' })
29+
// test: create database
30+
req = { database: 'db1' }
31+
await cl.createDatabase(req)
2932

30-
res = await cl.useDatabase({ database: 'db1' })
33+
// test: use database just created
34+
req = { database: 'db1' }
35+
res = await cl.useDatabase(req)
3136
t.type(res.token, 'string')
3237

33-
res = await cl.set({ key: 'key1', value: 'value1' })
38+
// test: add new item having the specified key and value
39+
req = { key: 'key1', value: 'value1' }
40+
res = await cl.set(req)
3441
t.equal(res.index, 0)
3542

43+
// test: list all databases available
3644
res = await cl.listDatabases()
3745
t.equal(res.databases[0], 'defaultdb')
3846
t.equal(res.databases[1], 'db1')
47+
t.notEqual(res.databases[1], 'defaultdb');
3948

49+
// test: print merkle tree
4050
res = await cl.printTree()
4151

52+
// test: check immudb health status
4253
res = await cl.health()
4354
t.true(res.status)
4455

@@ -51,18 +62,20 @@ tap.test('database management', (t) => {
5162

5263
tap.test('user management', (t) => {
5364
const options = {
54-
address: '127.0.0.1:56789',
65+
address: `${IMMUDB_HOST}:${IMMUDB_PORT}`,
5566
rootPath: `${__dirname}/../test/root.json`,
5667
}
5768
setup(options, t, async function(cl) {
5869
try {
5970
const rand = '' + Math.floor(Math.random()
6071
* Math.floor(100000))
6172

62-
let req = { username: 'immudb', password: 'immudb' }
73+
// test: login using the specified username and password
74+
let req = { username: IMMUDB_USER, password: IMMUDB_PWD}
6375
let res = await cl.login(req)
6476
t.type(res.token, 'string')
6577

78+
// test: create a new user
6679
req = {
6780
username: rand,
6881
password: 'Example12#',
@@ -71,8 +84,11 @@ tap.test('user management', (t) => {
7184
}
7285
await cl.createUser(req)
7386

74-
res = await cl.listUsers({})
87+
// test: list all users
88+
req = {}
89+
res = await cl.listUsers(req)
7590

91+
// test: change user permission
7692
req = {
7793
action: types.action.grant,
7894
username: rand,
@@ -81,19 +97,22 @@ tap.test('user management', (t) => {
8197
}
8298
await cl.changePermission(req)
8399

100+
// test: change user password
84101
req = {
85102
username: rand,
86103
old: 'Example12#',
87104
new: 'Example1234%',
88105
}
89106
await cl.changePassword(req)
90107

108+
// test: set active user
91109
req = {
92110
username: rand,
93111
active: true,
94112
}
95113
await cl.setActiveUser(req)
96114

115+
// test: logout
97116
await cl.logout()
98117

99118
t.end()
@@ -105,27 +124,45 @@ tap.test('user management', (t) => {
105124

106125
tap.test('operations', (t) => {
107126
const options = {
108-
address: '127.0.0.1:56789',
127+
address: `${IMMUDB_HOST}:${IMMUDB_PORT}`,
109128
}
110129
setup(options, t, async function(cl) {
111130
try {
112-
const rand = '' + Math.floor(Math.random()
113-
* Math.floor(100000))
131+
// const rand = '' + Math.floor(Math.random()
132+
// * Math.floor(100000))
133+
134+
const rand = 1
135+
const testDB = 'testdb'
114136

115-
let req = { username: 'immudb', password: 'immudb' }
137+
// test: login using the specified username and password
138+
let req = { username: IMMUDB_USER, password: IMMUDB_PWD}
116139
let res = await cl.login(req)
117140

118-
await cl.createDatabase({ database: rand })
141+
// test: create database
142+
req = { database: testDB }
143+
res = await cl.createDatabase(req)
119144

120-
res = await cl.useDatabase({ database: rand })
145+
// test: use database just created
146+
req = { database: testDB }
147+
res = await cl.useDatabase(req)
121148

122-
res = await cl.set({ key: rand, value: rand })
123-
const index = res.index
149+
// test: add new item having the specified key
150+
// and value
151+
req = { key: rand, value: rand }
152+
res = await cl.set(req)
153+
const index = res && res.index // saving for byIndex
124154

155+
// test: get item by key
156+
req = { key: rand }
125157
res = await cl.get({ key: rand })
126158

127-
res = await cl.count({ keyPrefix: rand })
159+
// test: count keys having the specified value
160+
// in the database in use
161+
req = { keyPrefix: rand }
162+
res = await cl.count(req)
128163

164+
// test: iterate over keys having the specified
165+
// prefix
129166
req = {
130167
keyPrefix: rand,
131168
offset: '10',
@@ -135,10 +172,16 @@ tap.test('operations', (t) => {
135172
}
136173
res = await cl.scan(req)
137174

138-
res = await cl.byIndex({ index: index })
175+
// test: return an element by index
176+
req = { index: index }
177+
res = await cl.byIndex()
139178

140-
res = await cl.history({ key: rand })
179+
// history: fetch history for the item having the
180+
// specified key
181+
req = { key: rand }
182+
res = await cl.history(req)
141183

184+
// test: iterate over a sorted set
142185
req = {
143186
set: rand,
144187
offset: '10',
@@ -147,44 +190,58 @@ tap.test('operations', (t) => {
147190
}
148191
res = await cl.zScan(req)
149192

150-
res = await cl.iScan({ pageSize: 1, pageNumber: 1 })
193+
// test: iterate over all elements by
194+
// insertion order
195+
req = { pageSize: 1, pageNumber: 1 }
196+
res = await cl.iScan(req)
151197

198+
// test: execute a batch read
152199
req = {
153200
keys: [{
154201
key: rand,
155202
}],
156203
}
157204
res = await cl.getBatch(req)
158205

206+
// test: add new item having the specified key
207+
// and value
159208
res = await cl.set({ key: rand*2, value: rand*2 })
160209

210+
// test: get current root info
161211
res = await cl.currentRoot()
162212

213+
// test: safely add new item having the specified key
214+
// and value
163215
req = {
164216
key: rand+10,
165217
value: rand+10,
166218
}
167219
res = await cl.safeSet(req)
168220

221+
// test: get current root info
169222
res = await cl.currentRoot()
170223

224+
// test: safely add new item having the specified key
225+
// and value
171226
req = {
172227
key: rand+11,
173228
value: rand+11,
174229
}
175230
res = await cl.safeSet(req)
176231

232+
// test: safely add new item having the specified key
233+
// and value
177234
req = {
178235
key: rand+12,
179236
value: rand+12,
180237
}
181238
res = await cl.safeSet(req)
182239

240+
// test: safely get item by key
183241
req = {
184242
key: rand+12,
185243
}
186244
res = await cl.safeGet(req)
187-
console.log(res)
188245

189246
t.end()
190247
} catch (err) {
@@ -195,30 +252,32 @@ tap.test('operations', (t) => {
195252

196253
tap.test('batches', (t) => {
197254
const options = {
198-
address: '127.0.0.1:56789',
255+
address: `${IMMUDB_HOST}:${IMMUDB_PORT}`,
199256
}
200257
setup(options, t, async function(cl) {
201258
try {
202-
const rand = '' + Math.floor(Math.random()
203-
* Math.floor(100000))
204-
205-
let unix = Math.floor(Date.now()/1000)
206-
let req = { username: 'immudb', password: 'immudb' }
259+
// test: login using the specified username and password
260+
let req = { username: IMMUDB_USER, password: IMMUDB_PWD}
207261
let res = await cl.login(req)
208262

209-
res = await cl.useDatabase({ database: 'defaultdb' })
263+
// test: use default database
264+
req = { database: 'defaultdb' }
265+
res = await cl.useDatabase(req)
210266

211-
req = {
212-
kvList : []
213-
}
267+
// test: execute a batch insert
268+
req = { keys : [] }
214269
for (let i = 0; i < 20; i++) {
215-
req.kvList.push({
216-
key: 'batchKey'+i,
217-
value: 'value'+i,
218-
})
270+
req.keys.push({ key: i, value: i })
219271
}
220272
res = await cl.setBatch(req)
221273

274+
// test: execute a batch read
275+
req = { keys : [] }
276+
for (let i = 0; i < 20; i++) {
277+
req.keys.push({ key: i })
278+
}
279+
res = await cl.getBatch(req)
280+
222281
t.end()
223282
} catch (err) {
224283
t.error(err)

0 commit comments

Comments
 (0)