]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/XML/Injuries.hs
Update TSN.XML modules to use the new Child class.
[dead/htsn-import.git] / src / TSN / XML / Injuries.hs
index eb369e03eb8d1ecae646a383a4d0a95e9e2b8659..6af9ad9226feebd20356ab439c1c03a419366dc3 100644 (file)
@@ -3,7 +3,6 @@
 {-# LANGUAGE GADTs #-}
 {-# LANGUAGE QuasiQuotes #-}
 {-# LANGUAGE RecordWildCards #-}
-{-# LANGUAGE StandaloneDeriving #-}
 {-# LANGUAGE TemplateHaskell #-}
 {-# LANGUAGE TypeFamilies #-}
 
@@ -17,6 +16,7 @@
 --   time_stamp.
 --
 module TSN.XML.Injuries (
+  dtd,
   pickle_message,
   -- * Tests
   injuries_tests,
@@ -29,11 +29,18 @@ where
 import Data.Data ( Data )
 import Data.Time ( UTCTime )
 import Data.Typeable ( Typeable )
-import Database.Groundhog ( migrate )
+import Database.Groundhog (
+  countAll,
+  deleteAll,
+  migrate,
+  runMigration,
+  silentMigrationLogger )
 import Database.Groundhog.Core ( DefaultKey )
+import Database.Groundhog.Generic ( runDbConn )
 import Database.Groundhog.TH (
   groundhog,
   mkPersist )
+import Database.Groundhog.Sqlite ( withSqliteConn )
 import Data.Tuple.Curry ( uncurryN )
 import Test.Tasty ( TestTree, testGroup )
 import Test.Tasty.HUnit ( (@?=), testCase )
@@ -57,13 +64,21 @@ import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
 import TSN.Picklers ( 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 = "injuriesxml.dtd"
+
 --
 -- DB/XML Data types
 --
@@ -108,10 +123,11 @@ instance ToDb InjuriesListingXml where
   -- | The DB analogue of a 'InjuriesListingXml' is a 'InjuriesListing'
   type Db InjuriesListingXml = InjuriesListing
 
-instance FromXmlFk InjuriesListingXml where
+instance Child InjuriesListingXml where
   -- | Our foreign key points to an 'Injuries'.
   type Parent InjuriesListingXml = Injuries
 
+instance FromXmlFk InjuriesListingXml where
   -- | To convert between a 'InjuriesListingXml' and a
   --   'InjuriesListing', we simply append the foreign key.
   from_xml_fk fk InjuriesListingXml{..} =
@@ -288,7 +304,8 @@ injuries_tests :: TestTree
 injuries_tests =
   testGroup
     "Injuries tests"
-    [ test_pickle_of_unpickle_is_identity,
+    [ test_on_delete_cascade,
+      test_pickle_of_unpickle_is_identity,
       test_unpickle_succeeds ]
 
 
@@ -309,7 +326,30 @@ test_pickle_of_unpickle_is_identity =
 test_unpickle_succeeds :: TestTree
 test_unpickle_succeeds =
   testCase "unpickling succeeds" $ do
-  let path = "test/xml/injuriesxml.xml"
-  actual <- unpickleable path pickle_message
-  let expected = True
-  actual @?= expected
+    let path = "test/xml/injuriesxml.xml"
+    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 an injuries deletes its children" $ do
+    let path = "test/xml/injuriesxml.xml"
+    inj <- unsafe_unpickle path pickle_message
+    let a = undefined :: Injuries
+    let b = undefined :: InjuriesListing
+    actual <- withSqliteConn ":memory:" $ runDbConn $ do
+                runMigration silentMigrationLogger $ do
+                  migrate a
+                  migrate b
+                _ <- dbimport inj
+                deleteAll a
+                count_a <- countAll a
+                count_b <- countAll b
+                return $ count_a + count_b
+    let expected = 0
+    actual @?= expected