@@ -3,6 +3,7 @@ module Driver where
3
3
import Data.Monoid ((<>) )
4
4
import Data.Foldable
5
5
import Control.Monad
6
+ import Data.Either
6
7
import qualified Data.ByteString as B
7
8
import qualified Data.ByteString.Char8 as BS
8
9
@@ -14,20 +15,26 @@ import Database.PostgreSQL.Protocol.Types
14
15
15
16
import Connection
16
17
18
+ testDriver :: TestTree
19
+ testDriver = testGroup " Driver"
20
+ [ testCase " Single batch" testBatch
21
+ , testCase " Two batches" testTwoBatches
22
+ , testCase " Empty query" testEmptyQuery
23
+ , testCase " Query without result" testQueryWithoutResult
24
+ , testCase " Invalid queries" testInvalidBatch
25
+ ]
26
+
17
27
makeQuery1 :: B. ByteString -> Query
18
28
makeQuery1 n = Query " SELECT $1" [Oid 23 ] [n] Text Text
19
29
20
30
makeQuery2 :: B. ByteString -> B. ByteString -> Query
21
31
makeQuery2 n1 n2 = Query " SELECT $1 + $2" [Oid 23 , Oid 23 ] [n1, n2] Text Text
22
32
23
- testDriver = testGroup " Driver"
24
- [ testCase " Single batch" testBatch
25
- , testCase " Two batches" testTwoBatches
26
- ]
27
-
33
+ fromRight :: Either e a -> a
28
34
fromRight (Right v) = v
29
35
fromRight _ = error " fromRight"
30
36
37
+
31
38
testBatch :: IO ()
32
39
testBatch = withConnection $ \ c -> do
33
40
let a = " 5"
@@ -58,3 +65,55 @@ testTwoBatches = withConnection $ \c -> do
58
65
fromMessage (DataMessage [[v]]) = v
59
66
fromMessage _ = error " from message"
60
67
68
+ testEmptyQuery :: IO ()
69
+ testEmptyQuery = assertQueryNoData $
70
+ Query " " [] [] Text Text
71
+
72
+ testQueryWithoutResult :: IO ()
73
+ testQueryWithoutResult = assertQueryNoData $
74
+ Query " SET client_encoding TO UTF8" [] [] Text Text
75
+
76
+ -- helper
77
+ assertQueryNoData :: Query -> IO ()
78
+ assertQueryNoData q = withConnection $ \ c -> do
79
+ sendBatchAndSync c [q]
80
+ r <- fromRight <$> readNextData c
81
+ readReadyForQuery c
82
+ DataMessage [] @=? r
83
+
84
+ -- | Asserts that all the received data rows are in form (Right _)
85
+ checkRightResult :: Connection -> Int -> Assertion
86
+ checkRightResult conn 0 = pure ()
87
+ checkRightResult conn n = readNextData conn >>=
88
+ either (const $ assertFailure " Result is invalid" )
89
+ (const $ checkRightResult conn (n - 1 ))
90
+
91
+ -- | Asserts that (Left _) as result exists in the received data rows.
92
+ checkInvalidResult :: Connection -> Int -> Assertion
93
+ checkInvalidResult conn 0 = assertFailure " Result is right"
94
+ checkInvalidResult conn n = readNextData conn >>=
95
+ either (const $ pure () )
96
+ (const $ checkInvalidResult conn (n - 1 ))
97
+
98
+ testInvalidBatch :: IO ()
99
+ testInvalidBatch = do
100
+ let rightQuery = makeQuery1 " 5"
101
+ q1 = Query " SEL $1" [Oid 23 ] [" 5" ] Text Text
102
+ q2 = Query " SELECT $1" [Oid 23 ] [" a" ] Text Text
103
+ q3 = Query " SELECT $1" [Oid 23 ] [] Text Text
104
+ q4 = Query " SELECT $1" [] [" 5" ] Text Text
105
+
106
+ assertInvalidBatch " Parse error" [q1]
107
+ assertInvalidBatch " Invalid param" [ q2]
108
+ assertInvalidBatch " Missed param" [ q3]
109
+ assertInvalidBatch " Missed oid of param" [ q4]
110
+ assertInvalidBatch " Parse error" [rightQuery, q1]
111
+ assertInvalidBatch " Invalid param" [rightQuery, q2]
112
+ assertInvalidBatch " Missed param" [rightQuery, q3]
113
+ assertInvalidBatch " Missed oid of param" [rightQuery, q4]
114
+ where
115
+ assertInvalidBatch desc qs = withConnection $ \ c -> do
116
+ sendBatchAndSync c qs
117
+ readReadyForQuery c
118
+ checkInvalidResult c $ length qs
119
+
0 commit comments