Created
December 1, 2011 23:21
-
-
Save ocharles/1420642 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Initialize a random database with languages, and keep track of what languages | |
-- are available. | |
instance Arbitrary (DBState (LoadedEntity Language)) where | |
arbitrary = do | |
l <- Language <$> (T.pack <$> name) <*> name `suchThat` (not . null) | |
return $ DBState (void $ insertLanguage l) (toEnt l) | |
where insertLanguage l = HDBC.run "INSERT INTO language (iso_code, name) VALUES (?, ?)" | |
[ toSql $ languageIsoCode l | |
, toSql $ languageName l ] | |
toEnt l = Entity { entityRef = Ref (toSql $ languageIsoCode l) | |
, entityInfo = l } | |
prop_allLanguages = monadicIO $ do | |
states <- setBy (entityRef . entity) <$> pick arbitrary | |
let languages = entity `map` states | |
fetched <- run $ databaseTest $ initDb `mapM` states >> allLanguages | |
assert $ languages `eqSet` fetched | |
where eqSet a b = all (`elem` a) b && all (`elem` b) a | |
prop_getByPk = monadicIO $ do | |
states <- setBy (entityRef . entity) <$> pick (listOf1 arbitrary :: Gen [DBState (LoadedEntity Language)]) | |
let language = head $ entity `map` states | |
fetched <- run $ databaseTest $ initDb `mapM` states >> getByPk (entityRef language) | |
assert $ language == fetched |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment