1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
8 -- | Parse TSN XML for the DTD "Injuries_Detail_XML.dtd". Each
9 -- document contains a root element \<message\> that in turn
10 -- contains zero or more \<Listing\>s (note: capitalization). The
11 -- \<Listing\>s contain \<PlayerListing\>s then contain the real
12 -- meat; everything contained in the parent \<Listing\> can also be
13 -- found within the \<PlayerListing\>s.
15 -- The player listings will be mapped to a database table called
16 -- "injuries_detail" automatically. The root "message" and "listing"
19 module TSN.XML.InjuriesDetail (
20 injuries_detail_tests,
24 import Data.Time ( UTCTime )
25 import Data.Tuple.Curry ( uncurryN )
26 import Database.Groundhog (
28 import Database.Groundhog.TH (
32 import Test.Tasty ( TestTree, testGroup )
33 import Test.Tasty.HUnit ( (@?=), testCase )
34 import Text.XML.HXT.Core (
47 import TSN.Picklers( xp_date, xp_team_id )
48 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
49 import TSN.XmlImport ( XmlImport(..) )
50 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
55 team_id :: String, -- ^ TeamIDs are (apparently) three characters long
56 -- and not necessarily numeric.
63 fantasy :: Maybe String, -- ^ Nobody knows what this is.
65 injury_type :: String -- ^ "type" is a reserved keyword so we can't use it
69 instance FromXml PlayerListing where
70 type Db PlayerListing = PlayerListing
73 instance XmlImport PlayerListing
77 listing_team_id :: String -- ^ Avoid conflict with PlayerListing's team_id.
78 -- TeamIDs are (apparently) three characters
79 -- long and not necessarily numeric.
80 , full_name :: String, -- ^ Team full name
81 player_listings :: [PlayerListing] }
91 listings :: [Listing],
92 time_stamp :: String }
95 instance DbImport Message where
97 mapM_ insert_xml (concatMap player_listings $ listings msg)
98 return ImportSucceeded
100 dbmigrate _ = run_dbmigrate $ migrate (undefined :: PlayerListing)
102 mkPersist defaultCodegenConfig [groundhog|
103 - entity: PlayerListing
104 dbName: injuries_detail_player_listings
106 - name: PlayerListing
113 pickle_player_listing :: PU PlayerListing
114 pickle_player_listing =
115 xpElem "PlayerListing" $
116 xpWrap (from_tuple, to_tuple) $
117 xp10Tuple (xpElem "TeamID" xp_team_id)
118 (xpElem "PlayerID" xpInt)
119 (xpElem "Date" xp_date)
120 (xpElem "Pos" xpText)
121 (xpElem "Name" xpText)
122 (xpElem "Injury" xpText)
123 (xpElem "Status" xpText)
124 (xpElem "Fantasy" $ xpOption xpText)
125 (xpElem "Injured" xpPrim)
126 (xpElem "Type" xpText)
128 from_tuple = uncurryN PlayerListing
129 to_tuple pl = (team_id pl,
141 pickle_listing :: PU Listing
144 xpWrap (from_tuple, to_tuple) $
145 xpTriple (xpElem "TeamID" xp_team_id)
146 (xpElem "FullName" xpText)
147 (xpList pickle_player_listing)
149 from_tuple = uncurryN Listing
150 to_tuple l = (listing_team_id l, full_name l, player_listings l)
153 pickle_message :: PU Message
156 xpWrap (from_tuple, to_tuple) $
157 xp6Tuple (xpElem "XML_File_ID" xpInt)
158 (xpElem "heading" xpText)
159 (xpElem "category" xpText)
160 (xpElem "sport" xpText)
161 (xpList pickle_listing)
162 (xpElem "time_stamp" xpText)
164 from_tuple = uncurryN Message
165 to_tuple m = (xml_file_id m,
174 injuries_detail_tests :: TestTree
175 injuries_detail_tests =
177 "InjuriesDetail tests"
178 [ test_pickle_of_unpickle_is_identity,
179 test_unpickle_succeeds ]
182 -- | Warning, succeess of this test does not mean that unpickling
184 test_pickle_of_unpickle_is_identity :: TestTree
185 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
186 [ check "pickle composed with unpickle is the identity"
187 "test/xml/Injuries_Detail_XML.xml",
189 check "pickle composed with unpickle is the identity (non-int team_id)"
190 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
192 check desc path = testCase desc $ do
193 (expected, actual) <- pickle_unpickle pickle_message path
197 test_unpickle_succeeds :: TestTree
198 test_unpickle_succeeds = testGroup "unpickle tests"
199 [ check "unpickling succeeds"
200 "test/xml/Injuries_Detail_XML.xml",
202 check "unpickling succeeds (non-int team_id)"
203 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
205 check desc path = testCase desc $ do
206 actual <- unpickleable path pickle_message