mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-02 21:53:05 +00:00
* shorten lines * only return data when `query.value == true` * test `query.value = false` * close mounted ds * allow passing dispose to query iter constructor * fix fs querying * use currentSourcePath * remove `dsobj` extensions from directories * don't return error on missing key delete * return `DatastoreKeyNotFound` on empty `get` * return `DatastoreKeyNotFound` on missing sql key
159 lines
3.6 KiB
Nim
159 lines
3.6 KiB
Nim
import std/os
|
|
|
|
import pkg/chronos
|
|
import pkg/asynctest
|
|
import pkg/stew/byteutils
|
|
|
|
import pkg/sqlite3_abi
|
|
import pkg/datastore/key
|
|
import pkg/datastore/sql/sqlitedsdb
|
|
import pkg/datastore/sql/sqliteds
|
|
|
|
suite "Test Open SQLite Datastore DB":
|
|
let
|
|
path = currentSourcePath() # get this file's name
|
|
basePath = "tests_data"
|
|
basePathAbs = path.parentDir / basePath
|
|
filename = "test_store" & DbExt
|
|
dbPathAbs = basePathAbs / filename
|
|
|
|
setupAll:
|
|
removeDir(basePathAbs)
|
|
require(not dirExists(basePathAbs))
|
|
createDir(basePathAbs)
|
|
|
|
teardownAll:
|
|
removeDir(basePathAbs)
|
|
require(not dirExists(basePathAbs))
|
|
|
|
test "Should create and open datastore DB":
|
|
let
|
|
dsDb = SQLiteDsDb.open(
|
|
path = dbPathAbs,
|
|
flags = SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE).tryGet()
|
|
|
|
defer:
|
|
dsDb.close()
|
|
|
|
check:
|
|
fileExists(dbPathAbs)
|
|
|
|
test "Should open existing DB":
|
|
let
|
|
dsDb = SQLiteDsDb.open(
|
|
path = dbPathAbs,
|
|
flags = SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE).tryGet()
|
|
|
|
defer:
|
|
dsDb.close()
|
|
|
|
check:
|
|
fileExists(dbPathAbs)
|
|
|
|
test "Should open existing DB in read only mode":
|
|
check:
|
|
fileExists(dbPathAbs)
|
|
|
|
let
|
|
dsDb = SQLiteDsDb.open(
|
|
path = dbPathAbs,
|
|
flags = SQLITE_OPEN_READONLY).tryGet()
|
|
|
|
defer:
|
|
dsDb.close()
|
|
|
|
test "Should fail open non existent DB in read only mode":
|
|
removeDir(basePathAbs)
|
|
check:
|
|
not fileExists(dbPathAbs)
|
|
SQLiteDsDb.open(path = dbPathAbs).isErr
|
|
|
|
suite "Test SQLite Datastore DB operations":
|
|
let
|
|
path = currentSourcePath() # get this file's name
|
|
basePath = "tests_data"
|
|
basePathAbs = path.parentDir / basePath
|
|
filename = "test_store" & DbExt
|
|
dbPathAbs = basePathAbs / filename
|
|
|
|
key = Key.init("test/key").tryGet()
|
|
data = "some data".toBytes
|
|
otherData = "some other data".toBytes
|
|
|
|
var
|
|
dsDb: SQLiteDsDb
|
|
readOnlyDb: SQLiteDsDb
|
|
|
|
setupAll:
|
|
removeDir(basePathAbs)
|
|
require(not dirExists(basePathAbs))
|
|
createDir(basePathAbs)
|
|
|
|
dsDb = SQLiteDsDb.open(
|
|
path = dbPathAbs,
|
|
flags = SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE).tryGet()
|
|
|
|
readOnlyDb = SQLiteDsDb.open(
|
|
path = dbPathAbs,
|
|
flags = SQLITE_OPEN_READONLY).tryGet()
|
|
|
|
teardownAll:
|
|
dsDb.close()
|
|
readOnlyDb.close()
|
|
|
|
removeDir(basePathAbs)
|
|
require(not dirExists(basePathAbs))
|
|
|
|
test "Should insert key":
|
|
check:
|
|
readOnlyDb.putStmt.exec((key.id, data, timestamp())).isErr()
|
|
|
|
dsDb.putStmt.exec((key.id, data, timestamp())).tryGet()
|
|
|
|
test "Should select key":
|
|
let
|
|
dataCol = dsDb.getDataCol
|
|
|
|
var bytes: seq[byte]
|
|
proc onData(s: RawStmtPtr) =
|
|
bytes = dataCol()
|
|
|
|
check:
|
|
dsDb.getStmt.query((key.id), onData).tryGet()
|
|
bytes == data
|
|
|
|
test "Should update key":
|
|
check:
|
|
readOnlyDb.putStmt.exec((key.id, otherData, timestamp())).isErr()
|
|
|
|
dsDb.putStmt.exec((key.id, otherData, timestamp())).tryGet()
|
|
|
|
test "Should select updated key":
|
|
let
|
|
dataCol = dsDb.getDataCol
|
|
|
|
var bytes: seq[byte]
|
|
proc onData(s: RawStmtPtr) =
|
|
bytes = dataCol()
|
|
|
|
check:
|
|
dsDb.getStmt.query((key.id), onData).tryGet()
|
|
bytes == otherData
|
|
|
|
test "Should delete key":
|
|
check:
|
|
readOnlyDb.deleteStmt.exec((key.id)).isErr()
|
|
|
|
dsDb.deleteStmt.exec((key.id)).tryGet()
|
|
|
|
test "Should not contain key":
|
|
var
|
|
exists = false
|
|
|
|
proc onData(s: RawStmtPtr) =
|
|
exists = sqlite3_column_int64(s, ContainsStmtExistsCol.cint).bool
|
|
|
|
check:
|
|
dsDb.containsStmt.query((key.id), onData).tryGet()
|
|
not exists
|