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,
22 -- * WARNING: these are private but exported to silence warnings
23 PlayerListingConstructor(..) )
26 import Data.Time ( UTCTime )
27 import Data.Tuple.Curry ( uncurryN )
28 import Database.Groundhog (
30 import Database.Groundhog.TH (
34 import Test.Tasty ( TestTree, testGroup )
35 import Test.Tasty.HUnit ( (@?=), testCase )
36 import Text.XML.HXT.Core (
49 import TSN.Picklers( xp_date, xp_team_id )
50 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
51 import TSN.XmlImport ( XmlImport(..) )
52 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
57 team_id :: String, -- ^ TeamIDs are (apparently) three characters long
58 -- and not necessarily numeric.
65 fantasy :: Maybe String, -- ^ Nobody knows what this is.
67 injury_type :: String -- ^ "type" is a reserved keyword so we can't use it
71 instance FromXml PlayerListing where
72 type Db PlayerListing = PlayerListing
75 instance XmlImport PlayerListing
79 listing_team_id :: String -- ^ Avoid conflict with PlayerListing's team_id.
80 -- TeamIDs are (apparently) three characters
81 -- long and not necessarily numeric.
82 , full_name :: String, -- ^ Team full name
83 player_listings :: [PlayerListing] }
93 listings :: [Listing],
94 time_stamp :: String }
97 instance DbImport Message where
99 mapM_ insert_xml (concatMap player_listings $ listings msg)
100 return ImportSucceeded
102 dbmigrate _ = run_dbmigrate $ migrate (undefined :: PlayerListing)
104 mkPersist defaultCodegenConfig [groundhog|
105 - entity: PlayerListing
106 dbName: injuries_detail_player_listings
108 - name: PlayerListing
115 pickle_player_listing :: PU PlayerListing
116 pickle_player_listing =
117 xpElem "PlayerListing" $
118 xpWrap (from_tuple, to_tuple) $
119 xp10Tuple (xpElem "TeamID" xp_team_id)
120 (xpElem "PlayerID" xpInt)
121 (xpElem "Date" xp_date)
122 (xpElem "Pos" xpText)
123 (xpElem "Name" xpText)
124 (xpElem "Injury" xpText)
125 (xpElem "Status" xpText)
126 (xpElem "Fantasy" $ xpOption xpText)
127 (xpElem "Injured" xpPrim)
128 (xpElem "Type" xpText)
130 from_tuple = uncurryN PlayerListing
131 to_tuple pl = (team_id pl,
143 pickle_listing :: PU Listing
146 xpWrap (from_tuple, to_tuple) $
147 xpTriple (xpElem "TeamID" xp_team_id)
148 (xpElem "FullName" xpText)
149 (xpList pickle_player_listing)
151 from_tuple = uncurryN Listing
152 to_tuple l = (listing_team_id l, full_name l, player_listings l)
155 pickle_message :: PU Message
158 xpWrap (from_tuple, to_tuple) $
159 xp6Tuple (xpElem "XML_File_ID" xpInt)
160 (xpElem "heading" xpText)
161 (xpElem "category" xpText)
162 (xpElem "sport" xpText)
163 (xpList pickle_listing)
164 (xpElem "time_stamp" xpText)
166 from_tuple = uncurryN Message
167 to_tuple m = (xml_file_id m,
176 injuries_detail_tests :: TestTree
177 injuries_detail_tests =
179 "InjuriesDetail tests"
180 [ test_pickle_of_unpickle_is_identity,
181 test_unpickle_succeeds ]
184 -- | Warning, succeess of this test does not mean that unpickling
186 test_pickle_of_unpickle_is_identity :: TestTree
187 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
188 [ check "pickle composed with unpickle is the identity"
189 "test/xml/Injuries_Detail_XML.xml",
191 check "pickle composed with unpickle is the identity (non-int team_id)"
192 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
194 check desc path = testCase desc $ do
195 (expected, actual) <- pickle_unpickle pickle_message path
199 test_unpickle_succeeds :: TestTree
200 test_unpickle_succeeds = testGroup "unpickle tests"
201 [ check "unpickling succeeds"
202 "test/xml/Injuries_Detail_XML.xml",
204 check "unpickling succeeds (non-int team_id)"
205 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
207 check desc path = testCase desc $ do
208 actual <- unpickleable path pickle_message