graphql-engine/server/src-test/Test/Backend/Postgres/Update.hs
Samir Talwar 96a768e526 Upgrade HLint to v3.4.1.
This upgrades CI and anyone using Nix to HLint v3.4.1.

If you're not using Nix, this doesn't actually _do_ anything on your
local machine; it's just a suggestion.

It also applies a bunch of simple HLint refactors, using
`make lint-hs-fix`.

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6324
GitOrigin-RevId: de8267e4909d6dcd3f83543188517f3aaeebc5f3
2022-10-12 19:14:56 +00:00

86 lines
3.0 KiB
Haskell

-- | 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"