refactor: simplify tests where mutations need to be verified in the db
This commit is contained in:
+34
-1
@@ -18,6 +18,7 @@ import Text.Regex.TDFA ((=~))
|
||||
import Network.HTTP.Types
|
||||
import Test.Hspec
|
||||
import Test.Hspec.Wai
|
||||
import Test.Hspec.Wai.JSON
|
||||
import Text.Heredoc
|
||||
|
||||
import PostgREST.Config (AppConfig (..),
|
||||
@@ -27,7 +28,7 @@ import PostgREST.Config (AppConfig (..),
|
||||
parseSecret)
|
||||
import PostgREST.DbStructure.Identifiers (QualifiedIdentifier (..))
|
||||
import PostgREST.MediaType (MediaType (..))
|
||||
import Protolude hiding (toS)
|
||||
import Protolude hiding (get, toS)
|
||||
import Protolude.Conv (toS)
|
||||
|
||||
matchContentTypeJson :: MatchHeader
|
||||
@@ -243,3 +244,35 @@ isErrorFormat s =
|
||||
obj = decode s :: Maybe (M.Map Text Value)
|
||||
keys = maybe S.empty M.keysSet obj
|
||||
validKeys = S.fromList ["message", "details", "hint", "code"]
|
||||
|
||||
-- | Follows these steps to verify if the table data changed in the db:
|
||||
-- * Verifies the table data in the db before the change
|
||||
-- * Does the mutation
|
||||
-- * Verifies that the table data changed in the db
|
||||
-- * Resets the table with the original data
|
||||
shouldMutateInto :: MutationCheck -> ResponseMatcher -> WaiExpectation ()
|
||||
shouldMutateInto (MutationCheck (BaseTable tblName tblOrd dataBefore) mutation) dataAfter = do
|
||||
get ("/" <> tblName) `shouldRespondWith` [json|#{dataBefore}|]
|
||||
mutation
|
||||
get ("/" <> tblName <> "?order=" <> tblOrd) `shouldRespondWith` dataAfter
|
||||
request methodPost "/rpc/reset_table"
|
||||
[("Prefer", "tx=commit")]
|
||||
[json| {"tbl_name": #{decodeUtf8 tblName}, "tbl_data": #{dataBefore}} |]
|
||||
`shouldRespondWith` 204
|
||||
|
||||
-- | How the base table data will change using the requested mutation
|
||||
mutatesWith :: BaseTable -> WaiExpectation () -> MutationCheck
|
||||
mutatesWith = MutationCheck
|
||||
|
||||
-- | The original table data before it is modified.
|
||||
-- The column order is needed for an accurate comparison after the mutation
|
||||
baseTable :: ByteString -> ByteString -> Value -> BaseTable
|
||||
baseTable = BaseTable
|
||||
|
||||
-- | The mutation (update/delete) that will be applied to the base table
|
||||
requestMutation :: Method -> ByteString -> BL.ByteString -> WaiExpectation ()
|
||||
requestMutation method path body =
|
||||
request method path [("Prefer", "tx=commit")] body `shouldRespondWith` 204
|
||||
|
||||
data BaseTable = BaseTable ByteString ByteString Value
|
||||
data MutationCheck = MutationCheck BaseTable (WaiExpectation ())
|
||||
|
||||
Reference in New Issue
Block a user