]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/InjuriesDetail.hs
Don't bother with the three-character limit on team_id fields; Postgres doesn't care.
[dead/htsn-import.git] / src / TSN / XML / InjuriesDetail.hs
1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE StandaloneDeriving #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
7
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.
14 --
15 -- The player listings will be mapped to a database table called
16 -- \"injuries_detail_player_listings\" automatically. The root
17 -- \"message\" and \"listing\" are not retained.
18 --
19 module TSN.XML.InjuriesDetail (
20 pickle_message,
21 -- * Tests
22 injuries_detail_tests,
23 -- * WARNING: these are private but exported to silence warnings
24 PlayerListingConstructor(..) )
25 where
26
27 -- System imports.
28 import Data.Time ( UTCTime )
29 import Data.Tuple.Curry ( uncurryN )
30 import Database.Groundhog (
31 migrate )
32 import Database.Groundhog.TH (
33 defaultCodegenConfig,
34 groundhog,
35 mkPersist )
36 import Test.Tasty ( TestTree, testGroup )
37 import Test.Tasty.HUnit ( (@?=), testCase )
38 import Text.XML.HXT.Core (
39 PU,
40 xpTriple,
41 xp6Tuple,
42 xp10Tuple,
43 xpElem,
44 xpInt,
45 xpList,
46 xpOption,
47 xpPrim,
48 xpText,
49 xpWrap )
50
51 -- Local imports.
52 import TSN.Picklers( xp_date )
53 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
54 import TSN.XmlImport ( XmlImport(..) )
55 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
56
57
58 -- | Database representation of a \<PlayerListing\>, the main type of
59 -- element contains in Injuries_Detail_XML messages.
60 --
61 data PlayerListing =
62 PlayerListing {
63 team_id :: String, -- ^ TeamIDs are (apparently) three
64 -- characters long and not necessarily
65 -- numeric. Postgres imposes no performance
66 -- penalty on a lengthless text field,
67 -- so we ignore the likely upper bound of
68 -- three characters.
69 player_id :: Int,
70 date :: UTCTime,
71 pos :: String,
72 name :: String,
73 injury :: String,
74 status :: String,
75 fantasy :: Maybe String, -- ^ Nobody knows what this is.
76 injured :: Bool,
77 injury_type :: String -- ^ "type" is a reserved keyword so we can't use it
78 }
79 deriving (Eq, Show)
80
81
82 instance FromXml PlayerListing where
83 -- | The DB analogue of a 'PlayerListing' is... itself!
84 type Db PlayerListing = PlayerListing
85
86 -- | To convert between a 'PlayerListing' and a 'PlayerListing',
87 -- we do nothing.
88 from_xml = id
89
90 -- | This lets us call 'insert_xml' on a 'PlayerListing' without
91 -- having to explicitly convert it to its database analogue first.
92 --
93 instance XmlImport PlayerListing
94
95
96 -- | XML incarnation of a \<Listing\> element. We don't store these;
97 -- the data type is used only for parsing.
98 --
99 data Listing =
100 Listing {
101 listing_team_id :: String -- ^ Avoid conflict with PlayerListing's team_id.
102 -- TeamIDs are (apparently) three characters
103 -- long and not necessarily numeric.
104 , full_name :: String, -- ^ Team full name
105 player_listings :: [PlayerListing] }
106 deriving (Eq, Show)
107
108
109 -- | XML representation of the top-level \<message\> element. These
110 -- are not stored; the data type is used only for parsing.
111 --
112 data Message =
113 Message {
114 xml_file_id :: Int,
115 heading :: String,
116 category :: String,
117 sport :: String,
118 listings :: [Listing],
119 time_stamp :: String }
120 deriving (Eq, Show)
121
122 instance DbImport Message where
123 -- | To import a 'Message', we import all of its 'PlayerListing's,
124 -- which we have to dig out of its 'Listing's.
125 dbimport msg = do
126 mapM_ insert_xml (concatMap player_listings $ listings msg)
127 return ImportSucceeded
128
129 dbmigrate _ = run_dbmigrate $ migrate (undefined :: PlayerListing)
130
131
132 mkPersist defaultCodegenConfig [groundhog|
133 - entity: PlayerListing
134 dbName: injuries_detail_player_listings
135 |]
136
137
138 -- | Convert 'PlayerListing's to/from XML.
139 --
140 pickle_player_listing :: PU PlayerListing
141 pickle_player_listing =
142 xpElem "PlayerListing" $
143 xpWrap (from_tuple, to_tuple) $
144 xp10Tuple (xpElem "TeamID" xpText)
145 (xpElem "PlayerID" xpInt)
146 (xpElem "Date" xp_date)
147 (xpElem "Pos" xpText)
148 (xpElem "Name" xpText)
149 (xpElem "Injury" xpText)
150 (xpElem "Status" xpText)
151 (xpElem "Fantasy" $ xpOption xpText)
152 (xpElem "Injured" xpPrim)
153 (xpElem "Type" xpText)
154 where
155 from_tuple = uncurryN PlayerListing
156 to_tuple pl = (team_id pl,
157 player_id pl,
158 date pl,
159 pos pl,
160 name pl,
161 injury pl,
162 status pl,
163 fantasy pl,
164 injured pl,
165 injury_type pl)
166
167
168 -- | Convert 'Listing's to/from XML.
169 --
170 pickle_listing :: PU Listing
171 pickle_listing =
172 xpElem "Listing" $
173 xpWrap (from_tuple, to_tuple) $
174 xpTriple (xpElem "TeamID" xpText)
175 (xpElem "FullName" xpText)
176 (xpList pickle_player_listing)
177 where
178 from_tuple = uncurryN Listing
179 to_tuple l = (listing_team_id l, full_name l, player_listings l)
180
181
182 -- | Convert 'Message's to/from XML.
183 --
184 pickle_message :: PU Message
185 pickle_message =
186 xpElem "message" $
187 xpWrap (from_tuple, to_tuple) $
188 xp6Tuple (xpElem "XML_File_ID" xpInt)
189 (xpElem "heading" xpText)
190 (xpElem "category" xpText)
191 (xpElem "sport" xpText)
192 (xpList pickle_listing)
193 (xpElem "time_stamp" xpText)
194 where
195 from_tuple = uncurryN Message
196 to_tuple m = (xml_file_id m,
197 heading m,
198 category m,
199 sport m,
200 listings m,
201 time_stamp m)
202
203
204 --
205 -- Tasty Tests
206 --
207
208 -- | A list of all tests for this module.
209 --
210 injuries_detail_tests :: TestTree
211 injuries_detail_tests =
212 testGroup
213 "InjuriesDetail tests"
214 [ test_pickle_of_unpickle_is_identity,
215 test_unpickle_succeeds ]
216
217
218 -- | If we unpickle something and then pickle it, we should wind up
219 -- with the same thing we started with. WARNING: succeess of this
220 -- test does not mean that unpickling succeeded.
221 --
222 test_pickle_of_unpickle_is_identity :: TestTree
223 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
224 [ check "pickle composed with unpickle is the identity"
225 "test/xml/Injuries_Detail_XML.xml",
226
227 check "pickle composed with unpickle is the identity (non-int team_id)"
228 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
229 where
230 check desc path = testCase desc $ do
231 (expected, actual) <- pickle_unpickle pickle_message path
232 actual @?= expected
233
234
235 -- | Make sure we can actually unpickle these things.
236 --
237 test_unpickle_succeeds :: TestTree
238 test_unpickle_succeeds = testGroup "unpickle tests"
239 [ check "unpickling succeeds"
240 "test/xml/Injuries_Detail_XML.xml",
241
242 check "unpickling succeeds (non-int team_id)"
243 "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
244 where
245 check desc path = testCase desc $ do
246 actual <- unpickleable path pickle_message
247 let expected = True
248 actual @?= expected