add memory (test) ds

This commit is contained in:
Jaremy Creechley 2023-08-28 18:43:42 -07:00
parent 3dd21593c7
commit 5cc0af4b56
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
2 changed files with 7 additions and 77 deletions

View File

@ -84,6 +84,6 @@ method close*(self: MemoryDatastore): Future[?!void] {.async.} =
self.store.clear() self.store.clear()
return success() return success()
func new*(tp: typedesc[MemoryDatastore]): ?!MemoryDatastore = func new*(tp: typedesc[MemoryDatastore]): MemoryDatastore =
var self = default(tp) var self = default(tp)
success self return self

View File

@ -8,35 +8,24 @@ import pkg/chronos
import pkg/stew/results import pkg/stew/results
import pkg/stew/byteutils import pkg/stew/byteutils
import pkg/datastore/fsds import pkg/datastore/memoryds
import ./dscommontests import ./dscommontests
import ./querycommontests import ./querycommontests
suite "Test Basic MemoryDatastore": suite "Test Basic MemoryDatastore":
let let
path = currentSourcePath() # get this file's name
basePath = "tests_data"
basePathAbs = path.parentDir / basePath
key = Key.init("/a/b").tryGet() key = Key.init("/a/b").tryGet()
bytes = "some bytes".toBytes bytes = "some bytes".toBytes
otherBytes = "some other bytes".toBytes otherBytes = "some other bytes".toBytes
var var
fsStore: MemoryDatastore memStore: MemoryDatastore
setupAll: setupAll:
removeDir(basePathAbs) memStore = MemoryDatastore.new()
require(not dirExists(basePathAbs))
createDir(basePathAbs)
fsStore = MemoryDatastore.new(root = basePathAbs, depth = 3).tryGet() basicStoreTests(memStore, key, bytes, otherBytes)
teardownAll:
removeDir(basePathAbs)
require(not dirExists(basePathAbs))
basicStoreTests(fsStore, key, bytes, otherBytes)
suite "Test Misc MemoryDatastore": suite "Test Misc MemoryDatastore":
let let
@ -54,65 +43,6 @@ suite "Test Misc MemoryDatastore":
removeDir(basePathAbs) removeDir(basePathAbs)
require(not dirExists(basePathAbs)) require(not dirExists(basePathAbs))
test "Test validDepth()":
let
fs = MemoryDatastore.new(root = "/", depth = 3).tryGet()
invalid = Key.init("/a/b/c/d").tryGet()
valid = Key.init("/a/b/c").tryGet()
check:
not fs.validDepth(invalid)
fs.validDepth(valid)
test "Test invalid key (path) depth":
let
fs = MemoryDatastore.new(root = basePathAbs, depth = 3).tryGet()
key = Key.init("/a/b/c/d").tryGet()
check:
(await fs.put(key, bytes)).isErr
(await fs.get(key)).isErr
(await fs.delete(key)).isErr
(await fs.has(key)).isErr
test "Test valid key (path) depth":
let
fs = MemoryDatastore.new(root = basePathAbs, depth = 3).tryGet()
key = Key.init("/a/b/c").tryGet()
check:
(await fs.put(key, bytes)).isOk
(await fs.get(key)).isOk
(await fs.delete(key)).isOk
(await fs.has(key)).isOk
test "Test key cannot write outside of root":
let
fs = MemoryDatastore.new(root = basePathAbs, depth = 3).tryGet()
key = Key.init("/a/../../c").tryGet()
check:
(await fs.put(key, bytes)).isErr
(await fs.get(key)).isErr
(await fs.delete(key)).isErr
(await fs.has(key)).isErr
test "Test key cannot convert to invalid path":
let
fs = MemoryDatastore.new(root = basePathAbs).tryGet()
for c in invalidFilenameChars:
if c == ':': continue
if c == '/': continue
let
key = Key.init("/" & c).tryGet()
check:
(await fs.put(key, bytes)).isErr
(await fs.get(key)).isErr
(await fs.delete(key)).isErr
(await fs.has(key)).isErr
suite "Test Query": suite "Test Query":
let let
@ -128,7 +58,7 @@ suite "Test Query":
require(not dirExists(basePathAbs)) require(not dirExists(basePathAbs))
createDir(basePathAbs) createDir(basePathAbs)
ds = MemoryDatastore.new(root = basePathAbs, depth = 5).tryGet() ds = MemoryDatastore.new()
teardown: teardown: