graphql-engine/server/src-test/Hasura/Backends/Postgres/SQL/ValueSpec.hs
Abby Sassel 71597778c9 server/postgres: fix non-latin text elements in array literals (copy). close hasura/graphql-engine#8961
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5916
Co-authored-by: Gil Mizrahi <8547573+soupi@users.noreply.github.com>
GitOrigin-RevId: 9063f7ff39f684c4ab43f093706b872021b67b13
2022-09-18 16:08:15 +00:00

76 lines
3.0 KiB
Haskell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{-# LANGUAGE QuasiQuotes #-}
module Hasura.Backends.Postgres.SQL.ValueSpec (spec) where
import Data.Text.RawString (raw)
import Hasura.Backends.Postgres.SQL.DML
import Hasura.Backends.Postgres.SQL.Types
import Hasura.Backends.Postgres.SQL.Value (PGScalarValue (..), parsePGValue, pgScalarValueToJson, txtEncoder)
import Hasura.Base.Error (Code (ParseFailed), QErr (..), runAesonParser)
import Hasura.Base.Error.TestInstances ()
import Hasura.Prelude
import Network.HTTP.Types qualified as HTTP
import Test.Hspec
spec :: Spec
spec = do
txtEncoderSpec
jsonValueSpec
singleElement, multiElement, edgeCaseStrings, nestedArray, nestedArray', malformedArray, nonLatinArray :: PGScalarValue
singleElement = PGValArray [PGValInteger 1]
multiElement = PGValArray [PGValVarchar "a", PGValVarchar "b"]
edgeCaseStrings = PGValArray $ map PGValVarchar ["a", "", [raw|"|], [raw|\|], [raw|,|], [raw|}|]]
nestedArray = PGValArray [multiElement, multiElement]
nestedArray' = PGValArray [nestedArray]
malformedArray = PGValArray [PGValInteger 1]
nonLatinArray = PGValArray [PGValVarchar "שלום"]
txtEncoderSpec :: Spec
txtEncoderSpec =
describe "txtEncoder should encode a valid Postgres array of:" $ do
it "a single element" $ do
txtEncoder singleElement `shouldBe` SELit [raw|{1}|]
it "multiple elements" $ do
txtEncoder multiElement `shouldBe` SELit [raw|{"a","b"}|]
it "simple nested arrays" $ do
txtEncoder nestedArray `shouldBe` SELit [raw|{{"a","b"},{"a","b"}}|]
it "more deeply nested arrays" $ do
txtEncoder nestedArray' `shouldBe` SELit [raw|{{{"a","b"},{"a","b"}}}|]
it "edge case strings" $ do
txtEncoder edgeCaseStrings `shouldBe` SELit [raw|{"a","","\"","\\",",","}"}|]
it "non-latin characters" $ do
txtEncoder nonLatinArray `shouldBe` SELit [raw|{"שלום"}|]
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
}