10000 Asserting invalid batches · postgres-haskell/postgres-wire@3a4ca4c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a4ca4c

Browse files
Asserting invalid batches
1 parent f34eabe commit 3a4ca4c

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

tests/Driver.hs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Driver where
33
import Data.Monoid ((<>))
44
import Data.Foldable
55
import Control.Monad
6+
import Data.Either
67
import qualified Data.ByteString as B
78
import qualified Data.ByteString.Char8 as BS
89

@@ -14,20 +15,26 @@ import Database.PostgreSQL.Protocol.Types
1415

1516
import Connection
1617

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+
1727
makeQuery1 :: B.ByteString -> Query
1828
makeQuery1 n = Query "SELECT $1" [Oid 23] [n] Text Text
1929

2030
makeQuery2 :: B.ByteString -> B.ByteString -> Query
2131
makeQuery2 n1 n2 = Query "SELECT $1 + $2" [Oid 23, Oid 23] [n1, n2] Text Text
2232

23-
testDriver = testGroup "Driver"
24-
[ testCase "Single batch" testBatch
25-
, testCase "Two batches" testTwoBatches
26-
]
27-
33+
fromRight :: Either e a -> a
2834
fromRight (Right v) = v
2935
fromRight _ = error "fromRight"
3036

37+
3138
testBatch :: IO ()
3239
testBatch = withConnection $ \c -> do
3340
let a = "5"
@@ -58,3 +65,55 @@ testTwoBatches = withConnection $ \c -> do
5865
fromMessage (DataMessage [[v]]) = v
5966
fromMessage _ = error "from message"
6067

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

Comments
 (0)
0