Use Vector for column table for constant element access
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
module Database.PostgreSQL.OpiumSpec (spec) where
|
||||
|
||||
import Data.ByteString (ByteString)
|
||||
import Data.Proxy (Proxy (..))
|
||||
import Data.Text (Text)
|
||||
import Database.PostgreSQL.LibPQ (Connection)
|
||||
@@ -41,22 +42,35 @@ isLeft :: Either a b -> Bool
|
||||
isLeft (Left _) = True
|
||||
isLeft _ = False
|
||||
|
||||
shouldHaveColumns
|
||||
:: Opium.FromRow a
|
||||
=> Proxy a
|
||||
-> Connection
|
||||
-> ByteString
|
||||
-> [LibPQ.Column]
|
||||
-> IO ()
|
||||
shouldHaveColumns proxy conn query expectedColumns = do
|
||||
Just result <- LibPQ.execParams conn query [] LibPQ.Text
|
||||
columnTable <- Opium.getColumnTable proxy result
|
||||
let actualColumns = fmap (map fst . Opium.toListColumnTable) columnTable
|
||||
actualColumns `shouldBe` Right expectedColumns
|
||||
|
||||
spec :: SpecWith Connection
|
||||
spec = do
|
||||
describe "getColumnTable" $ 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
|
||||
fmap (map fst) columnTable `shouldBe` Right [0, 1]
|
||||
shouldHaveColumns @Person Proxy conn
|
||||
"SELECT name, age FROM person"
|
||||
[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
|
||||
fmap (map fst) columnTable0 `shouldBe` Right [1, 0]
|
||||
shouldHaveColumns @Person Proxy conn
|
||||
"SELECT age, name FROM person"
|
||||
[1, 0]
|
||||
|
||||
Just result1 <- LibPQ.execParams conn "SELECT 0 AS a, 1 AS b, 2 AS c, age, 4 AS d, name FROM person" [] LibPQ.Text
|
||||
columnTable1 <- Opium.getColumnTable @Person Proxy result1
|
||||
fmap (map fst) columnTable1 `shouldBe` Right [5, 3]
|
||||
shouldHaveColumns @Person Proxy conn
|
||||
"SELECT 0 AS a, 1 AS b, 2 AS c, age, 4 AS d, name FROM person"
|
||||
[5, 3]
|
||||
|
||||
it "Fails for missing columns" $ \conn -> do
|
||||
Just result <- LibPQ.execParams conn "SELECT 0 AS a FROM person" [] LibPQ.Text
|
||||
|
||||
Reference in New Issue
Block a user