2. Write a test for test/xml/Odds_XML-long-import.xml once it no
longer takes 10 minutes to import (Postgres only?).
-3. Write a test for the ON DELETE CASCADE behavior (Odds_XML, weatherxml).
-
-4. Add support the the second type of weatherxml (see man page).
+3. Add support the the second type of weatherxml (see man page).
test_on_delete_cascade :: TestTree
test_on_delete_cascade = testGroup "cascading delete tests"
[ check "deleting news deletes its children"
- "test/xml/newsxml.xml" ]
+ "test/xml/newsxml.xml"
+ 4 -- 2 news_teams and 2 news_locations that should remain.
+ ]
where
- check desc path = testCase desc $ do
+ check desc path expected = testCase desc $ do
news <- unsafe_unpickle path pickle_message
let a = undefined :: News
let b = undefined :: NewsTeam
count_d <- countAll d
count_e <- countAll e
return $ count_a + count_b + count_c + count_d + count_e
- -- There are 2 news_teams and 2 news_locations that should remain.
- let expected = 4
actual @?= expected
import Database.Groundhog (
(=.),
(==.),
+ countAll,
+ executeRaw,
insert_,
migrate,
+ runMigration,
+ silentMigrationLogger,
update )
import Database.Groundhog.Core ( DefaultKey )
+import Database.Groundhog.Generic ( runDbConn )
+import Database.Groundhog.Sqlite ( withSqliteConn )
import Database.Groundhog.TH (
groundhog,
mkPersist )
FromXmlFk(..),
ToDb(..),
pickle_unpickle,
- unpickleable )
+ unpickleable,
+ unsafe_unpickle )
--
odds_tests =
testGroup
"Odds tests"
- [ test_pickle_of_unpickle_is_identity,
+ [ test_on_delete_cascade,
+ test_pickle_of_unpickle_is_identity,
test_unpickle_succeeds ]
actual <- unpickleable path pickle_message
let expected = True
actual @?= expected
+
+
+-- | Make sure everything gets deleted when we delete the top-level
+-- record.
+--
+test_on_delete_cascade :: TestTree
+test_on_delete_cascade = testGroup "cascading delete tests"
+ [ check "deleting odds deleted its children"
+ "test/xml/Odds_XML.xml"
+ 13 -- 5 casinos, 8 teams
+ ,
+
+ check "deleting odds deleted its children (non-int team_id)"
+ "test/xml/Odds_XML-noninteger-team-id.xml"
+ 51 -- 5 casinos, 46 teams
+ ,
+
+ check "deleting odds deleted its children (positive(+) line)"
+ "test/xml/Odds_XML-positive-line.xml"
+ 17 -- 5 casinos, 12 teams
+ ,
+
+ check "deleting odds deleted its children (large file)"
+ "test/xml/Odds_XML-largefile.xml"
+ 189 -- 5 casinos, 184 teams
+ ]
+ where
+ check desc path expected = testCase desc $ do
+ odds <- unsafe_unpickle path pickle_message
+ let a = undefined :: Odds
+ let b = undefined :: OddsCasino
+ let c = undefined :: OddsGameTeam
+ let d = undefined :: OddsGame
+ let e = undefined :: OddsGame_OddsGameTeam
+ let f = undefined :: OddsGameLine
+ actual <- withSqliteConn ":memory:" $ runDbConn $ do
+ runMigration silentMigrationLogger $ do
+ migrate a
+ migrate b
+ migrate c
+ migrate d
+ migrate e
+ migrate f
+ _ <- dbimport odds
+ -- No idea how 'delete' works, so do this instead.
+ executeRaw False "DELETE FROM odds;" []
+ count_a <- countAll a
+ count_b <- countAll b
+ count_c <- countAll c
+ count_d <- countAll d
+ count_e <- countAll e
+ count_f <- countAll f
+ return $ sum [count_a, count_b, count_c,
+ count_d, count_e, count_f ]
+ actual @?= expected
import Control.Monad ( forM_ )
import Data.Time ( UTCTime )
import Data.Tuple.Curry ( uncurryN )
-import Database.Groundhog ( migrate )
+import Database.Groundhog (
+ countAll,
+ executeRaw,
+ migrate,
+ runMigration,
+ silentMigrationLogger )
import Database.Groundhog.Core ( DefaultKey )
+import Database.Groundhog.Generic ( runDbConn )
+import Database.Groundhog.Sqlite ( withSqliteConn )
import Database.Groundhog.TH (
groundhog,
mkPersist )
FromXmlFk(..),
ToDb(..),
pickle_unpickle,
- unpickleable )
+ unpickleable,
+ unsafe_unpickle )
--
weather_tests =
testGroup
"Weather tests"
- [ test_pickle_of_unpickle_is_identity,
+ [ test_on_delete_cascade,
+ test_pickle_of_unpickle_is_identity,
test_unpickle_succeeds ]
actual <- unpickleable path pickle_message
let expected = True
actual @?= expected
+
+
+-- | Make sure everything gets deleted when we delete the top-level
+-- record.
+--
+test_on_delete_cascade :: TestTree
+test_on_delete_cascade =
+ testCase "deleting weather deletes its children" $ do
+ let path = "test/xml/weatherxml.xml"
+ weather <- unsafe_unpickle path pickle_message
+ let a = undefined :: Weather
+ let b = undefined :: WeatherForecast
+ let c = undefined :: WeatherForecastListing
+ actual <- withSqliteConn ":memory:" $ runDbConn $ do
+ runMigration silentMigrationLogger $ do
+ migrate a
+ migrate b
+ migrate c
+ _ <- dbimport weather
+ -- No idea how 'delete' works, so do this instead.
+ executeRaw False "DELETE FROM weather;" []
+ count_a <- countAll a
+ count_b <- countAll b
+ count_c <- countAll c
+ return $ count_a + count_b + count_c
+ let expected = 0
+ actual @?= expected