|
| 1 | +# Copyright Thomas T. Jarløv (TTJ) - ttj@ttj.dk |
| 2 | + |
| 3 | +when NimMajor >= 2: |
| 4 | + import |
| 5 | + db_connector/db_common |
| 6 | +else: |
| 7 | + import |
| 8 | + std/db_common |
| 9 | + |
| 10 | +import |
| 11 | + std/strutils, |
| 12 | + std/unittest |
| 13 | + |
| 14 | +import |
| 15 | + src/sqlbuilder, |
| 16 | + src/sqlbuilderpkg/utils_private |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +suite "insert - custom args": |
| 21 | + |
| 22 | + test "sqlInsert - dynamic columns": |
| 23 | + var test: SqlQuery |
| 24 | + |
| 25 | + let (s, a1) = genArgsColumns(SQLQueryType.INSERT, (true, "name", ""), (true, "age", 30), (false, "nim", ""), (true, "", "154")) |
| 26 | + test = sqlInsert("my-table", s, a1.query) |
| 27 | + check querycompare(test, sql("INSERT INTO my-table (age) VALUES (?)")) |
| 28 | + |
| 29 | + |
| 30 | + test "sqlInsert - setting null": |
| 31 | + var test: SqlQuery |
| 32 | + |
| 33 | + let a2 = genArgsSetNull("hje", "") |
| 34 | + test = sqlInsert("my-table", ["name", "age"], a2.query) |
| 35 | + check querycompare(test, sql("INSERT INTO my-table (name) VALUES (?)")) |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +suite "insert - default": |
| 40 | + |
| 41 | + test "sqlInsert - default": |
| 42 | + var test: SqlQuery |
| 43 | + |
| 44 | + test = sqlInsert("my-table", ["name", "age"]) |
| 45 | + check querycompare(test, sql("INSERT INTO my-table (name, age) VALUES (?, ?)")) |
| 46 | + |
| 47 | + |
| 48 | + test "sqlInsert - with manual null": |
| 49 | + var test: SqlQuery |
| 50 | + |
| 51 | + test = sqlInsert("my-table", ["name", "age = NULL"]) |
| 52 | + check querycompare(test, sql("INSERT INTO my-table (name, age) VALUES (?, NULL)")) |
| 53 | + |
| 54 | + |
| 55 | + test "sqlInsert - with args check for null #1": |
| 56 | + var test: SqlQuery |
| 57 | + |
| 58 | + let vals = @["thomas", "30"] |
| 59 | + test = sqlInsert("my-table", ["name", "age"], vals) |
| 60 | + check querycompare(test, sql("INSERT INTO my-table (name, age) VALUES (?, ?)")) |
| 61 | + |
| 62 | + |
| 63 | + test "sqlInsert - with args check for null #2": |
| 64 | + var test: SqlQuery |
| 65 | + |
| 66 | + let vals = @["thomas", ""] |
| 67 | + test = sqlInsert("my-table", ["name", "age"], vals) |
| 68 | + check querycompare(test, sql("INSERT INTO my-table (name, age) VALUES (?, NULL)")) |
| 69 | + |
| 70 | + |
| 71 | +suite "insert - macro": |
| 72 | + |
| 73 | + test "sqlInsert - default": |
| 74 | + var test: SqlQuery |
| 75 | + |
| 76 | + test = sqlInsertMacro("my-table", ["name", "age"]) |
| 77 | + check querycompare(test, sql("INSERT INTO my-table (name, age) VALUES (?, ?)")) |
| 78 | + |
| 79 | + |
| 80 | + test "sqlInsertMacro - with manual null": |
| 81 | + var test: SqlQuery |
| 82 | + |
| 83 | + test = sqlInsertMacro("my-table", ["name", "age = NULL"]) |
| 84 | + check querycompare(test, sql("INSERT INTO my-table (name, age) VALUES (?, NULL)")) |
0 commit comments