Skip to content

Commit a96615e

Browse files
committed
Add tests around Option[] type
1 parent feddd5a commit a96615e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/test_option.nim

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import std/unittest
2+
import std/options
3+
4+
import easy_sqlite3
5+
6+
proc returnOptionNone(): tuple[col: Option[int]] {.importdb: "SELECT NULL;".}
7+
proc returnOptionSome(): tuple[col: Option[int]] {.importdb: "SELECT 1;".}
8+
9+
proc takeOption(col: Option[int]): tuple[val: int, is_null: bool] {.importdb: "SELECT COALESCE($col, 0), $col IS NULL".}
10+
11+
iterator options(a: Option[int], b: Option[int], c: Option[int]): tuple[val: Option[int]] {.importdb: "VALUES ($a+1), ($b+1), ($c+1)".} = discard
12+
13+
suite "option":
14+
setup:
15+
var db = initDatabase(":memory:")
16+
17+
test "return option none":
18+
let col = db.returnOptionNone().col
19+
check col.is_none()
20+
21+
test "return option some":
22+
let col = db.returnOptionSome().col
23+
check col.is_some()
24+
check col.get == 1
25+
26+
test "take option none":
27+
let res = db.takeOption(none(int))
28+
check res.is_null
29+
check res.val == 0
30+
31+
test "take option some":
32+
let res = db.takeOption(some(1))
33+
check not res.is_null
34+
check res.val == 1
35+
36+
test "iterator options":
37+
var res: seq[Option[int]] = @[]
38+
for row in db.options(some(1), none(int), some(2)):
39+
res.add(row.val)
40+
check res == @[some(2), none(int), some(3)]

0 commit comments

Comments
 (0)