graphql-engine/server/src-test/Hasura/Backends/Postgres/SQL/ValueSpec.hs
Abby Sassel 9d93baa96f server/postgres: parameterise array variables in queries/subscriptions
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4776
Co-authored-by: Tom Harding <6302310+i-am-tom@users.noreply.github.com>
Co-authored-by: Brandon Martin <40686+codedmart@users.noreply.github.com>
GitOrigin-RevId: ca962e3bfaa28ef00c5d04273d6604348d7da1c8
2022-06-29 13:37:11 +00:00

66 lines
2.4 KiB
Haskell

module Hasura.Backends.Postgres.SQL.ValueSpec (spec) where
import Hasura.Backends.Postgres.SQL.DML
import Hasura.Backends.Postgres.SQL.Types
import Hasura.Backends.Postgres.SQL.Value
import Hasura.Base.Error (Code (ParseFailed), QErr (..), runAesonParser)
import Hasura.Prelude
import Network.HTTP.Types qualified as HTTP
import Test.Hspec
spec :: Spec
spec = do
txtEncoderSpec
jsonValueSpec
singleElement, multiElement, nestedArray, nestedArray', malformedArray :: PGScalarValue
singleElement = PGValArray [PGValInteger 1]
multiElement = PGValArray [PGValVarchar "a", PGValVarchar "b"]
nestedArray = PGValArray [multiElement, multiElement]
nestedArray' = PGValArray [nestedArray]
malformedArray = PGValArray [PGValInteger 1]
txtEncoderSpec :: Spec
txtEncoderSpec =
describe "txtEncoder should encode a valid Postgres array of:" $ do
it "a single element" $ do
txtEncoder singleElement `shouldBe` SELit "{1}"
it "multiple elements" $ do
txtEncoder multiElement `shouldBe` SELit "{a,b}"
it "simple nested arrays" $ do
txtEncoder nestedArray `shouldBe` SELit "{{a,b},{a,b}}"
it "more deeply nested arrays" $ do
txtEncoder nestedArray' `shouldBe` SELit "{{{a,b},{a,b}}}"
pgArrayRoundtrip :: PGScalarValue -> PGScalarType -> Expectation
pgArrayRoundtrip v t = do
let parsedValue = runExcept $ runAesonParser (parsePGValue t) (pgScalarValueToJson v)
parsedValue `shouldBe` Right v
jsonValueSpec :: Spec
jsonValueSpec = describe "JSON Roundtrip: PGArray" do
describe "parsePGValue" $ do
it "singleElement PGArray" $ do
pgArrayRoundtrip singleElement (PGArray PGInteger)
it "multiElement PGArray" $ do
pgArrayRoundtrip multiElement (PGArray PGVarchar)
it "nestedArray PGArray" $ do
pgArrayRoundtrip nestedArray (PGArray (PGArray PGVarchar))
it "nestedArray' PGArray" $ do
pgArrayRoundtrip nestedArray' (PGArray (PGArray (PGArray PGVarchar)))
it "malformedArray PGArray" $ do
let parsedValue = runExcept $ runAesonParser (parsePGValue PGVarchar) (pgScalarValueToJson malformedArray)
parsedValue
`shouldBe` Left
QErr
{ qePath = [],
qeStatus =
HTTP.Status
{ statusCode = 400,
statusMessage = "Bad Request"
},
qeError = "parsing Text failed, expected String, but encountered Array",
qeCode = ParseFailed,
qeInternal = Nothing
}