/
Main.purs
77 lines (68 loc) · 2.08 KB
/
Main.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module Test.Main where
import Prelude
import Data.Either (Either(..))
import Data.Foldable (for_)
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
import Node.FS.Aff (unlink)
import Node.FS.Sync (exists)
import SQLite3 (closeDB, newDB, queryDB, queryObjectDB)
import Simple.JSON (read)
import Test.Unit (failure, suite, test)
import Test.Unit.Assert (assert, equal)
import Test.Unit.Main (runTest)
type TableRow =
{ name :: String
, detail :: String
}
main :: Effect Unit
main = launchAff_ do
let testPath = "./test.sqlite3"
(flip when) (unlink testPath) =<< liftEffect (exists testPath)
db <- newDB testPath
_ <- queryDB db
"""
CREATE TABLE IF NOT EXISTS mytable
( name text primary key unique
, detail text
);
""" []
liftEffect $ runTest do
suite "SQLite3" do
test ("db connection worked and created " <> testPath) do
assert "exists testPath" =<< liftEffect (exists testPath)
test "we can insert rows and retrieve them" do
_ <- queryDB db
"""
INSERT INTO mytable
( name, detail )
VALUES
( 'aa', 'bbbb' )
""" []
results <- read <$> queryDB db
"""
SELECT name, detail FROM mytable
""" []
case results of
Right (as :: Array TableRow) ->
for_ as \a -> do
equal a.name "aa"
equal a.detail "bbbb"
Left e ->
failure $ "row didn't deserialize correctly: " <> show e
test "we can use queryObjectDB to retrieve records" do
results <- read <$> queryObjectDB db "SELECT name, detail FROM mytable WHERE name = $asdf" { "$asdf": "aa" }
case results of
Right (as :: Array TableRow) ->
for_ as \a -> do
equal a.name "aa"
equal a.detail "bbbb"
Left e ->
failure $ "row didn't deserialize correctly: " <> show e
test "we can close and re-open a database" do
let path = "./test-close.sqlite3"
cDb <- newDB path
closeDB cDb
cDb' <- newDB path
closeDB cDb'