1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE RecordWildCards #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
8 -- | GameInfo represents a collection of DTDs that we don't really
9 -- handle but want to make available. The raw XML gets stored in the
10 -- database along with the XML_File_ID, but we don't parse any of it.
12 -- See also: TSN.XML.SportInfo
14 module TSN.XML.GameInfo (
18 -- * WARNING: these are private but exported to silence warnings
19 GameInfoConstructor(..) )
23 import Data.Either ( rights )
24 import Data.Time.Clock ( UTCTime )
25 import Database.Groundhog (
29 silentMigrationLogger )
30 import Database.Groundhog.Generic ( runDbConn )
31 import Database.Groundhog.Sqlite ( withSqliteConn )
32 import Database.Groundhog.TH (
35 import Test.Tasty ( TestTree, testGroup )
36 import Test.Tasty.HUnit ( (@?=), testCase )
37 import Text.XML.HXT.Core ( XmlTree )
38 import Text.XML.HXT.DOM.ShowXml ( xshow )
41 import TSN.Codegen ( tsn_codegen_config )
49 parse_xml_time_stamp )
50 import TSN.XmlImport ( XmlImport(..) )
54 unsafe_read_document )
57 -- | The DTDs for everything that we consider "Game Info."
59 -- TODO: This is the list from the old implementation. We need to
60 -- make sure that we are really receiving XML for these DTDs
61 -- (i.e. the names are correct).
65 [ "CBASK_Lineup_XML.dtd", -- missing DTD
66 "cbaskpreviewxml.dtd", -- missing DTD
67 "cflpreviewxml.dtd", -- missing DTD
68 "Matchup_NBA_NHL_XML.dtd", -- missing DTD
70 "MLB_Gaming_Matchup_XML.dtd",
71 "MLB.dtd", -- missing DTD
73 "MLB_Matchup_XML.dtd",
74 "MLS_Preview_XML.dtd",
75 "NBA_Gaming_Matchup_XML.dtd",
76 "NBA.dtd", -- missing DTD
77 "NBA_Playoff_Matchup_XML.dtd",
80 "NCAA_FB_Preview_XML.dtd", -- missing DTD
81 "nflpreviewxml.dtd", -- missing DTD
82 "NFL_NCAA_FB_Matchup_XML.dtd", -- missing DTD
85 "WorldBaseballPreviewXML.dtd" -- missing DTD
89 -- | XML representation of a GameInfo \<message\>.
94 xml_xml_file_id :: Int,
95 xml_time_stamp :: UTCTime,
100 -- | Attempt to parse a 'Message' from an 'XmlTree'. If we cannot,
101 -- we fail with an error message.
103 parse_xml :: String -> XmlTree -> Either String Message
104 parse_xml dtdname xmltree = do
105 xmlfid <- parse_xmlfid xmltree
106 timestamp <- parse_xml_time_stamp xmltree
107 message <- parse_message xmltree
108 return $ Message dtdname (fromInteger xmlfid) timestamp (xshow [message])
111 -- | Database representation of a 'Message'.
116 db_xml_file_id :: Int,
117 db_time_stamp :: UTCTime,
121 instance ToDb Message where
122 -- | The database analogue of a 'Message' is an 'GameInfo'.
123 type Db Message = GameInfo
125 instance FromXml Message where
126 -- | The XML to DB conversion is trivial here.
128 from_xml Message{..} = GameInfo {
130 db_xml_file_id = xml_xml_file_id,
131 db_time_stamp = xml_time_stamp,
135 -- | This allows us to insert the XML representation 'Message'
138 instance XmlImport Message
145 instance DbImport Message where
147 run_dbmigrate $ migrate (undefined :: GameInfo)
149 -- | We import a 'Message' by inserting the whole thing at
150 -- once. Nothing fancy going on here.
153 return ImportSucceeded
156 -- | The database schema for GameInfo is trivial; all we need is for
157 -- the XML_File_ID to be unique.
159 mkPersist tsn_codegen_config [groundhog|
164 - name: unique_game_info
166 # Prevent multiple imports of the same message.
167 fields: [db_xml_file_id]
175 -- | A list of all tests for this module.
177 gameinfo_tests :: TestTree
181 [ test_parse_xml_succeeds,
182 test_dbimport_succeeds ]
185 -- | Sample XML documents for GameInfo types.
187 gameinfo_test_files :: [FilePath]
188 gameinfo_test_files =
189 [ "test/xml/gameinfo/MLB_Gaming_Matchup_XML.xml",
190 "test/xml/gameinfo/MLB_Lineup_XML.xml",
191 "test/xml/gameinfo/MLB_Matchup_XML.xml",
192 "test/xml/gameinfo/mlbpreviewxml.xml",
193 "test/xml/gameinfo/MLS_Preview_XML.xml",
194 "test/xml/gameinfo/NBA_Gaming_Matchup_XML.xml",
195 "test/xml/gameinfo/NBALineupXML.xml",
196 "test/xml/gameinfo/NBA_Playoff_Matchup_XML.xml",
197 "test/xml/gameinfo/nbapreviewxml.xml",
198 "test/xml/gameinfo/nhlpreviewxml.xml",
199 "test/xml/gameinfo/recapxml.xml" ]
202 -- | Make sure we can parse every element of 'gameinfo_test_files'.
204 test_parse_xml_succeeds :: TestTree
205 test_parse_xml_succeeds =
206 testGroup "parse_xml" $ map check gameinfo_test_files
208 check t = testCase t $ do
209 x <- unsafe_read_document t
210 let result = parse_xml "dummy" x
211 let actual = case result of -- isRight appears in base-4.7
218 -- | Ensure that each element of 'gameinfo_test_files' can be imported
219 -- by counting the total number of database records (after
220 -- importing) and comparing it against the length of
221 -- 'gameinfo_test_files'.
223 test_dbimport_succeeds :: TestTree
224 test_dbimport_succeeds = testCase "dbimport succeeds" $ do
225 xmltrees <- mapM unsafe_read_document gameinfo_test_files
226 let msgs = rights $ map (parse_xml "dummy") xmltrees
227 actual <- withSqliteConn ":memory:" $ runDbConn $ do
228 runMigration silentMigrationLogger $ do
229 migrate (undefined :: GameInfo)
231 countAll (undefined :: GameInfo)
235 expected = length gameinfo_test_files