mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-08 00:23:10 +00:00
refactor - tests
This commit is contained in:
parent
f587677be8
commit
9e946e683e
@ -23,7 +23,7 @@ type
|
|||||||
proc isRootSubdir*(root, path: string): bool =
|
proc isRootSubdir*(root, path: string): bool =
|
||||||
path.startsWith(root)
|
path.startsWith(root)
|
||||||
|
|
||||||
proc validDepth(self: FSDatastore, key: Key): bool =
|
proc validDepth*(self: FSDatastore, key: Key): bool =
|
||||||
key.len <= self.depth
|
key.len <= self.depth
|
||||||
|
|
||||||
proc findPath*[K,V](self: FSDatastore[K,V], key: K): ?!string =
|
proc findPath*[K,V](self: FSDatastore[K,V], key: K): ?!string =
|
||||||
|
|||||||
@ -67,81 +67,81 @@ suite "Test Basic FSDatastore":
|
|||||||
removeDir(basePathAbs)
|
removeDir(basePathAbs)
|
||||||
require(not dirExists(basePathAbs))
|
require(not dirExists(basePathAbs))
|
||||||
|
|
||||||
# suite "Test Misc FSDatastore":
|
suite "Test Misc FSDatastore":
|
||||||
# let
|
let
|
||||||
# path = currentSourcePath() # get this file's name
|
path = currentSourcePath() # get this file's name
|
||||||
# basePath = "tests_data"
|
basePath = "tests_data"
|
||||||
# basePathAbs = path.parentDir / basePath
|
basePathAbs = path.parentDir / basePath
|
||||||
# bytes = "some bytes".toBytes
|
bytes = "some bytes".toBytes
|
||||||
|
|
||||||
# setup:
|
setup:
|
||||||
# removeDir(basePathAbs)
|
removeDir(basePathAbs)
|
||||||
# require(not dirExists(basePathAbs))
|
require(not dirExists(basePathAbs))
|
||||||
# createDir(basePathAbs)
|
createDir(basePathAbs)
|
||||||
|
|
||||||
# teardown:
|
teardown:
|
||||||
# removeDir(basePathAbs)
|
removeDir(basePathAbs)
|
||||||
# require(not dirExists(basePathAbs))
|
require(not dirExists(basePathAbs))
|
||||||
|
|
||||||
# test "Test validDepth()":
|
test "Test validDepth()":
|
||||||
# let
|
let
|
||||||
# fs = FSDatastore.new(root = "/", depth = 3).tryGet()
|
fs = newFSDatastore[Key, seq[byte]](root = basePathAbs, depth = 3).tryGet()
|
||||||
# invalid = Key.init("/a/b/c/d").tryGet()
|
invalid = Key.init("/a/b/c/d").tryGet()
|
||||||
# valid = Key.init("/a/b/c").tryGet()
|
valid = Key.init("/a/b/c").tryGet()
|
||||||
|
|
||||||
# check:
|
check:
|
||||||
# not fs.validDepth(invalid)
|
not fs.validDepth(invalid)
|
||||||
# fs.validDepth(valid)
|
fs.validDepth(valid)
|
||||||
|
|
||||||
# test "Test invalid key (path) depth":
|
test "Test invalid key (path) depth":
|
||||||
# let
|
let
|
||||||
# fs = FSDatastore.new(root = basePathAbs, depth = 3).tryGet()
|
fs = newFSDatastore[Key, seq[byte]](root = basePathAbs, depth = 3).tryGet()
|
||||||
# key = Key.init("/a/b/c/d").tryGet()
|
key = Key.init("/a/b/c/d").tryGet()
|
||||||
|
|
||||||
# check:
|
check:
|
||||||
# (await fs.put(key, bytes)).isErr
|
(fs.put(key, bytes)).isErr
|
||||||
# (await fs.get(key)).isErr
|
(fs.get(key)).isErr
|
||||||
# (await fs.delete(key)).isErr
|
(fs.delete(key)).isErr
|
||||||
# (await fs.has(key)).isErr
|
(fs.has(key)).isErr
|
||||||
|
|
||||||
# test "Test valid key (path) depth":
|
test "Test valid key (path) depth":
|
||||||
# let
|
let
|
||||||
# fs = FSDatastore.new(root = basePathAbs, depth = 3).tryGet()
|
fs = newFSDatastore[Key, seq[byte]](root = basePathAbs, depth = 3).tryGet()
|
||||||
# key = Key.init("/a/b/c").tryGet()
|
key = Key.init("/a/b/c").tryGet()
|
||||||
|
|
||||||
# check:
|
check:
|
||||||
# (await fs.put(key, bytes)).isOk
|
(fs.put(key, bytes)).isOk
|
||||||
# (await fs.get(key)).isOk
|
(fs.get(key)).isOk
|
||||||
# (await fs.delete(key)).isOk
|
(fs.delete(key)).isOk
|
||||||
# (await fs.has(key)).isOk
|
(fs.has(key)).isOk
|
||||||
|
|
||||||
# test "Test key cannot write outside of root":
|
test "Test key cannot write outside of root":
|
||||||
# let
|
let
|
||||||
# fs = FSDatastore.new(root = basePathAbs, depth = 3).tryGet()
|
fs = newFSDatastore[Key, seq[byte]](root = basePathAbs, depth = 3).tryGet()
|
||||||
# key = Key.init("/a/../../c").tryGet()
|
key = Key.init("/a/../../c").tryGet()
|
||||||
|
|
||||||
# check:
|
check:
|
||||||
# (await fs.put(key, bytes)).isErr
|
(fs.put(key, bytes)).isErr
|
||||||
# (await fs.get(key)).isErr
|
(fs.get(key)).isErr
|
||||||
# (await fs.delete(key)).isErr
|
(fs.delete(key)).isErr
|
||||||
# (await fs.has(key)).isErr
|
(fs.has(key)).isErr
|
||||||
|
|
||||||
# test "Test key cannot convert to invalid path":
|
test "Test key cannot convert to invalid path":
|
||||||
# let
|
let
|
||||||
# fs = FSDatastore.new(root = basePathAbs).tryGet()
|
fs = newFSDatastore[Key, seq[byte]](root = basePathAbs).tryGet()
|
||||||
|
|
||||||
# for c in invalidFilenameChars:
|
for c in invalidFilenameChars:
|
||||||
# if c == ':': continue
|
if c == ':': continue
|
||||||
# if c == '/': continue
|
if c == '/': continue
|
||||||
|
|
||||||
# let
|
let
|
||||||
# key = Key.init("/" & c).tryGet()
|
key = Key.init("/" & c).tryGet()
|
||||||
|
|
||||||
# check:
|
check:
|
||||||
# (await fs.put(key, bytes)).isErr
|
(fs.put(key, bytes)).isErr
|
||||||
# (await fs.get(key)).isErr
|
(fs.get(key)).isErr
|
||||||
# (await fs.delete(key)).isErr
|
(fs.delete(key)).isErr
|
||||||
# (await fs.has(key)).isErr
|
(fs.has(key)).isErr
|
||||||
|
|
||||||
|
|
||||||
# suite "Test Query":
|
# suite "Test Query":
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user