X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FWeather.hs;h=be591c10276d76570fa4f29a8fdc34de7cfbd714;hb=b0a87f9323223a0af538184940b35a081f5763af;hp=e9515edad67fb85901d596d1e7857cddfa035b28;hpb=bdd0f2c4f6661f41ba4b7c9f7682e0fc8ee5e32b;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/Weather.hs b/src/TSN/XML/Weather.hs index e9515ed..be591c1 100644 --- a/src/TSN/XML/Weather.hs +++ b/src/TSN/XML/Weather.hs @@ -10,6 +10,7 @@ -- that league contains a bunch of listings. -- module TSN.XML.Weather ( + dtd, pickle_message, -- * Tests weather_tests, @@ -23,8 +24,15 @@ where import Control.Monad ( forM_ ) import Data.Time ( UTCTime ) import Data.Tuple.Curry ( uncurryN ) -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.Sqlite ( withSqliteConn ) import Database.Groundhog.TH ( groundhog, mkPersist ) @@ -49,11 +57,20 @@ import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate ) import TSN.Picklers ( xp_gamedate, 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 = "weatherxml.dtd" -- @@ -85,15 +102,18 @@ data WeatherForecastListing = instance ToDb WeatherForecastListingXml where type Db WeatherForecastListingXml = WeatherForecastListing --- | This is needed to define the 'XmlImportFk' instance for --- 'WeatherForecastListing'. --- -instance FromXmlFk WeatherForecastListingXml where + +instance Child WeatherForecastListingXml where -- | Each 'WeatherForecastListingXml' is contained in a -- 'WeatherForecast'. -- type Parent WeatherForecastListingXml = WeatherForecast + +-- | This is needed to define the 'XmlImportFk' instance for +-- 'WeatherForecastListing'. +-- +instance FromXmlFk WeatherForecastListingXml where from_xml_fk fk WeatherForecastListingXml{..} = WeatherForecastListing { db_weather_forecasts_id = fk, @@ -148,9 +168,15 @@ instance ToDb WeatherForecastXml where -- type Db WeatherForecastXml = WeatherForecast -instance FromXmlFk WeatherForecastXml where + +instance Child WeatherForecastXml where + -- | The database type containing a 'WeatherForecastXml' is + -- 'Weather'. type Parent WeatherForecastXml = Weather + +instance FromXmlFk WeatherForecastXml where + -- | To convert a 'WeatherForecastXml' into a 'WeatherForecast', we -- replace the 'WeatherLeague' with its name. -- @@ -351,7 +377,8 @@ weather_tests :: TestTree weather_tests = testGroup "Weather tests" - [ test_pickle_of_unpickle_is_identity, + [ test_on_delete_cascade, + test_pickle_of_unpickle_is_identity, test_unpickle_succeeds ] @@ -376,3 +403,29 @@ test_unpickle_succeeds = 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 weather deletes its children" $ do + let path = "test/xml/weatherxml.xml" + weather <- unsafe_unpickle path pickle_message + let a = undefined :: Weather + let b = undefined :: WeatherForecast + let c = undefined :: WeatherForecastListing + actual <- withSqliteConn ":memory:" $ runDbConn $ do + runMigration silentMigrationLogger $ do + migrate a + migrate b + migrate c + _ <- dbimport weather + 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