Skip to content

Commit

Permalink
add integration test example and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
sigrdrifa committed Jul 2, 2024
1 parent f0eabdf commit e32f29e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ hastl is primarily tested with PostgreSQL but it uses the popular <a href="https
</p>
<p align="left"><b>Live reloading with GHCID</b> - since we're writing almost all of our code in the backend, hastl uses GHCID to instantly reload your project as you make changes
</p>
<p align="left"><b>Integration testing with TestContainers (COMING SOON)</b> - hastl uses <a href="https://testcontainers.com">testcontainers</a> to spin up a database on the fly to run integration tests and give you confidence in your business logic
<p align="left"><b>Integration testing with TestContainers</b> - hastl uses <a href="https://testcontainers.com">testcontainers</a> to spin up a database on the fly to run integration tests and give you confidence in your business logic
</p>


Expand Down Expand Up @@ -56,6 +56,28 @@ To run the development live reloading mode, make sure that <a href="https://gith
```
make ghcid-devel
```

#### Running unit tests

The unit tests of the project can be found in the `test` directory and can assert things like HTML generation from database types.

You can run all the unit tests with:

```
make test
```

#### Running integration tests

Hastl ships with built-in integration tests that use testcontainers to start a local postgreSQL database inside a container (using docker or podman) on-the-fly, as well as running the project web-server, allowing the tests to exercise the actual HTTP endpoints to assert correctness.

The integration tests manage the containers, starting them and tearing them down as the tests complete.

You can run all the integration tests with:
```
make integration-test
```

#### Changing the routes and templates

*COMING SOON*
Expand Down
3 changes: 3 additions & 0 deletions hastl.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,12 @@ test-suite hastl-test
test-suite hastl-test-integration
import: warnings
default-language: GHC2021
default-extensions:
OverloadedStrings
type: exitcode-stdio-1.0
hs-source-dirs: test-integration
main-is: Main.hs
other-modules: TestHttpSpec
build-depends:
base ^>=4.17.2.1,
hastl,
Expand Down
2 changes: 1 addition & 1 deletion lib/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,4 @@ envPool Production = 8
@""@ for 'Development' or @"test"@ for 'Test'.
-}
connStr :: BS.ByteString -> ConnectionString
connStr sfx = "host=localhost dbname=postgres" <> sfx <> " user=postgres password=okra123 port=5432"
connStr sfx = "host=localhost dbname=postgres" <> sfx <> " user=postgres password=postgres port=5432"
7 changes: 4 additions & 3 deletions test-integration/TestHttpSpec.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE OverloadedStrings #-}
module TestHttpSpec (specHttp) where

import Test.Hspec
Expand All @@ -9,10 +8,12 @@ import Network.HTTP.Simple

containers :: TC.TestContainer Int
containers = do
image <- TC.build (TC.fromBuildContext "./" (Just "./test-integration/test-server/Dockerfile"))
tc <-
TC.run
( TC.containerRequest
(TC.fromBuildContext "./" (Just "./test-integration/test-server/Dockerfile"))
( TC.containerRequest image
TC.& TC.setFixedName "hastl-integration-test"
TC.& TC.withFollowLogs TC.consoleLogConsumer
TC.& TC.setExpose [8000]
TC.& TC.setWaitingFor (TC.waitUntilMappedPortReachable 8000)
)
Expand Down
13 changes: 8 additions & 5 deletions test-integration/test-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
FROM docker.io/haskell:latest
FROM docker.io/haskell:9.4.8-slim

RUN apt update
RUN apt install -y python3 git make
ENV POSTGRES_PASSWORD test

RUN apt update
RUN apt install -y git make libpq-dev postgresql

COPY ./../ ./app

WORKDIR /app

RUN ls

ENV PORT 8000

CMD make run
RUN cabal update
RUN cabal build

CMD service postgresql start && cabal run

0 comments on commit e32f29e

Please sign in to comment.