fix: inconsistent Preference-Applied

* Don't apply `tx=commit` if the transaction doesn't commit
* Apply `count=exact`
* Also simplifies the Preference-Applied logic, removing the need for
  some functions.
This commit is contained in:
steve-chavez
2023-10-04 00:07:09 -03:00
committed by Steve Chavez
parent d6cd5d0fb4
commit 6475f254f7
11 changed files with 81 additions and 160 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ spec =
`shouldRespondWith` [json|[{"id":2}]|]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "*/1"
, "Preference-Applied" <:> "return=representation"]
, "Preference-Applied" <:> "return=representation, count=exact"]
}
it "ignores ?select= when return not set or return=minimal" $ do
+1 -1
View File
@@ -102,7 +102,7 @@ spec actualPgVersion = do
, matchHeaders = [ matchContentTypeJson
, matchHeaderAbsent hLocation
, "Content-Range" <:> "*/1"
, "Preference-Applied" <:> "return=representation"]
, "Preference-Applied" <:> "return=representation, count=exact"]
}
it "can rename and cast the selected columns" $
+8 -16
View File
@@ -72,8 +72,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 4 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular
, "Preference-Applied" <:> "tx=commit" ]
, matchHeaders = [ matchContentTypeSingular ]
}
-- the rows should not be updated, either
@@ -88,8 +87,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 4 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular
, "Preference-Applied" <:> "tx=commit" ]
, matchHeaders = [ matchContentTypeSingular ]
}
-- the rows should not be updated, either
@@ -145,8 +143,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 2 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular
, "Preference-Applied" <:> "tx=commit" ]
, matchHeaders = [ matchContentTypeSingular ]
}
-- the rows should not exist, either
@@ -161,8 +158,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 2 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular
, "Preference-Applied" <:> "tx=commit" ]
, matchHeaders = [ matchContentTypeSingular ]
}
-- the rows should not exist, either
@@ -177,8 +173,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 2 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular
, "Preference-Applied" <:> "tx=commit" ]
, matchHeaders = [ matchContentTypeSingular ]
}
-- the rows should not exist, either
@@ -226,8 +221,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 5 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular
, "Preference-Applied" <:> "tx=commit" ]
, matchHeaders = [ matchContentTypeSingular ]
}
-- the rows should still exist
@@ -244,8 +238,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 5 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular
, "Preference-Applied" <:> "tx=commit" ]
, matchHeaders = [ matchContentTypeSingular ]
}
-- the rows should still exist
@@ -318,8 +311,7 @@ spec =
`shouldRespondWith`
[json|{"details":"The result contains 2 rows","message":"JSON object requested, multiple (or no) rows returned","code":"PGRST116","hint":null}|]
{ matchStatus = 406
, matchHeaders = [ matchContentTypeSingular
, "Preference-Applied" <:> "tx=commit" ]
, matchHeaders = [ matchContentTypeSingular]
}
-- should rollback function
+2 -2
View File
@@ -37,8 +37,8 @@ preferCommit = [("Prefer", "return=representation"), ("Prefer", "tx=commit")]
preferRollback = [("Prefer", "return=representation"), ("Prefer", "tx=rollback")]
withoutPreferenceApplied = []
withPreferenceCommitApplied = [ "Preference-Applied" <:> "tx=commit" ]
withPreferenceRollbackApplied = [ "Preference-Applied" <:> "tx=rollback" ]
withPreferenceCommitApplied = [ matchHeaderValuePresent "Preference-Applied" "tx=commit" ]
withPreferenceRollbackApplied = [ matchHeaderValuePresent "Preference-Applied" "tx=rollback" ]
shouldRespondToReads reqHeaders respHeaders = do
it "responds to GET" $ do
+6
View File
@@ -46,6 +46,12 @@ matchCTArrayStrip = "Content-Type" <:> "application/vnd.pgrst.array+json;nulls=s
matchCTSingularStrip :: MatchHeader
matchCTSingularStrip = "Content-Type" <:> "application/vnd.pgrst.object+json;nulls=stripped; charset=utf-8"
matchHeaderValuePresent :: HeaderName -> BS.ByteString -> MatchHeader
matchHeaderValuePresent name val = MatchHeader $ \headers _ ->
case lookup name headers of
Just hdr -> if val `BS.isInfixOf` hdr then Nothing else Just $ "missing header value: " <> toS val <> "\n"
Nothing -> Just $ "missing header: " <> toS (original name) <> "\n"
matchHeaderAbsent :: HeaderName -> MatchHeader
matchHeaderAbsent name = MatchHeader $ \headers _body ->
case lookup name headers of