From 2662e24991914123bec592e85c62fd7ce6cbe53c Mon Sep 17 00:00:00 2001 From: Ruslan Talpa Date: Thu, 22 Oct 2015 16:17:23 +0300 Subject: [PATCH] a few more tests fixed --- src/PostgREST/App.hs | 17 +++++++++-------- src/PostgREST/PgQuery.hs | 3 ++- test/Feature/InsertSpec.hs | 22 +++++++++++++++++----- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index dca5a2262..111ad490a 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -205,7 +205,7 @@ app dbstructure conf authenticator reqBody dbrole req = locationH = fromMaybe "" locationRaw return $ responseLBS status201 [ - jsonH, + contentTypeH, (hLocation, "/" <> cs table <> "?" <> cs locationH) ] $ if echoRequested then body else "" @@ -493,13 +493,14 @@ parsePostRequest httpRequest reqBody = rows <- (map V.toList . V.toList) <$> CSV.decode CSV.NoHeader reqBody if null rows then Left "CSV requires header" else Right (head rows, (map $ map $ parseCsvCell . cs) (tail rows)) - else jsn >>= \val -> convertJson val - jsn = eitherDecode reqBody - returnSingle = first cs $ jsn >>= (\v-> - case v of - Object _ -> Right True - _ -> Right False - ) + else eitherDecode reqBody >>= \val -> convertJson val + -- jsn = eitherDecode reqBody + -- returnSingle = first cs $ jsn >>= (\v-> + -- case v of + -- Object _ -> Right True + -- _ -> Right False + -- ) + returnSingle = (==1) . length . snd <$> parsed hdrs = requestHeaders httpRequest lookupHeader = flip lookup hdrs rootTableName = cs $ head $ pathInfo httpRequest -- TODO unsafe head diff --git a/src/PostgREST/PgQuery.hs b/src/PostgREST/PgQuery.hs index 4997786e7..87cd58a85 100644 --- a/src/PostgREST/PgQuery.hs +++ b/src/PostgREST/PgQuery.hs @@ -378,7 +378,8 @@ locationF :: [T.Text] -> T.Text locationF pKeys = "(" <> " WITH s AS (SELECT row_to_json(source) as r from source limit 1)" <> - " SELECT string_agg(json_data.key || '=eq.' || json_data.value, '&')" <> +-- " SELECT string_agg(json_data.key || '=eq.' || json_data.value, '&')" <> + " SELECT string_agg(json_data.key || '=' || coalesce( 'eq.' || json_data.value, 'is.null'), '&')" <> " FROM s, json_each_text(s.r) AS json_data" <> ( if null pKeys diff --git a/test/Feature/InsertSpec.hs b/test/Feature/InsertSpec.hs index 7d19e5683..e4f042e04 100644 --- a/test/Feature/InsertSpec.hs +++ b/test/Feature/InsertSpec.hs @@ -1,6 +1,6 @@ module Feature.InsertSpec where -import Test.Hspec +import Test.Hspec hiding (pendingWith) import Test.Hspec.Wai import Test.Hspec.Wai.JSON import Network.Wai.Test (SResponse(simpleBody,simpleHeaders,simpleStatus)) @@ -130,17 +130,29 @@ spec = afterAll_ resetDb $ around withApp $ do "Location" <:> "/no_pk?a=eq.bar&b=eq.baz"] } - it "can post nulls" $ + -- it "can post nulls (old way)" $ do + -- pendingWith "changed the response when in csv mode" + -- request methodPost "/no_pk" + -- [("Content-Type", "text/csv"), ("Prefer", "return=representation")] + -- "a,b\nNULL,foo" + -- `shouldRespondWith` ResponseMatcher { + -- matchBody = Just [json| { "a":null, "b":"foo" } |] + -- , matchStatus = 201 + -- , matchHeaders = ["Content-Type" <:> "application/json", + -- "Location" <:> "/no_pk?a=is.null&b=eq.foo"] + -- } + it "can post nulls" $ do request methodPost "/no_pk" - [("Content-Type", "text/csv"), ("Prefer", "return=representation")] + [("Content-Type", "text/csv"), ("Accept", "text/csv"), ("Prefer", "return=representation")] "a,b\nNULL,foo" `shouldRespondWith` ResponseMatcher { - matchBody = Just [json| { "a":null, "b":"foo" } |] + matchBody = Just "a,b\n,foo" , matchStatus = 201 - , matchHeaders = ["Content-Type" <:> "application/json", + , matchHeaders = ["Content-Type" <:> "text/csv", "Location" <:> "/no_pk?a=is.null&b=eq.foo"] } + after_ (clearTable "no_pk") . context "with wrong number of columns" $ do it "fails for too few" $ do p <- request methodPost "/no_pk" [("Content-Type", "text/csv")] "a,b\nfoo,bar\nbaz"