1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE RecordWildCards #-}
5 {-# LANGUAGE StandaloneDeriving #-}
6 {-# LANGUAGE TemplateHaskell #-}
7 {-# LANGUAGE TypeFamilies #-}
9 -- | Parse TSN XML for the DTD "Injuries_Detail_XML.dtd". Each
10 -- document contains a root element \<message\> that in turn
11 -- contains zero or more \<Listing\>s (note: capitalization). The
12 -- \<Listing\>s contain \<PlayerListing\>s which then contain the
15 module TSN.XML.InjuriesDetail (
18 injuries_detail_tests,
19 -- * WARNING: these are private but exported to silence warnings
20 InjuriesDetailConstructor(..),
21 InjuriesDetailListingConstructor(..),
22 InjuriesDetailListingPlayerListingConstructor(..) )
26 import Control.Monad ( forM_ )
27 import Data.Time ( UTCTime )
28 import Data.Tuple.Curry ( uncurryN )
29 import Database.Groundhog (
35 silentMigrationLogger )
36 import Database.Groundhog.Generic ( runDbConn )
37 import Database.Groundhog.Sqlite ( withSqliteConn )
38 import Database.Groundhog.TH (
41 import Test.Tasty ( TestTree, testGroup )
42 import Test.Tasty.HUnit ( (@?=), testCase )
43 import Text.XML.HXT.Core (
57 import TSN.Codegen ( tsn_codegen_config )
58 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
59 import TSN.Picklers( xp_date, xp_time_stamp )
60 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
75 -- * InjuriesDetail/Message
78 -- | XML representation of the top-level \<message\> element. These
79 -- are not stored; the data type is used only for parsing.
83 xml_xml_file_id :: Int,
84 xml_heading :: String,
85 xml_category :: String,
87 xml_listings :: [InjuriesDetailListingXml],
88 xml_time_stamp :: UTCTime }
91 -- | Database representation of a 'Message'.
95 db_xml_file_id :: Int,
97 db_time_stamp :: UTCTime }
100 instance ToDb Message where
101 -- | The database representation of a 'Message' is an
104 type Db Message = InjuriesDetail
106 instance FromXml Message where
107 -- | To convert a 'Message' into an 'InjuriesDetail', we simply drop
110 from_xml Message{..} =
112 db_xml_file_id = xml_xml_file_id,
113 db_sport = xml_sport,
114 db_time_stamp = xml_time_stamp }
117 -- | This allows us to insert the XML representation 'Message'
120 instance XmlImport Message
124 -- * InjuriesDetailListing/InjuriesDetailListingXml
126 -- | Database representation of a \<Listing\> element. It has a
127 -- foreign key pointing to its parent 'InjuriesDetail', and does not
128 -- contain the list of 'xml_player_listings' (which get their own
131 data InjuriesDetailListing =
132 InjuriesDetailListing {
133 db_injuries_detail_id :: DefaultKey InjuriesDetail,
134 db_team_id :: String,
135 db_full_name :: String }
138 -- | XML incarnation of a \<Listing\> element. We don't store these;
139 -- the data type is used only for parsing.
141 data InjuriesDetailListingXml =
142 InjuriesDetailListingXml {
143 xml_team_id :: String, -- ^ TeamIDs are (apparently) three
144 -- characters long and not necessarily
147 xml_full_name :: String, -- ^ Team full name
148 xml_player_listings :: [InjuriesDetailListingPlayerListingXml] }
151 instance ToDb InjuriesDetailListingXml where
152 -- | The database analogue of an 'InjuriesDetailListingXml' is a
153 -- 'InjuriesDetailListing'.
154 type Db InjuriesDetailListingXml = InjuriesDetailListing
156 instance FromXmlFk InjuriesDetailListingXml where
157 -- | Each 'InjuriesDetailListingXml' is contained in an
159 type Parent InjuriesDetailListingXml = InjuriesDetail
161 -- | Construct a 'InjuriesDetailListing' from a
162 -- 'InjuriesDetailListingXml' and a foreign key to a
165 from_xml_fk fk InjuriesDetailListingXml{..} =
166 InjuriesDetailListing {
167 db_injuries_detail_id = fk,
168 db_team_id = xml_team_id,
169 db_full_name = xml_full_name }
171 -- | This allows us to insert the XML representation
172 -- 'InjuriesDetailListingXml' directly.
174 instance XmlImportFk InjuriesDetailListingXml
177 -- * InjuriesDetailListingPlayerListing
179 -- | XML representation of a \<PlayerListing\>, the main type of
180 -- element contains in Injuries_Detail_XML messages.
182 data InjuriesDetailListingPlayerListingXml =
183 InjuriesDetailListingPlayerListingXml {
184 xml_player_team_id :: String, -- ^ TeamIDs are (apparently) three
185 -- characters long and not
186 -- necessarily numeric. Postgres
187 -- imposes no performance penalty
188 -- on a lengthless text field, so
189 -- we ignore the likely upper
190 -- bound of three characters.
191 -- We add the \"player\" to avoid conflict
192 -- with 'InjuriesDetailListingXml'.
193 xml_player_id :: Int,
197 xml_injury :: String,
198 xml_status :: String,
199 xml_fantasy :: Maybe String, -- ^ Nobody knows what this is.
206 -- | Database representation of a
207 -- 'InjuriesDetailListingPlayerListingXml'. We drop the team_id
208 -- because it's redundant.
210 data InjuriesDetailListingPlayerListing =
211 InjuriesDetailListingPlayerListing {
212 db_injuries_detail_listings_id :: DefaultKey InjuriesDetailListing,
219 db_fantasy :: Maybe String, -- ^ Nobody knows what this is.
224 instance ToDb InjuriesDetailListingPlayerListingXml where
225 -- | The DB analogue of a 'InjuriesDetailListingPlayerListingXml' is
226 -- 'InjuriesDetailListingPlayerListing'.
227 type Db InjuriesDetailListingPlayerListingXml =
228 InjuriesDetailListingPlayerListing
230 instance FromXmlFk InjuriesDetailListingPlayerListingXml where
231 -- | Each 'InjuriesDetailListingPlayerListingXml' is contained in an
232 -- 'InjuriesDetailListing'.
234 type Parent InjuriesDetailListingPlayerListingXml = InjuriesDetailListing
236 -- | To construct a 'InjuriesDetailListingPlayerListing' from a
237 -- 'InjuriesDetailListingPlayerListingXml' we need to supply a
238 -- foreign key to an 'InjuriesDetailListing'.
240 from_xml_fk fk InjuriesDetailListingPlayerListingXml{..} =
241 InjuriesDetailListingPlayerListing {
242 db_injuries_detail_listings_id = fk,
243 db_player_id = xml_player_id,
247 db_injury = xml_injury,
248 db_status = xml_status,
249 db_fantasy = xml_fantasy,
250 db_injured = xml_injured,
253 -- | This lets us insert the XML representation
254 -- 'InjuriesDetailListingPlayerListingXml' directly.
256 instance XmlImportFk InjuriesDetailListingPlayerListingXml
263 instance DbImport Message where
266 migrate (undefined :: InjuriesDetail)
267 migrate (undefined :: InjuriesDetailListing)
268 migrate (undefined :: InjuriesDetailListingPlayerListing)
270 -- | To import a 'Message', we import all of its
271 -- 'InjuriesDetailListingPlayerListingXml's, which we have to dig
272 -- out of its 'Listing's.
275 msg_id <- insert_xml msg
277 forM_ (xml_listings msg) $ \listing -> do
278 l_id <- insert_xml_fk msg_id listing
279 mapM_ (insert_xml_fk_ l_id) (xml_player_listings listing)
281 return ImportSucceeded
284 mkPersist tsn_codegen_config [groundhog|
285 - entity: InjuriesDetail
286 dbName: injuries_detail
288 - name: InjuriesDetail
290 - name: unique_injuries_detail
292 # Prevent multiple imports of the same message.
293 fields: [db_xml_file_id]
295 - entity: InjuriesDetailListing
296 dbName: injuries_detail_listings
298 - name: InjuriesDetailListing
300 - name: db_injuries_detail_id
304 - entity: InjuriesDetailListingPlayerListing
305 dbName: injuries_detail_listings_player_listings
307 - name: InjuriesDetailListingPlayerListing
309 - name: db_injuries_detail_listings_id
320 -- | Convert 'InjuriesDetailListingPlayerListingXml's to/from XML.
322 pickle_player_listing :: PU InjuriesDetailListingPlayerListingXml
323 pickle_player_listing =
324 xpElem "PlayerListing" $
325 xpWrap (from_tuple, to_tuple) $
326 xp10Tuple (xpElem "TeamID" xpText)
327 (xpElem "PlayerID" xpInt)
328 (xpElem "Date" xp_date)
329 (xpElem "Pos" xpText)
330 (xpElem "Name" xpText)
331 (xpElem "Injury" xpText)
332 (xpElem "Status" xpText)
333 (xpElem "Fantasy" $ xpOption xpText)
334 (xpElem "Injured" xpPrim)
335 (xpElem "Type" xpText)
337 from_tuple = uncurryN InjuriesDetailListingPlayerListingXml
338 to_tuple pl = (xml_player_team_id pl,
350 -- | Convert 'Listing's to/from XML.
352 pickle_listing :: PU InjuriesDetailListingXml
355 xpWrap (from_tuple, to_tuple) $
356 xpTriple (xpElem "TeamID" xpText)
357 (xpElem "FullName" xpText)
358 (xpList pickle_player_listing)
360 from_tuple = uncurryN InjuriesDetailListingXml
361 to_tuple l = (xml_team_id l,
363 xml_player_listings l)
366 -- | Convert 'Message's to/from XML.
368 pickle_message :: PU Message
371 xpWrap (from_tuple, to_tuple) $
372 xp6Tuple (xpElem "XML_File_ID" xpInt)
373 (xpElem "heading" xpText)
374 (xpElem "category" xpText)
375 (xpElem "sport" xpText)
376 (xpList pickle_listing)
377 (xpElem "time_stamp" xp_time_stamp)
379 from_tuple = uncurryN Message
380 to_tuple m = (xml_xml_file_id m,
392 -- | A list of all tests for this module.
394 injuries_detail_tests :: TestTree
395 injuries_detail_tests =
397 "InjuriesDetail tests"
398 [ test_on_delete_cascade,
399 test_pickle_of_unpickle_is_identity,
400 test_unpickle_succeeds ]
403 -- | If we unpickle something and then pickle it, we should wind up
404 -- with the same thing we started with. WARNING: success of this
405 -- test does not mean that unpickling succeeded.
407 test_pickle_of_unpickle_is_identity :: TestTree
408 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
409 [ check "pickle composed with unpickle is the identity"
410 "test/xml/Injuries_Detail_XML.xml",
412 check "pickle composed with unpickle is the identity (non-int team_id)"
413 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
415 check desc path = testCase desc $ do
416 (expected, actual) <- pickle_unpickle pickle_message path
420 -- | Make sure we can actually unpickle these things.
422 test_unpickle_succeeds :: TestTree
423 test_unpickle_succeeds = testGroup "unpickle tests"
424 [ check "unpickling succeeds"
425 "test/xml/Injuries_Detail_XML.xml",
427 check "unpickling succeeds (non-int team_id)"
428 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
430 check desc path = testCase desc $ do
431 actual <- unpickleable path pickle_message
436 -- | Make sure everything gets deleted when we delete the top-level
439 test_on_delete_cascade :: TestTree
440 test_on_delete_cascade = testGroup "cascading delete tests"
441 [ check "delete of injuries_detail deletes its children"
442 "test/xml/Injuries_Detail_XML.xml",
444 check "delete of injuries_detail deletes its children (non-int team_id)"
445 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
447 check desc path = testCase desc $ do
448 inj <- unsafe_unpickle path pickle_message
449 let a = undefined :: InjuriesDetail
450 let b = undefined :: InjuriesDetailListing
451 let c = undefined :: InjuriesDetailListingPlayerListing
452 actual <- withSqliteConn ":memory:" $ runDbConn $ do
453 runMigration silentMigrationLogger $ do
458 -- No idea how 'delete' works, so do this instead.
459 executeRaw False "DELETE FROM injuries_detail;" []
460 count_a <- countAll a
461 count_b <- countAll b
462 count_c <- countAll c
463 return $ count_a + count_b + count_c