-- other... disorganized... information.
--
module TSN.XML.Odds (
+ dtd,
pickle_message,
-- * Tests
odds_tests,
import Database.Groundhog (
(=.),
(==.),
+ countAll,
+ deleteAll,
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 )
+
+
+-- | The DTD to which this module corresponds. Used to invoke dbimport.
+--
+dtd :: String
+dtd = "Odds_XML.dtd"
--
- name: OddsGameLine
fields:
- name: ogl_odds_games_id
- references:
+ reference:
onDelete: cascade
- name: ogl_odds_casinos_id
- references:
+ reference:
onDelete: cascade
- entity: OddsGame_OddsGameTeam
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 deletes its children"
+ "test/xml/Odds_XML.xml"
+ 13 -- 5 casinos, 8 teams
+ ,
+
+ check "deleting odds deletes 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
+ deleteAll a
+ 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