nim-datastore/tests/datastore/sql/testsqliteds.nim

90 lines
2.0 KiB
Nim
Raw Normal View History

2022-09-16 21:15:08 -06:00
import std/options
import std/os
import std/sequtils
from std/algorithm import sort, reversed
2022-09-16 21:15:08 -06:00
2023-09-07 17:26:34 -06:00
import pkg/asynctest
2022-09-16 21:15:08 -06:00
import pkg/chronos
import pkg/stew/results
import pkg/stew/byteutils
import pkg/datastore/sql/sqliteds
import ../dscommontests
import ../querycommontests
2022-09-19 15:53:38 -06:00
suite "Test Basic SQLiteDatastore":
2022-09-16 21:15:08 -06:00
let
ds = SQLiteDatastore.new(Memory).tryGet()
2022-09-16 21:15:08 -06:00
key = Key.init("a:b/c/d:e").tryGet()
bytes = "some bytes".toBytes
otherBytes = "some other bytes".toBytes
teardownAll:
(await ds.close()).tryGet()
2022-09-19 15:53:38 -06:00
basicStoreTests(ds, key, bytes, otherBytes)
2022-09-19 15:53:38 -06:00
suite "Test Read Only SQLiteDatastore":
let
path = currentSourcePath() # get this file's name
2022-09-19 15:53:38 -06:00
basePath = "tests_data"
basePathAbs = path.parentDir / basePath
filename = "test_store" & DbExt
dbPathAbs = basePathAbs / filename
key = Key.init("a:b/c/d:e").tryGet()
bytes = "some bytes".toBytes
2022-09-16 21:15:08 -06:00
var
dsDb: SQLiteDatastore
readOnlyDb: SQLiteDatastore
setupAll:
removeDir(basePathAbs)
require(not dirExists(basePathAbs))
createDir(basePathAbs)
dsDb = SQLiteDatastore.new(path = dbPathAbs).tryGet()
readOnlyDb = SQLiteDatastore.new(path = dbPathAbs, readOnly = true).tryGet()
teardownAll:
2022-09-20 09:21:41 -06:00
(await dsDb.close()).tryGet()
(await readOnlyDb.close()).tryGet()
2022-09-16 21:15:08 -06:00
removeDir(basePathAbs)
require(not dirExists(basePathAbs))
test "put":
check:
(await readOnlyDb.put(key, bytes)).isErr
(await dsDb.put(key, bytes)).tryGet()
test "get":
check:
(await readOnlyDb.get(key)).tryGet() == bytes
(await dsDb.get(key)).tryGet() == bytes
test "delete":
check:
(await readOnlyDb.delete(key)).isErr
(await dsDb.delete(key)).tryGet()
test "contains":
check:
2022-12-02 16:25:44 -06:00
not (await readOnlyDb.has(key)).tryGet()
not (await dsDb.has(key)).tryGet()
2022-09-16 21:15:08 -06:00
suite "Test Query":
var
ds: SQLiteDatastore
2022-09-16 21:15:08 -06:00
setup:
ds = SQLiteDatastore.new(Memory).tryGet()
2022-09-16 21:15:08 -06:00
teardown:
(await ds.close()).tryGet
2022-09-16 21:15:08 -06:00
queryTests(ds)