Added tests for dstore module.
h3rald h3rald@h3rald.com
Sun, 20 Dec 2020 21:42:24 +0100
3 files changed,
41 insertions(+),
9 deletions(-)
M
lib/min_dstore.nim
→
lib/min_dstore.nim
@@ -14,10 +14,9 @@
def.symbol("dsinit") do (i: In): let vals = i.expect("'sym") let p = vals[0].getString - var j = %*{"collections": {}} - p.writeFile(j.pretty) + p.writeFile("{}") var d = newDict(i.scope) - i.dset(d, "data", i.fromJson(j)) + i.dset(d, "data", newDict(i.scope)) i.dset(d, "path", p.newVal) d.objType = "datastore" i.push d@@ -38,6 +37,7 @@ let ds = vals[0]
let p = i.dget(ds, "path".newVal).getString let data = i%(i.dget(ds, "data".newVal)) p.writeFile(data.pretty) + i.push ds def.symbol("dshas?") do (i: In): let vals = i.expect("'sym", "dict:datastore")@@ -47,6 +47,8 @@ let parts = s.split("/")
let collection = parts[0] let id = parts[1] let data = i.dget(ds, "data".newVal) + if not dhas(data, collection): + raiseInvalid("Collection '$#' does not exist" % collection) let cll = i.dget(data, collection.newVal) if dhas(cll, id.newVal): i.push true.newVal@@ -61,6 +63,8 @@ let parts = s.split("/")
let collection = parts[0] let id = parts[1] let data = i.dget(ds, "data".newVal) + if not dhas(data, collection): + raiseInvalid("Collection '$#' does not exist" % collection) let cll = i.dget(data, collection) i.push i.dget(cll, id.newVal)@@ -70,14 +74,19 @@ var filter = vals[0]
let collection = vals[1] let ds = vals[2] let data = i.dget(ds, "data".newVal) + if not dhas(data, collection): + raiseInvalid("Collection '$#' does not exist" % collection.getString) let cll = i.dget(data, collection) var res = newSeq[MinValue](0) for e in i.values(cll).qVal: i.push e - i.dequote(filter) - var check = i.pop - if check.isBool and check.boolVal == true: - res.add e + try: + i.dequote(filter) + var check = i.pop + if check.isBool and check.boolVal == true: + res.add e + except: + discard i.push res.newVal def.symbol("dspost") do (i: In):@@ -102,8 +111,12 @@ let s = vals[1].getString
let ds = vals[2] let parts = s.split("/") let collection = parts[0] + if parts.len < 2: + raiseInvalid("collection/id not specified") let id = parts[1] var data = i.dget(ds, "data".newVal) + if not dhas(data, collection): + raiseInvalid("Collection '$#' does not exist" % collection) var cll = i.dget(data, collection) i.dset(cll, id, d) #i.dset(ds, "data", data)@@ -114,9 +127,13 @@ let vals = i.expect("'sym", "dict:datastore")
let s = vals[0].getString let ds = vals[1] let parts = s.split("/") + if parts.len < 2: + raiseInvalid("collection/id not specified") let collection = parts[0] let id = parts[1] var data = i.dget(ds, "data".newVal) + if not dhas(data, collection): + raiseInvalid("Collection '$#' does not exist" % collection) var cll = i.dget(data, collection) i.ddel(cll, id) i.push ds
M
tests/all.min
→
tests/all.min
@@ -17,7 +17,6 @@ 'num load
'str load 'sys load 'time load -'t load 'dstore load 'fs load 'crypto load
M
tests/dstore.min
→
tests/dstore.min
@@ -11,8 +11,24 @@ ds "tests"
{} 1 %test1 2 %test2 - dspost + dspost dswrite "tests" (pop true) dsquery size 1 == + ) assert + + ( + "dstore.json" dsread :ds + ds "tests/aaa" + {} + 1 %test1 + 3 %test3 + dsput dswrite + "tests" (/test1 1 ==) dsquery size 2 == + ) assert + + ( + "dstore.json" dsread :ds + ds "tests/aaa" dsdelete + "tests" (/id "aaa" ==) dsquery size 0 == ) assert "dstore.json" rm