From 1fdb700bc8e5a5d1a8e7d65baf643076c4388541 Mon Sep 17 00:00:00 2001 From: Ruslan Talpa Date: Mon, 26 Oct 2015 13:50:56 +0200 Subject: [PATCH] Fix a few tests --- src/PostgREST/Parsers.hs | 6 ++-- src/PostgREST/PgQuery.hs | 4 +-- test/Feature/InsertSpec.hs | 71 +++++++++++++++++++++++++++----------- test/Feature/QuerySpec.hs | 2 +- 4 files changed, 57 insertions(+), 26 deletions(-) diff --git a/src/PostgREST/Parsers.hs b/src/PostgREST/Parsers.hs index 6e2ffe4c0..753067567 100644 --- a/src/PostgREST/Parsers.hs +++ b/src/PostgREST/Parsers.hs @@ -4,9 +4,9 @@ module PostgREST.Parsers where import Control.Applicative hiding ((<$>)) -import Control.Monad (join) -import Data.List (delete, find) -import Data.Maybe +--import Control.Monad (join) +--import Data.List (delete, find) +--import Data.Maybe import Data.Monoid import Data.String.Conversions (cs) import Data.Text (Text) diff --git a/src/PostgREST/PgQuery.hs b/src/PostgREST/PgQuery.hs index 62cb78327..2b9cb71ca 100644 --- a/src/PostgREST/PgQuery.hs +++ b/src/PostgREST/PgQuery.hs @@ -275,7 +275,7 @@ asJsonSingleF :: T.Text --TODO! unsafe when the query actually returns multiple asJsonSingleF = "string_agg(row_to_json(t)::text, ',')::character varying " asCsvF :: T.Text -asCsvF = asCsvHeaderF <> " || '\r' || " <> asCsvBodyF +asCsvF = asCsvHeaderF <> " || '\n' || " <> asCsvBodyF asCsvHeaderF :: T.Text asCsvHeaderF = @@ -289,7 +289,7 @@ asCsvHeaderF = ")" asCsvBodyF :: T.Text -asCsvBodyF = "coalesce(string_agg(substring(t::text, 2, length(t::text) - 2), '\r'), '')" +asCsvBodyF = "coalesce(string_agg(substring(t::text, 2, length(t::text) - 2), '\n'), '')" fromF :: T.Text -> T.Text fromF limit = "FROM (SELECT * FROM source " <> limit <> ") t" diff --git a/test/Feature/InsertSpec.hs b/test/Feature/InsertSpec.hs index 5bf3db354..51db4fb56 100644 --- a/test/Feature/InsertSpec.hs +++ b/test/Feature/InsertSpec.hs @@ -92,31 +92,62 @@ spec = afterAll_ resetDb $ around withApp $ do context "jsonb" . after_ (clearTable "json") $ do it "serializes nested object" $ do let inserted = [json| { "data": { "foo":"bar" } } |] - p <- request methodPost "json" [("Prefer", "return=representation")] inserted - liftIO $ do - simpleBody p `shouldBe` inserted - simpleHeaders p `shouldSatisfy` matchHeader hLocation "/json\\?data=eq\\.%7B%22foo%22%3A%22bar%22%7D" - simpleStatus p `shouldBe` created201 + request methodPost "/json" + [("Prefer", "return=representation")] + inserted + `shouldRespondWith` ResponseMatcher { + matchBody = Just inserted + , matchStatus = 201 + , matchHeaders = ["Location" <:> [str|/json?data=eq.{"foo":"bar"}|]] + } + + -- TODO! the test above seems right, why was the one below working before and not now + -- p <- request methodPost "/json" [("Prefer", "return=representation")] inserted + -- liftIO $ do + -- simpleBody p `shouldBe` inserted + -- simpleHeaders p `shouldSatisfy` matchHeader hLocation "/json\\?data=eq\\.%7B%22foo%22%3A%22bar%22%7D" + -- simpleStatus p `shouldBe` created201 + it "serializes nested array" $ do let inserted = [json| { "data": [1,2,3] } |] - p <- request methodPost "json" [("Prefer", "return=representation")] inserted - liftIO $ do - simpleBody p `shouldBe` inserted - simpleHeaders p `shouldSatisfy` matchHeader hLocation "/json\\?data=eq\\.%5B1%2C2%2C3%5D" - simpleStatus p `shouldBe` created201 + request methodPost "/json" + [("Prefer", "return=representation")] + inserted + `shouldRespondWith` ResponseMatcher { + matchBody = Just inserted + , matchStatus = 201 + , matchHeaders = ["Location" <:> [str|/json?data=eq.[1,2,3]|]] + } + -- TODO! the test above seems right, why was the one below working before and not now + -- p <- request methodPost "/json" [("Prefer", "return=representation")] inserted + -- liftIO $ do + -- simpleBody p `shouldBe` inserted + -- simpleHeaders p `shouldSatisfy` matchHeader hLocation "/json\\?data=eq\\.%5B1%2C2%2C3%5D" + -- simpleStatus p `shouldBe` created201 describe "CSV insert" $ do after_ (clearTable "menagerie") . context "disparate csv types" $ it "succeeds with multipart response" $ do - p <- request methodPost "/menagerie" [("Content-Type", "text/csv")] - [str|integer,double,varchar,boolean,date,money,enum - |13,3.14159,testing!,false,1900-01-01,$3.99,foo - |12,0.1,a string,true,1929-10-01,12,bar - |] - liftIO $ do - simpleBody p `shouldBe` "Content-Type: application/json\nLocation: /menagerie?integer=eq.13\n\n\n--postgrest_boundary\nContent-Type: application/json\nLocation: /menagerie?integer=eq.12\n\n" - simpleStatus p `shouldBe` created201 + let inserted = [str|integer,double,varchar,boolean,date,money,enum + |13,3.14159,testing!,false,1900-01-01,$3.99,foo + |12,0.1,a string,true,1929-10-01,12,bar + |] + request methodPost "/menagerie" [("Content-Type", "text/csv"), ("Accept", "text/csv"), ("Prefer", "return=representation")] inserted + + `shouldRespondWith` ResponseMatcher { + matchBody = Just inserted + , matchStatus = 201 + , matchHeaders = ["Content-Type" <:> "text/csv"] + } + -- p <- request methodPost "/menagerie" [("Content-Type", "text/csv")] + -- [str|integer,double,varchar,boolean,date,money,enum + -- |13,3.14159,testing!,false,1900-01-01,$3.99,foo + -- |12,0.1,a string,true,1929-10-01,12,bar + -- |] + -- liftIO $ do + -- simpleBody p `shouldBe` "Content-Type: application/json\nLocation: /menagerie?integer=eq.13\n\n\n--postgrest_boundary\nContent-Type: application/json\nLocation: /menagerie?integer=eq.12\n\n" + -- simpleStatus p `shouldBe` created201 after_ (clearTable "no_pk") . context "requesting full representation" $ do it "returns full details of inserted record" $ @@ -124,7 +155,7 @@ spec = afterAll_ resetDb $ around withApp $ do [("Content-Type", "text/csv"), ("Accept", "text/csv"), ("Prefer", "return=representation")] "a,b\nbar,baz" `shouldRespondWith` ResponseMatcher { - matchBody = Just "a,b\rbar,baz" + matchBody = Just "a,b\nbar,baz" , matchStatus = 201 , matchHeaders = ["Content-Type" <:> "text/csv", "Location" <:> "/no_pk?a=eq.bar&b=eq.baz"] @@ -146,7 +177,7 @@ spec = afterAll_ resetDb $ around withApp $ do [("Content-Type", "text/csv"), ("Accept", "text/csv"), ("Prefer", "return=representation")] "a,b\nNULL,foo" `shouldRespondWith` ResponseMatcher { - matchBody = Just "a,b\r,foo" + matchBody = Just "a,b\n,foo" , matchStatus = 201 , matchHeaders = ["Content-Type" <:> "text/csv", "Location" <:> "/no_pk?a=is.null&b=eq.foo"] diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index 04bdfee88..c094934cc 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -272,7 +272,7 @@ spec = request methodGet "/simple_pk" (acceptHdrs "text/csv; version=1") "" `shouldRespondWith` ResponseMatcher { - matchBody = Just "k,extra\rxyyx,u\rxYYx,v" + matchBody = Just "k,extra\nxyyx,u\nxYYx,v" , matchStatus = 200 , matchHeaders = ["Content-Type" <:> "text/csv"] }