]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/XML/InjuriesDetail.hs
Update TSN.XML modules to use the new Child class.
[dead/htsn-import.git] / src / TSN / XML / InjuriesDetail.hs
index 995101de618d875cd0828554c947061b24ab98b9..0eb3e238b99b6a986701c35edd6eee724a417298 100644 (file)
@@ -2,7 +2,6 @@
 {-# LANGUAGE GADTs #-}
 {-# LANGUAGE QuasiQuotes #-}
 {-# LANGUAGE RecordWildCards #-}
-{-# LANGUAGE StandaloneDeriving #-}
 {-# LANGUAGE TemplateHaskell #-}
 {-# LANGUAGE TypeFamilies #-}
 
@@ -13,6 +12,7 @@
 --   real meat.
 --
 module TSN.XML.InjuriesDetail (
+  dtd,
   pickle_message,
   -- * Tests
   injuries_detail_tests,
@@ -28,7 +28,13 @@ import Data.Time ( UTCTime )
 import Data.Tuple.Curry ( uncurryN )
 import Database.Groundhog (
   DefaultKey,
-  migrate )
+  countAll,
+  deleteAll,
+  migrate,
+  runMigration,
+  silentMigrationLogger )
+import Database.Groundhog.Generic ( runDbConn )
+import Database.Groundhog.Sqlite ( withSqliteConn )
 import Database.Groundhog.TH (
   groundhog,
   mkPersist )
@@ -53,11 +59,20 @@ import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
 import TSN.Picklers( xp_date, xp_time_stamp )
 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
 import Xml (
+  Child(..),
   FromXml(..),
   FromXmlFk(..),
   ToDb(..),
   pickle_unpickle,
-  unpickleable )
+  unpickleable,
+  unsafe_unpickle )
+
+
+
+-- | The DTD to which this module corresponds. Used to invoke dbimport.
+--
+dtd :: String
+dtd = "Injuries_Detail_XML.dtd"
 
 
 --
@@ -146,11 +161,14 @@ instance ToDb InjuriesDetailListingXml where
   --   'InjuriesDetailListing'.
   type Db InjuriesDetailListingXml = InjuriesDetailListing
 
-instance FromXmlFk InjuriesDetailListingXml where
+
+instance Child InjuriesDetailListingXml where
   -- | Each 'InjuriesDetailListingXml' is contained in an
   --   'InjuriesDetail'.
   type Parent InjuriesDetailListingXml = InjuriesDetail
 
+
+instance FromXmlFk InjuriesDetailListingXml where
   -- | Construct a 'InjuriesDetailListing' from a
   --   'InjuriesDetailListingXml' and a foreign key to a
   --   'InjuriesDetail'.
@@ -220,12 +238,15 @@ instance ToDb InjuriesDetailListingPlayerListingXml where
   type Db InjuriesDetailListingPlayerListingXml =
     InjuriesDetailListingPlayerListing
 
-instance FromXmlFk InjuriesDetailListingPlayerListingXml where
+
+instance Child InjuriesDetailListingPlayerListingXml where
   -- | Each 'InjuriesDetailListingPlayerListingXml' is contained in an
   --   'InjuriesDetailListing'.
   --
   type Parent InjuriesDetailListingPlayerListingXml = InjuriesDetailListing
 
+
+instance FromXmlFk InjuriesDetailListingPlayerListingXml where
   -- | To construct a 'InjuriesDetailListingPlayerListing' from a
   --   'InjuriesDetailListingPlayerListingXml' we need to supply a
   --   foreign key to an 'InjuriesDetailListing'.
@@ -388,7 +409,8 @@ injuries_detail_tests :: TestTree
 injuries_detail_tests =
   testGroup
     "InjuriesDetail tests"
-    [ test_pickle_of_unpickle_is_identity,
+    [ test_on_delete_cascade,
+      test_pickle_of_unpickle_is_identity,
       test_unpickle_succeeds ]
 
 
@@ -423,3 +445,34 @@ 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 "delete of injuries_detail deletes its children"
+          "test/xml/Injuries_Detail_XML.xml",
+
+    check "delete of injuries_detail deletes its children (non-int team_id)"
+          "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
+  where
+    check desc path = testCase desc $ do
+      inj <- unsafe_unpickle path pickle_message
+      let a = undefined :: InjuriesDetail
+      let b = undefined :: InjuriesDetailListing
+      let c = undefined :: InjuriesDetailListingPlayerListing
+      actual <- withSqliteConn ":memory:" $ runDbConn $ do
+                  runMigration silentMigrationLogger $ do
+                    migrate a
+                    migrate b
+                    migrate c
+                  _ <- dbimport inj
+                  deleteAll a
+                  count_a <- countAll a
+                  count_b <- countAll b
+                  count_c <- countAll c
+                  return $ count_a + count_b + count_c
+      let expected = 0
+      actual @?= expected