Make getColumnTable report missing columns

This commit is contained in:
2023-09-16 04:58:23 +02:00
parent 591f4cddfa
commit ab396b9db6
3 changed files with 28 additions and 28 deletions
+15 -14
View File
@@ -36,19 +36,24 @@ spec = do
it "Gets the column table for a result" $ \conn -> do
Just result <- LibPQ.execParams conn "SELECT name, age FROM person" [] LibPQ.Text
columnTable <- Opium.getColumnTable @Person Proxy result
columnTable `shouldBe` [0, 1]
columnTable `shouldBe` Right [0, 1]
it "Gets the numbers right for funky configurations" $ \conn -> do
Just result0 <- LibPQ.execParams conn "SELECT age, name FROM person" [] LibPQ.Text
columnTable0 <- Opium.getColumnTable @Person Proxy result0
columnTable0 `shouldBe` [1, 0]
columnTable0 `shouldBe` Right [1, 0]
Just result1 <- LibPQ.execParams conn "SELECT 0 a, 1 b, 2 c, age, 4 d, name FROM person" [] LibPQ.Text
columnTable1 <- Opium.getColumnTable @Person Proxy result1
columnTable1 `shouldBe` [5, 3]
columnTable1 `shouldBe` Right [5, 3]
it "Fails for missing columns" $ \conn -> do
Just result <- LibPQ.execParams conn "SELECT 0 a FROM person" [] LibPQ.Text
columnTable <- Opium.getColumnTable @Person Proxy result
columnTable `shouldBe` Left (Opium.ErrorMissingColumn "name")
describe "fromRow" $ do
it "decodes rows in a Result" $ \conn -> do
it "Decodes rows in a Result" $ \conn -> do
Just result <- LibPQ.execParams conn "SELECT * FROM person" [] LibPQ.Text
row0 <- Opium.fromRow @Person (LibPQ.Row 0) result
@@ -57,35 +62,31 @@ spec = do
row1 <- Opium.fromRow @Person (LibPQ.Row 1) result
row1 `shouldBe` Right (Person "albus" 103)
it "decodes NULL into Nothing for Maybes" $ \conn -> do
it "Decodes NULL into Nothing for Maybes" $ \conn -> do
Just result <- LibPQ.execParams conn "SELECT NULL AS a" [] LibPQ.Text
row <- Opium.fromRow (LibPQ.Row 0) result
row `shouldBe` Right (MaybeTest Nothing)
it "decodes values into Just for Maybes" $ \conn -> do
it "Decodes values into Just for Maybes" $ \conn -> do
Just result <- LibPQ.execParams conn "SELECT 'abc' AS a" [] LibPQ.Text
row <- Opium.fromRow (LibPQ.Row 0) result
row `shouldBe` Right (MaybeTest $ Just "abc")
describe "fetch_" $ do
it "retrieves a list of rows" $ \conn -> do
it "Retrieves a list of rows" $ \conn -> do
rows <- Opium.fetch_ conn "SELECT * FROM person"
rows `shouldBe` Right [Person "paul" 25, Person "albus" 103]
it "fails for invalid queries" $ \conn -> do
it "Fails for invalid queries" $ \conn -> do
rows <- Opium.fetch_ @Person conn "MRTLBRNFT"
rows `shouldSatisfy` isLeft
it "fails for missing columns" $ \conn -> do
rows <- Opium.fetch_ @Person conn "SELECT name FROM person"
rows `shouldBe` Left (Opium.ErrorMissingColumn "age")
it "fails for unexpected NULLs" $ \conn -> do
it "Fails for unexpected NULLs" $ \conn -> do
rows <- Opium.fetch_ @Person conn "SELECT NULL AS name, 0 AS age"
rows `shouldBe` Left (Opium.ErrorUnexpectedNull (LibPQ.Row 0) "name")
it "fails for the wrong column type" $ \conn -> do
it "Fails for the wrong column type" $ \conn -> do
rows <- Opium.fetch_ @Person conn "SELECT 'quby' AS name, 'indeterminate' AS age"
rows `shouldBe` Left (Opium.ErrorDecode (LibPQ.Row 0) "age" $ Opium.FieldErrorInvalidOid $ LibPQ.Oid 25)