Skip to content

Commit

Permalink
Make transaction-rollback=true the default for test-suite (#1663)
Browse files Browse the repository at this point in the history
Change test-suite to use db-tx-rollback-all = true by default
  • Loading branch information
wolfgangwalther committed Dec 3, 2020
1 parent 10a70d4 commit 609c9ae
Show file tree
Hide file tree
Showing 21 changed files with 706 additions and 493 deletions.
45 changes: 30 additions & 15 deletions test/Feature/AndOrParamsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,24 @@ spec actualPgVersion =
{"id": 1, "child_entities": [ { "id": 1 }, { "id": 2 } ] }, { "id": 2, "child_entities": []},
{"id": 3, "child_entities": []}, {"id": 4, "child_entities": []}
]|] { matchHeaders = [matchContentTypeJson] }

it "can do logic on the third level" $
get "/entities?child_entities.grandchild_entities.or=(id.eq.1,id.eq.2)&select=id,child_entities(id,grandchild_entities(id))" `shouldRespondWith`
[json|[
{"id": 1, "child_entities": [ { "id": 1, "grandchild_entities": [ { "id": 1 }, { "id": 2 } ]}, { "id": 2, "grandchild_entities": []}]},
{"id": 2, "child_entities": [ { "id": 3, "grandchild_entities": []} ]},
{"id": 3, "child_entities": []}, {"id": 4, "child_entities": []}
]|] { matchHeaders = [matchContentTypeJson] }
get "/entities?child_entities.grandchild_entities.or=(id.eq.1,id.eq.2)&select=id,child_entities(id,grandchild_entities(id))"
`shouldRespondWith`
[json|[
{"id": 1, "child_entities": [
{ "id": 1, "grandchild_entities": [ { "id": 1 }, { "id": 2 } ]},
{ "id": 2, "grandchild_entities": []},
{ "id": 4, "grandchild_entities": []},
{ "id": 5, "grandchild_entities": []}
]},
{"id": 2, "child_entities": [
{ "id": 3, "grandchild_entities": []},
{ "id": 6, "grandchild_entities": []}
]},
{"id": 3, "child_entities": []},
{"id": 4, "child_entities": []}
]|]

context "and/or params combined" $ do
it "can be nested inside the same expression" $
Expand Down Expand Up @@ -210,12 +221,15 @@ spec actualPgVersion =
context "used with POST" $
it "includes related data with filters" $
request methodPost "/child_entities?select=id,entities(id)&entities.or=(id.eq.2,id.eq.3)&entities.order=id"
[("Prefer", "return=representation")]
[json|[{"id":4,"name":"entity 4","parent_id":1},
{"id":5,"name":"entity 5","parent_id":2},
{"id":6,"name":"entity 6","parent_id":3}]|] `shouldRespondWith`
[json|[{"id": 4, "entities":null}, {"id": 5, "entities": {"id": 2}}, {"id": 6, "entities": {"id": 3}}]|]
{ matchStatus = 201, matchHeaders = [matchContentTypeJson] }
[("Prefer", "return=representation")]
[json|[
{"id":7,"name":"entity 4","parent_id":1},
{"id":8,"name":"entity 5","parent_id":2},
{"id":9,"name":"entity 6","parent_id":3}
]|]
`shouldRespondWith`
[json|[{"id": 7, "entities":null}, {"id": 8, "entities": {"id": 2}}, {"id": 9, "entities": {"id": 3}}]|]
{ matchStatus = 201 }

context "used with PATCH" $
it "succeeds when using and/or params" $
Expand All @@ -228,9 +242,10 @@ spec actualPgVersion =
context "used with DELETE" $
it "succeeds when using and/or params" $
request methodDelete "/grandchild_entities?or=(id.eq.1,id.eq.2)&select=id,name"
[("Prefer", "return=representation")] "" `shouldRespondWith`
[json|[{ "id": 1, "name" : "updated grandchild entity"},{ "id": 2, "name" : "updated grandchild entity"}]|]
{ matchHeaders = [matchContentTypeJson] }
[("Prefer", "return=representation")]
""
`shouldRespondWith`
[json|[{ "id": 1, "name" : "grandchild entity 1" },{ "id": 2, "name" : "grandchild entity 2" }]|]

