X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FWeather.hs;h=0866f6f09ee7a666f54da7cb4ce47e1f60e8c47a;hb=fe55e0de738d00b94ad1269bafe32beb83860387;hp=c2eee4a6c739625d9ec19f114fadae12f854072b;hpb=6ccdf3223bd4025a8e57cec1c99633a381a9698e;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/Weather.hs b/src/TSN/XML/Weather.hs index c2eee4a..0866f6f 100644 --- a/src/TSN/XML/Weather.hs +++ b/src/TSN/XML/Weather.hs @@ -11,7 +11,9 @@ -- module TSN.XML.Weather ( dtd, + is_type1, pickle_message, + teams_are_normal, -- * Tests weather_tests, -- * WARNING: these are private but exported to silence warnings @@ -42,6 +44,18 @@ import Test.Tasty ( TestTree, testGroup ) import Test.Tasty.HUnit ( (@?=), testCase ) import Text.XML.HXT.Core ( PU, + XmlTree, + (/>), + (>>>), + addNav, + descendantAxis, + filterAxis, + followingSiblingAxis, + hasName, + readDocument, + remNav, + runLA, + runX, xp8Tuple, xp9Tuple, xpAttr, @@ -65,6 +79,7 @@ import Xml ( FromXml(..), FromXmlFk(..), ToDb(..), + parse_opts, pickle_unpickle, unpickleable, unsafe_unpickle ) @@ -173,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 { @@ -234,7 +250,7 @@ data WeatherDetailedWeatherListingItem = db_home_team :: String, db_weather_type :: Int, db_description :: String, - db_temp_adjust :: String, + db_temp_adjust :: Maybe String, db_temperature :: Int } @@ -250,7 +266,7 @@ data WeatherDetailedWeatherListingItemXml = xml_home_team :: String, xml_weather_type :: Int, xml_description :: String, - xml_temp_adjust :: String, + xml_temp_adjust :: Maybe String, xml_temperature :: Int } deriving (Eq, Show) @@ -384,6 +400,61 @@ mkPersist tsn_codegen_config [groundhog| |] + +-- | 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 @@ -477,7 +548,7 @@ pickle_item = (xpElem "HomeTeam" xpText) (xpElem "WeatherType" xpInt) (xpElem "Description" xpText) - (xpElem "TempAdjust" xpText) + (xpElem "TempAdjust" (xpOption xpText)) (xpElem "Temperature" xpInt) where from_tuple = uncurryN WeatherDetailedWeatherListingItemXml @@ -553,7 +624,9 @@ weather_tests = "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 @@ -619,3 +692,53 @@ test_on_delete_cascade = testGroup "cascading delete tests" 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