Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Create primer-testlib and primer-rel8-testlib packages. #758

Merged
merged 8 commits into from
Nov 3, 2022
6 changes: 6 additions & 0 deletions hie.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ cradle:
- path: "primer/gen"
component: "primer:lib:primer-hedgehog"

- path: "primer/testlib"
component: "primer:lib:primer-testlib"

- path: "primer/test"
component: "primer:test:primer-test"

- path: "primer-rel8/src"
component: "lib:primer-rel8"

- path: "primer-rel8/testlib"
component: "primer-rel8:lib:primer-rel8-testlib"

- path: "primer-rel8/test"
component: "primer-rel8:test:primer-rel8-test"

Expand Down
54 changes: 41 additions & 13 deletions primer-rel8/primer-rel8.cabal
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cabal-version: 2.4
cabal-version: 3.0
name: primer-rel8
version: 0.7.2.0
license: AGPL-3.0-or-later
Expand Down Expand Up @@ -48,6 +48,41 @@ library
, time ^>=1.11
, uuid ^>=1.3.15

library primer-rel8-testlib
visibility: public
exposed-modules: Primer.Database.Rel8.Test.Util
hs-source-dirs: testlib
default-language: GHC2021
default-extensions:
NoImplicitPrelude
DataKinds
DerivingStrategies
DerivingVia
LambdaCase
OverloadedStrings

ghc-options:
-Wall -Wincomplete-uni-patterns -Wincomplete-record-updates
-Wcompat -Widentities -Wredundant-constraints -fhide-source-paths

build-depends:
, base
, bytestring
, hasql
, hasql-pool
, logging-effect
, port-utils ^>=0.2.1
, postgres-options ^>=0.2
, primer
, primer-rel8
, rel8
, temporary ^>=1.3
, time
, tmp-postgres ^>=1.34.1.0
, typed-process >=0.2.8 && <0.2.11
, utf8-string ^>=1.0
, uuid

test-suite primer-rel8-test
type: exitcode-stdio-1.0
main-is: Test.hs
Expand All @@ -58,7 +93,6 @@ test-suite primer-rel8-test
Tests.QuerySessionId
Tests.UpdateSessionApp
Tests.UpdateSessionName
TestUtils

default-language: GHC2021
default-extensions:
Expand Down Expand Up @@ -87,20 +121,14 @@ test-suite primer-rel8-test
, hasql
, hasql-pool
, logging-effect
, port-utils ^>=0.2.1
, postgres-options ^>=0.2
, primer
, primer-rel8
, primer-rel8:{primer-rel8, primer-rel8-testlib}
, primer:{primer, primer-testlib}
, rel8
, tasty ^>=1.4.2.1
, tasty-discover ^>=4.2.4
, tasty-hunit ^>=0.10.0
, temporary ^>=1.3
, tasty ^>=1.4.2.1
, tasty-discover ^>=4.2.4
, tasty-hunit ^>=0.10.0
, text
, time
, tmp-postgres ^>=1.34.1.0
, typed-process >=0.2.8 && <0.2.11
, utf8-string ^>=1.0
, uuid

--TODO This currently breaks with haskell.nix, so we manually add it to `flake.nix` instead.
Expand Down
38 changes: 21 additions & 17 deletions primer-rel8/test/Tests/InsertSession.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ import Primer.Database (
import Primer.Database.Rel8.Rel8Db (
Rel8DbException (InsertError),
)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)
import TestUtils (
assertException,
import Primer.Database.Rel8.Test.Util (
lowPrecisionCurrentTime,
runTmpDb,
testApp,
)
import Primer.Test.App (
comprehensive,
)
import Primer.Test.Util (
assertException,
(@?=),
)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)

expectedError :: SessionId -> Rel8DbException -> Bool
expectedError id_ (InsertError s _) = s == id_
Expand All @@ -37,44 +41,44 @@ test_insertSession_roundtrip :: TestTree
test_insertSession_roundtrip = testCaseSteps "insertSession database round-tripping" $ \step' ->
runTmpDb $ do
let step = liftIO . step'
step "Insert testApp"
step "Insert comprehensive"
now <- lowPrecisionCurrentTime
let version = "git123"
let name = safeMkSessionName "testApp"
let name = safeMkSessionName "comprehensive"
sessionId <- liftIO newSessionId
insertSession version sessionId testApp name now
insertSession version sessionId comprehensive name now

step "Retrieve it"
result <- querySessionId sessionId
result @?= Right (SessionData testApp name now)
result @?= Right (SessionData comprehensive name now)

let jpName = safeMkSessionName "サンプルプログラム"
step "Insert app with Japanese name"
sid1 <- liftIO newSessionId
insertSession version sid1 testApp jpName now
insertSession version sid1 comprehensive jpName now
r1 <- querySessionId sid1
r1 @?= Right (SessionData testApp jpName now)
r1 @?= Right (SessionData comprehensive jpName now)

let cnName = safeMkSessionName "示例程序"
step "Insert app with simplified Chinese name"
sid2 <- liftIO newSessionId
insertSession version sid2 testApp cnName now
insertSession version sid2 comprehensive cnName now
r2 <- querySessionId sid2
r2 @?= Right (SessionData testApp cnName now)
r2 @?= Right (SessionData comprehensive cnName now)

