mirror of
https://github.com/logos-storage/nim-datastore.git
synced 2026-01-04 06:33:11 +00:00
refactor tests
This commit is contained in:
parent
ed34fe2d3c
commit
2a96b1bcef
@ -15,7 +15,7 @@ export datastore
|
|||||||
push: {.upraises: [].}
|
push: {.upraises: [].}
|
||||||
|
|
||||||
type
|
type
|
||||||
FSDatastore* = object
|
FSDatastore*[K, V] = object
|
||||||
root*: DataBuffer
|
root*: DataBuffer
|
||||||
ignoreProtected: bool
|
ignoreProtected: bool
|
||||||
depth: int
|
depth: int
|
||||||
@ -26,11 +26,12 @@ proc isRootSubdir*(root, path: string): bool =
|
|||||||
proc validDepth(self: FSDatastore, key: Key): bool =
|
proc validDepth(self: FSDatastore, key: Key): bool =
|
||||||
key.len <= self.depth
|
key.len <= self.depth
|
||||||
|
|
||||||
proc findPath*(self: FSDatastore, key: Key): ?!string =
|
proc findPath*[K,V](self: FSDatastore[K,V], key: K): ?!string =
|
||||||
## Return filename corresponding to the key
|
## Return filename corresponding to the key
|
||||||
## or failure if the key doesn't correspond to a valid filename
|
## or failure if the key doesn't correspond to a valid filename
|
||||||
##
|
##
|
||||||
let root = $self.root
|
let root = $self.root
|
||||||
|
let key = Key.init($key).get()
|
||||||
if not self.validDepth(key):
|
if not self.validDepth(key):
|
||||||
return failure "Path has invalid depth!"
|
return failure "Path has invalid depth!"
|
||||||
|
|
||||||
@ -64,14 +65,14 @@ proc findPath*(self: FSDatastore, key: Key): ?!string =
|
|||||||
|
|
||||||
return success fullname
|
return success fullname
|
||||||
|
|
||||||
proc has*(self: FSDatastore, key: KeyId): ?!bool =
|
proc has*[K,V](self: FSDatastore[K,V], key: KeyId): ?!bool =
|
||||||
let key = key.toKey()
|
let key = key.toKey()
|
||||||
return self.findPath(key).?fileExists()
|
return self.findPath(key).?fileExists()
|
||||||
|
|
||||||
proc contains*[K](self: FSDatastore, key: K): bool =
|
proc contains*[K](self: FSDatastore, key: K): bool =
|
||||||
return self.has(key).get()
|
return self.has(key).get()
|
||||||
|
|
||||||
proc delete*(self: FSDatastore, key: KeyId): ?!void =
|
proc delete*[K,V](self: FSDatastore[K,V], key: KeyId): ?!void =
|
||||||
let key = key.toKey()
|
let key = key.toKey()
|
||||||
|
|
||||||
without path =? self.findPath(key), error:
|
without path =? self.findPath(key), error:
|
||||||
@ -87,7 +88,7 @@ proc delete*(self: FSDatastore, key: KeyId): ?!void =
|
|||||||
|
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
proc delete*(self: FSDatastore, keys: openArray[KeyId]): ?!void =
|
proc delete*[K,V](self: FSDatastore[K,V], keys: openArray[KeyId]): ?!void =
|
||||||
for key in keys:
|
for key in keys:
|
||||||
if err =? self.delete(key).errorOption:
|
if err =? self.delete(key).errorOption:
|
||||||
return failure err
|
return failure err
|
||||||
@ -130,7 +131,7 @@ proc readFile[V](self: FSDatastore, path: string): ?!V =
|
|||||||
except CatchableError as e:
|
except CatchableError as e:
|
||||||
return failure e
|
return failure e
|
||||||
|
|
||||||
proc get*(self: FSDatastore, key: KeyId): ?!DataBuffer =
|
proc get*[K,V](self: FSDatastore[K,V], key: KeyId): ?!DataBuffer =
|
||||||
let key = key.toKey()
|
let key = key.toKey()
|
||||||
without path =? self.findPath(key), error:
|
without path =? self.findPath(key), error:
|
||||||
return failure error
|
return failure error
|
||||||
@ -141,11 +142,10 @@ proc get*(self: FSDatastore, key: KeyId): ?!DataBuffer =
|
|||||||
|
|
||||||
return readFile[DataBuffer](self, path)
|
return readFile[DataBuffer](self, path)
|
||||||
|
|
||||||
proc put*(
|
proc put*[K,V](
|
||||||
self: FSDatastore,
|
self: FSDatastore[K,V],
|
||||||
key: KeyId,
|
key: KeyId,
|
||||||
data: DataBuffer): ?!void =
|
data: DataBuffer): ?!void =
|
||||||
let key = key.toKey()
|
|
||||||
|
|
||||||
without path =? self.findPath(key), error:
|
without path =? self.findPath(key), error:
|
||||||
return failure error
|
return failure error
|
||||||
@ -176,11 +176,11 @@ iterator dirIter(path: string): string {.gcsafe.} =
|
|||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
raise newException(Defect, exc.msg)
|
raise newException(Defect, exc.msg)
|
||||||
|
|
||||||
proc close*(self: FSDatastore): ?!void =
|
proc close*[K,V](self: FSDatastore[K,V]): ?!void =
|
||||||
return success()
|
return success()
|
||||||
|
|
||||||
type
|
type
|
||||||
FsQueryEnv* = tuple[self: FSDatastore, basePath: DataBuffer]
|
FsQueryEnv*[K,V] = tuple[self: FSDatastore[K,V], basePath: DataBuffer]
|
||||||
|
|
||||||
proc query*(
|
proc query*(
|
||||||
self: FSDatastore,
|
self: FSDatastore,
|
||||||
@ -231,12 +231,11 @@ iterator iter*[K, V](handle: var DbQueryHandle[K, V, DataBuffer]): ?!DbQueryResp
|
|||||||
|
|
||||||
yield success (key.some, data)
|
yield success (key.some, data)
|
||||||
|
|
||||||
proc new*(
|
proc newFSDatastore*[K,V](root: string,
|
||||||
T: type FSDatastore,
|
depth = 2,
|
||||||
root: string,
|
caseSensitive = true,
|
||||||
depth = 2,
|
ignoreProtected = false
|
||||||
caseSensitive = true,
|
): ?!FSDatastore[K,V] =
|
||||||
ignoreProtected = false): ?!T =
|
|
||||||
|
|
||||||
let root = ? (
|
let root = ? (
|
||||||
block:
|
block:
|
||||||
@ -246,7 +245,7 @@ proc new*(
|
|||||||
if not dirExists(root):
|
if not dirExists(root):
|
||||||
return failure "directory does not exist: " & root
|
return failure "directory does not exist: " & root
|
||||||
|
|
||||||
success T(
|
success FSDatastore[K,V](
|
||||||
root: DataBuffer.new root,
|
root: DataBuffer.new root,
|
||||||
ignoreProtected: ignoreProtected,
|
ignoreProtected: ignoreProtected,
|
||||||
depth: depth)
|
depth: depth)
|
||||||
|
|||||||
@ -227,7 +227,7 @@ proc newSQLiteBackend*[K,V](
|
|||||||
success SQLiteBackend[K,V](db: ? SQLiteDsDb[K,V].open(path, flags))
|
success SQLiteBackend[K,V](db: ? SQLiteDsDb[K,V].open(path, flags))
|
||||||
|
|
||||||
|
|
||||||
proc newSQLiteBackend*[K,V](
|
proc newSQLiteBackend*[K,V](db: SQLiteDsDb[K,V]
|
||||||
db: SQLiteDsDb[K,V]): ?!SQLiteBackend[K,V] =
|
): ?!SQLiteBackend[K,V] =
|
||||||
|
|
||||||
success SQLiteBackend[K,V](db: db)
|
success SQLiteBackend[K,V](db: db)
|
||||||
|
|||||||
@ -34,7 +34,7 @@ suite "Test Basic FSDatastore":
|
|||||||
createDir(basePathAbs)
|
createDir(basePathAbs)
|
||||||
|
|
||||||
var
|
var
|
||||||
fsStore = FSDatastore.new(root = basePathAbs, depth = 3).tryGet()
|
fsStore = newFSDatastore[KeyId, DataBuffer](root = basePathAbs, depth = 3).tryGet()
|
||||||
|
|
||||||
testBasicBackend(fsStore, key, bytes, otherBytes, batch)
|
testBasicBackend(fsStore, key, bytes, otherBytes, batch)
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ suite "Test Basic FSDatastore":
|
|||||||
createDir(basePathAbs)
|
createDir(basePathAbs)
|
||||||
|
|
||||||
var
|
var
|
||||||
fsStore = FSDatastore.new(root = basePathAbs, depth = 3).tryGet()
|
fsStore = newFSDatastore[KeyId, DataBuffer](root = basePathAbs, depth = 3).tryGet()
|
||||||
|
|
||||||
testBasicBackend(fsStore, key, bytes, otherBytes, batch)
|
testBasicBackend(fsStore, key, bytes, otherBytes, batch)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user