Skip to content

Commit

Permalink
Add callJob to testing (#1173)
Browse files Browse the repository at this point in the history
* Add callJob

* Add callJob

* Apply suggestions from code review

Co-authored-by: Marc Scholten <marcphilipscholten@gmail.com>

* Update Mocking.hs

* Update IHP/Test/Mocking.hs

Co-authored-by: Marc Scholten <marcphilipscholten@gmail.com>

* Update IHP/Test/Mocking.hs

Co-authored-by: Marc Scholten <marcphilipscholten@gmail.com>
  • Loading branch information
amitaibu and mpscholten committed Oct 27, 2021
1 parent 402c706 commit 11ff0eb
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions IHP/Test/Mocking.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import qualified IHP.AutoRefresh.Types as AutoRefresh
import qualified IHP.Controller.Context as Context
import IHP.Controller.RequestContext (RequestBody (..), RequestContext (..))
import IHP.ControllerSupport (InitControllerContext, Controller, runActionWithNewContext)
import IHP.FrameworkConfig (ConfigBuilder (..), FrameworkConfig (..))
import IHP.FrameworkConfig (ConfigBuilder (..), FrameworkConfig (..), getFrameworkConfig)
import qualified IHP.FrameworkConfig as FrameworkConfig
import IHP.ModelSupport (createModelContext, Id')
import IHP.Prelude
import IHP.Log.Types
import IHP.Job.Types
import qualified IHP.Test.Database as Database
import Test.Hspec
import qualified Data.Text as Text
Expand Down Expand Up @@ -72,7 +73,7 @@ withIHPApp application configBuilder hspecAction = do
, frameworkConfig = frameworkConfig }

(hspecAction MockContext { .. })


mockContextNoDatabase :: (InitControllerContext application) => application -> ConfigBuilder -> IO (MockContext application)
mockContextNoDatabase application configBuilder = do
Expand Down Expand Up @@ -132,6 +133,26 @@ callActionWithParams controller params = do
Just response -> pure response
Nothing -> error "mockAction: The action did not render a response"

-- | Run a Job in a mock environment
--
-- __Example:__
--
-- Let's say you have a Job called @JobPost@ that you would like to process as part of a test.
--
-- > let postJob <- fetch ...
-- >
-- > callJob postJob
--
-- Note that 'callJob' doesn't set the Job status that is initially set 'IHP.Job.Types.JobStatusNotStarted', as that is
-- done by the Job queue (see 'IHP.Job.Queue.jobDidSucceed' for example).
--
callJob :: forall application job. (ContextParameters application, Typeable application, Job job) => job -> IO ()
callJob job = do
let frameworkConfig = getFrameworkConfig ?context
let ?context = frameworkConfig
perform job


-- | mockAction has been renamed to callAction
mockAction :: _ => _
mockAction = callAction
Expand Down Expand Up @@ -168,7 +189,7 @@ responseStatusShouldBe response status = responseStatus response `shouldBe` stat
-- > user <- newRecord @User
-- > |> set #email "marc@digitallyinduced.com"
-- > |> createRecord
-- >
-- >
-- > response <- withUser user do
-- > callAction CreatePostAction
--
Expand All @@ -192,7 +213,7 @@ withUser user callback =
where
newContext = ?context { request = newRequest }
newRequest = request { Wai.vault = newVault }

newSession :: Network.Wai.Session.Session IO ByteString ByteString
newSession = (lookupSession, insertSession)

Expand All @@ -204,7 +225,7 @@ withUser user callback =

newVault = Vault.insert vaultKey newSession (Wai.vault request)
RequestContext { request, vault = vaultKey } = get #requestContext ?mocking

sessionValue = Serialize.encode (get #id user)
sessionKey = cs (Session.sessionKey @user)

Expand All @@ -228,4 +249,4 @@ withUser user callback =
idToParam :: forall table. (Show (Id' table)) => Id' table -> ByteString
idToParam id = id
|> tshow
|> cs
|> cs

0 comments on commit 11ff0eb

Please sign in to comment.