it "can query columns that begin with and/or reserved words" $
get "/grandchild_entities?or=(and_starting_col.eq.smth, or_starting_col.eq.smth)" `shouldRespondWith` 200
Expand Down
8 changes: 0 additions & 8 deletions test/Feature/DeleteSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ spec =
, matchHeaders = ["Content-Range" <:> "*/*"]
}

it "actually clears items ouf the db" $ do
_ <- request methodDelete "/items?id=lt.15" [] ""
get "/items"
`shouldRespondWith` [json|[{"id":15}]|]
{ matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-0/*"]
}

context "known route, no records matched" $
it "includes [] body if return=rep" $
request methodDelete "/items?id=eq.101"
Expand Down
6 changes: 3 additions & 3 deletions test/Feature/EmbedDisambiguationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,9 @@ spec =
{ matchHeaders = [matchContentTypeJson] }

it "can request two parents with fks" $
get "/articleStars?select=createdAt,article(owner),user(name)&limit=1" `shouldRespondWith`
[json|[{"createdAt":"2015-12-08T04:22:57.472738","article":{"owner": "postgrest_test_authenticator"},"user":{"name": "Angela Martin"}}]|]
{ matchHeaders = [matchContentTypeJson] }
get "/articleStars?select=createdAt,article(id),user(name)&limit=1"
`shouldRespondWith`
[json|[{"createdAt":"2015-12-08T04:22:57.472738","article":{"id": 1},"user":{"name": "Angela Martin"}}]|]

it "can specify a view!fk" $
get "/message?select=id,body,sender:person_detail!message_sender_fkey(name,sent),recipient:person_detail!message_recipient_fkey(name,received)&id=lt.4" `shouldRespondWith`
Expand Down
89 changes: 53 additions & 36 deletions test/Feature/InsertSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,21 @@ spec actualPgVersion = do

context "with no pk supplied" $ do
context "into a table with auto-incrementing pk" $
it "succeeds with 201 and link" $ do
p <- post "/auto_incrementing_pk" [json| { "non_nullable_string":"not null"} |]
liftIO $ do
simpleBody p `shouldBe` ""
simpleHeaders p `shouldSatisfy` matchHeader hLocation "/auto_incrementing_pk\\?id=eq\\.[0-9]+"
simpleStatus p `shouldBe` created201
let Just location = lookup hLocation $ simpleHeaders p
r <- get location
let [record] = fromJust (JSON.decode $ simpleBody r :: Maybe [IncPK])
liftIO $ do
incStr record `shouldBe` "not null"
incNullableStr record `shouldBe` Nothing
it "succeeds with 201 and location header" $ do
-- reset pk sequence first to make test repeatable
request methodPost "/rpc/reset_sequence"
[("Prefer", "tx=commit")]
[json|{"name": "auto_incrementing_pk_id_seq", "value": 2}|]
`shouldRespondWith`
[json|""|]

post "/auto_incrementing_pk"
[json| { "non_nullable_string":"not null"} |]
`shouldRespondWith`
""
{ matchStatus = 201
, matchHeaders = [ "Location" <:> "/auto_incrementing_pk?id=eq.2" ]
}

context "into a table with simple pk" $
it "fails with 400 and error" $
Expand Down Expand Up @@ -197,21 +200,14 @@ spec actualPgVersion = do

context "with compound pk supplied" $
it "builds response location header appropriately" $ do
let inserted = [json| { "k1":12, "k2":"Rock & R+ll" } |]
expectedObj = CompoundPK 12 "Rock & R+ll" Nothing
expectedLoc = "/compound_pk?k1=eq.12&k2=eq.Rock%20%26%20R%2Bll"
p <- request methodPost "/compound_pk"
[("Prefer", "return=representation")]
inserted
liftIO $ do
JSON.decode (simpleBody p) `shouldBe` Just [expectedObj]
simpleStatus p `shouldBe` created201
lookup hLocation (simpleHeaders p) `shouldBe` Just expectedLoc

r <- get expectedLoc
liftIO $ do
JSON.decode (simpleBody r) `shouldBe` Just [expectedObj]
simpleStatus r `shouldBe` ok200
request methodPost "/compound_pk"
[("Prefer", "return=representation")]
[json| { "k1":12, "k2":"Rock & R+ll" } |]
`shouldRespondWith`
[json|[ { "k1":12, "k2":"Rock & R+ll", "extra": null } ]|]
{ matchStatus = 201
, matchHeaders = [ "Location" <:> "/compound_pk?k1=eq.12&k2=eq.Rock%20%26%20R%2Bll" ]
}

context "with bulk insert" $
it "returns 201 but no location header" $ do
Expand Down Expand Up @@ -248,9 +244,9 @@ spec actualPgVersion = do
context "attempting to insert a row with the same primary key" $
it "fails returning a 409 Conflict" $
post "/simple_pk"
[json| { "k":"k1", "extra":"e1" } |]
[json| { "k":"xyyx", "extra":"e1" } |]
`shouldRespondWith`
[json|{"hint":null,"details":"Key (k)=(k1) already exists.","code":"23505","message":"duplicate key value violates unique constraint \"simple_pk_pkey\""}|]
[json|{"hint":null,"details":"Key (k)=(xyyx) already exists.","code":"23505","message":"duplicate key value violates unique constraint \"simple_pk_pkey\""}|]
{ matchStatus = 409 }

context "attempting to insert a row with conflicting unique constraint" $
Expand Down Expand Up @@ -293,16 +289,30 @@ spec actualPgVersion = do
, matchHeaders = []
}

it "successfully inserts a row with all-default columns with prefer=rep" $
it "successfully inserts a row with all-default columns with prefer=rep" $ do
-- reset pk sequence first to make test repeatable
request methodPost "/rpc/reset_sequence"
[("Prefer", "tx=commit")]
[json|{"name": "items_id_seq", "value": 20}|]
`shouldRespondWith`
[json|""|]

request methodPost "/items" [("Prefer", "return=representation")] "{}"
`shouldRespondWith` [json|[{ id: 20 }]|]
{ matchStatus = 201,
matchHeaders = []
}

it "successfully inserts a row with all-default columns with prefer=rep and &select=" $
it "successfully inserts a row with all-default columns with prefer=rep and &select=" $ do
-- reset pk sequence first to make test repeatable
request methodPost "/rpc/reset_sequence"
[("Prefer", "tx=commit")]
[json|{"name": "items_id_seq", "value": 20}|]
`shouldRespondWith`
[json|""|]

request methodPost "/items?select=id" [("Prefer", "return=representation")] "{}"
`shouldRespondWith` [json|[{ id: 21 }]|]
`shouldRespondWith` [json|[{ id: 20 }]|]
{ matchStatus = 201,
matchHeaders = []
}
Expand Down Expand Up @@ -411,15 +421,22 @@ spec actualPgVersion = do
it "succeeds and returns usable location header" $ do
let payload = [json| { "k":"圍棋", "extra":"¥" } |]
p <- request methodPost "/simple_pk?select=extra,k"
[("Prefer", "return=representation")]
payload
[("Prefer", "tx=commit"), ("Prefer", "return=representation")]
payload
liftIO $ do
simpleBody p `shouldBe` "["<>payload<>"]"
simpleStatus p `shouldBe` created201

let Just location = lookup hLocation $ simpleHeaders p
r <- get (location <> "&select=extra,k")
liftIO $ simpleBody r `shouldBe` "["<>payload<>"]"
get location
`shouldRespondWith`
[json|[ { "k":"圍棋", "extra":"¥" } ]|]

request methodDelete location
[("Prefer", "tx=commit")]
""
`shouldRespondWith`
204

describe "Row level permission" $
it "set user_id when inserting rows" $ do
Expand Down
11 changes: 5 additions & 6 deletions test/Feature/JsonOperatorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,11 @@ spec actualPgVersion = describe "json and jsonb operators" $ do

context "Patching record, in a nonempty table" $
it "can set a json column to escaped value" $ do
_ <- post "/json_table" [json| { data: {"escaped":"bar"} } |]
request methodPatch "/json_table?data->>escaped=eq.bar"
[("Prefer", "return=representation")]
[json| { "data": { "escaped":" \"bar" } } |]
`shouldRespondWith` [json| [{ "data": { "escaped":" \"bar" } }] |]
{ matchStatus = 200 , matchHeaders = [] }
request methodPatch "/json_table?data->>id=eq.3"
[("Prefer", "return=representation")]
[json| { "data": { "id":" \"escaped" } } |]
`shouldRespondWith`
[json| [{ "data": { "id":" \"escaped" } }] |]

when (actualPgVersion >= pgVersion95) $
context "json array negative index" $ do
Expand Down
64 changes: 31 additions & 33 deletions test/Feature/MultipleSchemaSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,33 +77,37 @@ spec actualPgVersion =

context "Inserting tables on different schemas" $ do
it "succeeds inserting on default schema and returning it" $
request methodPost "/children" [("Prefer", "return=representation")] [json|{"name": "child v1-1", "parent_id": 1}|]
`shouldRespondWith`
[json|[{"id":1, "name": "child v1-1", "parent_id": 1}]|]
{
matchStatus = 201
, matchHeaders = [matchContentTypeJson, "Content-Profile" <:> "v1"]
}
request methodPost "/children"
[("Prefer", "return=representation")]
[json|{"id": 0, "name": "child v1-1", "parent_id": 1}|]
`shouldRespondWith`
[json|[{"id": 0, "name": "child v1-1", "parent_id": 1}]|]
{
matchStatus = 201
, matchHeaders = ["Content-Profile" <:> "v1"]
}

it "succeeds inserting on the v1 schema and returning its parent" $
request methodPost "/children?select=id,parent(*)" [("Prefer", "return=representation"), ("Content-Profile", "v1")]
[json|{"name": "child v1-2", "parent_id": 2}|]
request methodPost "/children?select=id,parent(*)"
[("Prefer", "return=representation"), ("Content-Profile", "v1")]
[json|{"id": 0, "name": "child v1-2", "parent_id": 2}|]
`shouldRespondWith`
[json|[{"id":2, "parent": {"id": 2, "name": "parent v1-2"}}]|]
{
matchStatus = 201
, matchHeaders = [matchContentTypeJson, "Content-Profile" <:> "v1"]
}
[json|[{"id": 0, "parent": {"id": 2, "name": "parent v1-2"}}]|]
{
matchStatus = 201
, matchHeaders = ["Content-Profile" <:> "v1"]
}

it "succeeds inserting on the v2 schema and returning its parent" $
request methodPost "/children?select=id,parent(*)" [("Prefer", "return=representation"), ("Content-Profile", "v2")]
[json|{"name": "child v2-3", "parent_id": 3}|]
request methodPost "/children?select=id,parent(*)"
[("Prefer", "return=representation"), ("Content-Profile", "v2")]
[json|{"id": 0, "name": "child v2-3", "parent_id": 3}|]
`shouldRespondWith`
[json|[{"id":1, "parent": {"id": 3, "name": "parent v2-3"}}]|]
{
matchStatus = 201
, matchHeaders = [matchContentTypeJson, "Content-Profile" <:> "v2"]
}
[json|[{"id": 0, "parent": {"id": 3, "name": "parent v2-3"}}]|]
{
matchStatus = 201
, matchHeaders = ["Content-Profile" <:> "v2"]
}

it "fails when inserting on an unknown schema" $
request methodPost "/children" [("Content-Profile", "unknown")]
Expand Down Expand Up @@ -180,18 +184,12 @@ spec actualPgVersion =
}

it "succeeds on deleting on the v2 schema" $ do
request methodDelete "/children?id=eq.1" [("Content-Profile", "v2"), ("Prefer", "return=representation")] ""
`shouldRespondWith` [json|[{"id": 1, "name": "child v2-1 updated", "parent_id": 3}]|]
{
matchStatus = 200
, matchHeaders = [matchContentTypeJson, "Content-Profile" <:> "v2"]
}
request methodGet "/children?id=eq.1" [("Accept-Profile", "v2")] ""
`shouldRespondWith` "[]"
{
matchStatus = 200
, matchHeaders = [matchContentTypeJson, "Content-Profile" <:> "v2"]
}
request methodDelete "/children?id=eq.1"
[("Content-Profile", "v2"), ("Prefer", "return=representation")]
""
`shouldRespondWith`
[json|[{"id": 1, "name": "child v2-3", "parent_id": 3}]|]
{ matchHeaders = ["Content-Profile" <:> "v2"] }

when (actualPgVersion >= pgVersion96) $
it "succeeds on PUT on the v2 schema" $
Expand Down
Loading

0 comments on commit 609c9ae

Please sign in to comment.