mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-02 13:43:11 +00:00
add sql backend tests
This commit is contained in:
parent
77807d8948
commit
5d29ad905d
158
tests/datastore/sql/testsqlitedsdb.nim
Normal file
158
tests/datastore/sql/testsqlitedsdb.nim
Normal file
@ -0,0 +1,158 @@
|
||||
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, _, _) = instantiationInfo(-1, fullPaths = true) # 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_READWRITE or SQLITE_OPEN_CREATE).tryGet()
|
||||
|
||||
defer:
|
||||
dsDb.close()
|
||||
|
||||
check:
|
||||
fileExists(dbPathAbs)
|
||||
|
||||
test "Should fail open existing DB in read only mode":
|
||||
removeDir(basePathAbs)
|
||||
check:
|
||||
not fileExists(dbPathAbs)
|
||||
SQLiteDsDb.open(path = dbPathAbs).isErr
|
||||
|
||||
suite "Test SQLite Datastore DB operations":
|
||||
let
|
||||
(path, _, _) = instantiationInfo(-1, fullPaths = true) # 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:
|
||||
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
|
||||
Loading…
x
Reference in New Issue
Block a user