forked from yesodweb/persistent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHtmlTest.hs
60 lines (51 loc) · 1.85 KB
/
HtmlTest.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# LANGUAGE QuasiQuotes, TemplateHaskell, CPP, GADTs, TypeFamilies, OverloadedStrings, FlexibleContexts, EmptyDataDecls, FlexibleInstances, GeneralizedNewtypeDeriving #-}
module HtmlTest (specs) where
import Database.Persist.Sqlite
import Database.Persist.TH
import Data.Char (generalCategory, GeneralCategory(..))
import qualified Data.Text as T
import System.Random (randomIO, randomRIO, Random)
import Control.Applicative ((<$>))
import Control.Monad.Trans.Resource (runResourceT)
import Init
import Text.Blaze.Html
import Text.Blaze.Html.Renderer.Text
-- Test lower case names
share [mkPersist persistSettings, mkMigrate "htmlMigrate"] [persistLowerCase|
HtmlTable
html Html
deriving
|]
cleanDB :: (MonadIO m, PersistQuery backend, PersistEntityBackend HtmlTable ~ backend) => ReaderT backend m ()
cleanDB = do
deleteWhere ([] :: [Filter HtmlTable])
specs :: Spec
specs = describe "html" $ do
it "works" $ asIO $ runResourceT $ runConn $ do
#ifndef WITH_MONGODB
_ <- runMigrationSilent htmlMigrate
-- Ensure reading the data from the database works...
_ <- runMigrationSilent htmlMigrate
#endif
sequence_ $ replicate 1000 $ do
x <- liftIO randomValue
key <- insert $ HtmlTable x
Just (HtmlTable y) <- get key
liftIO $ do
renderHtml x @?= renderHtml y
randomValue :: IO Html
randomValue =
preEscapedToMarkup
. T.pack
. filter ((`notElem` forbidden) . generalCategory)
. filter (<= '\xFFFF') -- only BMP
. filter (/= '\0') -- no nulls
<$> randomIOs
where forbidden = [NotAssigned, PrivateUse]
asIO :: IO a -> IO a
asIO = id
randomIOs :: Random a => IO [a]
randomIOs = do
len <- randomRIO (0, 20)
sequence $ replicate len randomIO