Add errors and optional fields
This commit is contained in:
@@ -32,8 +32,8 @@ instance FromRow SingleString where
|
||||
|
||||
shouldFetch :: (Eq a, FromRow a, Show a) => Connection -> ByteString -> [a] -> IO ()
|
||||
shouldFetch conn query expectedRows = do
|
||||
Just actualRows <- Opium.fetch_ conn query
|
||||
actualRows `shouldBe` expectedRows
|
||||
actualRows <- Opium.fetch_ conn query
|
||||
actualRows `shouldBe` Right expectedRows
|
||||
|
||||
spec :: SpecWith Connection
|
||||
spec = do
|
||||
|
||||
@@ -7,7 +7,7 @@ module Database.PostgreSQL.OpiumSpec (spec) where
|
||||
import Data.Text (Text)
|
||||
import Database.PostgreSQL.LibPQ (Connection)
|
||||
import GHC.Generics (Generic)
|
||||
import Test.Hspec (SpecWith, describe, it, shouldBe)
|
||||
import Test.Hspec (SpecWith, describe, it, shouldBe, shouldSatisfy)
|
||||
|
||||
import qualified Database.PostgreSQL.LibPQ as LibPQ
|
||||
import qualified Database.PostgreSQL.Opium as Opium
|
||||
@@ -19,6 +19,16 @@ data Person = Person
|
||||
|
||||
instance Opium.FromRow Person where
|
||||
|
||||
newtype MaybeTest = MaybeTest
|
||||
{ a :: Maybe String
|
||||
} deriving (Eq, Generic, Show)
|
||||
|
||||
instance Opium.FromRow MaybeTest where
|
||||
|
||||
isLeft :: Either a b -> Bool
|
||||
isLeft (Left _) = True
|
||||
isLeft _ = False
|
||||
|
||||
spec :: SpecWith Connection
|
||||
spec = do
|
||||
describe "fromRow" $ do
|
||||
@@ -26,12 +36,40 @@ spec = do
|
||||
Just result <- LibPQ.execParams conn "SELECT * FROM person" [] LibPQ.Text
|
||||
|
||||
row0 <- Opium.fromRow @Person (LibPQ.Row 0) result
|
||||
row0 `shouldBe` Just (Person "paul" 25)
|
||||
row0 `shouldBe` Right (Person "paul" 25)
|
||||
|
||||
row1 <- Opium.fromRow @Person (LibPQ.Row 1) result
|
||||
row1 `shouldBe` Just (Person "albus" 103)
|
||||
row1 `shouldBe` Right (Person "albus" 103)
|
||||
|
||||
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
|
||||
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
|
||||
rows <- Opium.fetch_ conn "SELECT * FROM person"
|
||||
rows `shouldBe` Just [Person "paul" 25, Person "albus" 103]
|
||||
rows `shouldBe` Right [Person "paul" 25, Person "albus" 103]
|
||||
|
||||
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
|
||||
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
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user