Added tests for nullsfirst / nullslast

This commit is contained in:
João Cristóvão
2015-03-15 11:37:06 -07:00
committed by Joe Nelson
parent 97c9bfe93f
commit 01c67ab793
2 changed files with 51 additions and 2 deletions
+38 -1
View File
@@ -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
+13 -1
View File
@@ -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