Skip to content

Commit eb8413e

Browse files
Merge pull request #27 from brandonchinn178/test-other-dbs
Run integration tests in PostgreSQL
2 parents 4c85ff5 + 7d04ad3 commit eb8413e

File tree

7 files changed

+268
-163
lines changed

7 files changed

+268
-163
lines changed

.circleci/config.yml

+32-12
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,28 @@ executors:
99
stack_yaml:
1010
type: string
1111
default: stack.yaml
12-
latest:
13-
type: boolean
14-
default: false
1512

1613
docker:
1714
- image: cimg/base:2020.01
1815
shell: /bin/bash -eux -o pipefail
1916
environment:
2017
STACK_YAML: << parameters.stack_yaml >>
21-
CI_LATEST: <<# parameters.latest >>1<</ parameters.latest >>
18+
19+
ubuntu_with_databases:
20+
parameters:
21+
stack_yaml:
22+
type: string
23+
default: stack.yaml
24+
25+
docker:
26+
- image: cimg/base:2020.01
27+
- image: postgres:13.1
28+
environment:
29+
POSTGRES_HOST_AUTH_METHOD: trust
30+
POSTGRES_DB: persistent_mtl
31+
shell: /bin/bash -eux -o pipefail
32+
environment:
33+
STACK_YAML: << parameters.stack_yaml >>
2234

2335
commands:
2436
install_stack:
@@ -30,6 +42,17 @@ commands:
3042
curl -sSL https://get.haskellstack.org/ | sh
3143
stack --version
3244
45+
build_stack_deps:
46+
steps:
47+
- run:
48+
name: Install system dependencies
49+
command: |
50+
sudo apt-get update
51+
sudo apt-get install -y postgresql libpq-dev
52+
- run:
53+
name: Build stack dependencies
54+
command: stack build --test --only-dependencies
55+
3356
with_stack_cache:
3457
parameters:
3558
install_deps:
@@ -101,18 +124,15 @@ jobs:
101124
default: false
102125

103126
executor:
104-
name: ubuntu
127+
name: ubuntu_with_databases
105128
stack_yaml: << parameters.stack_yaml >>
106-
latest: << parameters.latest >>
107129

108130
steps:
109131
- checkout
110132
- install_stack
111133
- with_stack_cache:
112134
install_deps:
113-
- run:
114-
name: Build external dependencies
115-
command: stack build --test --only-dependencies
135+
- build_stack_deps
116136
- when:
117137
condition: << parameters.latest >>
118138
steps:
@@ -140,6 +160,8 @@ jobs:
140160
<<# parameters.latest >>--coverage<</ parameters.latest >>
141161
)
142162
stack build "${STACK_ARGS[@]}"
163+
environment:
164+
TEST_POSTGRESQL: 1
143165
- when:
144166
condition: << parameters.latest >>
145167
steps:
@@ -156,9 +178,7 @@ jobs:
156178
- install_stack
157179
- with_stack_cache:
158180
install_deps:
159-
- run:
160-
name: Build external dependencies
161-
command: stack build --test --only-dependencies
181+
- build_stack_deps
162182
- run:
163183
name: Build haddock
164184
command: stack haddock --no-haddock-deps

package.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,10 @@ tests:
4343
- monad-logger
4444
- persistent
4545
- persistent-mtl
46+
- persistent-postgresql
4647
- persistent-sqlite
4748
- persistent-template
49+
- resource-pool
4850
- resourcet
4951
- tasty
5052
- tasty-golden

persistent-mtl.cabal

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 9b6ae20631ee5ad633b82ab405f64730ea1603ef8da2099ea41ef126cab9d817
7+
-- hash: a6b1252a25af52e3ddd10e843b4818e0255c269dfbc6a399eb29e41093ef0408
88

99
name: persistent-mtl
1010
version: 0.1.0.1
@@ -66,6 +66,7 @@ test-suite persistent-mtl-test
6666
Mocked
6767
MockSqlQueryT
6868
SqlQueryRepTest
69+
TestUtils.DB
6970
TestUtils.Match
7071
Paths_persistent_mtl
7172
hs-source-dirs:
@@ -79,8 +80,10 @@ test-suite persistent-mtl-test
7980
, monad-logger
8081
, persistent
8182
, persistent-mtl
83+
, persistent-postgresql
8284
, persistent-sqlite
8385
, persistent-template
86+
, resource-pool
8487
, resourcet
8588
, tasty
8689
, tasty-golden

stack-ghc-8.10.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ extra-deps:
55
# https://github.com/commercialhaskell/stackage/pull/5761
66
- persistent-2.11.0.1
77
- persistent-template-2.9.1.0
8+
- persistent-postgresql-2.11.0.0
89
- persistent-sqlite-2.11.0.0
910

1011
ghc-options:

test/Example.hs

+11-14
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ module Example
4040

4141
import Control.Arrow ((&&&))
4242
import Control.Monad.IO.Class (MonadIO(..))
43-
import Control.Monad.Logger (runNoLoggingT)
4443
import Control.Monad.Trans.Resource (MonadResource, ResourceT, runResourceT)
45-
import qualified Data.Text as Text
46-
import Database.Persist.Sql (Entity(..), EntityField, Key, Unique, toSqlKey)
47-
import Database.Persist.Sqlite (withSqlitePool)
44+
import Database.Persist.Sql
45+
(Entity(..), EntityField, Key, SelectOpt(..), Unique, toSqlKey)
4846
import Database.Persist.TH
4947
( mkDeleteCascade
5048
, mkMigrate
@@ -53,9 +51,10 @@ import Database.Persist.TH
5351
, share
5452
, sqlSettings
5553
)
56-
import UnliftIO (MonadUnliftIO(..), withSystemTempDirectory, wrappedWithRunInIO)
54+
import UnliftIO (MonadUnliftIO(..), wrappedWithRunInIO)
5755

5856
import Database.Persist.Monad
57+
import TestUtils.DB (BackendType(..), withTestDB)
5958

6059
share
6160
[ mkPersist sqlSettings
@@ -103,14 +102,12 @@ newtype TestApp a = TestApp
103102
instance MonadUnliftIO TestApp where
104103
withRunInIO = wrappedWithRunInIO TestApp unTestApp
105104

106-
runTestApp :: TestApp a -> IO a
107-
runTestApp m =
108-
withSystemTempDirectory "persistent-mtl-testapp" $ \dir -> do
109-
let db = Text.pack $ dir ++ "/db.sqlite"
110-
runNoLoggingT $ withSqlitePool db 5 $ \pool ->
111-
liftIO . runResourceT . runSqlQueryT pool . unTestApp $ do
112-
_ <- runMigrationSilent migration
113-
m
105+
runTestApp :: BackendType -> TestApp a -> IO a
106+
runTestApp backendType m =
107+
withTestDB backendType $ \pool ->
108+
runResourceT . runSqlQueryT pool . unTestApp $ do
109+
_ <- runMigrationSilent migration
110+
m
114111

115112
{- Person functions -}
116113

@@ -121,7 +118,7 @@ getName :: Entity Person -> String
121118
getName = personName . entityVal
122119

123120
getPeople :: MonadSqlQuery m => m [Person]
124-
getPeople = map entityVal <$> selectList [] []
121+
getPeople = map entityVal <$> selectList [] [Asc PersonId]
125122

126123
getPeopleNames :: MonadSqlQuery m => m [String]
127124
getPeopleNames = map personName <$> getPeople

0 commit comments

Comments
 (0)