From 75ab9bbb4774a7c47100f82cb60908ef20a81516 Mon Sep 17 00:00:00 2001 From: Xie Yanbo Date: Sun, 23 Feb 2020 21:37:54 +0800 Subject: [PATCH] test tool --- tests/test.nim | 56 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/tests/test.nim b/tests/test.nim index 6ef481e..cc56d9a 100644 --- a/tests/test.nim +++ b/tests/test.nim @@ -1,6 +1,11 @@ import unittest, options, os, osproc, sequtils, strutils, sugar import leveldb, leveldbpkg/raw +const + tmpDir = "/tmp/testleveldb" + tmpNimbleDir = tmpDir / "nimble" + tmpDbDir = tmpDir / "testdb" + template cd*(dir: string, body: untyped) = ## Sets the current dir to ``dir``, executes ``body`` and restores the ## previous working dir. @@ -11,7 +16,6 @@ template cd*(dir: string, body: untyped) = setCurrentDir(lastDir) proc execNimble(args: varargs[string]): tuple[output: string, exitCode: int] = - const tmpNimbleDir = "/tmp/testnimble" var quotedArgs = @args quotedArgs.insert("-y") quotedArgs.insert("--nimbleDir:" & tmpNimbleDir) @@ -23,6 +27,21 @@ proc execNimble(args: varargs[string]): tuple[output: string, exitCode: int] = checkpoint(cmd) checkpoint(result.output) +proc execTool(args: varargs[string]): tuple[output: string, exitCode: int] = + var quotedArgs = @args + quotedArgs.insert(tmpDbDir) + quotedArgs.insert("--database") + quotedArgs.insert(tmpNimbleDir / "bin" / "leveldb") + quotedArgs = quotedArgs.map((x: string) => ("\"" & x & "\"")) + + if not existsDir(tmpDbDir): + createDir(tmpDbDir) + + let cmd = quotedArgs.join(" ") + result = execCmdEx(cmd) + checkpoint(cmd) + checkpoint(result.output) + suite "leveldb": setup: @@ -198,3 +217,38 @@ suite "package": checkpoint output check exitCode == QuitSuccess check output.contains("leveldb works.") + +suite "tool": + + test "leveldb tool": + var (output, exitCode) = execNimble("install") + check exitCode == QuitSuccess + check output.contains("Building") + + check execTool("-v").exitCode == QuitSuccess + check execTool("create").exitCode == QuitSuccess + check execTool("list").exitCode == QuitSuccess + + check execTool("put", "hello", "world").exitCode == QuitSuccess + (output, exitCode) = execTool("get", "hello") + check exitCode == QuitSuccess + check output == "world\L" + (output, exitCode) = execTool("list") + check exitCode == QuitSuccess + check output == "hello world\L" + + check execTool("delete", "hello").exitCode == QuitSuccess + (output, exitCode) = execTool("get", "hello") + check exitCode == QuitSuccess + check output == "" + (output, exitCode) = execTool("list") + check exitCode == QuitSuccess + check output == "" + + check execTool("put", "hello", "6130", "-x").exitCode == QuitSuccess + check execTool("get", "hello", "-x").output == "6130\L" + check execTool("get", "hello").output == "a0\L" + check execTool("list", "-x").output == "hello 6130\L" + check execTool("put", "hello", "0061", "-x").exitCode == QuitSuccess + check execTool("get", "hello", "-x").output == "0061\L" + check execTool("delete", "hello").exitCode == QuitSuccess