Skip to content

Commit

Permalink
tests: updated unit test for variables
Browse files Browse the repository at this point in the history
FossilOrigin-Name: 9519d8c2c5d8e09dccef3ea03bc89c1e1b183cd3ad9d05700d0a2529d4eaa7eb
  • Loading branch information
thindil committed Oct 14, 2023
1 parent 4ac7402 commit 4039e7b
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions tests/tvariables/tvariables.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ discard """
"""

import std/[os, strutils, tables]
when (NimMajor, NimMinor, NimPatch) >= (1, 7, 3):
import db_connector/db_sqlite
else:
import std/db_sqlite
import ../../src/[commandslist, directorypath, lstring, nish, resultcode, variables]
import norm/sqlite

block:
assert buildQuery("/".DirectoryPath, "name") ==
Expand All @@ -20,16 +17,23 @@ block:

initVariables(db, commands)

if parseInt(db.getValue(sql"SELECT COUNT(*) FROM variables")) == 0:
if db.tryInsertID(sql"INSERT INTO variables (name, path, recursive, value, description) VALUES (?, ?, ?, ?, ?)",
"TESTS", "/", 1, "test_variable", "Test variable.") == -1:
if db.count(Variable) == 0:
try:
var variable = newVariable(name = "TESTS", path = "/", recursive = true,
value = "test_variable", description = "Test variable.")
db.insert(variable)
var variable2 = newVariable(name = "TESTS2", path = "/",
recursive = false, value = "test_variable2",
description = "Test variable 2.")
db.insert(variable2)
except:
quit QuitFailure
if db.tryInsertID(sql"INSERT INTO variables (name, path, recursive, value, description) VALUES (?, ?, ?, ?, ?)",
"TESTS2", "/", 0, "test_variable2", "Test variable 2.") == -1:
quit QuitFailure
if parseInt(db.getValue(sql"SELECT COUNT(*) FROM variables")) == 1:
if db.tryInsertID(sql"INSERT INTO variables (name, path, recursive, value, description) VALUES (?, ?, ?, ?, ?)",
"TESTS2", "/", 0, "test_variable2", "Test variable 2.") == -1:
if db.count(Variable) == 1:
try:
var variable = newVariable(name = "TESTS2", path = "/", recursive = false,
value = "test_variable2", description = "Test variable 2.")
db.insert(variable)
except:
quit QuitFailure

setVariables("/home".DirectoryPath, db)
Expand All @@ -38,19 +42,19 @@ block:

assert listVariables(initLimitedString(capacity = 4, text = "list"), db) ==
QuitSuccess, "Failed to show the list of available variables."
assert listVariables(initLimitedString(capacity = 8, text = "list all"), db) ==
QuitSuccess, "Failed to show the list of all variables."
assert listVariables(initLimitedString(capacity = 8, text = "werwerew"), db) ==
QuitSuccess, "Failed to show the list of available variables."
assert listVariables(initLimitedString(capacity = 8, text = "list all"),
db) == QuitSuccess, "Failed to show the list of all variables."
assert listVariables(initLimitedString(capacity = 8, text = "werwerew"),
db) == QuitSuccess, "Failed to show the list of available variables."

assert deleteVariable(initLimitedString(capacity = 10, text = "delete 123"),
db) == QuitFailure, "Failed to not delete a non-existing variable."
assert deleteVariable(initLimitedString(capacity = 10, text = "delete sdf"),
db) == QuitFailure, "Failed to not delete a non-existing variable with invalid index."
assert deleteVariable(initLimitedString(capacity = 8, text = "delete 2"), db) ==
QuitSuccess, "Failed to delete a variable."
assert deleteVariable(initLimitedString(capacity = 8, text = "delete 2"), db) ==
QuitFailure, "Failed to delete a deleted variable."
assert deleteVariable(initLimitedString(capacity = 8, text = "delete 2"),
db) == QuitSuccess, "Failed to delete a variable."
assert deleteVariable(initLimitedString(capacity = 8, text = "delete 2"),
db) == QuitFailure, "Failed to delete a deleted variable."

assert setCommand(initLimitedString(capacity = 13, text = "test=test_val")) ==
QuitSuccess, "Failed to set a variable."
Expand Down

0 comments on commit 4039e7b

Please sign in to comment.