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