Test for rating formatting/parsing

This commit is contained in:
Gregor Kleen 2017-10-09 22:57:06 +02:00
parent 57ee32cc9c
commit 710fec9b18
3 changed files with 40 additions and 4 deletions

View File

@ -10,7 +10,8 @@
{-# OPTIONS_GHC -fno-warn-name-shadowing #-} {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
module Handler.Utils.Zip.Rating module Handler.Utils.Zip.Rating
( getRating ( Rating(..)
, getRating
, formatRating , formatRating
, parseRating , parseRating
) where ) where
@ -77,9 +78,9 @@ formatRating Rating{..} = let
parseRating :: ByteString parseRating :: ByteString
-> Either Text ( Maybe Points -> Either Text ( Maybe Points
, Maybe Text -- ^ Rating comment , Maybe Text -- ^ Rating comment
) )
parseRating input = do parseRating input = do
inputText <- first tshow $ Text.decodeUtf8' input inputText <- first tshow $ Text.decodeUtf8' input
let let

View File

@ -0,0 +1,33 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Handler.Utils.Zip.RatingSpec where
import TestImport
import Handler.Utils.Zip.Rating
import qualified Data.ByteString.Lazy as Lazy.ByteString
import qualified Data.Text as Text
import Database.Persist.Class
import Database.Persist.Sql
instance Arbitrary Rating where
arbitrary = do
ratingCourseName <- arbitrary
ratingSheetName <- arbitrary
ratingSubmissionId <- SubmissionKey . SqlBackendKey <$> arbitrary
ratingComment <- fmap Text.strip <$> arbitrary `suchThat` maybe True (not . Text.null)
ratingPoints <- arbitrary
return Rating{..}
spec :: Spec
spec = describe "Rating files" $ do
it "have compatible formatting/parsing" . property $
\rating@(Rating{..}) -> parseRating (Lazy.ByteString.toStrict $ formatRating rating) `shouldBe` Right (ratingPoints, ratingComment)

View File

@ -86,3 +86,5 @@ authenticateAs (Entity _ User{..}) = do
-- checking is switched off in wipeDB for those database backends which need it. -- checking is switched off in wipeDB for those database backends which need it.
createUser :: Text -> Text -> YesodExample UniWorX (Entity User) createUser :: Text -> Text -> YesodExample UniWorX (Entity User)
createUser userPlugin userIdent = runDB $ insertEntity User{..} createUser userPlugin userIdent = runDB $ insertEntity User{..}
where
userMatrikelnummer = "DummyMatrikelnummer"