X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FJFile.hs;h=d434220a6ee8788e1b3c4a49249c8587b2e463c9;hb=f64dd9351c77beb0333bef5c38058194439a0254;hp=35768163cbe21eef1a5fbb01e98b973abc605d81;hpb=9e66d3ffc3ff1141c97911a6a58fc804cf16f423;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/JFile.hs b/src/TSN/XML/JFile.hs index 3576816..d434220 100644 --- a/src/TSN/XML/JFile.hs +++ b/src/TSN/XML/JFile.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE QuasiQuotes #-} @@ -27,6 +28,7 @@ import Data.List ( intercalate ) import Data.String.Utils ( split ) import Data.Time ( UTCTime(..) ) import Data.Tuple.Curry ( uncurryN ) +import qualified Data.Vector.HFixed as H ( HVector, convert ) import Database.Groundhog ( countAll, deleteAll, @@ -39,6 +41,7 @@ import Database.Groundhog.Sqlite ( withSqliteConn ) import Database.Groundhog.TH ( groundhog, mkPersist ) +import qualified GHC.Generics as GHC ( Generic ) import Test.Tasty ( TestTree, testGroup ) import Test.Tasty.HUnit ( (@?=), testCase ) import Text.XML.HXT.Core ( @@ -67,17 +70,20 @@ import TSN.Picklers ( xp_date, xp_date_padded, xp_datetime, - xp_time, + xp_tba_time, xp_time_dots, xp_time_stamp ) -import TSN.Team ( Team(..), HTeam(..), VTeam(..) ) +import TSN.Team ( + FromXmlFkTeams(..), + HTeam(..), + Team(..), + VTeam(..) ) import TSN.XmlImport ( XmlImport(..), XmlImportFkTeams(..) ) import Xml ( Child(..), FromXml(..), - FromXmlFkTeams(..), ToDb(..), pickle_unpickle, unpickleable, @@ -118,7 +124,12 @@ data Message = xml_sport :: String, xml_gamelist :: JFileGameListXml, xml_time_stamp :: UTCTime } - deriving (Eq, Show) + deriving (Eq, GHC.Generic, Show) + + +-- | For 'H.convert'. +-- +instance H.HVector Message instance ToDb Message where @@ -196,6 +207,7 @@ data JFileGameStatus = deriving (Eq, Show) + -- | Database representation of a \ contained within a -- \, and, implicitly, a \. -- @@ -211,7 +223,7 @@ data JFileGame = db_schedule_id :: Int, db_odds_info :: JFileGameOddsInfo, db_season_type :: Maybe String, - db_game_time :: UTCTime, + db_game_time :: Maybe UTCTime, db_vleague :: Maybe String, db_hleague :: Maybe String, db_vscore :: Int, @@ -233,7 +245,7 @@ data JFileGameXml = xml_odds_info :: JFileGameOddsInfo, xml_season_type :: Maybe String, xml_game_date :: UTCTime, - xml_game_time :: UTCTime, + xml_game_time :: Maybe UTCTime, xml_vteam :: VTeam, xml_vleague :: Maybe String, xml_hteam :: HTeam, @@ -242,7 +254,12 @@ data JFileGameXml = xml_hscore :: Int, xml_time_remaining :: Maybe String, xml_game_status :: JFileGameStatus } - deriving (Eq, Show) + deriving (Eq, GHC.Generic, Show) + + +-- | For 'H.convert'. +-- +instance H.HVector JFileGameXml -- * JFileGameListXml @@ -294,11 +311,15 @@ instance FromXmlFkTeams JFileGameXml where db_time_remaining = xml_time_remaining, db_game_status = xml_game_status } where - -- | Make the database \"game time\" from the XML - -- date/time. Simply take the day part from one and the time - -- from the other. - -- - make_game_time d t = UTCTime (utctDay d) (utctDayTime t) + -- | Construct the database game time from the XML \ + -- and \ elements. The \ elements + -- sometimes have a value of \"TBA\"; in that case, we don't + -- want to pretend that we know the time by setting it to + -- e.g. midnight, so instead we make the entire date/time + -- Nothing. + make_game_time :: UTCTime -> Maybe UTCTime -> Maybe UTCTime + make_game_time _ Nothing = Nothing + make_game_time d (Just t) = Just $ UTCTime (utctDay d) (utctDayTime t) -- | This allows us to insert the XML representation @@ -353,8 +374,8 @@ mkPersist tsn_codegen_config [groundhog| - name: db_status dbName: status -# Many of the JFileGameOddsInfo fields are redundant and have -# been left out. + # Many of the JFileGameOddsInfo fields are redundant and have + # been left out. - embedded: JFileGameOddsInfo fields: - name: db_list_date @@ -414,7 +435,7 @@ mkPersist tsn_codegen_config [groundhog| pickle_message :: PU Message pickle_message = xpElem "message" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, H.convert) $ xp6Tuple (xpElem "XML_File_ID" xpInt) (xpElem "heading" xpText) (xpElem "category" xpText) @@ -423,12 +444,7 @@ pickle_message = (xpElem "time_stamp" xp_time_stamp) where from_tuple = uncurryN Message - to_tuple m = (xml_xml_file_id m, - xml_heading m, - xml_category m, - xml_sport m, - xml_gamelist m, - xml_time_stamp m) + pickle_gamelist :: PU JFileGameListXml pickle_gamelist = @@ -444,13 +460,13 @@ pickle_gamelist = pickle_game :: PU JFileGameXml pickle_game = xpElem "game" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, H.convert) $ xp14Tuple (xpElem "game_id" xpInt) (xpElem "schedule_id" xpInt) pickle_odds_info (xpElem "seasontype" (xpOption xpText)) (xpElem "Game_Date" xp_date_padded) - (xpElem "Game_Time" xp_time) + (xpElem "Game_Time" xp_tba_time) pickle_away_team (xpOption $ xpElem "vleague" xpText) pickle_home_team @@ -461,25 +477,12 @@ pickle_game = pickle_status where from_tuple = uncurryN JFileGameXml - to_tuple m = (xml_game_id m, - xml_schedule_id m, - xml_odds_info m, - xml_season_type m, - xml_game_date m, - xml_game_time m, - xml_vteam m, - xml_vleague m, - xml_hteam m, - xml_hleague m, - xml_vscore m, - xml_hscore m, - xml_time_remaining m, - xml_game_status m) + pickle_odds_info :: PU JFileGameOddsInfo pickle_odds_info = xpElem "Odds_Info" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, to_tuple') $ xp19Tuple (xpElem "ListDate" (xpOption xp_date)) (xpElem "HomeTeamID" (xpOption xpText)) (xpElem "AwayTeamID" (xpOption xpText)) @@ -505,21 +508,21 @@ pickle_odds_info = where notes = intercalate "\n" [n1,n2,n3,n4,n5] - to_tuple o = (db_list_date o, - db_info_home_team_id o, - db_info_away_team_id o, - db_home_abbr o, - db_away_abbr o, - db_home_team_name o, - db_away_team_name o, - db_home_starter o, - db_away_starter o, - db_game_date o, - db_home_game_key o, - db_away_game_key o, - db_current_timestamp o, - db_live o, - n1,n2,n3,n4,n5) + to_tuple' o = (db_list_date o, + db_info_home_team_id o, + db_info_away_team_id o, + db_home_abbr o, + db_away_abbr o, + db_home_team_name o, + db_away_team_name o, + db_home_starter o, + db_away_starter o, + db_game_date o, + db_home_game_key o, + db_away_game_key o, + db_current_timestamp o, + db_live o, + n1,n2,n3,n4,n5) where note_lines = split "\n" (db_notes o) n1 = case note_lines of @@ -544,15 +547,13 @@ pickle_odds_info = pickle_home_team :: PU HTeam pickle_home_team = xpElem "hteam" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, to_tuple') $ xpTriple (xpAttr "teamid" xpText) -- Yeah, they're text. (xpAttr "abbr" (xpOption xpText)) -- Some are blank (xpOption xpText) -- Yup, some are nameless where from_tuple = HTeam . (uncurryN Team) - to_tuple (HTeam t) = (team_id t, - abbreviation t, - name t) + to_tuple' (HTeam t) = H.convert t -- | (Un)pickle an away team to/from the dual XML/DB representation @@ -561,28 +562,26 @@ pickle_home_team = pickle_away_team :: PU VTeam pickle_away_team = xpElem "vteam" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, to_tuple') $ xpTriple (xpAttr "teamid" xpText) -- Yeah, they're text. (xpAttr "abbr" (xpOption xpText)) -- Some are blank (xpOption xpText) -- Yup, some are nameless where from_tuple = VTeam . (uncurryN Team) - to_tuple (VTeam t) = (team_id t, - abbreviation t, - name t) + to_tuple' (VTeam t) = H.convert t pickle_status :: PU JFileGameStatus pickle_status = xpElem "status" $ - xpWrap (from_tuple, to_tuple) $ + xpWrap (from_tuple, to_tuple') $ xpPair (xpAttr "numeral" xpInt) (xpOption xpText) where from_tuple = uncurry JFileGameStatus - to_tuple s = (db_status_numeral s, - db_status s) + -- Avoid unused field warnings. + to_tuple' JFileGameStatus{..} = (db_status_numeral, db_status) -- @@ -609,7 +608,10 @@ test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests" [ check "pickle composed with unpickle is the identity" "test/xml/jfilexml.xml", check "pickle composed with unpickle is the identity (missing fields)" - "test/xml/jfilexml-missing-fields.xml" ] + "test/xml/jfilexml-missing-fields.xml", + + check "pickle composed with unpickle is the identity (TBA game time)" + "test/xml/jfilexml-tba-game-time.xml"] where check desc path = testCase desc $ do (expected, actual) <- pickle_unpickle pickle_message path @@ -622,8 +624,12 @@ test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests" test_unpickle_succeeds :: TestTree test_unpickle_succeeds = testGroup "unpickle tests" [ check "unpickling succeeds" "test/xml/jfilexml.xml", + check "unpickling succeeds (missing fields)" - "test/xml/jfilexml-missing-fields.xml" ] + "test/xml/jfilexml-missing-fields.xml", + + check "unpickling succeeds (TBA game time)" + "test/xml/jfilexml-tba-game-time.xml" ] where check desc path = testCase desc $ do actual <- unpickleable path pickle_message @@ -640,10 +646,15 @@ test_on_delete_cascade :: TestTree test_on_delete_cascade = testGroup "cascading delete tests" [ check "deleting auto_racing_results deletes its children" "test/xml/jfilexml.xml" - 20, + 20, -- teams + check "deleting auto_racing_results deletes its children (missing fields)" "test/xml/jfilexml-missing-fields.xml" - 44 ] + 44, + + check "deleting auto_racing_results deletes its children (TBA game time)" + "test/xml/jfilexml-tba-game-time.xml" + 8 ] where check desc path expected = testCase desc $ do results <- unsafe_unpickle path pickle_message