X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FWeather.hs;h=0866f6f09ee7a666f54da7cb4ce47e1f60e8c47a;hb=fe55e0de738d00b94ad1269bafe32beb83860387;hp=6df3ce37615cfa48ba28cc028fb355a90dcbbb1d;hpb=b2c39ebe5ff9c1ea3224231df5078c52d0ad8737;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/Weather.hs b/src/TSN/XML/Weather.hs index 6df3ce3..0866f6f 100644 --- a/src/TSN/XML/Weather.hs +++ b/src/TSN/XML/Weather.hs @@ -11,11 +11,14 @@ -- module TSN.XML.Weather ( dtd, + is_type1, pickle_message, + teams_are_normal, -- * Tests weather_tests, -- * WARNING: these are private but exported to silence warnings WeatherConstructor(..), + WeatherDetailedWeatherListingItemConstructor(..), WeatherForecastConstructor(..), WeatherForecastListingConstructor(..) ) where @@ -41,7 +44,20 @@ import Test.Tasty ( TestTree, testGroup ) import Test.Tasty.HUnit ( (@?=), testCase ) import Text.XML.HXT.Core ( PU, - xp7Tuple, + XmlTree, + (/>), + (>>>), + addNav, + descendantAxis, + filterAxis, + followingSiblingAxis, + hasName, + readDocument, + remNav, + runLA, + runX, + xp8Tuple, + xp9Tuple, xpAttr, xpElem, xpInt, @@ -49,19 +65,21 @@ import Text.XML.HXT.Core ( xpOption, xpPair, xpText, + xpTriple, xpWrap ) -- Local imports. import TSN.Codegen ( tsn_codegen_config ) import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate ) -import TSN.Picklers ( xp_gamedate, xp_time_stamp ) +import TSN.Picklers ( xp_datetime, xp_gamedate, xp_time_stamp ) import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) ) import Xml ( Child(..), FromXml(..), FromXmlFk(..), ToDb(..), + parse_opts, pickle_unpickle, unpickleable, unsafe_unpickle ) @@ -170,7 +188,8 @@ instance Child WeatherForecastXml where instance FromXmlFk WeatherForecastXml where -- | To convert a 'WeatherForecastXml' into a 'WeatherForecast', we - -- just copy everything verbatim. + -- add the foreign key to the containing 'Weather', and copy the + -- game date. -- from_xml_fk fk WeatherForecastXml{..} = WeatherForecast { @@ -183,10 +202,111 @@ instance FromXmlFk WeatherForecastXml where -- instance XmlImportFk WeatherForecastXml +-- * WeatherDetailedWeatherXml + +-- | XML Representation of a \, which just contains +-- a bunch iof \s. There is no associated database type +-- since these don't really contain any information. +-- +data WeatherDetailedWeatherXml = + WeatherDetailedWeatherXml { + xml_detailed_listings :: [WeatherDetailedWeatherListingXml] } + deriving (Eq, Show) + + +-- * WeatherDetailedWeatherXml + +-- | XML Representation of a \. The sport and sport code +-- come as attributes, but then these just contain a bunch of +-- \s. There is no associated database type since these don't +-- contain much information. The sport we already know from the +-- \, while the sport code is ignored since it's already +-- present in each \s. +-- +data WeatherDetailedWeatherListingXml = + WeatherDetailedWeatherListingXml { + xml_dtl_listing_sport :: String, + xml_dtl_listing_sport_code :: String, + xml_items :: [WeatherDetailedWeatherListingItemXml] } + deriving (Eq, Show) + +-- * WeatherDetailedWeatherListingItem / WeatherDetailedWeatherListingItemXml + +-- | Database representation of a detailed weather item. The away/home +-- teams don't use the representation in "TSN.Team" because all +-- we're given is a name, and a team id is required for "TSN.Team". +-- +-- We also drop the sport name, because it's given in the parent +-- 'Weather'. +-- +data WeatherDetailedWeatherListingItem = + WeatherDetailedWeatherListingItem { + db_dtl_weather_id :: DefaultKey Weather, -- ^ Avoid name collision by + -- using \"dtl\" prefix. + db_sport_code :: String, + db_game_id :: Int, + db_dtl_game_date :: UTCTime, -- ^ Avoid name clash with \"dtl\" prefix + db_away_team :: String, + db_home_team :: String, + db_weather_type :: Int, + db_description :: String, + db_temp_adjust :: Maybe String, + db_temperature :: Int } + + +-- | XML representation of a detailed weather item. Same as the +-- database representation, only without the foreign key and the +-- sport name that comes from the containing listing. +data WeatherDetailedWeatherListingItemXml = + WeatherDetailedWeatherListingItemXml { + xml_sport_code :: String, + xml_game_id :: Int, + xml_dtl_game_date :: UTCTime, + xml_away_team :: String, + xml_home_team :: String, + xml_weather_type :: Int, + xml_description :: String, + xml_temp_adjust :: Maybe String, + xml_temperature :: Int } + deriving (Eq, Show) + + +instance ToDb WeatherDetailedWeatherListingItemXml where + -- | Our database analogue is a 'WeatherDetailedWeatherListingItem'. + type Db WeatherDetailedWeatherListingItemXml = + WeatherDetailedWeatherListingItem + +instance Child WeatherDetailedWeatherListingItemXml where + -- | We skip two levels of containers and say that the items belong + -- to the top-level 'Weather'. + type Parent WeatherDetailedWeatherListingItemXml = Weather + +instance FromXmlFk WeatherDetailedWeatherListingItemXml where + -- | To convert from the XML to database representation, we simply + -- add the foreign key (to Weather) and copy the rest of the fields. + from_xml_fk fk WeatherDetailedWeatherListingItemXml{..} = + WeatherDetailedWeatherListingItem { + db_dtl_weather_id = fk, + db_sport_code = xml_sport_code, + db_game_id = xml_game_id, + db_dtl_game_date = xml_dtl_game_date, + db_away_team = xml_away_team, + db_home_team = xml_home_team, + db_weather_type = xml_weather_type, + db_description = xml_description, + db_temp_adjust = xml_temp_adjust, + db_temperature = xml_temperature } + +-- | This allows us to insert the XML representation directly without +-- having to do the manual XML -\> DB conversion. +-- +instance XmlImportFk WeatherDetailedWeatherListingItemXml -- * Weather/Message --- | The database representation of a weather message. +-- | The database representation of a weather message. We don't +-- contain the forecasts or the detailed weather since those are +-- foreigned-keyed to us. -- data Weather = Weather { @@ -206,6 +326,7 @@ data Message = xml_sport :: String, xml_title :: String, xml_forecasts :: [WeatherForecastXml], + xml_detailed_weather :: Maybe WeatherDetailedWeatherXml, xml_time_stamp :: UTCTime } deriving (Eq, Show) @@ -232,7 +353,7 @@ instance XmlImport Message -- --- Database stuff +-- * Database stuff -- mkPersist tsn_codegen_config [groundhog| @@ -263,15 +384,84 @@ mkPersist tsn_codegen_config [groundhog| reference: onDelete: cascade +# We rename the two fields that needed a "dtl" prefix to avoid a name clash. +- entity: WeatherDetailedWeatherListingItem + dbName: weather_detailed_items + constructors: + - name: WeatherDetailedWeatherListingItem + fields: + - name: db_dtl_weather_id + dbName: weather_id + reference: + onDelete: cascade + - name: db_dtl_game_date + dbName: game_date + |] + +-- | There are two different types of documents that claim to be +-- \"weatherxml.dtd\". The first, more common type has listings +-- within forecasts. The second type has forecasts within +-- listings. Clearly we can't parse both of these using the same +-- parser! +-- +-- For now we're simply punting on the issue and refusing to parse +-- the second type. This will check the given @xmltree@ to see if +-- there are any forecasts contained within listings. If there are, +-- then it's the second type that we don't know what to do with. +-- +is_type1 :: XmlTree -> Bool +is_type1 xmltree = + case elements of + [] -> True + _ -> False + where + parse :: XmlTree -> [XmlTree] + parse = runLA $ hasName "/" + /> hasName "message" + /> hasName "listing" + /> hasName "forecast" + + elements = parse xmltree + + +-- | Some weatherxml documents even have the Home/Away teams in the +-- wrong order. We can't parse that! This next bit of voodoo detects +-- whether or not there are any \ elements that are +-- directly followed by sibling \ elements. This is the +-- opposite of the usual order. +-- +teams_are_normal :: XmlTree -> Bool +teams_are_normal xmltree = + case elements of + [] -> True + _ -> False + where + parse :: XmlTree -> [XmlTree] + parse = runLA $ hasName "/" + /> hasName "message" + /> hasName "Detailed_Weather" + /> hasName "DW_Listing" + /> hasName "Item" + >>> addNav + >>> descendantAxis + >>> filterAxis (hasName "HomeTeam") + >>> followingSiblingAxis + >>> remNav + >>> hasName "AwayTeam" + + elements = parse xmltree + + instance DbImport Message where dbmigrate _ = run_dbmigrate $ do migrate (undefined :: Weather) migrate (undefined :: WeatherForecast) migrate (undefined :: WeatherForecastListing) + migrate (undefined :: WeatherDetailedWeatherListingItem) dbimport m = do -- First we insert the top-level weather record. @@ -297,9 +487,9 @@ instance DbImport Message where return ImportSucceeded ---- ---- Pickling ---- +-- +-- * Pickling +-- -- | Pickler to convert a 'WeatherForecastListingXml' to/from XML. -- @@ -345,19 +535,73 @@ pickle_forecast = +-- | (Un)pickle a 'WeatherDetailedWeatherListingItemXml'. +-- +pickle_item :: PU WeatherDetailedWeatherListingItemXml +pickle_item = + xpElem "Item" $ + xpWrap (from_tuple, to_tuple) $ + xp9Tuple (xpElem "Sportcode" xpText) + (xpElem "GameID" xpInt) + (xpElem "Gamedate" xp_datetime) + (xpElem "AwayTeam" xpText) + (xpElem "HomeTeam" xpText) + (xpElem "WeatherType" xpInt) + (xpElem "Description" xpText) + (xpElem "TempAdjust" (xpOption xpText)) + (xpElem "Temperature" xpInt) + where + from_tuple = uncurryN WeatherDetailedWeatherListingItemXml + to_tuple w = (xml_sport_code w, + xml_game_id w, + xml_dtl_game_date w, + xml_away_team w, + xml_home_team w, + xml_weather_type w, + xml_description w, + xml_temp_adjust w, + xml_temperature w) + + +-- | (Un)pickle a 'WeatherDetailedWeatherListingXml'. +-- +pickle_dw_listing :: PU WeatherDetailedWeatherListingXml +pickle_dw_listing = + xpElem "DW_Listing" $ + xpWrap (from_tuple, to_tuple) $ + xpTriple (xpAttr "SportCode" xpText) + (xpAttr "Sport" xpText) + (xpList pickle_item) + where + from_tuple = uncurryN WeatherDetailedWeatherListingXml + to_tuple w = (xml_dtl_listing_sport w, + xml_dtl_listing_sport_code w, + xml_items w) + + +-- | (Un)pickle a 'WeatherDetailedWeatherXml' +-- +pickle_detailed_weather :: PU WeatherDetailedWeatherXml +pickle_detailed_weather = + xpElem "Detailed_Weather" $ + xpWrap (WeatherDetailedWeatherXml, xml_detailed_listings) + (xpList pickle_dw_listing) + + -- | Pickler to convert a 'Message' to/from XML. -- pickle_message :: PU Message pickle_message = xpElem "message" $ xpWrap (from_tuple, to_tuple) $ - xp7Tuple + xp8Tuple (xpElem "XML_File_ID" xpInt) (xpElem "heading" xpText) (xpElem "category" xpText) (xpElem "sport" xpText) (xpElem "title" xpText) (xpList pickle_forecast) + (xpOption pickle_detailed_weather) (xpElem "time_stamp" xp_time_stamp) where from_tuple = uncurryN Message @@ -367,19 +611,22 @@ pickle_message = xml_sport, xml_title, xml_forecasts, + xml_detailed_weather, xml_time_stamp) + -- --- Tasty tests +-- * Tasty tests -- - weather_tests :: TestTree weather_tests = testGroup "Weather tests" [ test_on_delete_cascade, test_pickle_of_unpickle_is_identity, - test_unpickle_succeeds ] + test_unpickle_succeeds, + test_types_detected_correctly, + test_normal_teams_detected_correctly ] -- | If we unpickle something and then pickle it, we should wind up @@ -387,45 +634,111 @@ weather_tests = -- test does not mean that unpickling succeeded. -- test_pickle_of_unpickle_is_identity :: TestTree -test_pickle_of_unpickle_is_identity = - testCase "pickle composed with unpickle is the identity" $ do - let path = "test/xml/weatherxml.xml" - (expected, actual) <- pickle_unpickle pickle_message path - actual @?= expected +test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests" + [ check "pickle composed with unpickle is the identity" + "test/xml/weatherxml.xml", + + check "pickle composed with unpickle is the identity (detailed)" + "test/xml/weatherxml-detailed.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 = - testCase "unpickling succeeds" $ do - let path = "test/xml/weatherxml.xml" - actual <- unpickleable path pickle_message - let expected = True - actual @?= expected +test_unpickle_succeeds = testGroup "unpickle tests" + [ check "unpickling succeeds" + "test/xml/weatherxml.xml", + check "unpickling succeeds (detailed)" + "test/xml/weatherxml-detailed.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 = - 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 +test_on_delete_cascade = testGroup "cascading delete tests" + [ check "deleting weather deletes its children" + "test/xml/weatherxml.xml", + check "deleting weather deletes its children (detailed)" + "test/xml/weatherxml-detailed.xml" ] + where + check desc path = testCase desc $ do + weather <- unsafe_unpickle path pickle_message + let a = undefined :: Weather + let b = undefined :: WeatherForecast + let c = undefined :: WeatherForecastListing + let d = undefined :: WeatherDetailedWeatherListingItem + actual <- withSqliteConn ":memory:" $ runDbConn $ do + runMigration silentMigrationLogger $ do + migrate a + migrate b + migrate c + migrate d + _ <- dbimport weather + deleteAll a + count_a <- countAll a + count_b <- countAll b + count_c <- countAll c + count_d <- countAll d + return $ count_a + count_b + count_c + count_d + let expected = 0 + actual @?= expected + + +-- | This is used in a few tests to extract an 'XmlTree' from a path. +-- +unsafe_get_xmltree :: String -> IO XmlTree +unsafe_get_xmltree path = + fmap head $ runX $ readDocument parse_opts path + + +-- | We want to make sure type1 documents are detected as type1, and +-- type2 documents detected as type2.. +-- +test_types_detected_correctly :: TestTree +test_types_detected_correctly = + testGroup "weatherxml types detected correctly" $ + [ check "test/xml/weatherxml.xml" + "first type detected correctly" + True, + check "test/xml/weatherxml-detailed.xml" + "first type detected correctly (detailed)" + True, + check "test/xml/weatherxml-type2.xml" + "second type detected correctly" + False ] + where + check path desc expected = testCase desc $ do + xmltree <- unsafe_get_xmltree path + let actual = is_type1 xmltree + actual @?= expected + + +-- | We want to make sure normal teams are detected as normal, and the +-- backwards ones are flagged as backwards. +-- +test_normal_teams_detected_correctly :: TestTree +test_normal_teams_detected_correctly = + testGroup "team order is detected correctly" [ + + check "normal teams are detected correctly" + "test/xml/weatherxml.xml" + True, + + check "backwards teams are detected correctly" + "test/xml/weatherxml-backwards-teams.xml" + False ] + where + check desc path expected = testCase desc $ do + xmltree <- unsafe_get_xmltree path + let actual = teams_are_normal xmltree + actual @?= expected