add test scaffold and first test
This commit is contained in:
parent
45629d0abb
commit
0669bf651c
6 changed files with 125 additions and 32 deletions
53
default.nix
53
default.nix
|
@ -1,32 +1,23 @@
|
||||||
{ mkDerivation, aeson, base, base16-bytestring, base64-bytestring
|
{ # Fetch the latest haskell.nix and import its default.nix
|
||||||
, bytestring, case-insensitive, clock, containers, extra, HsYAML
|
haskellNix ? import (builtins.fetchTarball "https://github.com/input-output-hk/haskell.nix/archive/master.tar.gz") {}
|
||||||
, http-api-data, http-types, iproute, mtl, network, opaleye
|
|
||||||
, optparse-applicative, postgresql-simple
|
# haskell.nix provides access to the nixpkgs pins which are used by our CI,
|
||||||
, postgresql-simple-migration, product-profunctors, profunctors
|
# hence you will be more likely to get cache hits when using these.
|
||||||
, pureMD5, random-bytestring, servant, servant-rawm, servant-server
|
# But you can also just use your own, e.g. '<nixpkgs>'.
|
||||||
, stdenv, stm, text, time, wai, wai-logger, wai-middleware-throttle
|
, nixpkgsSrc ? haskellNix.sources.nixpkgs-2009
|
||||||
, warp
|
|
||||||
}:
|
# haskell.nix provides some arguments to be passed to nixpkgs, including some
|
||||||
mkDerivation {
|
# patches and also the haskell.nix functionality itself as an overlay.
|
||||||
pname = "mateamt";
|
, nixpkgsArgs ? haskellNix.nixpkgsArgs
|
||||||
version = "0.0.0.0";
|
|
||||||
src = ./.;
|
# import nixpkgs with overlays
|
||||||
isLibrary = true;
|
, pkgs ? import nixpkgsSrc nixpkgsArgs
|
||||||
isExecutable = true;
|
}: pkgs.haskell-nix.project {
|
||||||
libraryHaskellDepends = [
|
# 'cleanGit' cleans a source directory based on the files known by git
|
||||||
aeson base base16-bytestring base64-bytestring bytestring
|
src = pkgs.haskell-nix.haskellLib.cleanGit {
|
||||||
containers extra http-api-data http-types mtl opaleye
|
name = "mateamt";
|
||||||
postgresql-simple product-profunctors profunctors pureMD5
|
src = ./.;
|
||||||
random-bytestring servant servant-rawm servant-server stm text time
|
};
|
||||||
wai wai-logger warp
|
# Specify the GHC version to use.
|
||||||
];
|
compiler-nix-name = "ghc8103"; # Not required for `stack.yaml` based projects.
|
||||||
executableHaskellDepends = [
|
|
||||||
base base16-bytestring bytestring case-insensitive clock containers
|
|
||||||
HsYAML iproute mtl network opaleye optparse-applicative
|
|
||||||
postgresql-simple postgresql-simple-migration servant
|
|
||||||
servant-server stm text time wai wai-logger wai-middleware-throttle
|
|
||||||
warp
|
|
||||||
];
|
|
||||||
description = "A whole new matemat";
|
|
||||||
license = stdenv.lib.licenses.agpl3;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ flag develop
|
||||||
manual: True
|
manual: True
|
||||||
|
|
||||||
executable mateamt
|
executable mateamt
|
||||||
main-is: Main.hs
|
main-is: AppMain.hs
|
||||||
other-modules: AppTypes
|
other-modules: AppTypes
|
||||||
, AppTypes.Configuration
|
, AppTypes.Configuration
|
||||||
, Janitor
|
, Janitor
|
||||||
|
@ -120,3 +120,26 @@ library
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
if flag(develop)
|
if flag(develop)
|
||||||
cpp-options: -DDEVELOP
|
cpp-options: -DDEVELOP
|
||||||
|
|
||||||
|
test-suite spec
|
||||||
|
type: exitcode-stdio-1.0
|
||||||
|
main-is: Main.hs
|
||||||
|
hs-source-dirs: test
|
||||||
|
ghc-options: -Wall
|
||||||
|
build-depends: base
|
||||||
|
, mateamt
|
||||||
|
, hspec
|
||||||
|
, hspec-wai
|
||||||
|
, hspec-wai-json
|
||||||
|
, warp
|
||||||
|
, wai
|
||||||
|
, pg-transact
|
||||||
|
, tmp-postgres
|
||||||
|
, bytestring
|
||||||
|
, resource-pool
|
||||||
|
, postgresql-simple
|
||||||
|
other-modules: TestUtil
|
||||||
|
, Spec
|
||||||
|
, MainSpec
|
||||||
|
default-language: Haskell2010
|
||||||
|
build-tool-depends: hspec-discover:hspec-discover
|
||||||
|
|
8
test/Main.hs
Normal file
8
test/Main.hs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
{-# LANGUAGE PackageImports #-}
|
||||||
|
module Main where
|
||||||
|
|
||||||
|
import Test.Hspec
|
||||||
|
import qualified Spec
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = hspec Spec.spec
|
20
test/MainSpec.hs
Normal file
20
test/MainSpec.hs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE PackageImports #-}
|
||||||
|
module MainSpec where
|
||||||
|
|
||||||
|
import Database.PostgreSQL.Transact
|
||||||
|
import Database.PostgreSQL.Simple (Only(..))
|
||||||
|
|
||||||
|
import Test.Hspec
|
||||||
|
|
||||||
|
import Control.Monad.IO.Class (liftIO)
|
||||||
|
|
||||||
|
-- internal imports
|
||||||
|
|
||||||
|
import qualified "mateamt" Util as LibUtil
|
||||||
|
import TestUtil
|
||||||
|
|
||||||
|
spec = describeDB (const $ return ()) "Initialize DB" $
|
||||||
|
itDB "call initialization script" $ do
|
||||||
|
conn <- getConnection
|
||||||
|
liftIO $ shouldReturn (LibUtil.initDB conn) ()
|
1
test/Spec.hs
Normal file
1
test/Spec.hs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{-# OPTIONS_GHC -F -pgmF hspec-discover -optF --module-name=Spec #-}
|
50
test/TestUtil.hs
Normal file
50
test/TestUtil.hs
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
|
module TestUtil where
|
||||||
|
|
||||||
|
import Control.Exception
|
||||||
|
import Control.Monad
|
||||||
|
import qualified Data.ByteString.Char8 as BSC
|
||||||
|
import Data.Pool
|
||||||
|
import qualified Database.Postgres.Temp as Temp
|
||||||
|
import Database.PostgreSQL.Simple
|
||||||
|
import Database.PostgreSQL.Transact
|
||||||
|
import Test.Hspec
|
||||||
|
|
||||||
|
describeDB :: (Connection -> IO ()) -> String -> SpecWith TestDB -> Spec
|
||||||
|
describeDB migrate str =
|
||||||
|
beforeAll (setupDB migrate) . afterAll teardownDB . describe str
|
||||||
|
|
||||||
|
setupDB :: (Connection -> IO ()) -> IO TestDB
|
||||||
|
setupDB migrate = do
|
||||||
|
tempDB <- either throwIO return =<< Temp.start
|
||||||
|
putStrLn $ BSC.unpack $ Temp.toConnectionString tempDB
|
||||||
|
pool <- createPool
|
||||||
|
(connectPostgreSQL (Temp.toConnectionString tempDB))
|
||||||
|
close
|
||||||
|
1
|
||||||
|
100000000
|
||||||
|
50
|
||||||
|
withResource pool migrate
|
||||||
|
return TestDB {..}
|
||||||
|
|
||||||
|
teardownDB :: TestDB -> IO ()
|
||||||
|
teardownDB TestDB {..} = do
|
||||||
|
destroyAllResources pool
|
||||||
|
void $ Temp.stop tempDB
|
||||||
|
|
||||||
|
withPool :: TestDB -> (Connection -> IO a) -> IO a
|
||||||
|
withPool testDB = withResource (pool testDB)
|
||||||
|
|
||||||
|
itDB :: String -> DB a -> SpecWith TestDB
|
||||||
|
itDB msg action = it msg $ void . withDB action
|
||||||
|
|
||||||
|
withDB :: DB a -> TestDB -> IO a
|
||||||
|
withDB action testDB =
|
||||||
|
withResource (pool testDB) (runDBTSerializable action)
|
||||||
|
|
||||||
|
data TestDB = TestDB
|
||||||
|
{ tempDB :: Temp.DB
|
||||||
|
-- ^ Handle for temporary @postgres@ process
|
||||||
|
, pool :: Pool Connection
|
||||||
|
-- ^ Pool of 50 connections to the temporary @postgres@
|
||||||
|
}
|
Loading…
Reference in a new issue