File: Import.hs

package info (click to toggle)
haskell-esqueleto 3.6.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 732 kB
  • sloc: haskell: 9,857; makefile: 3
file content (87 lines) | stat: -rw-r--r-- 2,329 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
{-# LANGUAGE CPP, AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE UndecidableInstances #-}

module Common.Test.Import
    ( module Common.Test.Import
    , module X
    ) where

import System.Environment
import Control.Applicative
import Common.Test.Models as X
import Database.Esqueleto.Experimental as X hiding (random_)
import Test.Hspec as X
import UnliftIO as X
import Control.Monad
import Test.QuickCheck
import Data.Text  as X (Text)
import Control.Monad.Trans.Reader as X (ReaderT, mapReaderT, ask)

type SpecDb = SpecWith ConnectionPool

asserting :: MonadIO f => IO () -> SqlPersistT f ()
asserting a = liftIO a

noExceptions :: Expectation
noExceptions = pure ()

itDb
    :: (HasCallStack)
    => String
    -> SqlPersistT IO x
    -> SpecDb
itDb message action = do
    it message $ \connection -> do
        void $ testDb connection action

propDb
    :: (HasCallStack, Testable a)
    => String
    -> ((SqlPersistT IO () -> IO ()) -> a )
    -> SpecDb
propDb message action = do
    it message $ \connection -> do
        property (action (testDb connection))

testDb :: ConnectionPool -> SqlPersistT IO a -> IO a
testDb conn action =
    liftIO $ flip runSqlPool conn $ do
        a <- action
        transactionUndo
        pure a

setDatabaseState
    :: SqlPersistT IO a
    -> SqlPersistT IO ()
    -> SpecWith ConnectionPool
    -> SpecWith ConnectionPool
setDatabaseState create clean test =
    beforeWith (\conn -> runSqlPool create conn >> pure conn) $
    after (\conn -> runSqlPool clean conn) $
    test

isCI :: IO Bool
isCI =  do
    env <- getEnvironment
    return $ case lookup "TRAVIS" env <|> lookup "CI" env of
        Just "true" -> True
        _ -> False