{-# LANGUAGE OverloadedStrings #-}
module Hledger.Web.Test (
hledgerWebTest
) where
import Data.String (fromString)
import Data.Function ((&))
import qualified Data.Text as T
import Test.Hspec (hspec)
import Yesod.Default.Config
import Yesod.Test
import Hledger.Web.Application ( makeAppWith )
import Hledger.Web.WebOptions
import Hledger.Web.Import hiding (get, j)
import Hledger.Cli hiding (prognameandversion)
runTests :: String -> [(String,String)] -> Journal -> YesodSpec App -> IO ()
runTests :: [Char] -> [([Char], [Char])] -> Journal -> YesodSpec App -> IO ()
runTests [Char]
testsdesc [([Char], [Char])]
rawopts Journal
j YesodSpec App
tests = do
WebOpts
wopts <- RawOpts -> IO WebOpts
rawOptsToWebOpts forall a b. (a -> b) -> a -> b
$ [([Char], [Char])] -> RawOpts
mkRawOpts [([Char], [Char])]
rawopts
let yconf :: AppConfig DefaultEnv Extra
yconf = AppConfig{
appEnv :: DefaultEnv
appEnv = DefaultEnv
Testing
,appHost :: HostPreference
appHost = WebOpts -> [Char]
host_ WebOpts
wopts forall a b. a -> (a -> b) -> b
& forall a. IsString a => [Char] -> a
fromString
,appPort :: Int
appPort = WebOpts -> Int
port_ WebOpts
wopts
,appRoot :: Text
appRoot = WebOpts -> [Char]
base_url_ WebOpts
wopts forall a b. a -> (a -> b) -> b
& [Char] -> Text
T.pack
,appExtra :: Extra
appExtra = Extra
{ extraCopyright :: Text
extraCopyright = Text
""
, extraAnalytics :: Maybe Text
extraAnalytics = forall a. Maybe a
Nothing
, extraStaticRoot :: Maybe Text
extraStaticRoot = [Char] -> Text
T.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> WebOpts -> Maybe [Char]
file_url_ WebOpts
wopts
}
}
App
app <- Journal -> AppConfig DefaultEnv Extra -> WebOpts -> IO App
makeAppWith Journal
j AppConfig DefaultEnv Extra
yconf WebOpts
wopts
Spec -> IO ()
hspec forall a b. (a -> b) -> a -> b
$ forall site. YesodDispatch site => site -> YesodSpec site -> Spec
yesodSpec App
app forall a b. (a -> b) -> a -> b
$ forall site. [Char] -> YesodSpec site -> YesodSpec site
ydescribe [Char]
testsdesc YesodSpec App
tests
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 d :: Day
d = Year -> Int -> Int -> Day
fromGregorian Year
2000 Int
1 Int
1
[Char] -> [([Char], [Char])] -> Journal -> YesodSpec App -> IO ()
runTests [Char]
"hledger-web" [] Journal
nulljournal 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"
forall site. [Char] -> YesodExample site () -> YesodSpec site
yit [Char]
"hyperlinks use a base url made from the default host and port" 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
let defaultbaseurl :: [Char]
defaultbaseurl = [Char] -> Int -> [Char]
defbaseurl [Char]
defhost Int
defport
forall site. HasCallStack => [Char] -> YesodExample site ()
bodyContains ([Char]
"href=\"" forall a. [a] -> [a] -> [a]
++ [Char]
defaultbaseurl)
forall site. HasCallStack => [Char] -> YesodExample site ()
bodyContains ([Char]
"src=\"" forall a. [a] -> [a] -> [a]
++ [Char]
defaultbaseurl)
let
rawopts :: [([Char], [Char])]
rawopts = [([Char]
"forecast",[Char]
"")]
iopts :: InputOpts
iopts = Day -> RawOpts -> InputOpts
rawOptsToInputOpts Day
d forall a b. (a -> b) -> a -> b
$ [([Char], [Char])] -> RawOpts
mkRawOpts [([Char], [Char])]
rawopts
f :: [Char]
f = [Char]
"fake"
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]
f Text
"" Journal
pj
[Char] -> [([Char], [Char])] -> Journal -> YesodSpec App -> IO ()
runTests [Char]
"hledger-web with --forecast" [([Char], [Char])]
rawopts Journal
j forall a b. (a -> b) -> a -> b
$ do
forall site. [Char] -> YesodExample site () -> YesodSpec site
yit [Char]
"shows 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\""