let arName = safeMkSessionName "برنامج مثال"
step "Insert app with Arabic name"
sid3 <- liftIO newSessionId
insertSession version sid3 testApp arName now
insertSession version sid3 comprehensive arName now
r3 <- querySessionId sid3
r3 @?= Right (SessionData testApp arName now)
r3 @?= Right (SessionData comprehensive arName now)

let emName = safeMkSessionName "😄😂🤣🤗 🦊 🦈"
step "Insert app with emoji name"
sid4 <- liftIO newSessionId
insertSession version sid4 testApp emName now
insertSession version sid4 comprehensive emName now
r4 <- querySessionId sid4
r4 @?= Right (SessionData testApp emName now)
r4 @?= Right (SessionData comprehensive emName now)

test_insertSession_failure :: TestTree
test_insertSession_failure = testCaseSteps "insertSession failure modes" $ \step' ->
Expand Down
27 changes: 6 additions & 21 deletions primer-rel8/test/Tests/ListSessions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module Tests.ListSessions where

import Foreword

import Data.UUID.V4 (nextRandom)
import Primer.App (newApp)
import Primer.Database (
LastModified (..),
Expand All @@ -19,35 +18,21 @@ import Primer.Database (
import Primer.Database.Rel8 (
SessionRow (SessionRow, app, gitversion, lastmodified, name, uuid),
)
import Rel8 (Result)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)
import TestUtils (
lowPrecisionCurrentTime,
import Primer.Database.Rel8.Test.Util (
mkSessionRow,
runTmpDb,
(@?=),
)

mkSession :: Int -> IO (SessionRow Result)
mkSession n = do
u <- nextRandom
now <- lowPrecisionCurrentTime
pure $
SessionRow
{ uuid = u
, gitversion = "test-version"
, app = newApp
, name = "name-" <> show n
, lastmodified = utcTime now
}
import Primer.Test.Util ((@?=))
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)

test_listSessions :: TestTree
test_listSessions = testCaseSteps "listSessions" $ \step' ->
runTmpDb $ do
let step = liftIO . step'
let m = 345
step "Insert all sessions"
rows <- liftIO $ sortOn name <$> traverse mkSession [1 .. m]
rows <- liftIO $ sortOn name <$> traverse mkSessionRow [1 .. m]
forM_ rows (\SessionRow{..} -> insertSession gitversion uuid newApp (safeMkSessionName name) (LastModified lastmodified))
let expectedRows = map (\r -> Session (uuid r) (safeMkSessionName $ name r) (LastModified $ lastmodified r)) rows
step "Get all, offset+limit"
Expand Down
10 changes: 5 additions & 5 deletions primer-rel8/test/Tests/QuerySessionId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import Primer.Database (
import Primer.Database.Rel8.Schema qualified as Schema (
SessionRow (SessionRow, app, gitversion, lastmodified, name, uuid),
)
import Rel8 (lit)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)
import TestUtils (
import Primer.Database.Rel8.Test.Util (
insertSessionRow,
lowPrecisionCurrentTime,
runTmpDbWithPool,
(@?=),
)
import Primer.Test.Util ((@?=))
import Rel8 (lit)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)

-- Note: 'querySessionId' gets plenty of coverage in our other unit
-- tests by virtue of the fact we use it to retrieve results that we
Expand Down
22 changes: 13 additions & 9 deletions primer-rel8/test/Tests/UpdateSessionApp.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ import Primer.Database (
import Primer.Database.Rel8 (
Rel8DbException (UpdateAppNonExistentSession),
)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)
import TestUtils (
assertException,
import Primer.Database.Rel8.Test.Util (
lowPrecisionCurrentTime,
runTmpDb,
testApp,
)
import Primer.Test.App (
comprehensive,
)
import Primer.Test.Util (
assertException,
(@?=),
)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)

expectedError :: SessionId -> Rel8DbException -> Bool
expectedError id_ (UpdateAppNonExistentSession s) = s == id_
Expand Down Expand Up @@ -58,15 +62,15 @@ test_updateSessionApp_roundtrip = testCaseSteps "updateSessionApp database round
r2 @?= Right (SessionData newEmptyApp name now)

step "Update it with a new app"
updateSessionApp newVersion sessionId testApp now
updateSessionApp newVersion sessionId comprehensive now
r3 <- querySessionId sessionId
r3 @?= Right (SessionData testApp name now)
r3 @?= Right (SessionData comprehensive name now)

step "Update it with a new time"
now' <- lowPrecisionCurrentTime
updateSessionApp newVersion sessionId testApp now'
updateSessionApp newVersion sessionId comprehensive now'
r4 <- querySessionId sessionId
r4 @?= Right (SessionData testApp name now')
r4 @?= Right (SessionData comprehensive name now')

test_updateSessionApp_failure :: TestTree
test_updateSessionApp_failure = testCaseSteps "updateSessionApp failure modes" $ \step' ->
Expand Down
10 changes: 6 additions & 4 deletions primer-rel8/test/Tests/UpdateSessionName.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ import Primer.Database (
import Primer.Database.Rel8 (
Rel8DbException (UpdateNameNonExistentSession),
)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)
import TestUtils (
assertException,
import Primer.Database.Rel8.Test.Util (
lowPrecisionCurrentTime,
runTmpDb,
)
import Primer.Test.Util (
assertException,
(@?=),
)
import Test.Tasty (TestTree)
import Test.Tasty.HUnit (testCaseSteps)

expectedError :: SessionId -> Rel8DbException -> Bool
expectedError id_ (UpdateNameNonExistentSession s) = s == id_
Expand Down
Loading