@@ -17,15 +17,15 @@ import Database.Postgres (Query(Query), connect, end, execute, execute_, query,
17
17
import Database.Postgres.SqlValue (toSql )
18
18
import Database.Postgres.Transaction (withTransaction )
19
19
import Effect (Effect )
20
- import Effect.Aff (Aff , Error , apathize , attempt )
20
+ import Effect.Aff (Aff , Error , apathize , attempt , launchAff_ )
21
21
import Effect.Class (liftEffect )
22
22
import Effect.Exception (error )
23
23
import Foreign (Foreign )
24
24
import Simple.JSON as JSON
25
25
import Test.Spec (describe , it )
26
26
import Test.Spec.Assertions (fail , shouldEqual )
27
27
import Test.Spec.Reporter.Console (consoleReporter )
28
- import Test.Spec.Runner (run )
28
+ import Test.Spec.Runner (runSpec )
29
29
import Unsafe.Coerce (unsafeCoerce )
30
30
31
31
type Artist =
@@ -50,113 +50,114 @@ read' :: forall a. JSON.ReadForeign a => Foreign -> Either Error a
50
50
read' = lmap (error <<< show) <<< JSON .read
51
51
52
52
main :: Effect Unit
53
- main = run [consoleReporter] do
54
- describe " withClient" do
55
- it " Returns a client" do
56
- pool <- liftEffect $ mkPool connectionInfo
57
- withClient pool $ \c -> do
58
- execute_ (Query " delete from artist" ) c
59
- execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) c
60
- execute_ (Query " insert into artist values ('Deep Purple', 1968)" ) c
61
- let
62
- q :: Query Int
63
- q = Query " insert into artist values ('Fairport Convention', 1967) returning year"
64
-
65
- year <- queryValue_ read' q c
66
- year `shouldEqual` (Just 1967 )
67
-
68
- artists <- query_ read' (Query " select * from artist" :: Query Artist ) c
69
- length artists `shouldEqual` 3
53
+ main = launchAff_ do
54
+ runSpec [consoleReporter] do
55
+ describe " withClient" do
56
+ it " Returns a client" do
57
+ pool <- liftEffect $ mkPool connectionInfo
58
+ withClient pool $ \c -> do
59
+ execute_ (Query " delete from artist" ) c
60
+ execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) c
61
+ execute_ (Query " insert into artist values ('Deep Purple', 1968)" ) c
62
+ let
63
+ q :: Query Int
64
+ q = Query " insert into artist values ('Fairport Convention', 1967) returning year"
65
+
66
+ year <- queryValue_ read' q c
67
+ year `shouldEqual` (Just 1967 )
68
+
69
+ artists <- query_ read' (Query " select * from artist" :: Query Artist ) c
70
+ length artists `shouldEqual` 3
71
+ liftEffect $ end pool
72
+
73
+ describe " Low level API" do
74
+ it " Can be used to manage connections manually" do
75
+ pool <- liftEffect $ mkPool connectionInfo
76
+ client <- connect pool
77
+ execute_ (Query " delete from artist" ) client
78
+ execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) client
79
+
80
+ artists <- query_ read' (Query " select * from artist order by name desc" :: Query Artist ) client
81
+ artists `shouldEqual` [{ name: " Led Zeppelin" , year: 1968 }]
82
+
83
+ liftEffect $ release client
70
84
liftEffect $ end pool
71
85
72
- describe " Low level API" do
73
- it " Can be used to manage connections manually" do
74
- pool <- liftEffect $ mkPool connectionInfo
75
- client <- connect pool
76
- execute_ (Query " delete from artist" ) client
77
- execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) client
78
-
79
- artists <- query_ read' (Query " select * from artist order by name desc" :: Query Artist ) client
80
- artists `shouldEqual` [{ name: " Led Zeppelin" , year: 1968 }]
81
-
82
- liftEffect $ release client
83
- liftEffect $ end pool
84
-
85
- describe " Error handling" do
86
- it " When query cannot be converted to the requested data type we get an error" do
87
- res <- attempt exampleError
88
- either (const $ pure unit) (const $ fail " FAIL" ) res
89
-
90
- describe " Query params" do
91
- it " Select using a query param" do
92
- pool <- liftEffect $ mkPool connectionInfo
93
- withClient pool $ \c -> do
94
- execute_ (Query " delete from artist" ) c
95
- execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) c
96
- execute_ (Query " insert into artist values ('Deep Purple', 1968)" ) c
97
- execute_ (Query " insert into artist values ('Toto', 1977)" ) c
98
- artists <- query read' (Query " select * from artist where name = $1" :: Query Artist ) [toSql " Toto" ] c
99
- length artists `shouldEqual` 1
100
-
101
- noRows <- query read' (Query " select * from artist where name = $1" :: Query Artist ) [toSql " FAIL" ] c
102
- length noRows `shouldEqual` 0
103
- liftEffect $ end pool
104
-
105
- describe " data types" do
106
- it " datetimes can be inserted" do
107
- pool <- liftEffect $ mkPool connectionInfo
108
- withClient pool \c -> do
109
- execute_ (Query " delete from types" ) c
110
- let date = canonicalDate <$> toEnum 2016 <*> Just January <*> toEnum 25
111
- time = Time <$> toEnum 23 <*> toEnum 1 <*> toEnum 59 <*> toEnum 0
112
- dt = DateTime <$> date <*> time
113
- maybe (fail " Not a datetime" ) (\ts -> do
114
- execute (Query " insert into types(timestamp_no_tz) VALUES ($1)" ) [toSql ts] c
115
- ts' <- queryValue_ read' (Query " select timestamp_no_tz at time zone 'UTC' from types" :: Query Foreign ) c
116
- let res = unsafeCoerce <$> ts' >>= toDateTime
117
- res `shouldEqual` (Just ts)
118
- ) dt
119
- liftEffect $ end pool
120
-
121
- describe " sql arrays as parameters" $
122
- it " can be passed as a SqlValue" do
123
- pool <- liftEffect $ mkPool connectionInfo
124
- withClient pool \c -> do
125
- execute_ (Query " delete from artist" ) c
126
- execute_ (Query " insert into artist values ('Zed Leppelin', 1967)" ) c
127
- execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) c
128
- execute_ (Query " insert into artist values ('Deep Purple', 1969)" ) c
129
- artists <- query read' (Query " select * from artist where year = any ($1)" :: Query Artist ) [toSql [1968 , 1969 ]] c
130
- length artists `shouldEqual` 2
131
- liftEffect $ end pool
132
-
133
- describe " sql boolean as parameter" $
134
- it " can be passed as a SqlValue" do
135
- pool <- liftEffect $ mkPool connectionInfo
136
- withClient pool \c -> do
137
- execute_ (Query " delete from artist" ) c
138
- execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) c -- false by default
139
- execute_ (Query " insert into artist values ('Deep Purple', 1969, TRUE)" ) c
140
- aliveArtists <- query read' (Query " select * from artist where isAlive = ($1)" :: Query Artist ) [toSql true ] c
141
- notAliveArtists <- query read' (Query " select * from artist where isAlive = ($1)" :: Query Artist ) [toSql false ] c
142
- length aliveArtists `shouldEqual` 1
143
- length notAliveArtists `shouldEqual` 1
144
- liftEffect $ end pool
145
-
146
- describe " transactions" do
147
- it " does not commit after an error inside a transaction" do
148
- pool <- liftEffect $ mkPool connectionInfo
149
- withClient pool $ \c -> do
150
- execute_ (Query " delete from artist" ) c
151
- apathize $ tryInsert c
152
- one <- queryOne_ read' (Query " select * from artist" :: Query Artist ) c
153
-
154
- one `shouldEqual` Nothing
155
- liftEffect $ end pool
156
- where
157
- tryInsert = withTransaction $ \c -> do
158
- execute_ (Query " insert into artist values ('Not there', 1999)" ) c
159
- throwError $ error " fail"
86
+ describe " Error handling" do
87
+ it " When query cannot be converted to the requested data type we get an error" do
88
+ res <- attempt exampleError
89
+ either (const $ pure unit) (const $ fail " FAIL" ) res
90
+
91
+ describe " Query params" do
92
+ it " Select using a query param" do
93
+ pool <- liftEffect $ mkPool connectionInfo
94
+ withClient pool $ \c -> do
95
+ execute_ (Query " delete from artist" ) c
96
+ execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) c
97
+ execute_ (Query " insert into artist values ('Deep Purple', 1968)" ) c
98
+ execute_ (Query " insert into artist values ('Toto', 1977)" ) c
99
+ artists <- query read' (Query " select * from artist where name = $1" :: Query Artist ) [toSql " Toto" ] c
100
+ length artists `shouldEqual` 1
101
+
102
+ noRows <- query read' (Query " select * from artist where name = $1" :: Query Artist ) [toSql " FAIL" ] c
103
+ length noRows `shouldEqual` 0
104
+ liftEffect $ end pool
105
+
106
+ describe " data types" do
107
+ it " datetimes can be inserted" do
108
+ pool <- liftEffect $ mkPool connectionInfo
109
+ withClient pool \c -> do
110
+ execute_ (Query " delete from types" ) c
111
+ let date = canonicalDate <$> toEnum 2016 <*> Just January <*> toEnum 25
112
+ time = Time <$> toEnum 23 <*> toEnum 1 <*> toEnum 59 <*> toEnum 0
113
+ dt = DateTime <$> date <*> time
114
+ maybe (fail " Not a datetime" ) (\ts -> do
115
+ execute (Query " insert into types(timestamp_no_tz) VALUES ($1)" ) [toSql ts] c
116
+ ts' <- queryValue_ read' (Query " select timestamp_no_tz at time zone 'UTC' from types" :: Query Foreign ) c
117
+ let res = unsafeCoerce <$> ts' >>= toDateTime
118
+ res `shouldEqual` (Just ts)
119
+ ) dt
120
+ liftEffect $ end pool
121
+
122
+ describe " sql arrays as parameters" $
123
+ it " can be passed as a SqlValue" do
124
+ pool <- liftEffect $ mkPool connectionInfo
125
+ withClient pool \c -> do
126
+ execute_ (Query " delete from artist" ) c
127
+ execute_ (Query " insert into artist values ('Zed Leppelin', 1967)" ) c
128
+ execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) c
129
+ execute_ (Query " insert into artist values ('Deep Purple', 1969)" ) c
130
+ artists <- query read' (Query " select * from artist where year = any ($1)" :: Query Artist ) [toSql [1968 , 1969 ]] c
131
+ length artists `shouldEqual` 2
132
+ liftEffect $ end pool
133
+
134
+ describe " sql boolean as parameter" $
135
+ it " can be passed as a SqlValue" do
136
+ pool <- liftEffect $ mkPool connectionInfo
137
+ withClient pool \c -> do
138
+ execute_ (Query " delete from artist" ) c
139
+ execute_ (Query " insert into artist values ('Led Zeppelin', 1968)" ) c -- false by default
140
+ execute_ (Query " insert into artist values ('Deep Purple', 1969, TRUE)" ) c
141
+ aliveArtists <- query read' (Query " select * from artist where isAlive = ($1)" :: Query Artist ) [toSql true ] c
142
+ notAliveArtists <- query read' (Query " select * from artist where isAlive = ($1)" :: Query Artist ) [toSql false ] c
143
+ length aliveArtists `shouldEqual` 1
144
+ length notAliveArtists `shouldEqual` 1
145
+ liftEffect $ end pool
146
+
147
+ describe " transactions" do
148
+ it " does not commit after an error inside a transaction" do
149
+ pool <- liftEffect $ mkPool connectionInfo
150
+ withClient pool $ \c -> do
151
+ execute_ (Query " delete from artist" ) c
152
+ apathize $ tryInsert c
153
+ one <- queryOne_ read' (Query " select * from artist" :: Query Artist ) c
154
+
155
+ one `shouldEqual` Nothing
156
+ liftEffect $ end pool
157
+ where
158
+ tryInsert = withTransaction $ \c -> do
159
+ execute_ (Query " insert into artist values ('Not there', 1999)" ) c
160
+ throwError $ error " fail"
160
161
161
162
exampleError :: Aff (Maybe Artist )
162
163
exampleError = do
0 commit comments