1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
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 then contain the real
13 -- meat; everything contained in the parent \<Listing\> can also be
14 -- found within the \<PlayerListing\>s.
16 -- The player listings will be mapped to a database table called
17 -- "injuries_detail" automatically. The root "message" and "listing"
20 module TSN.InjuriesDetail (
21 Listing ( player_listings ),
24 injuries_detail_tests )
27 import Data.Time ( UTCTime )
28 import Data.Tuple.Curry ( uncurryN )
29 import Database.Groundhog()
30 import Database.Groundhog.TH
31 import Test.Tasty ( TestTree, testGroup )
32 import Test.Tasty.HUnit ( (@?=), testCase )
33 import Text.XML.HXT.Core (
46 import TSN.Picklers( xp_date, xp_team_id )
47 import Xml ( pickle_unpickle )
59 fantasy :: String, -- ^ Nobody knows what this is.
61 injury_type :: String -- ^ "type" is a reserved keyword so we can't use it
67 listing_team_id :: Int -- ^ Avoid conflict with PlayerListing's team_id
68 , full_name :: String, -- ^ Team full name
69 player_listings :: [PlayerListing] }
78 listings :: [Listing],
79 time_stamp :: String }
83 mkPersist defaultCodegenConfig [groundhog|
84 - entity: PlayerListing
85 dbName: injuries_detail
89 pickle_player_listing :: PU PlayerListing
90 pickle_player_listing =
91 xpElem "PlayerListing" $
92 xpWrap (from_tuple, to_tuple) $
93 xp10Tuple (xpElem "TeamID" xp_team_id)
94 (xpElem "PlayerID" xpPrim)
95 (xpElem "Date" xp_date)
97 (xpElem "Name" xpText)
98 (xpElem "Injury" xpText)
99 (xpElem "Status" xpText)
100 (xpElem "Fantasy" xpText0)
101 (xpElem "Injured" xpickle)
102 (xpElem "Type" xpText)
104 from_tuple = uncurryN PlayerListing
105 to_tuple pl = (team_id pl,
116 instance XmlPickler PlayerListing where
117 xpickle = pickle_player_listing
119 pickle_listing :: PU Listing
122 xpWrap (from_tuple, to_tuple) $
123 xpTriple (xpElem "TeamID" xp_team_id)
124 (xpElem "FullName" xpText)
125 (xpList pickle_player_listing)
127 from_tuple = uncurryN Listing
128 to_tuple l = (listing_team_id l, full_name l, player_listings l)
130 instance XmlPickler Listing where
131 xpickle = pickle_listing
134 pickle_message :: PU Message
137 xpWrap (from_tuple, to_tuple) $
138 xp6Tuple (xpElem "XML_File_ID" xpPrim)
139 (xpElem "heading" xpText)
140 (xpElem "category" xpText)
141 (xpElem "sport" xpText)
142 (xpList pickle_listing)
143 (xpElem "time_stamp" xpText)
145 from_tuple = uncurryN Message
146 to_tuple m = (xml_file_id m,
153 instance XmlPickler Message where
154 xpickle = pickle_message
158 injuries_detail_tests :: TestTree
159 injuries_detail_tests =
161 "InjuriesDetail tests"
162 [ test_pickle_of_unpickle_is_identity ]
165 test_pickle_of_unpickle_is_identity :: TestTree
166 test_pickle_of_unpickle_is_identity =
167 testCase "pickle composed with unpickle is the identity" $ do
168 let path = "test/xml/Injuries_Detail_XML.xml"
169 (expected :: [Message], actual) <- pickle_unpickle "message" path