]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/XML/Injuries.hs
Crop the AutoRacingResults dbschema diagram.
[dead/htsn-import.git] / src / TSN / XML / Injuries.hs
index d2b4e10f938519f849b4fa2cd6d6c87d1df906a2..3f0fea55e364b066dfe4e95a2979893f4f0e75c0 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,
@@ -26,15 +26,21 @@ module TSN.XML.Injuries (
 where
 
 -- System imports.
-import Control.Monad ( forM_ )
 import Data.Data ( Data )
 import Data.Time ( UTCTime )
 import Data.Typeable ( Typeable )
-import Database.Groundhog ( insert_, 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 )
@@ -56,8 +62,28 @@ import Text.XML.HXT.Core (
 import TSN.Codegen ( tsn_codegen_config )
 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
 import TSN.Picklers ( xp_time_stamp )
-import TSN.XmlImport ( XmlImport(..) )
-import Xml ( FromXml(..), FromXmlFk(..), pickle_unpickle, unpickleable )
+import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
+import Xml (
+  Child(..),
+  FromXml(..),
+  FromXmlFk(..),
+  ToDb(..),
+  pickle_unpickle,
+  unpickleable,
+  unsafe_unpickle )
+
+
+
+-- | The DTD to which this module corresponds. Used to invoke dbimport.
+--
+dtd :: String
+dtd = "injuriesxml.dtd"
+
+--
+-- DB/XML Data types
+--
+
+-- * InjuriesTeam
 
 -- | XML/Database representation of a team as they appear in the
 --   injuries documents.
@@ -69,39 +95,42 @@ data InjuriesTeam =
   deriving (Data, Eq, Show, Typeable)
 
 
--- | XML/Database representation of the injury listings.
+-- * InjuriesListing/InjuriesListingXml
+
+-- | XML representation of the injury listings.
 --
 data InjuriesListingXml =
   InjuriesListingXml {
     xml_team :: InjuriesTeam,
-    xml_teamno :: Maybe Int,
+    xml_teamno :: Maybe String, -- ^ Can contain non-numerics, e.g. \"ZR2\"
     xml_injuries :: String,
     xml_updated :: Maybe Bool }
   deriving (Eq, Show)
 
 -- | Database representation of a 'InjuriesListing'. It possesses a
--- foreign key to an 'Injuries' object so that we can easily delete
--- 'InjuriesListing's based on the parent message's time_stamp.
+--   foreign key to an 'Injuries' object so that we can easily delete
+--   'InjuriesListing's based on the parent message's time_stamp.
 --
 data InjuriesListing =
   InjuriesListing {
     db_injuries_id :: DefaultKey Injuries,
     db_team :: InjuriesTeam,
-    db_teamno :: Maybe Int,
+    db_teamno :: Maybe String, -- ^ Can contain non-numerics, e.g. \"ZR2\"
     db_injuries :: String,
     db_updated :: Maybe Bool }
 
-
-instance FromXmlFk InjuriesListingXml where
+instance ToDb InjuriesListingXml where
   -- | The DB analogue of a 'InjuriesListingXml' is a 'InjuriesListing'
-  type DbFk InjuriesListingXml = InjuriesListing
+  type Db InjuriesListingXml = InjuriesListing
 
+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 InjuriesListingXml{..} fk =
+  from_xml_fk fk InjuriesListingXml{..} =
     InjuriesListing {
       db_injuries_id = fk,
       db_team = xml_team,
@@ -109,6 +138,13 @@ instance FromXmlFk InjuriesListingXml where
       db_injuries = xml_injuries,
       db_updated = xml_updated }
 
+-- | This allows us to insert the XML representation
+--   'InjuriesListingXml' directly.
+--
+instance XmlImportFk InjuriesListingXml
+
+
+-- * Injuries/Message
 
 -- | XML representation of an injuriesxml \<message\>.
 --
@@ -122,50 +158,66 @@ data Message =
     xml_time_stamp :: UTCTime }
   deriving (Eq, Show)
 
--- | Database representation of a 'Message'. We really only care about
---   the time stamp.
+-- | Database representation of a 'Message'.
 --
 data Injuries =
   Injuries {
+    db_xml_file_id :: Int,
     db_sport :: String,
     db_time_stamp :: UTCTime }
 
-instance FromXml Message where
+instance ToDb Message where
   -- | The database analogue of a 'Message' is an 'Injuries'.
   type Db Message = Injuries
 
+instance FromXml Message where
   -- | To convert from XML to DB, we simply drop the fields we don't
   --   care about.
   --
   from_xml Message{..} =
     Injuries {
+      db_xml_file_id = xml_xml_file_id,
       db_sport = xml_sport,
       db_time_stamp = xml_time_stamp }
 
+-- | This allows us to insert the XML representation 'Message'
+--   directly.
+--
 instance XmlImport Message
 
 
+--
+-- Database code
+--
+
 instance DbImport Message where
   dbmigrate _ =
     run_dbmigrate $ do
       migrate (undefined :: Injuries)
       migrate (undefined :: InjuriesListing)
 
-  -- | We import a 'Message' by inserting all of its 'listings'.
+  -- | We import a 'Message' by inserting all of its 'listings', but
+  --   the listings require a foreign key to the parent 'Message'.
   --
   dbimport msg = do
     msg_id <- insert_xml msg
 
-    forM_ (xml_listings msg) $ \listing ->
-      -- Convert the XML listing to a DB one using the message id and
-      -- insert it (disregarding the result).
-      insert_ $ from_xml_fk listing msg_id
+    -- Convert each XML listing to a DB one using the message id and
+    -- insert it (disregarding the result).
+    mapM_ (insert_xml_fk_ msg_id) (xml_listings msg)
 
     return ImportSucceeded
 
 
 mkPersist tsn_codegen_config [groundhog|
 - entity: Injuries
+  constructors:
+    - name: Injuries
+      uniques:
+        - name: unique_injuries
+          type: constraint
+          # Prevent multiple imports of the same message.
+          fields: [db_xml_file_id]
 
 - entity: InjuriesListing
   dbName: injuries_listings
@@ -187,6 +239,11 @@ mkPersist tsn_codegen_config [groundhog|
 |]
 
 
+--
+-- XML Picklers
+--
+
+
 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
 --
 pickle_injuries_team :: PU InjuriesTeam
@@ -199,14 +256,15 @@ pickle_injuries_team =
     to_tuple m = (db_team_name m, db_team_league m)
 
 
--- | A pickler for 'InjuriesListingXml's that can convert them to/from XML.
+-- | A pickler for 'InjuriesListingXml's that can convert them to/from
+--   XML.
 --
 pickle_listing :: PU InjuriesListingXml
 pickle_listing =
   xpElem "listing" $
     xpWrap (from_tuple, to_tuple) $
     xp4Tuple pickle_injuries_team
-             (xpOption $ xpElem "teamno" xpInt)
+             (xpOption $ xpElem "teamno" xpText)
              (xpElem "injuries" xpText)
              (xpOption $ xpElem "updated" xpPrim)
   where
@@ -246,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 ]
 
 
@@ -267,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