2019-01-10 23:39:17 +00:00
|
|
|
# Nim-RocksDB
|
|
|
|
# Copyright 2018-2019 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
#
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * GPL license, version 2.0, ([LICENSE-GPLv2](LICENSE-GPLv2) or https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html)
|
|
|
|
#
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2020-04-17 22:18:04 +00:00
|
|
|
{.used.}
|
|
|
|
|
2019-01-10 23:39:17 +00:00
|
|
|
import
|
|
|
|
os, unittest,
|
|
|
|
tempfile,
|
|
|
|
../rocksdb
|
2018-06-24 23:13:10 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
MyDB = object
|
|
|
|
rocksdb: RocksDBInstance
|
|
|
|
|
2018-08-16 01:55:54 +00:00
|
|
|
# TODO no tests for failures / error reporting
|
|
|
|
|
2018-06-24 23:13:10 +00:00
|
|
|
proc initMyDb(path: string): MyDB =
|
|
|
|
let
|
|
|
|
dataDir = path / "data"
|
|
|
|
backupsDir = path / "backups"
|
|
|
|
|
|
|
|
createDir(dataDir)
|
|
|
|
createDir(backupsDir)
|
|
|
|
|
|
|
|
var s = result.rocksdb.init(dataDir, backupsDir)
|
2020-04-17 22:18:04 +00:00
|
|
|
doAssert s.isOk, $s
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2019-01-10 23:39:17 +00:00
|
|
|
suite "Nim API tests":
|
|
|
|
setup:
|
|
|
|
var
|
|
|
|
dbDir = mkdtemp()
|
|
|
|
db = initMyDb(dbDir)
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2019-01-10 23:39:17 +00:00
|
|
|
teardown:
|
|
|
|
close(db.rocksdb)
|
|
|
|
removeDir(dbDir)
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2019-01-10 23:39:17 +00:00
|
|
|
test "Basic operations":
|
2020-04-23 18:55:21 +00:00
|
|
|
proc test() =
|
|
|
|
let key = @[byte(1), 2, 3, 4, 5]
|
|
|
|
let otherKey = @[byte(1), 2, 3, 4, 5, 6]
|
|
|
|
let val = @[byte(1), 2, 3, 4, 5]
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2020-04-23 18:55:21 +00:00
|
|
|
var s = db.rocksdb.put(key, val)
|
|
|
|
check s.isok
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2020-04-23 18:55:21 +00:00
|
|
|
var bytes: seq[byte]
|
|
|
|
check db.rocksdb.get(key, proc(data: openArray[byte]) = bytes = @data)[]
|
|
|
|
check not db.rocksdb.get(
|
|
|
|
otherkey, proc(data: openArray[byte]) = bytes = @data)[]
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2020-04-23 18:55:21 +00:00
|
|
|
var r1 = db.rocksdb.getBytes(key)
|
|
|
|
check r1.isok and r1.value == val
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2020-04-23 18:55:21 +00:00
|
|
|
var r2 = db.rocksdb.getBytes(otherKey)
|
|
|
|
# there's no error string for missing keys
|
|
|
|
check r2.isok == false and r2.error.len == 0
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2020-04-23 18:55:21 +00:00
|
|
|
var e1 = db.rocksdb.contains(key)
|
|
|
|
check e1.isok and e1.value == true
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2020-04-23 18:55:21 +00:00
|
|
|
var e2 = db.rocksdb.contains(otherKey)
|
|
|
|
check e2.isok and e2.value == false
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2023-01-17 16:37:10 +00:00
|
|
|
var d = db.rocksdb.del(key)
|
|
|
|
check d.isok and d.value == true
|
2018-06-24 23:13:10 +00:00
|
|
|
|
2020-04-23 18:55:21 +00:00
|
|
|
e1 = db.rocksdb.contains(key)
|
|
|
|
check e1.isok and e1.value == false
|
|
|
|
|
|
|
|
test()
|