]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/XML/InjuriesDetail.hs
Use Generics.to_tuple in TSN.XML.InjuriesDetail.
[dead/htsn-import.git] / src / TSN / XML / InjuriesDetail.hs
index fee6ce2beb46d09da012a5ae60f9f5f37afd927a..90fb0cdb894a523ebbb342bf3ebc59620e1b228b 100644 (file)
@@ -1,8 +1,8 @@
+{-# LANGUAGE DeriveGeneric #-}
 {-# LANGUAGE FlexibleInstances #-}
 {-# LANGUAGE GADTs #-}
 {-# LANGUAGE QuasiQuotes #-}
 {-# LANGUAGE RecordWildCards #-}
-{-# LANGUAGE StandaloneDeriving #-}
 {-# LANGUAGE TemplateHaskell #-}
 {-# LANGUAGE TypeFamilies #-}
 
@@ -13,6 +13,7 @@
 --   real meat.
 --
 module TSN.XML.InjuriesDetail (
+  dtd,
   pickle_message,
   -- * Tests
   injuries_detail_tests,
@@ -28,10 +29,17 @@ 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 )
+import qualified GHC.Generics as GHC ( Generic )
 import Test.Tasty ( TestTree, testGroup )
 import Test.Tasty.HUnit ( (@?=), testCase )
 import Text.XML.HXT.Core (
@@ -48,16 +56,26 @@ import Text.XML.HXT.Core (
   xpWrap )
 
 -- Local imports.
+import Generics ( Generic(..), to_tuple )
 import TSN.Codegen ( tsn_codegen_config )
 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"
 
 
 --
@@ -79,12 +97,19 @@ data Message =
     xml_sport :: String,
     xml_listings :: [InjuriesDetailListingXml],
     xml_time_stamp :: UTCTime }
-  deriving (Eq, Show)
+  deriving (Eq, GHC.Generic, Show)
+
+
+-- | For 'Generics.to_tuple'.
+--
+instance Generic Message
+
 
 -- | Database representation of a 'Message'.
 --
 data InjuriesDetail =
   InjuriesDetail {
+    db_xml_file_id :: Int,
     db_sport :: String,
     db_time_stamp :: UTCTime }
   deriving (Eq, Show)
@@ -92,6 +117,7 @@ data InjuriesDetail =
 instance ToDb Message where
   -- | The database representation of a 'Message' is an
   --   'InjuriesDetail'.
+  --
   type Db Message = InjuriesDetail
 
 instance FromXml Message where
@@ -100,18 +126,25 @@ instance FromXml Message where
   --
   from_xml Message{..} =
     InjuriesDetail {
+      db_xml_file_id = xml_xml_file_id,
       db_sport = xml_sport,
       db_time_stamp = xml_time_stamp }
 
 
--- | This allows us to call 'insert_xml' directly on the XML
---   representation.
+-- | This allows us to insert the XML representation 'Message'
+--   directly.
+--
 instance XmlImport Message
 
 
 
 -- * InjuriesDetailListing/InjuriesDetailListingXml
 
+-- | Database representation of a \<Listing\> element. It has a
+--   foreign key pointing to its parent 'InjuriesDetail', and does not
+--   contain the list of 'xml_player_listings' (which get their own
+--   table).
+--
 data InjuriesDetailListing =
   InjuriesDetailListing {
     db_injuries_detail_id :: DefaultKey InjuriesDetail,
@@ -130,20 +163,40 @@ data InjuriesDetailListingXml =
 
     xml_full_name :: String, -- ^ Team full name
     xml_player_listings :: [InjuriesDetailListingPlayerListingXml] }
-  deriving (Eq, Show)
+  deriving (Eq, GHC.Generic, Show)
+
+
+-- | For 'Generics.to_tuple'.
+--
+instance Generic InjuriesDetailListingXml
+
 
 instance ToDb InjuriesDetailListingXml where
+  -- | The database analogue of an 'InjuriesDetailListingXml' is a
+  --   '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'.
+  --
   from_xml_fk fk InjuriesDetailListingXml{..} =
     InjuriesDetailListing {
       db_injuries_detail_id = fk,
       db_team_id = xml_team_id,
       db_full_name = xml_full_name }
 
+-- | This allows us to insert the XML representation
+--   'InjuriesDetailListingXml' directly.
+--
 instance XmlImportFk InjuriesDetailListingXml
 
 
@@ -172,9 +225,13 @@ data InjuriesDetailListingPlayerListingXml =
     xml_fantasy   :: Maybe String, -- ^ Nobody knows what this is.
     xml_injured     :: Bool,
     xml_type :: String }
-  deriving (Eq, Show)
+  deriving (Eq, GHC.Generic, Show)
 
 
+-- | For 'Generics.to_tuple'.
+--
+instance Generic InjuriesDetailListingPlayerListingXml
+
 
 -- | Database representation of a
 --   'InjuriesDetailListingPlayerListingXml'. We drop the team_id
@@ -200,11 +257,19 @@ 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
 
-  -- | To convert between a 'InjuriesDetailListingPlayerListingXml'
-  --   and a 'InjuriesDetailListingPlayerListingXml', we do nothing.
+
+instance FromXmlFk InjuriesDetailListingPlayerListingXml where
+  -- | To construct a 'InjuriesDetailListingPlayerListing' from a
+  --   'InjuriesDetailListingPlayerListingXml' we need to supply a
+  --   foreign key to an 'InjuriesDetailListing'.
+  --
   from_xml_fk fk InjuriesDetailListingPlayerListingXml{..} =
     InjuriesDetailListingPlayerListing {
       db_injuries_detail_listings_id = fk,
@@ -218,9 +283,8 @@ instance FromXmlFk InjuriesDetailListingPlayerListingXml where
       db_injured = xml_injured,
       db_type = xml_type }
 
--- | This lets us call 'insert_xml' on a
---   'InjuriesDetailListingPlayerListingXml' without having to
---   explicitly convert it to its database analogue first.
+-- | This lets us insert the XML representation
+--   'InjuriesDetailListingPlayerListingXml' directly.
 --
 instance XmlImportFk InjuriesDetailListingPlayerListingXml
 
@@ -230,9 +294,16 @@ instance XmlImportFk InjuriesDetailListingPlayerListingXml
 --
 
 instance DbImport Message where
+  dbmigrate _ =
+    run_dbmigrate $ do
+      migrate (undefined :: InjuriesDetail)
+      migrate (undefined :: InjuriesDetailListing)
+      migrate (undefined :: InjuriesDetailListingPlayerListing)
+
   -- | To import a 'Message', we import all of its
-  --   'InjuriesDetailListingPlayerListingXml's, which we have to dig out of its
-  --   'Listing's.
+  --   'InjuriesDetailListingPlayerListingXml's, which we have to dig
+  --   out of its 'Listing's.
+  --
   dbimport msg = do
     msg_id <- insert_xml msg
 
@@ -242,16 +313,17 @@ instance DbImport Message where
 
     return ImportSucceeded
 
-  dbmigrate _ =
-    run_dbmigrate $ do
-      migrate (undefined :: InjuriesDetail)
-      migrate (undefined :: InjuriesDetailListing)
-      migrate (undefined :: InjuriesDetailListingPlayerListing)
-
 
 mkPersist tsn_codegen_config [groundhog|
 - entity: InjuriesDetail
   dbName: injuries_detail
+  constructors:
+    - name: InjuriesDetail
+      uniques:
+        - name: unique_injuries_detail
+          type: constraint
+          # Prevent multiple imports of the same message.
+          fields: [db_xml_file_id]
 
 - entity: InjuriesDetailListing
   dbName: injuries_detail_listings
@@ -296,16 +368,6 @@ pickle_player_listing =
               (xpElem "Type" xpText)
   where
     from_tuple = uncurryN InjuriesDetailListingPlayerListingXml
-    to_tuple pl = (xml_player_team_id pl,
-                   xml_player_id pl,
-                   xml_date pl,
-                   xml_pos pl,
-                   xml_name pl,
-                   xml_injury pl,
-                   xml_status pl,
-                   xml_fantasy pl,
-                   xml_injured pl,
-                   xml_type pl)
 
 
 -- | Convert 'Listing's to/from XML.
@@ -319,9 +381,6 @@ pickle_listing =
              (xpList pickle_player_listing)
   where
     from_tuple = uncurryN InjuriesDetailListingXml
-    to_tuple l = (xml_team_id l,
-                  xml_full_name l,
-                  xml_player_listings l)
 
 
 -- | Convert 'Message's to/from XML.
@@ -338,12 +397,6 @@ pickle_message =
              (xpElem "time_stamp" xp_time_stamp)
   where
     from_tuple = uncurryN Message
-    to_tuple m = (xml_xml_file_id m,
-                  xml_heading m,
-                  xml_category m,
-                  xml_sport m,
-                  xml_listings m,
-                  xml_time_stamp m)
 
 
 --
@@ -356,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 ]
 
 
@@ -391,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