refactor - tests

This commit is contained in:
Jaremy Creechley 2023-09-27 18:53:46 -07:00
parent f587677be8
commit 9e946e683e
No known key found for this signature in database
GPG Key ID: 4E66FB67B21D3300
2 changed files with 62 additions and 62 deletions

View File

@ -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 =

View File

@ -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":