Add like/ilike

This commit is contained in:
Adam C. Baker
2015-02-15 15:23:18 -08:00
committed by Joe Nelson
parent 5280b9fd6d
commit 5cc77709ba
2 changed files with 78 additions and 42 deletions
+44 -13
View File
@@ -1,27 +1,58 @@
{-# LANGUAGE QuasiQuotes, NoMonomorphismRestriction #-}
module Feature.QuerySpec where
import Test.Hspec
import Test.Hspec.Wai
import Test.Hspec.Wai.JSON
import Hasql as H
import Hasql.Postgres as H
import Control.Monad (void)
import Data.Text(Text)
import SpecHelper
testSet :: IO ()
testSet = do {
clearTable "items" >> clearTable "no_pk";
createItems 15;
pool :: H.Pool H.Postgres <- H.acquirePool pgSettings testPoolOpts;
void . liftIO $ H.session pool $ H.tx Nothing $ do
H.unitEx $
([H.stmt|insert into "1".no_pk (a, b) values (?,?), (?,?), (?,?), (?,?)|]
:: Text->Text->Text->Text->Text->Text->Text->Text-> H.Stmt H.Postgres)
"lick" "Fun"
"trick" "funky"
"barb" "foo"
"BARD" "FOOD"
}
spec :: Spec
spec = beforeAll (clearTable "items" >> createItems 15)
. afterAll_ (clearTable "items") . around withApp $ do
spec = beforeAll testSet . afterAll_ (clearTable "items") . around withApp $ do
describe "Querying a nonexistent table" $
it "causes a 404" $
get "/faketable" `shouldRespondWith` 404
describe "Filtering response" $
context "column equality" $
describe "Filtering response" $ do
it "matches with equality" $
get "/items?id=eq.5"
`shouldRespondWith` ResponseMatcher {
matchBody = Just "[{\"id\":5}]"
, matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-0/1"]
}
it "matches the predicate" $
get "/items?id=eq.5"
`shouldRespondWith` ResponseMatcher {
matchBody = Just "[{\"id\":5}]"
, matchStatus = 200
, matchHeaders = ["Content-Range" <:> "0-0/1"]
}
it "matches with like" $ do
get "/no_pk?a=like.*ick&order=asc.a" `shouldRespondWith` [json|[
{"a":"lick","b":"Fun"},{"a":"trick","b":"funky"}]|]
get "/no_pk?b=like.f*&order=asc.a" `shouldRespondWith` [json|[
{"a":"barb","b":"foo"},{"a":"trick","b":"funky"}]|]
get "/no_pk?a=like.*AR*&order=asc.a" `shouldRespondWith` [json|[
{"a":"BARD","b":"FOOD"}]|]
it "matches with ilike" $ do
get "/no_pk?b=ilike.fun*&order=asc.a" `shouldRespondWith` [json|[
{"a":"lick","b":"Fun"},{"a":"trick","b":"funky"}]|]
get "/no_pk?a=ilike.*AR*&order=asc.a" `shouldRespondWith` [json|[
{"a":"barb","b":"foo"},{"a":"BARD","b":"FOOD"}]|]
describe "ordering response" $ do
it "by a column asc" $
@@ -52,9 +83,9 @@ spec = beforeAll (clearTable "items" >> createItems 15)
}
it "Omits question mark when there are no params" $
get "/no_pk"
get "/simple_pk"
`shouldRespondWith` ResponseMatcher {
matchBody = Just "[]"
, matchStatus = 200
, matchHeaders = ["Content-Location" <:> "/no_pk"]
, matchHeaders = ["Content-Location" <:> "/simple_pk"]
}