1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
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 (
19 injuries_detail_tests,
20 -- * WARNING: these are private but exported to silence warnings
21 InjuriesDetailConstructor(..),
22 InjuriesDetailListingConstructor(..),
23 InjuriesDetailListingPlayerListingConstructor(..) )
27 import Control.Monad ( forM_ )
28 import Data.Time ( UTCTime )
29 import Data.Tuple.Curry ( uncurryN )
30 import qualified Data.Vector.HFixed as H (
36 import Database.Groundhog (
41 import Database.Groundhog.Generic ( runDbConn, runMigrationSilent )
42 import Database.Groundhog.Sqlite ( withSqliteConn )
43 import Database.Groundhog.TH (
46 import qualified GHC.Generics as GHC ( Generic )
47 import Test.Tasty ( TestTree, testGroup )
48 import Test.Tasty.HUnit ( (@?=), testCase )
49 import Text.XML.HXT.Core (
63 import TSN.Codegen ( tsn_codegen_config )
64 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
65 import TSN.Picklers( xp_date, xp_time_stamp )
66 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
78 -- | The DTD to which this module corresponds. Used to invoke dbimport.
81 dtd = "Injuries_Detail_XML.dtd"
89 -- * InjuriesDetail/Message
92 -- | XML representation of the top-level \<message\> element. These
93 -- are not stored; the data type is used only for parsing.
97 xml_xml_file_id :: Int,
98 xml_heading :: String,
99 xml_category :: String,
101 xml_listings :: [InjuriesDetailListingXml],
102 xml_time_stamp :: UTCTime }
103 deriving (Eq, GHC.Generic, Show)
106 -- | For 'H.convert'.
108 instance H.HVector Message
111 -- | Database representation of a 'Message'.
113 data InjuriesDetail =
115 db_xml_file_id :: Int,
117 db_time_stamp :: UTCTime }
120 instance ToDb Message where
121 -- | The database representation of a 'Message' is an
124 type Db Message = InjuriesDetail
126 instance FromXml Message where
127 -- | To convert a 'Message' into an 'InjuriesDetail', we simply drop
130 from_xml Message{..} =
132 db_xml_file_id = xml_xml_file_id,
133 db_sport = xml_sport,
134 db_time_stamp = xml_time_stamp }
137 -- | This allows us to insert the XML representation 'Message'
140 instance XmlImport Message
144 -- * InjuriesDetailListing/InjuriesDetailListingXml
146 -- | Database representation of a \<Listing\> element. It has a
147 -- foreign key pointing to its parent 'InjuriesDetail', and does not
148 -- contain the list of 'xml_player_listings' (which get their own
151 data InjuriesDetailListing =
152 InjuriesDetailListing {
153 db_injuries_detail_id :: DefaultKey InjuriesDetail,
154 db_team_id :: String,
155 db_full_name :: String }
158 -- | XML incarnation of a \<Listing\> element. We don't store these;
159 -- the data type is used only for parsing.
161 data InjuriesDetailListingXml =
162 InjuriesDetailListingXml {
163 xml_team_id :: String, -- ^ TeamIDs are (apparently) three
164 -- characters long and not necessarily
167 xml_full_name :: String, -- ^ Team full name
168 xml_player_listings :: [InjuriesDetailListingPlayerListingXml] }
169 deriving (Eq, GHC.Generic, Show)
172 -- | For 'H.convert'.
174 instance H.HVector InjuriesDetailListingXml
177 instance ToDb InjuriesDetailListingXml where
178 -- | The database analogue of an 'InjuriesDetailListingXml' is a
179 -- 'InjuriesDetailListing'.
180 type Db InjuriesDetailListingXml = InjuriesDetailListing
183 instance Child InjuriesDetailListingXml where
184 -- | Each 'InjuriesDetailListingXml' is contained in an
186 type Parent InjuriesDetailListingXml = InjuriesDetail
189 instance FromXmlFk InjuriesDetailListingXml where
190 -- | Construct a 'InjuriesDetailListing' from a
191 -- 'InjuriesDetailListingXml' and a foreign key to a
194 from_xml_fk fk InjuriesDetailListingXml{..} =
195 InjuriesDetailListing {
196 db_injuries_detail_id = fk,
197 db_team_id = xml_team_id,
198 db_full_name = xml_full_name }
200 -- | This allows us to insert the XML representation
201 -- 'InjuriesDetailListingXml' directly.
203 instance XmlImportFk InjuriesDetailListingXml
206 -- * InjuriesDetailListingPlayerListing
208 -- | XML representation of a \<PlayerListing\>, the main type of
209 -- element contains in Injuries_Detail_XML messages. The leading
210 -- underscores prevent unused field warnings.
212 data InjuriesDetailListingPlayerListingXml =
213 InjuriesDetailListingPlayerListingXml {
214 _xml_player_team_id :: String, -- ^ TeamIDs are (apparently) three
215 -- characters long and not
216 -- necessarily numeric. Postgres
217 -- imposes no performance penalty
218 -- on a lengthless text field, so
219 -- we ignore the likely upper
220 -- bound of three characters.
221 -- We add the \"player\" to avoid conflict
222 -- with 'InjuriesDetailListingXml'.
223 _xml_player_id :: Int,
224 _xml_date :: UTCTime,
227 _xml_injury :: String,
228 _xml_status :: String,
229 _xml_fantasy :: Maybe String, -- ^ Nobody knows what this is.
230 _xml_injured :: Bool,
231 _xml_type :: String }
232 deriving (Eq, GHC.Generic, Show)
235 -- | For 'H.convert'.
237 instance H.HVector InjuriesDetailListingPlayerListingXml
240 -- | Database representation of a
241 -- 'InjuriesDetailListingPlayerListingXml'. We drop the team_id
242 -- because it's redundant. The leading underscores prevent unused
245 data InjuriesDetailListingPlayerListing =
246 InjuriesDetailListingPlayerListing {
247 _db_injuries_detail_listings_id :: DefaultKey InjuriesDetailListing,
248 _db_player_id :: Int,
252 _db_injury :: String,
253 _db_status :: String,
254 _db_fantasy :: Maybe String, -- ^ Nobody knows what this is.
257 deriving ( GHC.Generic )
259 -- | For 'H.cons', 'H.tail', etc.
261 instance H.HVector InjuriesDetailListingPlayerListing
264 instance ToDb InjuriesDetailListingPlayerListingXml where
265 -- | The DB analogue of a 'InjuriesDetailListingPlayerListingXml' is
266 -- 'InjuriesDetailListingPlayerListing'.
267 type Db InjuriesDetailListingPlayerListingXml =
268 InjuriesDetailListingPlayerListing
271 instance Child InjuriesDetailListingPlayerListingXml where
272 -- | Each 'InjuriesDetailListingPlayerListingXml' is contained in an
273 -- 'InjuriesDetailListing'.
275 type Parent InjuriesDetailListingPlayerListingXml = InjuriesDetailListing
278 instance FromXmlFk InjuriesDetailListingPlayerListingXml where
279 -- | To construct a 'InjuriesDetailListingPlayerListing' from a
280 -- 'InjuriesDetailListingPlayerListingXml' we need to supply a
281 -- foreign key to an 'InjuriesDetailListing' after dropping the
282 -- '_xml_player_team_id'.
284 -- The 'H.asCVec' trick allows type inference to proceed in the
285 -- middle of two different magics.
287 from_xml_fk fk = (H.cons fk) . H.asCVec . H.tail
290 -- | This lets us insert the XML representation
291 -- 'InjuriesDetailListingPlayerListingXml' directly.
293 instance XmlImportFk InjuriesDetailListingPlayerListingXml
300 instance DbImport Message where
303 migrate (undefined :: InjuriesDetail)
304 migrate (undefined :: InjuriesDetailListing)
305 migrate (undefined :: InjuriesDetailListingPlayerListing)
307 -- | To import a 'Message', we import all of its
308 -- 'InjuriesDetailListingPlayerListingXml's, which we have to dig
309 -- out of its 'Listing's.
312 msg_id <- insert_xml msg
314 forM_ (xml_listings msg) $ \listing -> do
315 l_id <- insert_xml_fk msg_id listing
316 mapM_ (insert_xml_fk_ l_id) (xml_player_listings listing)
318 return ImportSucceeded
321 mkPersist tsn_codegen_config [groundhog|
322 - entity: InjuriesDetail
323 dbName: injuries_detail
325 - name: InjuriesDetail
327 - name: unique_injuries_detail
329 # Prevent multiple imports of the same message.
330 fields: [db_xml_file_id]
332 - entity: InjuriesDetailListing
333 dbName: injuries_detail_listings
335 - name: InjuriesDetailListing
337 - name: db_injuries_detail_id
341 - entity: InjuriesDetailListingPlayerListing
342 dbName: injuries_detail_listings_player_listings
344 - name: InjuriesDetailListingPlayerListing
346 - name: _db_injuries_detail_listings_id
357 -- | Convert 'InjuriesDetailListingPlayerListingXml's to/from XML.
359 pickle_player_listing :: PU InjuriesDetailListingPlayerListingXml
360 pickle_player_listing =
361 xpElem "PlayerListing" $
362 xpWrap (from_tuple, H.convert) $
363 xp10Tuple (xpElem "TeamID" xpText)
364 (xpElem "PlayerID" xpInt)
365 (xpElem "Date" xp_date)
366 (xpElem "Pos" xpText)
367 (xpElem "Name" xpText)
368 (xpElem "Injury" xpText)
369 (xpElem "Status" xpText)
370 (xpElem "Fantasy" $ xpOption xpText)
371 (xpElem "Injured" xpPrim)
372 (xpElem "Type" xpText)
374 from_tuple = uncurryN InjuriesDetailListingPlayerListingXml
377 -- | Convert 'Listing's to/from XML.
379 pickle_listing :: PU InjuriesDetailListingXml
382 xpWrap (from_tuple, H.convert) $
383 xpTriple (xpElem "TeamID" xpText)
384 (xpElem "FullName" xpText)
385 (xpList pickle_player_listing)
387 from_tuple = uncurryN InjuriesDetailListingXml
390 -- | Convert 'Message's to/from XML.
392 pickle_message :: PU Message
395 xpWrap (from_tuple, H.convert) $
396 xp6Tuple (xpElem "XML_File_ID" xpInt)
397 (xpElem "heading" xpText)
398 (xpElem "category" xpText)
399 (xpElem "sport" xpText)
400 (xpList pickle_listing)
401 (xpElem "time_stamp" xp_time_stamp)
403 from_tuple = uncurryN Message
410 -- | A list of all tests for this module.
412 injuries_detail_tests :: TestTree
413 injuries_detail_tests =
415 "InjuriesDetail tests"
416 [ test_on_delete_cascade,
417 test_pickle_of_unpickle_is_identity,
418 test_unpickle_succeeds ]
421 -- | If we unpickle something and then pickle it, we should wind up
422 -- with the same thing we started with. WARNING: success of this
423 -- test does not mean that unpickling succeeded.
425 test_pickle_of_unpickle_is_identity :: TestTree
426 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
427 [ check "pickle composed with unpickle is the identity"
428 "test/xml/Injuries_Detail_XML.xml",
430 check "pickle composed with unpickle is the identity (non-int team_id)"
431 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
433 check desc path = testCase desc $ do
434 (expected, actual) <- pickle_unpickle pickle_message path
438 -- | Make sure we can actually unpickle these things.
440 test_unpickle_succeeds :: TestTree
441 test_unpickle_succeeds = testGroup "unpickle tests"
442 [ check "unpickling succeeds"
443 "test/xml/Injuries_Detail_XML.xml",
445 check "unpickling succeeds (non-int team_id)"
446 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
448 check desc path = testCase desc $ do
449 actual <- unpickleable path pickle_message
454 -- | Make sure everything gets deleted when we delete the top-level
457 test_on_delete_cascade :: TestTree
458 test_on_delete_cascade = testGroup "cascading delete tests"
459 [ check "delete of injuries_detail deletes its children"
460 "test/xml/Injuries_Detail_XML.xml",
462 check "delete of injuries_detail deletes its children (non-int team_id)"
463 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
465 check desc path = testCase desc $ do
466 inj <- unsafe_unpickle path pickle_message
467 let a = undefined :: InjuriesDetail
468 let b = undefined :: InjuriesDetailListing
469 let c = undefined :: InjuriesDetailListingPlayerListing
470 actual <- withSqliteConn ":memory:" $ runDbConn $ do
471 runMigrationSilent $ do
477 count_a <- countAll a
478 count_b <- countAll b
479 count_c <- countAll c
480 return $ count_a + count_b + count_c