diff --git a/test/Feature/QuerySpec.hs b/test/Feature/QuerySpec.hs index 0bfc1dcd7..1450f3d22 100644 --- a/test/Feature/QuerySpec.hs +++ b/test/Feature/QuerySpec.hs @@ -9,6 +9,7 @@ import Control.Monad (void) import Data.Text(Text) import SpecHelper +import Data.Monoid testSet :: IO () testSet = do @@ -24,7 +25,11 @@ testSet = do insertNoPk = [H.stmt|insert into "1".no_pk (a, b) values (?,?)|] spec :: Spec -spec = beforeAll testSet . afterAll_ (clearTable "items") . around withApp $ do +spec = do + beforeAll (clearTable "items" >> createItems 15) + . beforeAll (clearTable "no_pk" >> createNulls 2) + . afterAll_ (clearTable "items" >> clearTable "no_pk") + . around withApp $ do describe "Querying a nonexistent table" $ it "causes a 404" $ get "/faketable" `shouldRespondWith` 404 @@ -76,6 +81,38 @@ spec = beforeAll testSet . afterAll_ (clearTable "items") . around withApp $ do , matchHeaders = ["Content-Range" <:> "0-1/2"] } + it "by a column asc with nulls last" $ + get "/no_pk?order=a.asc.nullslast" + `shouldRespondWith` ResponseMatcher { + matchBody = Just $ "[{\"a\":\"1\",\"b\":\"0\"}" + <> ",{\"a\":\"2\",\"b\":\"0\"}" + <> ",{\"a\":null,\"b\":null}]" + + , matchStatus = 200 + , matchHeaders = ["Content-Range" <:> "0-2/3"] + } + + it "by a column desc with nulls first" $ + get "/no_pk?order=a.desc.nullsfirst" + `shouldRespondWith` ResponseMatcher { + matchBody = Just $ "[{\"a\":null,\"b\":null}" + <> ",{\"a\":\"2\",\"b\":\"0\"}" + <> ",{\"a\":\"1\",\"b\":\"0\"}]" + , matchStatus = 200 + , matchHeaders = ["Content-Range" <:> "0-2/3"] + } + + it "by a column desc with nulls last" $ + get "/no_pk?order=a.desc.nullslast" + `shouldRespondWith` ResponseMatcher { + matchBody = Just $ "[{\"a\":\"2\",\"b\":\"0\"}" + <> ",{\"a\":\"1\",\"b\":\"0\"}" + <> ",{\"a\":null,\"b\":null}]" + + , matchStatus = 200 + , matchHeaders = ["Content-Range" <:> "0-2/3"] + } + it "without other constraints" $ get "/items?order=asc.id" `shouldRespondWith` 200 diff --git a/test/SpecHelper.hs b/test/SpecHelper.hs index 29c08d417..65f884690 100644 --- a/test/SpecHelper.hs +++ b/test/SpecHelper.hs @@ -109,9 +109,21 @@ createItems n = do <- H.acquirePool pgSettings testPoolOpts void . liftIO $ H.session pool $ H.tx Nothing txn where - txn = sequence_ $ map H.unitEx stmts + txn = mapM_ H.unitEx stmts stmts = map [H.stmt|insert into "1".items (id) values (?)|] [1..n] +createNulls :: Int -> IO () +createNulls n = do + pool :: H.Pool H.Postgres + <- H.acquirePool pgSettings testPoolOpts + void . liftIO $ H.session pool $ H.tx Nothing txn + where + txn = mapM_ H.unitEx (stmt':stmts) + stmt' = [H.stmt|insert into "1".no_pk (a,b) values (null,null)|] + stmts = map [H.stmt|insert into "1".no_pk (a,b) values (?,0)|] [1..n] + + + -- for hspec-wai pending_ :: WaiSession () pending_ = liftIO Test.Hspec.pending