1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE RecordWildCards #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
8 -- | SportInfo 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 -- This is almost completely redundant with "TSN.XML.GameInfo", but
13 -- the redundancy is necessary: we need separate message types so
14 -- that we can have separate 'DbImport' instances. It would take
15 -- more code/work to abstract (if it's even possible) than to
18 module TSN.XML.SportInfo (
22 -- * WARNING: these are private but exported to silence warnings
23 SportInfoConstructor(..) )
27 import Data.Either ( rights )
28 import Data.String.Utils ( replace )
29 import Data.Time.Clock ( UTCTime )
30 import Database.Groundhog (
35 silentMigrationLogger )
36 import Database.Groundhog.Generic ( runDbConn )
37 import Database.Groundhog.Sqlite ( withSqliteConn )
38 import Database.Groundhog.TH (
42 import Test.Tasty ( TestTree, testGroup )
43 import Test.Tasty.HUnit ( (@?=), testCase )
44 import Text.XML.HXT.Core ( XmlTree )
45 import Text.XML.HXT.DOM.ShowXml ( xshow )
56 parse_xml_time_stamp )
57 import Xml ( unsafe_read_document )
60 -- | The DTDs for everything that we consider \"Sport Info.\"
64 [ "CBASK_3PPctXML.dtd",
65 "Cbask_All_Tourn_Teams_XML.dtd",
66 "CBASK_AssistsXML.dtd",
67 "Cbask_Awards_XML.dtd",
68 "CBASK_BlocksXML.dtd",
69 "Cbask_Conf_Standings_XML.dtd",
70 "Cbask_DivII_III_Indv_Stats_XML.dtd",
71 "Cbask_DivII_Team_Stats_XML.dtd",
72 "Cbask_DivIII_Team_Stats_XML.dtd",
76 "Cbask_Indv_Scoring_XML.dtd",
77 "CBASK_MinutesXML.dtd",
78 "Cbask_Polls_XML.dtd",
79 "CBASK_ReboundsXML.dtd",
80 "CBASK_ScoringLeadersXML.dtd",
81 "Cbask_Team_ThreePT_Made_XML.dtd",
82 "Cbask_Team_ThreePT_PCT_XML.dtd",
83 "Cbask_Team_Win_Pct_XML.dtd",
84 "Cbask_Top_Twenty_Five_XML.dtd",
85 "CBASK_TopTwentyFiveResult_XML.dtd",
86 "Cbask_Tourn_Awards_XML.dtd",
87 "Cbask_Tourn_Champs_XML.dtd",
88 "Cbask_Tourn_Indiv_XML.dtd",
89 "Cbask_Tourn_Leaders_XML.dtd",
90 "Cbask_Tourn_MVP_XML.dtd",
91 "Cbask_Tourn_Records_XML.dtd",
92 "LeagueScheduleXML.dtd",
94 "Minor_Baseball_League_Leaders_XML.dtd",
95 "Minor_Baseball_Standings_XML.dtd",
96 "Minor_Baseball_Transactions_XML.dtd",
97 "mlbbattingavgxml.dtd",
98 "mlbdoublesleadersxml.dtd",
99 "MLBGamesPlayedXML.dtd",
101 "MLBHitByPitchXML.dtd",
102 "mlbhitsleadersxml.dtd",
103 "mlbhomerunsxml.dtd",
105 "MLBIntWalksXML.dtd",
107 "mlbonbasepctxml.dtd",
109 "MLBPlateAppsXML.dtd",
111 "mlbrunsleadersxml.dtd",
112 "MLBSacFliesXML.dtd",
113 "MLBSacrificesXML.dtd",
114 "MLBSBSuccessXML.dtd",
115 "mlbsluggingpctxml.dtd",
117 "mlbstandxml_preseason.dtd",
118 "mlbstolenbasexml.dtd",
119 "mlbtotalbasesleadersxml.dtd",
120 "mlbtriplesleadersxml.dtd",
121 "MLBWalkRateXML.dtd",
122 "mlbwalksleadersxml.dtd",
123 "MLBXtraBaseHitsXML.dtd",
124 "MLB_ERA_Leaders.dtd",
125 "MLB_Fielding_XML.dtd",
126 "MLB_Pitching_Appearances_Leaders.dtd",
127 "MLB_Pitching_Balks_Leaders.dtd",
128 "MLB_Pitching_CG_Leaders.dtd",
129 "MLB_Pitching_ER_Allowed_Leaders.dtd",
130 "MLB_Pitching_Hits_Allowed_Leaders.dtd",
131 "MLB_Pitching_Hit_Batters_Leaders.dtd",
132 "MLB_Pitching_HR_Allowed_Leaders.dtd",
133 "MLB_Pitching_IP_Leaders.dtd",
134 "MLB_Pitching_Runs_Allowed_Leaders.dtd",
135 "MLB_Pitching_Saves_Leaders.dtd",
136 "MLB_Pitching_Shut_Outs_Leaders.dtd",
137 "MLB_Pitching_Starts_Leaders.dtd",
138 "MLB_Pitching_Strike_Outs_Leaders.dtd",
139 "MLB_Pitching_Walks_Leaders.dtd",
140 "MLB_Pitching_WHIP_Leaders.dtd",
141 "MLB_Pitching_Wild_Pitches_Leaders.dtd",
142 "MLB_Pitching_Win_Percentage_Leaders.dtd",
143 "MLB_Pitching_WL_Leaders.dtd",
144 "NBA_Team_Stats_XML.dtd",
150 "nbadivisionsxml.dtd",
155 "NBAReboundsXML.dtd",
159 "nbateamleadersxml.dtd",
160 "nbatripledoublexml.dtd",
161 "NBATurnoversXML.dtd",
162 "NCAA_Conference_Schedule_XML.dtd",
163 "nflfirstdownxml.dtd",
164 "NFLFumbleLeaderXML.dtd",
165 "NFLGrassTurfDomeOutsideXML.dtd",
166 "NFLGiveTakeXML.dtd",
167 "NFLInside20XML.dtd",
168 "NFLInterceptionLeadersXML.dtd",
169 "NFLKickoffsXML.dtd",
170 "NFLMondayNightXML.dtd",
171 "NFLPassingLeadersXML.dtd",
172 "NFLPassLeadXML.dtd",
173 "NFLQBStartsXML.dtd",
174 "NFLReceivingLeadersXML.dtd",
175 "NFLRushingLeadersXML.dtd",
176 "NFLSackLeadersXML.dtd",
178 "NFLTeamRankingsXML.dtd",
179 "NFLTopPerformanceXML.dtd",
180 "NFLTotalYardageXML.dtd",
181 "NFL_KickingLeaders_XML.dtd",
182 "NFL_NBA_Draft_XML.dtd",
183 "NFL_Roster_XML.dtd",
184 "NFLTackleFFLeadersXML.dtd",
185 "NFL_Team_Stats_XML.dtd",
186 "Transactions_XML.dtd",
187 "Weekly_Sched_XML.dtd",
188 "WNBA_Team_Leaders_XML.dtd",
190 "WNBAAssistsXML.dtd",
195 "WNBAMinutesXML.dtd",
196 "WNBAReboundsXML.dtd",
197 "WNBAScorersXML.dtd",
200 "WNBATurnoversXML.dtd" ]
203 -- | This serves as both the database and XML representation of a
204 -- SportInfo \<message\>.
210 time_stamp :: UTCTime,
215 -- | Attempt to parse a 'SportInfo' from an 'XmlTree'. If we cannot,
216 -- we fail with an error message.
218 parse_xml :: String -> XmlTree -> Either ParseError SportInfo
219 parse_xml dtdname xmltree = do
220 xmlfid <- parse_xmlfid xmltree
221 timestamp <- parse_xml_time_stamp xmltree
222 message <- parse_message xmltree
223 return $ SportInfo dtdname xmlfid timestamp (xshow [message])
230 instance DbImport SportInfo where
232 run_dbmigrate $ migrate (undefined :: SportInfo)
234 -- | We import a 'SportInfo' by inserting the whole thing at
235 -- once. Nothing fancy going on here.
238 return ImportSucceeded
241 -- | The database schema for SportInfo is trivial; all we need is for
242 -- the XML_File_ID to be unique.
244 mkPersist defaultCodegenConfig [groundhog|
250 - name: unique_sport_info
252 # Prevent multiple imports of the same message.
253 fields: [xml_file_id]
261 -- | A list of all tests for this module.
263 sport_info_tests :: TestTree
268 test_parse_xml_succeeds,
269 test_dbimport_succeeds ]
272 -- | Make sure the accessors work and that we can parse one file. Ok,
273 -- so the real point of this is to make the unused fields (dtd, xml,
274 -- ...) warning go away without having to mangle the groundhog code.
276 test_accessors :: TestTree
277 test_accessors = testCase "we can access a parsed sport_info" $ do
278 xmltree <- unsafe_read_document "test/xml/sportinfo/wnbastandxml.xml"
279 let Right t = parse_xml "wnbastandxml.dtd" xmltree
281 let ex1 = "wnbastandxml.dtd"
282 let a2 = xml_file_id t
284 let a3 = show $ time_stamp t
285 let ex3 = "2009-09-27 19:50:00 UTC"
286 let a4 = take 9 (xml t)
287 let ex4 = "<message>"
288 let actual = (a1,a2,a3,a4)
289 let expected = (ex1,ex2,ex3,ex4)
293 -- | Sample XML documents for SportInfo types.
295 sport_info_test_files :: [FilePath]
296 sport_info_test_files =
297 map (change_suffix . add_path) dtds
299 add_path = ("test/xml/sportinfo/" ++ )
300 change_suffix = replace ".dtd" ".xml"
303 -- | Make sure we can parse every element of 'sport_info_test_files'.
305 test_parse_xml_succeeds :: TestTree
306 test_parse_xml_succeeds =
307 testGroup "parse_xml" $ map check sport_info_test_files
309 check t = testCase t $ do
310 x <- unsafe_read_document t
311 let result = parse_xml "dummy" x
312 let actual = case result of -- isRight appears in base-4.7
319 -- | Ensure that each element of 'sport_info_test_files' can be imported
320 -- by counting the total number of database records (after
321 -- importing) and comparing it against the length of
322 -- 'sport_info_test_files'.
324 test_dbimport_succeeds :: TestTree
325 test_dbimport_succeeds = testCase "dbimport succeeds" $ do
326 xmltrees <- mapM unsafe_read_document sport_info_test_files
327 let msgs = rights $ map (parse_xml "dummy") xmltrees
328 actual <- withSqliteConn ":memory:" $ runDbConn $ do
329 runMigration silentMigrationLogger $
330 migrate (undefined :: SportInfo)
332 countAll (undefined :: SportInfo)
336 expected = length sport_info_test_files