2022-08-06 00:40:41 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
|
|
|
module Hasura.Backends.Postgres.Translate.UpdateSpec
|
|
|
|
( spec,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Database.PG.Query.Pool qualified as QQ
|
|
|
|
import Hasura.Backends.Postgres.Types.Update (UpdateOpExpression (..))
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.IR.BoolExp (OpExpG (..))
|
|
|
|
import Hasura.RQL.IR.Returning (MutFldG (..), MutationOutputG (..))
|
2022-08-18 11:59:59 +03:00
|
|
|
import Test.Backend.Postgres.Misc qualified as P
|
2022-08-06 00:40:41 +03:00
|
|
|
import Test.Backend.Postgres.Update qualified as Test
|
|
|
|
import Test.Hspec
|
|
|
|
import Test.Parser.Expectation qualified as Expect
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec =
|
|
|
|
describe "Postgres.Translate.UpdateSpec" do
|
|
|
|
Test.runTest
|
|
|
|
Test.TestBuilder
|
|
|
|
{ name = "set field where id",
|
|
|
|
table = Expect.mkTable "test",
|
2022-08-18 11:59:59 +03:00
|
|
|
columns = [P.idColumn, P.nameColumn],
|
2022-08-11 13:45:39 +03:00
|
|
|
mutationOutput = MOutMultirowFields [("affected_rows", MCount)],
|
2022-08-18 14:30:54 +03:00
|
|
|
where_ = [(P.idColumn, [AEQ True P.integerOne])],
|
|
|
|
update = Expect.UpdateTable [(P.nameColumn, UpdateSet P.textNew)],
|
2022-08-11 13:45:39 +03:00
|
|
|
expectedSQL =
|
2022-08-06 00:40:41 +03:00
|
|
|
[QQ.sql|
|
|
|
|
UPDATE "public"."test"
|
2022-08-18 14:30:54 +03:00
|
|
|
SET "name" = ('new name'):: text
|
2022-08-06 00:40:41 +03:00
|
|
|
WHERE
|
2022-08-18 14:30:54 +03:00
|
|
|
(("public"."test"."id") = (('1')::integer))
|
2022-08-06 00:40:41 +03:00
|
|
|
RETURNING * , ('true')::boolean AS "check__constraint"
|
|
|
|
|]
|
|
|
|
}
|
|
|
|
|
|
|
|
Test.runTest
|
|
|
|
Test.TestBuilder
|
|
|
|
{ name = "set multiple field where id",
|
|
|
|
table = Expect.mkTable "test",
|
2022-08-18 11:59:59 +03:00
|
|
|
columns = [P.idColumn, P.nameColumn, P.descColumn],
|
2022-08-11 13:45:39 +03:00
|
|
|
mutationOutput = MOutMultirowFields [("affected_rows", MCount)],
|
2022-08-18 14:30:54 +03:00
|
|
|
where_ = [(P.idColumn, [AEQ True P.integerOne])],
|
2022-08-18 11:59:59 +03:00
|
|
|
update =
|
|
|
|
Expect.UpdateTable
|
2022-08-18 14:30:54 +03:00
|
|
|
[ (P.nameColumn, UpdateSet P.textNew),
|
|
|
|
(P.descColumn, UpdateSet P.textOther)
|
2022-08-18 11:59:59 +03:00
|
|
|
],
|
2022-08-11 13:45:39 +03:00
|
|
|
expectedSQL =
|
2022-08-06 00:40:41 +03:00
|
|
|
[QQ.sql|
|
|
|
|
UPDATE "public"."test"
|
2022-08-18 14:30:54 +03:00
|
|
|
SET "name" = ('new name')::text, "description" = ('other')::text
|
2022-08-06 00:40:41 +03:00
|
|
|
WHERE
|
2022-08-18 14:30:54 +03:00
|
|
|
(("public"."test"."id") = (('1')::integer))
|
2022-08-06 00:40:41 +03:00
|
|
|
RETURNING * , ('true')::boolean AS "check__constraint"
|
|
|
|
|]
|
|
|
|
}
|
|
|
|
|
|
|
|
Test.runTest
|
|
|
|
Test.TestBuilder
|
|
|
|
{ name = "set field where id and old value",
|
|
|
|
table = Expect.mkTable "test",
|
2022-08-18 11:59:59 +03:00
|
|
|
columns = [P.idColumn, P.nameColumn, P.descColumn],
|
2022-08-11 13:45:39 +03:00
|
|
|
mutationOutput = MOutMultirowFields [("affected_rows", MCount)],
|
2022-08-18 11:59:59 +03:00
|
|
|
where_ =
|
2022-08-18 14:30:54 +03:00
|
|
|
[ (P.idColumn, [AEQ True P.integerOne]),
|
|
|
|
(P.nameColumn, [AEQ False P.textOld])
|
2022-08-18 11:59:59 +03:00
|
|
|
],
|
2022-08-18 14:30:54 +03:00
|
|
|
update = Expect.UpdateTable [(P.nameColumn, UpdateSet P.textNew)],
|
2022-08-11 13:45:39 +03:00
|
|
|
expectedSQL =
|
2022-08-06 00:40:41 +03:00
|
|
|
[QQ.sql|
|
|
|
|
UPDATE "public"."test"
|
2022-08-18 14:30:54 +03:00
|
|
|
SET "name" = ('new name')::text
|
2022-08-06 00:40:41 +03:00
|
|
|
WHERE
|
2022-08-18 14:30:54 +03:00
|
|
|
((("public"."test"."id") = (('1')::integer))
|
2022-08-06 00:40:41 +03:00
|
|
|
AND
|
2022-08-18 14:30:54 +03:00
|
|
|
((("public"."test"."name") = (('old name')::text))
|
2022-08-06 00:40:41 +03:00
|
|
|
OR
|
|
|
|
((("public"."test"."name") IS NULL)
|
2022-08-18 14:30:54 +03:00
|
|
|
AND ((('old name')::text) IS NULL))
|
2022-08-06 00:40:41 +03:00
|
|
|
))
|
|
|
|
RETURNING * , ('true')::boolean AS "check__constraint"
|
|
|
|
|]
|
|
|
|
}
|
|
|
|
|
|
|
|
Test.runMultipleUpdates
|
|
|
|
Test.TestBuilder
|
|
|
|
{ name = "update_many with two updates",
|
|
|
|
table = Expect.mkTable "test",
|
2022-08-18 11:59:59 +03:00
|
|
|
columns = [P.idColumn, P.nameColumn, P.descColumn],
|
2022-08-11 13:45:39 +03:00
|
|
|
mutationOutput = MOutMultirowFields [("affected_rows", MCount)],
|
2022-08-06 00:40:41 +03:00
|
|
|
where_ = [],
|
|
|
|
update =
|
|
|
|
Expect.UpdateMany $
|
|
|
|
[ Expect.MultiRowUpdateBuilder
|
2022-08-18 11:59:59 +03:00
|
|
|
{ mrubWhere =
|
2022-08-18 14:30:54 +03:00
|
|
|
[ (P.idColumn, [AEQ True P.integerOne]),
|
|
|
|
(P.nameColumn, [AEQ False P.textNew])
|
2022-08-18 11:59:59 +03:00
|
|
|
],
|
2022-08-18 14:30:54 +03:00
|
|
|
mrubUpdate = [(P.nameColumn, UpdateSet P.textNew)]
|
2022-08-06 00:40:41 +03:00
|
|
|
},
|
|
|
|
Expect.MultiRowUpdateBuilder
|
2022-08-18 14:30:54 +03:00
|
|
|
{ mrubWhere = [(P.idColumn, [AEQ True P.integerOne])],
|
|
|
|
mrubUpdate = [(P.descColumn, UpdateSet P.textNew)]
|
2022-08-06 00:40:41 +03:00
|
|
|
}
|
|
|
|
],
|
2022-08-11 13:45:39 +03:00
|
|
|
expectedSQL =
|
2022-08-06 00:40:41 +03:00
|
|
|
[ [QQ.sql|
|
|
|
|
UPDATE "public"."test"
|
2022-08-18 14:30:54 +03:00
|
|
|
SET "name" = ('new name')::text
|
2022-08-06 00:40:41 +03:00
|
|
|
WHERE
|
2022-08-18 14:30:54 +03:00
|
|
|
((("public"."test"."id") = (('1')::integer))
|
2022-08-06 00:40:41 +03:00
|
|
|
AND
|
2022-08-18 14:30:54 +03:00
|
|
|
((("public"."test"."name") = (('new name')::text))
|
2022-08-06 00:40:41 +03:00
|
|
|
OR
|
|
|
|
((("public"."test"."name") IS NULL)
|
2022-08-18 14:30:54 +03:00
|
|
|
AND ((('new name')::text) IS NULL))
|
2022-08-06 00:40:41 +03:00
|
|
|
))
|
|
|
|
RETURNING * , ('true')::boolean AS "check__constraint"
|
|
|
|
|],
|
|
|
|
[QQ.sql|
|
|
|
|
UPDATE "public"."test"
|
2022-08-18 14:30:54 +03:00
|
|
|
SET "description" = ('new name')::text
|
2022-08-06 00:40:41 +03:00
|
|
|
WHERE
|
2022-08-18 14:30:54 +03:00
|
|
|
(("public"."test"."id") = (('1')::integer))
|
2022-08-06 00:40:41 +03:00
|
|
|
RETURNING * , ('true')::boolean AS "check__constraint"
|
|
|
|
|]
|
|
|
|
]
|
|
|
|
}
|