Skip to content

Commit 22687cd

Browse files
Dmitrii Kovanikovvrom911
andcommitted
[#2] Add query tests for named parameters (#7)
* [#2] Add query tests for named parameters Resolves #2 * Don't specify postgresql version * Change user from root to postgres * Fix DB name * Update test/Spec.hs Co-Authored-By: Veronika Romashkina <[email protected]>
1 parent 174b5ff commit 22687cd

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-2
lines changed

.travis.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ cache:
1212
- "$HOME/.stack"
1313
- "$TRAVIS_BUILD_DIR/.stack-work"
1414

15+
# add PostgreSQL with version 10
16+
services:
17+
- postgresql
18+
1519
matrix:
1620
include:
1721
- ghc: 8.6.5
@@ -35,6 +39,9 @@ install:
3539
stack build --system-ghc --test --bench --no-run-tests --no-run-benchmarks --ghc-options=-Werror
3640
fi
3741
42+
before_script:
43+
- psql -c "CREATE DATABASE pg_named;" -U postgres
44+
3845
script:
3946
- |
4047
if [ -z "$STACK_YAML" ]; then

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@
77
[![Build status](https://img.shields.io/travis/Holmusk/postgresql-simple-named.svg?logo=travis)](https://travis-ci.org/Holmusk/postgresql-simple-named)
88

99
Implementation of named parameters for `postgresql-simple` library
10+
11+
## How to build
12+
13+
Build the library with either `cabal new-build` or `stack build`.
14+
15+
## How to test locally
16+
17+
* Run DB in a Docker in a separate terminal window using command:
18+
```
19+
docker run -p 5432\:5432 -e POSTGRES_USER=postgres -e POSTGRES_DB=pg_named postgres\:10.5-alpine
20+
```
21+
* Run tests using `cabal new-test` or `stack test`

postgresql-simple-named.cabal

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,12 @@ test-suite postgresql-simple-named-test
7878
hs-source-dirs: test
7979
main-is: Spec.hs
8080

81-
build-depends: postgresql-simple-named
81+
build-depends: bytestring
82+
, hspec >= 2.6 && < 2.8
83+
, postgresql-simple-named
84+
, postgresql-simple
85+
, resource-pool
86+
, transformers
8287

8388
ghc-options: -threaded -rtsopts -with-rtsopts=-N
8489

test/Spec.hs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,63 @@
1+
{-# LANGUAGE DeriveAnyClass #-}
2+
{-# LANGUAGE DerivingStrategies #-}
3+
14
module Main (main) where
25

6+
import Control.Monad.Trans.Except (ExceptT, runExceptT)
7+
import Data.Bifunctor (second)
8+
import Data.ByteString (ByteString)
9+
import GHC.Generics (Generic)
10+
import System.IO (hSetEncoding, stderr, stdout, utf8)
11+
import Test.Hspec (Spec, describe, hspec, it, shouldReturn)
12+
13+
import PgNamed (PgNamedError (..), queryNamed, (=?))
14+
15+
import qualified Data.Pool as Pool
16+
import qualified Database.PostgreSQL.Simple as Sql
17+
18+
19+
connectionSettings :: ByteString
20+
connectionSettings = "host=localhost port=5432 user=postgres dbname=pg_named"
21+
322
main :: IO ()
4-
main = putStrLn ("Test suite not yet implemented" :: String)
23+
main = do
24+
hSetEncoding stdout utf8
25+
hSetEncoding stderr utf8
26+
dbPool <- Pool.createPool (Sql.connectPostgreSQL connectionSettings) Sql.close 10 5 10
27+
hspec $ unitTests dbPool
28+
29+
unitTests :: Pool.Pool Sql.Connection -> Spec
30+
unitTests dbPool = describe "Testing: postgresql-simple-named" $ do
31+
it "returns error when named parameter is not specified" $
32+
missingNamedParam `shouldReturn` Left (PgNamedParam "bar")
33+
it "no named parameters in a query" $
34+
noNamedParams `shouldReturn` Left (PgNoNames "SELECT 42")
35+
it "empty name in a query with named parameters" $
36+
emptyName `shouldReturn` Left (PgEmptyName "SELECT ?foo, ?")
37+
it "named parameters are parsed and passed correctly" $
38+
queryTestValue `shouldReturn` Right (TestValue 42 42 "baz")
39+
where
40+
missingNamedParam :: IO (Either PgNamedError TestValue)
41+
missingNamedParam = runNamedQuery $ queryNamed dbPool "SELECT ?foo, ?bar" ["foo" =? True]
42+
43+
noNamedParams :: IO (Either PgNamedError TestValue)
44+
noNamedParams = runNamedQuery $ queryNamed dbPool "SELECT 42" []
45+
46+
emptyName :: IO (Either PgNamedError TestValue)
47+
emptyName = runNamedQuery $ queryNamed dbPool "SELECT ?foo, ?" ["foo" =? True]
48+
49+
queryTestValue :: IO (Either PgNamedError TestValue)
50+
queryTestValue = runNamedQuery $ queryNamed dbPool "SELECT ?intVal, ?intVal, ?txtVal"
51+
[ "intVal" =? (42 :: Int)
52+
, "txtVal" =? ("baz" :: ByteString)
53+
]
54+
55+
runNamedQuery :: ExceptT PgNamedError IO [TestValue] -> IO (Either PgNamedError TestValue)
56+
runNamedQuery = fmap (second head) . runExceptT
57+
58+
data TestValue = TestValue
59+
{ intVal1 :: !Int
60+
, intVal2 :: !Int
61+
, txtVal :: !ByteString
62+
} deriving stock (Show, Eq, Generic)
63+
deriving anyclass (Sql.FromRow, Sql.ToRow)

0 commit comments

Comments
 (0)