graphql-engine/server/src-test/Test/Backend/Postgres/Update.hs

86 lines
3.0 KiB
Haskell
Raw Normal View History

-- | Test Backend Postgres Update
--
-- Helpers to build 'Hasura.RQL.IR.Update.AnnotatedUpdateG' and test the results
-- of running 'Hasura.Backends.Postgres.Translate.Update.mkUpdateCTE' as raw
-- SQL.
--
-- See 'Hasura.Backends.Postgres.Translate.UpdateSpec.spec' for usage examples.
module Test.Backend.Postgres.Update
( TestBuilder (..),
runTest,
runMultipleUpdates,
)
where
import Hasura.Backends.Postgres.SQL.Types (QualifiedTable)
import Hasura.Backends.Postgres.Translate.Update qualified as Update
import Hasura.Prelude
import Hasura.RQL.IR.BoolExp (OpExpG (..))
import Hasura.RQL.IR.Returning (MutationOutputG (..))
import Hasura.RQL.IR.Value (UnpreparedValue (..))
import Hasura.RQL.Types.Column (ColumnInfo)
import Hasura.SQL.Backend (PostgresKind (Vanilla))
import Hasura.SQL.Types (toSQLTxt)
import Test.Backend.Postgres.Misc
import Test.HUnit.Base (assertFailure)
import Test.Hspec
import Test.Parser.Expectation qualified as Expect
import Test.SIString qualified as SI
-- | Describes a /mkUpdateCTE/ test.
data TestBuilder e = TestBuilder
{ -- | test name
name :: String,
-- | table details
table :: QualifiedTable,
-- | table columnd
columns :: [ColumnInfo PG],
-- | expected output fields
mutationOutput :: MutationOutputG PG Void (UnpreparedValue PG),
-- | where clause for the query (usually empty for /update_many/)
where_ :: [(ColumnInfo PG, [OpExpG PG (UnpreparedValue PG)])],
-- | update clause
update :: Expect.BackendUpdateBuilder (ColumnInfo PG),
-- | expected result; this is either 'Text' or '[Text]'
expectedSQL :: e
}
-- | Runs a test for single updates.
runTest :: TestBuilder Text -> Spec
runTest TestBuilder {..} =
it name do
let upd =
unpreparedValueToSQLExp
<$> Expect.mkAnnotatedUpdate @Void
Expect.AnnotatedUpdateBuilder
{ Expect.aubTable = table,
Expect.aubOutput = mutationOutput,
Expect.aubColumns = columns,
Expect.aubWhere = where_,
Expect.aubUpdate = update
}
case Update.mkUpdateCTE @'Vanilla upd of
(Update.Update cte) ->
(SI.fromText $ toSQLTxt cte) `shouldBe` SI.fromText expectedSQL
_ -> assertFailure "expected single update, got multiple updates"
-- | Runs a test for /update_many/
runMultipleUpdates :: TestBuilder [Text] -> Spec
runMultipleUpdates TestBuilder {..} =
it name do
let upd =
unpreparedValueToSQLExp
<$> Expect.mkAnnotatedUpdate @Void
Expect.AnnotatedUpdateBuilder
{ Expect.aubTable = table,
Expect.aubOutput = mutationOutput,
Expect.aubColumns = columns,
Expect.aubWhere = where_,
Expect.aubUpdate = update
}
case Update.mkUpdateCTE @'Vanilla upd of
(Update.MultiUpdate ctes) ->
SI.fromText . toSQLTxt <$> ctes
`shouldBe` SI.fromText <$> expectedSQL
_ -> assertFailure "expedted update_many, got single update"