2022-08-26 12:40:41 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-06-29 16:35:59 +03:00
|
|
|
module Hasura.Backends.Postgres.SQL.ValueSpec (spec) where
|
|
|
|
|
2022-08-26 12:40:41 +03:00
|
|
|
import Data.Text.RawString (raw)
|
2022-06-29 16:35:59 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.DML
|
|
|
|
import Hasura.Backends.Postgres.SQL.Types
|
2022-08-26 12:40:41 +03:00
|
|
|
import Hasura.Backends.Postgres.SQL.Value (PGScalarValue (..), parsePGValue, pgScalarValueToJson, txtEncoder)
|
2022-06-29 16:35:59 +03:00
|
|
|
import Hasura.Base.Error (Code (ParseFailed), QErr (..), runAesonParser)
|
2022-07-01 14:47:20 +03:00
|
|
|
import Hasura.Base.Error.TestInstances ()
|
2022-06-29 16:35:59 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Network.HTTP.Types qualified as HTTP
|
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
txtEncoderSpec
|
|
|
|
jsonValueSpec
|
|
|
|
|
2022-08-26 12:40:41 +03:00
|
|
|
singleElement, multiElement, edgeCaseStrings, nestedArray, nestedArray', malformedArray :: PGScalarValue
|
2022-06-29 16:35:59 +03:00
|
|
|
singleElement = PGValArray [PGValInteger 1]
|
|
|
|
multiElement = PGValArray [PGValVarchar "a", PGValVarchar "b"]
|
2022-08-26 12:40:41 +03:00
|
|
|
edgeCaseStrings = PGValArray $ map PGValVarchar ["a", "", [raw|"|], [raw|\|], [raw|,|], [raw|}|]]
|
2022-06-29 16:35:59 +03:00
|
|
|
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
|
2022-08-26 12:40:41 +03:00
|
|
|
txtEncoder singleElement `shouldBe` SELit [raw|{1}|]
|
2022-06-29 16:35:59 +03:00
|
|
|
it "multiple elements" $ do
|
2022-08-26 12:40:41 +03:00
|
|
|
txtEncoder multiElement `shouldBe` SELit [raw|{"a","b"}|]
|
2022-06-29 16:35:59 +03:00
|
|
|
it "simple nested arrays" $ do
|
2022-08-26 12:40:41 +03:00
|
|
|
txtEncoder nestedArray `shouldBe` SELit [raw|{{"a","b"},{"a","b"}}|]
|
2022-06-29 16:35:59 +03:00
|
|
|
it "more deeply nested arrays" $ do
|
2022-08-26 12:40:41 +03:00
|
|
|
txtEncoder nestedArray' `shouldBe` SELit [raw|{{{"a","b"},{"a","b"}}}|]
|
|
|
|
it "edge case strings" $ do
|
|
|
|
txtEncoder edgeCaseStrings `shouldBe` SELit [raw|{"a","","\"","\\",",","}"}|]
|
2022-06-29 16:35:59 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|