X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FInjuries.hs;h=368f8460c1cb95876eec92c2a6ed22dd663eed47;hb=8b7fa6040be218653c0d385134ea895d93f70bf7;hp=6cc802f9752d2dcbc13d59e3415cd38f7d9fb963;hpb=ee8df55da3731b3c73939cb32844e6476f4cec27;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/Injuries.hs b/src/TSN/XML/Injuries.hs index 6cc802f..368f846 100644 --- a/src/TSN/XML/Injuries.hs +++ b/src/TSN/XML/Injuries.hs @@ -1,9 +1,9 @@ +{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE RecordWildCards #-} -{-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} @@ -17,6 +17,7 @@ -- time_stamp. -- module TSN.XML.Injuries ( + dtd, pickle_message, -- * Tests injuries_tests, @@ -29,11 +30,19 @@ 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 qualified GHC.Generics as GHC ( Generic ) +import Database.Groundhog.Sqlite ( withSqliteConn ) import Data.Tuple.Curry ( uncurryN ) import Test.Tasty ( TestTree, testGroup ) import Test.Tasty.HUnit ( (@?=), testCase ) @@ -52,18 +61,27 @@ 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_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 -- @@ -82,15 +100,21 @@ data InjuriesTeam = -- * InjuriesListing/InjuriesListingXml --- | XML/Database representation of the injury listings. +-- | 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) + deriving (Eq, GHC.Generic, Show) + + +-- | For 'Generics.to_tuple'. +-- +instance Generic InjuriesListingXml + -- | Database representation of a 'InjuriesListing'. It possesses a -- foreign key to an 'Injuries' object so that we can easily delete @@ -100,7 +124,7 @@ 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 } @@ -108,10 +132,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{..} = @@ -122,6 +147,9 @@ 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 @@ -137,10 +165,15 @@ data Message = xml_sport :: String, xml_listings :: [InjuriesListingXml], xml_time_stamp :: UTCTime } - deriving (Eq, Show) + deriving (Eq, GHC.Generic, Show) + + +-- | For 'Generics.to_tuple'. +-- +instance Generic Message + --- | Database representation of a 'Message'. We really only care about --- the time stamp. +-- | Database representation of a 'Message'. -- data Injuries = Injuries { @@ -162,6 +195,9 @@ instance FromXml Message where db_sport = xml_sport, db_time_stamp = xml_time_stamp } +-- | This allows us to insert the XML representation 'Message' +-- directly. +-- instance XmlImport Message @@ -175,7 +211,8 @@ instance DbImport Message where 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 @@ -227,12 +264,13 @@ mkPersist tsn_codegen_config [groundhog| pickle_injuries_team :: PU InjuriesTeam pickle_injuries_team = xpElem "team" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, to_tuple') $ xpPair xpText (xpAttrImplied "league" xpText) where from_tuple = uncurryN InjuriesTeam - to_tuple m = (db_team_name m, db_team_league m) + -- Pointless, but silences two unused field warnings. + to_tuple' InjuriesTeam{..} = (db_team_name, db_team_league) -- | A pickler for 'InjuriesListingXml's that can convert them to/from -- XML. @@ -242,12 +280,12 @@ 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 from_tuple = uncurryN InjuriesListingXml - to_tuple l = (xml_team l, xml_teamno l, xml_injuries l, xml_updated l) + -- | A pickler for 'Message's that can convert them to/from XML. @@ -264,12 +302,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) -- @@ -282,7 +314,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 ] @@ -303,7 +336,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