Skip to content

Commit

Permalink
wip: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sigrdrifa committed Jun 8, 2024
1 parent 6586e01 commit d143951
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ run: tailwind-build ## compiles tailwindcss styles and runs the project with cab

.PHONY: test
test: ## runs the unit test project
cabal test hastl-test --test-show-details=direct
cabal test hastl-test --enable-tests --test-show-details=direct

.PHONY: test-integration
test-integration: ## runs the integration test project
Expand Down
11 changes: 7 additions & 4 deletions hastl.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,18 @@ test-suite hastl-test
default-language: GHC2021
other-modules:
Api.UserSpec
-- other-extensions:
default-extensions:
OverloadedStrings
type: exitcode-stdio-1.0
hs-source-dirs: test
main-is: Main.hs
build-tool-depends: hspec-discover:hspec-discover
build-depends:
base ^>=4.17.2.1,
hastl,
hspec
base ^>=4.17.2.1
, hastl
, hspec
, persistent-postgresql
, time
ghc-options:
-threaded -rtsopts -with-rtsopts=-N -fwarn-unused-imports

Expand Down
5 changes: 1 addition & 4 deletions lib/Api/User.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Control.Monad.Except (MonadIO (liftIO))
import Control.Monad.Logger (logDebugNS, logErrorNS)
import Database.Persist.Postgresql (
Entity (..),
PersistStoreRead (get),
getEntity,
insert,
selectFirst,
Expand All @@ -24,22 +23,20 @@ import Servant (
Proxy (..),
ReqBody,
ServerError (errBody, errHTTPCode, errHeaders),
addHeader,
err404,
err500,
throwError,
type (:<|>) (..),
type (:>),
)

import Api.Templates.Helpers.Htmx (hxTarget_)
import Api.Templates.User.User (renderUser, renderUsersComponent)
import Config (AppT (..))
import Data.Aeson (FromJSON)
import Data.Text (Text, pack)
import Data.Time (getCurrentTime)
import GHC.Generics (Generic)
import Lucid (Html, ToHtml (toHtml), class_, div_, id_, p_, renderBS)
import Lucid (Html, ToHtml (toHtml), class_, div_, id_, renderBS)
import Models (User (User), runDb, tryRunDb)
import Models qualified as Md
import Servant.API.ContentTypes.Lucid (HTML)
Expand Down
32 changes: 25 additions & 7 deletions test/Api/UserSpec.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
module Api.User.UserSpec where
module Api.UserSpec where

import Test.Hspec (Spec, describe, it, shouldBe)
import hastl.Api.Templates.User (renderUser)
import Api.Templates.User.User (renderUser)

Check warning on line 3 in test/Api/UserSpec.hs

View workflow job for this annotation

GitHub Actions / build

The import of ‘Api.Templates.User.User’ is redundant
import Control.Monad.IO.Class (liftIO)

Check warning on line 4 in test/Api/UserSpec.hs

View workflow job for this annotation

GitHub Actions / build

The import of ‘Control.Monad.IO.Class’ is redundant
import Data.Time (getCurrentTime)
import Models (User (User), userCreatedAt, userEmail, userName)
import Test.Hspec (Spec, before, context, describe, it, shouldBe)

import Database.Persist.Postgresql (

Check warning on line 9 in test/Api/UserSpec.hs

View workflow job for this annotation

GitHub Actions / build

The import of ‘Database.Persist.Postgresql’ is redundant

Check warning on line 9 in test/Api/UserSpec.hs

View workflow job for this annotation

GitHub Actions / build

The import item ‘Key(..)’ suggests that
Entity (..),
Key (..),
)

spec :: Spec
spec = do
describe "renderUser" $ do
it "renders a user" $ do
renderUser `shouldBe` "<div>user</div>"
spec = before
( do
time <- getCurrentTime
pure time
)
$ do
describe "templates" $ do
context "when rendering a user" $ do
it "renders a user" $
( \t -> do
let user = User{userName = "user", userEmail = "[email protected]", userCreatedAt = t}
-- let entity = Entity (Key user) user
userName user `shouldBe` "user"
)

0 comments on commit d143951

Please sign in to comment.