]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/XML/Odds.hs
Implement the other ON DELETE CASCADE tests and update the TODO.
[dead/htsn-import.git] / src / TSN / XML / Odds.hs
index 9cc3818f63697ac006be484dcab42f05e9cf5f98..8cfe4ce35679eb7bd1fe49d54d3358e109b0085b 100644 (file)
@@ -31,10 +31,16 @@ import Data.Tuple.Curry ( uncurryN )
 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 )
@@ -67,7 +73,8 @@ import Xml (
   FromXmlFk(..),
   ToDb(..),
   pickle_unpickle,
-  unpickleable )
+  unpickleable,
+  unsafe_unpickle )
 
 
 --
@@ -731,7 +738,8 @@ odds_tests :: TestTree
 odds_tests =
   testGroup
     "Odds tests"
-    [ test_pickle_of_unpickle_is_identity,
+    [ test_on_delete_cascade,
+      test_pickle_of_unpickle_is_identity,
       test_unpickle_succeeds ]
 
 
@@ -778,3 +786,58 @@ test_unpickle_succeeds = testGroup "unpickle tests"
       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