]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/GameInfo.hs
2830295a78d16d898989d0d45df94858c760e3f1
[dead/htsn-import.git] / src / TSN / XML / GameInfo.hs
1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE RecordWildCards #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
7
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.
11 --
12 -- See also: TSN.XML.SportInfo
13 --
14 module TSN.XML.GameInfo (
15 dtds,
16 game_info_tests,
17 parse_xml,
18 -- * WARNING: these are private but exported to silence warnings
19 GameInfoConstructor(..) )
20 where
21
22 -- System imports.
23 import Data.Either ( rights )
24 import Data.String.Utils ( replace )
25 import Data.Time.Clock ( UTCTime )
26 import Database.Groundhog (
27 countAll,
28 insert_,
29 migrate,
30 runMigration,
31 silentMigrationLogger )
32 import Database.Groundhog.Generic ( runDbConn )
33 import Database.Groundhog.Sqlite ( withSqliteConn )
34 import Database.Groundhog.TH (
35 defaultCodegenConfig,
36 groundhog,
37 mkPersist )
38 import Test.Tasty ( TestTree, testGroup )
39 import Test.Tasty.HUnit ( (@?=), testCase )
40 import Text.XML.HXT.Core ( XmlTree )
41 import Text.XML.HXT.DOM.ShowXml ( xshow )
42
43 -- Local imports.
44 import TSN.DbImport (
45 DbImport(..),
46 ImportResult(..),
47 run_dbmigrate )
48 import TSN.Parse (
49 ParseError,
50 parse_game_id,
51 parse_message,
52 parse_schedule_id,
53 parse_xmlfid,
54 parse_xml_time_stamp )
55 import Xml ( unsafe_read_document )
56
57
58 -- | The DTDs for everything that we consider \"Game Info.\"
59 --
60 -- TODO: This is the list from the old implementation. We need to
61 -- make sure that we are really receiving XML for these DTDs
62 -- (i.e. the names are correct).
63 --
64 dtds :: [String]
65 dtds =
66 [ "CBASK_Lineup_XML.dtd",
67 "cbaskpreviewxml.dtd",
68 "cflpreviewxml.dtd",
69 "Matchup_NBA_NHL_XML.dtd",
70 "mlbpreviewxml.dtd",
71 "MLB_Gaming_Matchup_XML.dtd",
72 "MLB_Lineup_XML.dtd",
73 "MLB_Matchup_XML.dtd",
74 "MLS_Preview_XML.dtd",
75 "NBA_Gaming_Matchup_XML.dtd",
76 "NBA_Playoff_Matchup_XML.dtd",
77 "NBALineupXML.dtd",
78 "nbapreviewxml.dtd",
79 "NCAA_FB_Preview_XML.dtd",
80 "nflpreviewxml.dtd",
81 "NFL_NCAA_FB_Matchup_XML.dtd",
82 "nhlpreviewxml.dtd",
83 "recapxml.dtd",
84 "WorldBaseballPreviewXML.dtd" ]
85
86
87 -- | This serves as both the database and XML representation of a
88 -- GameInfo \<message\>.
89 --
90 -- The 'game_id' and 'schedule_id' fields are foreign keys, but they
91 -- key into multiple tables and key on records which may not exist
92 -- when we import the GameInfo document. We therefore don't declare
93 -- them as foreign keys; i.e. we don't require them to point
94 -- anywhere in particular. But if they do, that's nice.
95 --
96 data GameInfo =
97 GameInfo {
98 dtd :: String,
99 xml_file_id :: Int,
100 game_id :: Maybe Int, -- ^ These are optional because they are missing
101 -- from at least the MLB_Matchup_XML.dtd documents.
102 -- They provide foreign keys into any tables storing
103 -- games with their IDs.
104
105 schedule_id :: Int, -- ^ Required foreign key into any table storing a
106 -- schedule along with its ID.
107 time_stamp :: UTCTime,
108 xml :: String }
109 deriving (Eq, Show)
110
111
112 -- | Attempt to parse a 'GameInfo' from an 'XmlTree'. If we cannot,
113 -- we fail with an error message.
114 --
115 parse_xml :: String -> XmlTree -> Either ParseError GameInfo
116 parse_xml dtdname xmltree = do
117 xmlfid <- parse_xmlfid xmltree
118 game_id <- parse_game_id xmltree
119 schedule_id <- parse_schedule_id xmltree
120 timestamp <- parse_xml_time_stamp xmltree
121 message <- parse_message xmltree
122 return $ GameInfo
123 dtdname
124 xmlfid
125 game_id
126 schedule_id
127 timestamp
128 (xshow [message])
129
130 --
131 -- * Database code
132 --
133
134 instance DbImport GameInfo where
135 dbmigrate _ =
136 run_dbmigrate $ migrate (undefined :: GameInfo)
137
138 -- | We import a 'GameInfo' by inserting the whole thing at
139 -- once. Nothing fancy going on here.
140 dbimport msg = do
141 insert_ msg
142 return ImportSucceeded
143
144
145 -- | The database schema for GameInfo is trivial; all we need is for
146 -- the XML_File_ID to be unique.
147 --
148 mkPersist defaultCodegenConfig [groundhog|
149 - entity: GameInfo
150 dbName: game_info
151 constructors:
152 - name: GameInfo
153 uniques:
154 - name: unique_game_info
155 type: constraint
156 # Prevent multiple imports of the same message.
157 fields: [xml_file_id]
158 |]
159
160
161 --
162 -- Tasty Tests
163 --
164
165 -- | A list of all tests for this module.
166 --
167 game_info_tests :: TestTree
168 game_info_tests =
169 testGroup
170 "GameInfo tests"
171 [ test_accessors,
172 test_parse_xml_succeeds,
173 test_dbimport_succeeds ]
174
175
176 -- | Make sure the accessors work and that we can parse one file. Ok,
177 -- so the real point of this is to make the unused fields (dtd, xml,
178 -- ...) warning go away without having to mangle the groundhog code.
179 --
180 test_accessors :: TestTree
181 test_accessors = testCase "we can access a parsed game_info" $ do
182 xmltree <- unsafe_read_document "test/xml/gameinfo/recapxml.xml"
183 let Right t = parse_xml "recapxml.dtd" xmltree
184 let a1 = dtd t
185 let ex1 = "recapxml.dtd"
186 let a2 = xml_file_id t
187 let ex2 = 21201550
188 let a3 = show $ time_stamp t
189 let ex3 = "2014-05-31 15:13:00 UTC"
190 let a4 = game_id t
191 let ex4 = Just 39978
192 let a5 = schedule_id t
193 let ex5 = 39978
194 let a6 = take 9 (xml t)
195 let ex6 = "<message>"
196 let actual = (a1,a2,a3,a4,a5,a6)
197 let expected = (ex1,ex2,ex3,ex4,ex5,ex6)
198 actual @?= expected
199
200
201 -- | Sample XML documents for GameInfo types.
202 --
203 game_info_test_files :: [FilePath]
204 game_info_test_files =
205 map (change_suffix . add_path) dtds
206 where
207 add_path = ("test/xml/gameinfo/" ++ )
208 change_suffix = replace ".dtd" ".xml"
209
210 -- | Make sure we can parse every element of 'game_info_test_files'.
211 --
212 test_parse_xml_succeeds :: TestTree
213 test_parse_xml_succeeds =
214 testGroup "parse_xml" $ map check game_info_test_files
215 where
216 check t = testCase t $ do
217 x <- unsafe_read_document t
218 let result = parse_xml "dummy" x
219 let actual = case result of -- isRight appears in base-4.7
220 Left _ -> False
221 Right _ -> True
222 let expected = True
223 actual @?= expected
224
225
226 -- | Ensure that each element of 'game_info_test_files' can be imported
227 -- by counting the total number of database records (after
228 -- importing) and comparing it against the length of
229 -- 'game_info_test_files'.
230 --
231 test_dbimport_succeeds :: TestTree
232 test_dbimport_succeeds = testCase "dbimport succeeds" $ do
233 xmltrees <- mapM unsafe_read_document game_info_test_files
234 let msgs = rights $ map (parse_xml "dummy") xmltrees
235 actual <- withSqliteConn ":memory:" $ runDbConn $ do
236 runMigration silentMigrationLogger $
237 migrate (undefined :: GameInfo)
238 mapM_ dbimport msgs
239 countAll (undefined :: GameInfo)
240
241 actual @?= expected
242 where
243 expected = length game_info_test_files