X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FInjuries.hs;h=81243e44f20f8edd0dbd0f77a46c992379b2132f;hb=b830faafac5d720a8eecae369806d9142a186485;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..81243e4 100644 --- a/src/TSN/XML/Injuries.hs +++ b/src/TSN/XML/Injuries.hs @@ -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, + executeRaw, + 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,7 +68,8 @@ import Xml ( FromXmlFk(..), ToDb(..), pickle_unpickle, - unpickleable ) + unpickleable, + unsafe_unpickle ) -- @@ -82,7 +90,7 @@ data InjuriesTeam = -- * InjuriesListing/InjuriesListingXml --- | XML/Database representation of the injury listings. +-- | XML representation of the injury listings. -- data InjuriesListingXml = InjuriesListingXml { @@ -122,6 +130,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 +150,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 +172,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 +188,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 +296,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 +318,31 @@ 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 + -- No idea how 'delete' works, so do this instead. + executeRaw False "DELETE FROM injuries;" [] + count_a <- countAll a + count_b <- countAll b + return $ count_a + count_b + let expected = 0 + actual @?= expected