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.XML.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 (
34 import Test.Tasty ( TestTree, testGroup )
35 import Test.Tasty.HUnit ( (@?=), testCase )
36 import Text.XML.HXT.Core (
50 import TSN.DbImport ( DbImport(..), import_generic )
51 import TSN.Picklers( xp_date, xp_team_id )
52 import Xml ( pickle_unpickle )
64 fantasy :: String, -- ^ Nobody knows what this is.
66 injury_type :: String -- ^ "type" is a reserved keyword so we can't use it
72 listing_team_id :: Int -- ^ Avoid conflict with PlayerListing's team_id
73 , full_name :: String, -- ^ Team full name
74 player_listings :: [PlayerListing] }
83 listings :: [Listing],
84 time_stamp :: String }
88 mkPersist defaultCodegenConfig [groundhog|
89 - entity: PlayerListing
90 dbName: injuries_detail
94 pickle_player_listing :: PU PlayerListing
95 pickle_player_listing =
96 xpElem "PlayerListing" $
97 xpWrap (from_tuple, to_tuple) $
98 xp10Tuple (xpElem "TeamID" xp_team_id)
99 (xpElem "PlayerID" xpInt)
100 (xpElem "Date" xp_date)
101 (xpElem "Pos" xpText)
102 (xpElem "Name" xpText)
103 (xpElem "Injury" xpText)
104 (xpElem "Status" xpText)
105 (xpElem "Fantasy" xpText0)
106 (xpElem "Injured" xpPrim)
107 (xpElem "Type" xpText)
109 from_tuple = uncurryN PlayerListing
110 to_tuple pl = (team_id pl,
121 instance XmlPickler PlayerListing where
122 xpickle = pickle_player_listing
124 pickle_listing :: PU Listing
127 xpWrap (from_tuple, to_tuple) $
128 xpTriple (xpElem "TeamID" xp_team_id)
129 (xpElem "FullName" xpText)
130 (xpList pickle_player_listing)
132 from_tuple = uncurryN Listing
133 to_tuple l = (listing_team_id l, full_name l, player_listings l)
135 instance XmlPickler Listing where
136 xpickle = pickle_listing
139 pickle_message :: PU Message
142 xpWrap (from_tuple, to_tuple) $
143 xp6Tuple (xpElem "XML_File_ID" xpInt)
144 (xpElem "heading" xpText)
145 (xpElem "category" xpText)
146 (xpElem "sport" xpText)
147 (xpList pickle_listing)
148 (xpElem "time_stamp" xpText)
150 from_tuple = uncurryN Message
151 to_tuple m = (xml_file_id m,
158 instance XmlPickler Message where
159 xpickle = pickle_message
161 instance DbImport PlayerListing where
162 dbimport = import_generic ( (concatMap player_listings) . listings)
166 injuries_detail_tests :: TestTree
167 injuries_detail_tests =
169 "InjuriesDetail tests"
170 [ test_pickle_of_unpickle_is_identity ]
173 test_pickle_of_unpickle_is_identity :: TestTree
174 test_pickle_of_unpickle_is_identity =
175 testCase "pickle composed with unpickle is the identity" $ do
176 let path = "test/xml/Injuries_Detail_XML.xml"
177 (expected :: [Message], actual) <- pickle_unpickle "message" path