X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FAutoRacingResults.hs;h=e7ab014e50579c77fe9488639bd42549ce75f3b6;hb=6d0766209118bd725438d24620d3ac2ffa374fd8;hp=fc19656ea40c7615806e22e20546f33cacf3061c;hpb=3a2418d302ee14b9605ebe3013cf417ea5cf0057;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/AutoRacingResults.hs b/src/TSN/XML/AutoRacingResults.hs index fc19656..e7ab014 100644 --- a/src/TSN/XML/AutoRacingResults.hs +++ b/src/TSN/XML/AutoRacingResults.hs @@ -6,13 +6,15 @@ {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} --- | Parse TSN XML for the DTD \"AutoRacingResultsXML.dtd\". +-- | Parse TSN XML for the DTD \"AutoRacingResultsXML.dtd\". Each +-- \ element contains a \ and a bunch of +-- \s. -- module TSN.XML.AutoRacingResults ( dtd, pickle_message, -- * Tests --- auto_racing_results_tests, + auto_racing_results_tests, -- * WARNING: these are private but exported to silence warnings AutoRacingResultsConstructor(..), AutoRacingResultsListingConstructor(..), @@ -42,9 +44,9 @@ import Test.Tasty.HUnit ( (@?=), testCase ) import Text.XML.HXT.Core ( PU, xp11Tuple, - xp12Tuple, xp13Tuple, xpAttr, + xpDefault, xpElem, xpInt, xpList, @@ -56,12 +58,16 @@ import Text.XML.HXT.Core ( xpWrap ) -- Local imports. -import TSN.Codegen ( - tsn_codegen_config ) +import TSN.Codegen ( tsn_codegen_config ) import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate ) -import TSN.Picklers ( xp_earnings, xp_racedate, xp_time_stamp ) +import TSN.Picklers ( + xp_earnings, + xp_fracpart_only_double, + xp_datetime, + xp_time_stamp ) import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) ) import Xml ( + Child(..), FromXml(..), FromXmlFk(..), ToDb(..), @@ -81,7 +87,9 @@ dtd = "AutoRacingResultsXML.dtd" -- * AutoRacingResults/Message --- | Database representation of a 'Message'. +-- | Database representation of a 'Message'. Comparatively, it lacks +-- the listings and race information since they are linked via a +-- foreign key. -- data AutoRacingResults = AutoRacingResults { @@ -100,7 +108,9 @@ data AutoRacingResults = --- | XML Representation of an 'AutoRacingResults'. +-- | XML Representation of an 'AutoRacingResults'. It has the same +-- fields, but in addition contains the 'xml_listings' and +-- 'xml_race_information'. -- data Message = Message { @@ -157,7 +167,7 @@ instance XmlImport Message -- * AutoRacingResultsListing/AutoRacingResultsListingXml -- | Database representation of a \ contained within a --- \. +-- \. -- data AutoRacingResultsListing = AutoRacingResultsListing { @@ -176,6 +186,7 @@ data AutoRacingResultsListing = db_nc :: Maybe Bool, db_earnings :: Maybe Int } + -- | XML representation of a \ contained within a -- \. -- @@ -203,12 +214,15 @@ instance ToDb AutoRacingResultsListingXml where -- type Db AutoRacingResultsListingXml = AutoRacingResultsListing -instance FromXmlFk AutoRacingResultsListingXml where + +instance Child AutoRacingResultsListingXml where -- | Each 'AutoRacingResultsListingXml' is contained in (i.e. has a -- foreign key to) a 'AutoRacingResults'. -- type Parent AutoRacingResultsListingXml = AutoRacingResults + +instance FromXmlFk AutoRacingResultsListingXml where -- | To convert an 'AutoRacingResultsListingXml' to an -- 'AutoRacingResultsListing', we add the foreign key and copy -- everything else verbatim. @@ -246,23 +260,34 @@ instance XmlImportFk AutoRacingResultsListingXml -- the \"db_\" prefix since our field namer is going to strip of -- everything before the first underscore. -- +-- We make the three fields optional because the entire +-- \ is apparently optional (although it is +-- usually present). A 'Nothing' in the XML should get turned into +-- three 'Nothing's in the DB. +-- data MostLapsLeading = MostLapsLeading { - db_most_laps_leading_driver_id :: Int, - db_most_laps_leading_driver :: String, - db_most_laps_leading_number_of_laps :: Int } + db_most_laps_leading_driver_id :: Maybe Int, + db_most_laps_leading_driver :: Maybe String, + db_most_laps_leading_number_of_laps :: Maybe Int } deriving (Data, Eq, Show, Typeable) --- | Database representation of a \ contained within a --- \. +-- | Database representation of a \ contained +-- within a \. +-- +-- The 'db_most_laps_leading' field is not optional because when we +-- convert from our XML representation, a missing 'MostLapsLeading' +-- will be replaced with a 'MostLapsLeading' with three missing +-- fields. -- data AutoRacingResultsRaceInformation = AutoRacingResultsRaceInformation { -- Note the apostrophe to disambiguate it from the - -- AutoRacingResultsListing filed. + -- AutoRacingResultsListing field. db_auto_racing_results_id' :: DefaultKey AutoRacingResults, - db_track_length :: Double, + db_track_length :: String, -- ^ Usually a Double, but sometimes a String, + -- like \"1.25 miles\". db_track_length_kph :: Double, db_laps :: Int, db_average_speed_mph :: Maybe Double, @@ -281,7 +306,7 @@ data AutoRacingResultsRaceInformation = -- data AutoRacingResultsRaceInformationXml = AutoRacingResultsRaceInformationXml { - xml_track_length :: Double, + xml_track_length :: String, xml_track_length_kph :: Double, xml_laps :: Int, xml_average_speed_mph :: Maybe Double, @@ -292,7 +317,7 @@ data AutoRacingResultsRaceInformationXml = xml_cautions :: Maybe String, xml_lead_changes :: Maybe String, xml_lap_leaders :: Maybe String, - xml_most_laps_leading :: MostLapsLeading } + xml_most_laps_leading :: Maybe MostLapsLeading } deriving (Eq,Show) @@ -304,12 +329,15 @@ instance ToDb AutoRacingResultsRaceInformationXml where type Db AutoRacingResultsRaceInformationXml = AutoRacingResultsRaceInformation -instance FromXmlFk AutoRacingResultsRaceInformationXml where + +instance Child AutoRacingResultsRaceInformationXml where -- | Each 'AutoRacingResultsRaceInformationXml' is contained in -- (i.e. has a foreign key to) a 'AutoRacingResults'. -- type Parent AutoRacingResultsRaceInformationXml = AutoRacingResults + +instance FromXmlFk AutoRacingResultsRaceInformationXml where -- | To convert an 'AutoRacingResultsRaceInformationXml' to an -- 'AutoRacingResultsRaceInformartion', we add the foreign key and -- copy everything else verbatim. @@ -328,8 +356,15 @@ instance FromXmlFk AutoRacingResultsRaceInformationXml where db_cautions = xml_cautions, db_lead_changes = xml_lead_changes, db_lap_leaders = xml_lap_leaders, - db_most_laps_leading = xml_most_laps_leading } - + db_most_laps_leading = most_laps_leading } + where + -- If we didn't get a \, indicate that in + -- the database with an (embedded) 'MostLapsLeading' with three + -- missing fields. + most_laps_leading = + case xml_most_laps_leading of + Just mll -> mll + Nothing -> MostLapsLeading Nothing Nothing Nothing -- | This allows us to insert the XML representation -- 'AutoRacingResultsRaceInformationXml' directly. @@ -338,9 +373,9 @@ instance XmlImportFk AutoRacingResultsRaceInformationXml ---- ---- Database stuff. ---- +-- +-- * Database stuff. +-- instance DbImport Message where dbmigrate _ = @@ -349,7 +384,17 @@ instance DbImport Message where migrate (undefined :: AutoRacingResultsListing) migrate (undefined :: AutoRacingResultsRaceInformation) - dbimport = undefined + -- | We insert the message, then use its ID to insert the listings + -- and race information. + dbimport m = do + msg_id <- insert_xml m + + insert_xml_fk_ msg_id (xml_race_information m) + + forM_ (xml_listings m) $ insert_xml_fk_ msg_id + + return ImportSucceeded + mkPersist tsn_codegen_config [groundhog| @@ -358,7 +403,7 @@ mkPersist tsn_codegen_config [groundhog| constructors: - name: AutoRacingResults uniques: - - name: unique_auto_racing_schedule + - name: unique_auto_racing_results type: constraint # Prevent multiple imports of the same message. fields: [db_xml_file_id] @@ -373,19 +418,24 @@ mkPersist tsn_codegen_config [groundhog| reference: onDelete: cascade - + # Note the apostrophe in the foreign key. This is to disambiguate + # it from the AutoRacingResultsListing foreign key of the same name. + # We strip it out of the dbName. - entity: AutoRacingResultsRaceInformation dbName: auto_racing_results_race_information constructors: - name: AutoRacingResultsRaceInformation fields: - name: db_auto_racing_results_id' + dbName: auto_racing_results_id reference: onDelete: cascade - name: db_most_laps_leading embeddedType: - - {name: most_laps_leading_driver_id, dbName: most_laps_leading_driver_id} - - {name: most_laps_leading_driver, dbName: most_laps_leading_driver} + - {name: most_laps_leading_driver_id, + dbName: most_laps_leading_driver_id} + - {name: most_laps_leading_driver, + dbName: most_laps_leading_driver} - embedded: MostLapsLeading fields: @@ -402,6 +452,8 @@ mkPersist tsn_codegen_config [groundhog| --- Pickling --- +-- | Pickler for the \s contained within \s. +-- pickle_listing :: PU AutoRacingResultsListingXml pickle_listing = xpElem "Listing" $ @@ -437,6 +489,7 @@ pickle_listing = -- | Pickler for the top-level 'Message'. +-- pickle_message :: PU Message pickle_message = xpElem "message" $ @@ -446,7 +499,7 @@ pickle_message = (xpElem "category" xpText) (xpElem "sport" xpText) (xpElem "RaceID" xpInt) - (xpElem "RaceDate" xp_racedate) + (xpElem "RaceDate" xp_datetime) (xpElem "Title" xpText) (xpElem "Track_Location" xpText) (xpElem "Laps_Remaining" xpInt) @@ -471,27 +524,63 @@ pickle_message = xml_time_stamp m) -pickle_most_laps_leading :: PU MostLapsLeading +-- | Pickler for the \ child of a +-- \. This is complicated by the fact that the +-- three fields we're trying to parse are not actually optional; +-- only the entire \ is. So we always wrap what +-- we parse in a 'Just', and when converting from the DB to XML, +-- we'll drop the entire element if any of its fields are missing +-- (which they never should be). +-- +pickle_most_laps_leading :: PU (Maybe MostLapsLeading) pickle_most_laps_leading = xpElem "Most_Laps_Leading" $ xpWrap (from_tuple, to_tuple) $ - xpTriple (xpElem "DriverID" xpInt) - (xpElem "Driver" xpText) - (xpElem "NumberOfLaps" xpInt) + xpTriple (xpOption $ xpElem "DriverID" xpInt) + (xpOption $ xpElem "Driver" xpText) + (xpOption $ xpElem "NumberOfLaps" xpInt) where - from_tuple = uncurryN MostLapsLeading - to_tuple m = (db_most_laps_leading_driver_id m, - db_most_laps_leading_driver m, - db_most_laps_leading_number_of_laps m) - + from_tuple :: (Maybe Int, Maybe String, Maybe Int) -> Maybe MostLapsLeading + from_tuple (Just x, Just y, Just z) = + Just $ MostLapsLeading (Just x) (Just y) (Just z) + from_tuple _ = Nothing + + -- Sure had to go out of my way to avoid the warnings about unused + -- db_most_laps_foo fields here. + to_tuple :: Maybe MostLapsLeading -> (Maybe Int, Maybe String, Maybe Int) + to_tuple Nothing = (Nothing, Nothing, Nothing) + to_tuple (Just (MostLapsLeading Nothing _ _)) = (Nothing, Nothing, Nothing) + to_tuple (Just (MostLapsLeading _ Nothing _)) = (Nothing, Nothing, Nothing) + to_tuple (Just (MostLapsLeading _ _ Nothing)) = (Nothing, Nothing, Nothing) + to_tuple (Just m) = (db_most_laps_leading_driver_id m, + db_most_laps_leading_driver m, + db_most_laps_leading_number_of_laps m) + + +-- | Pickler for the \ child of \. +-- +-- There's so much voodoo going on here. We have a double-layered +-- Maybe on top of the MostLapsLeading. When unpickling, we return a +-- Nothing (i.e. a Maybe MostLapsLeading) if any of its fields are +-- missing. But if the entire element is missing, unpickling +-- fails. 'xpOption' doesn't fix this because it would give us a +-- Maybe (Maybe MostLapsLeading). But we can use 'xpDefault' with a +-- default of (Nothing :: Maybe MostLapsLeading) to stick one in +-- there if unpicking a (Maybe MostLapsLeading) fails because +-- \ is missing. +-- +-- Clear as mud, I know. +-- pickle_race_information :: PU AutoRacingResultsRaceInformationXml pickle_race_information = xpElem "Race_Information" $ xpWrap (from_tuple, to_tuple) $ xp11Tuple (-- I can't think of another way to get both the -- TrackLength and its KPH attribute. So we shove them - -- both in a 2-tuple. - xpElem "TrackLength" $ xpPair xpPrim (xpAttr "KPH" xpPrim) ) + -- both in a 2-tuple. This should probably be an embedded type! + xpElem "TrackLength" $ + xpPair xpText + (xpAttr "KPH" xp_fracpart_only_double) ) (xpElem "Laps" xpInt) (xpOption $ xpElem "AverageSpeedMPH" xpPrim) (xpOption $ xpElem "AverageSpeedKPH" xpPrim) @@ -501,7 +590,7 @@ pickle_race_information = (xpOption $ xpElem "Cautions" xpText) (xpOption $ xpElem "LeadChanges" xpText) (xpOption $ xpElem "LapLeaders" xpText) - pickle_most_laps_leading + (xpDefault Nothing pickle_most_laps_leading) where -- Derp. Since the first two are paired, we have to -- manually unpack the bazillion arguments. @@ -521,3 +610,93 @@ pickle_race_information = xml_lead_changes m, xml_lap_leaders m, xml_most_laps_leading m) + +-- +-- * Tasty Tests +-- + +-- | A list of all tests for this module. +-- +auto_racing_results_tests :: TestTree +auto_racing_results_tests = + testGroup + "AutoRacingResults tests" + [ test_on_delete_cascade, + test_pickle_of_unpickle_is_identity, + test_unpickle_succeeds ] + +-- | If we unpickle something and then pickle it, we should wind up +-- with the same thing we started with. WARNING: success of this +-- test does not mean that unpickling succeeded. +-- +test_pickle_of_unpickle_is_identity :: TestTree +test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests" + [ check "pickle composed with unpickle is the identity" + "test/xml/AutoRacingResultsXML.xml", + + check "pickle composed with unpickle is the identity (fractional KPH)" + "test/xml/AutoRacingResultsXML-fractional-kph.xml", + + check "pickle composed with unpickle is the identity (No Most_Laps_Leading)" + "test/xml/AutoRacingResultsXML-no-most-laps-leading.xml"] + where + check desc path = testCase desc $ do + (expected, actual) <- pickle_unpickle pickle_message path + actual @?= expected + + + +-- | Make sure we can actually unpickle these things. +-- +test_unpickle_succeeds :: TestTree +test_unpickle_succeeds = testGroup "unpickle tests" + [ check "unpickling succeeds" + "test/xml/AutoRacingResultsXML.xml", + + check "unpickling succeeds (fractional KPH)" + "test/xml/AutoRacingResultsXML-fractional-kph.xml", + + check "unpickling succeeds (no Most_Laps_Leading)" + "test/xml/AutoRacingResultsXML-no-most-laps-leading.xml" ] + where + check desc path = testCase desc $ do + 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 = testGroup "cascading delete tests" + [ check "deleting auto_racing_results deletes its children" + "test/xml/AutoRacingResultsXML.xml", + + check "deleting auto_racing_results deletes its children (fractional KPH)" + "test/xml/AutoRacingResultsXML-fractional-kph.xml", + + check ("deleting auto_racing_results deletes its children " ++ + "(No Most_Laps_Leading)") + "test/xml/AutoRacingResultsXML-no-most-laps-leading.xml" ] + where + check desc path = testCase desc $ do + results <- unsafe_unpickle path pickle_message + let a = undefined :: AutoRacingResults + let b = undefined :: AutoRacingResultsListing + let c = undefined :: AutoRacingResultsRaceInformation + + actual <- withSqliteConn ":memory:" $ runDbConn $ do + runMigration silentMigrationLogger $ do + migrate a + migrate b + migrate c + _ <- dbimport results + deleteAll a + count_a <- countAll a + count_b <- countAll b + count_c <- countAll c + return $ sum [count_a, count_b, count_c] + let expected = 0 + actual @?= expected