X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FInjuries.hs;h=1d1d3149638e72abce975d50983fb195a8028aff;hb=b4b15dad064fdd910a4ad8f36b6969ce909e05b2;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..1d1d314 100644 --- a/src/TSN/XML/Injuries.hs +++ b/src/TSN/XML/Injuries.hs @@ -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 ) @@ -61,9 +68,16 @@ import Xml ( 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,7 +96,7 @@ data InjuriesTeam = -- * InjuriesListing/InjuriesListingXml --- | XML/Database representation of the injury listings. +-- | XML representation of the injury listings. -- data InjuriesListingXml = InjuriesListingXml { @@ -122,6 +136,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 @@ -139,8 +156,7 @@ 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 { @@ -162,6 +178,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 +194,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 @@ -282,7 +302,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 +324,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