{-# LANGUAGE OverloadedStrings #-}
module Hledger.Web.Test (
hledgerWebTest
) where
import qualified Data.Text as T
import Test.Hspec (hspec)
import Yesod.Default.Config
import Yesod.Test
import Hledger.Web.Application ( makeFoundationWith )
import Hledger.Web.WebOptions ( WebOpts(cliopts_), defwebopts, prognameandversion )
import Hledger.Web.Import hiding (get, j)
import Hledger.Cli hiding (prognameandversion)
runHspecTestsWith :: AppConfig DefaultEnv Extra -> WebOpts -> Journal -> YesodSpec App -> IO ()
runHspecTestsWith :: AppConfig DefaultEnv Extra
-> WebOpts -> Journal -> YesodSpec App -> IO ()
runHspecTestsWith AppConfig DefaultEnv Extra
yesodconf WebOpts
hledgerwebopts Journal
j YesodSpec App
specs = do
App
app <- Journal -> AppConfig DefaultEnv Extra -> WebOpts -> IO App
makeFoundationWith Journal
j AppConfig DefaultEnv Extra
yesodconf WebOpts
hledgerwebopts
Spec -> IO ()
hspec forall a b. (a -> b) -> a -> b
$ forall site. YesodDispatch site => site -> YesodSpec site -> Spec
yesodSpec App
app YesodSpec App
specs
hledgerWebTest :: IO ()
hledgerWebTest :: IO ()
hledgerWebTest = do
[Char] -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ [Char]
"Running tests for " forall a. [a] -> [a] -> [a]
++ [Char]
prognameandversion
let conf :: AppConfig DefaultEnv Extra
conf = AppConfig{
appEnv :: DefaultEnv
appEnv = DefaultEnv
Testing
,appPort :: Int
appPort = Int
3000
,appRoot :: Text
appRoot = Text
"http://localhost:3000"
,appHost :: HostPreference
appHost = HostPreference
"*4"
,appExtra :: Extra
appExtra = Extra
{ extraCopyright :: Text
extraCopyright = Text
""
, extraAnalytics :: Maybe Text
extraAnalytics = forall a. Maybe a
Nothing
, extraStaticRoot :: Maybe Text
extraStaticRoot = forall a. Maybe a
Nothing
}
}
AppConfig DefaultEnv Extra
-> WebOpts -> Journal -> YesodSpec App -> IO ()
runHspecTestsWith AppConfig DefaultEnv Extra
conf WebOpts
defwebopts Journal
nulljournal forall a b. (a -> b) -> a -> b
$ do
forall site. [Char] -> YesodSpec site -> YesodSpec site
ydescribe [Char]
"hledger-web" forall a b. (a -> b) -> a -> b
$ do
forall site. [Char] -> YesodExample site () -> YesodSpec site
yit [Char]
"serves a reasonable-looking journal page" forall a b. (a -> b) -> a -> b
$ do
forall site url.
(Yesod site, RedirectUrl site url) =>
url -> YesodExample site ()
get Route App
JournalR
forall site. HasCallStack => Int -> YesodExample site ()
statusIs Int
200
forall site. HasCallStack => [Char] -> YesodExample site ()
bodyContains [Char]
"Add a transaction"
forall site. [Char] -> YesodExample site () -> YesodSpec site
yit [Char]
"serves a reasonable-looking register page" forall a b. (a -> b) -> a -> b
$ do
forall site url.
(Yesod site, RedirectUrl site url) =>
url -> YesodExample site ()
get Route App
RegisterR
forall site. HasCallStack => Int -> YesodExample site ()
statusIs Int
200
forall site. HasCallStack => [Char] -> YesodExample site ()
bodyContains [Char]
"accounts"
let
iopts :: InputOpts
iopts = InputOpts
definputopts{forecast_ :: Maybe DateSpan
forecast_=forall a. a -> Maybe a
Just DateSpan
nulldatespan}
copts :: CliOpts
copts = CliOpts
defcliopts{inputopts_ :: InputOpts
inputopts_=InputOpts
iopts, file_ :: [[Char]]
file_=[[Char]
""]}
wopts :: WebOpts
wopts = WebOpts
defwebopts{cliopts_ :: CliOpts
cliopts_=CliOpts
copts}
Journal
pj <- Text -> IO Journal
readJournal' ([Char] -> Text
T.pack forall a b. (a -> b) -> a -> b
$ [[Char]] -> [Char]
unlines
[[Char]
"~ monthly"
,[Char]
" assets 10"
,[Char]
" income"
])
Journal
j <- forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a. HasCallStack => [Char] -> a
error forall a. a -> a
id) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT forall a b. (a -> b) -> a -> b
$ InputOpts -> [Char] -> Text -> Journal -> ExceptT [Char] IO Journal
journalFinalise InputOpts
iopts [Char]
"fake" Text
"" Journal
pj
AppConfig DefaultEnv Extra
-> WebOpts -> Journal -> YesodSpec App -> IO ()
runHspecTestsWith AppConfig DefaultEnv Extra
conf WebOpts
wopts Journal
j forall a b. (a -> b) -> a -> b
$ do
forall site. [Char] -> YesodSpec site -> YesodSpec site
ydescribe [Char]
"hledger-web --forecast" forall a b. (a -> b) -> a -> b
$ do
forall site. [Char] -> YesodExample site () -> YesodSpec site
yit [Char]
"serves a journal page showing forecasted transactions" forall a b. (a -> b) -> a -> b
$ do
forall site url.
(Yesod site, RedirectUrl site url) =>
url -> YesodExample site ()
get Route App
JournalR
forall site. HasCallStack => Int -> YesodExample site ()
statusIs Int
200
forall site. HasCallStack => [Char] -> YesodExample site ()
bodyContains [Char]
"id=\"transaction-2-1\""
forall site. HasCallStack => [Char] -> YesodExample site ()
bodyContains [Char]
"id=\"transaction-2-2\""