1 {-# LANGUAGE BangPatterns #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleInstances #-}
5 {-# LANGUAGE QuasiQuotes #-}
6 {-# LANGUAGE RecordWildCards #-}
7 {-# LANGUAGE ScopedTypeVariables #-}
8 {-# LANGUAGE StandaloneDeriving #-}
9 {-# LANGUAGE TemplateHaskell #-}
10 {-# LANGUAGE TypeFamilies #-}
18 -- | Parse TSN XML for the DTD "Odds_XML.dtd". Each document contains
19 -- a root element \<message\> that contains a bunch of other
23 import Data.Data ( Data, constrFields, dataTypeConstrs, dataTypeOf )
24 import Data.List.Utils ( join, split )
25 import Data.Tuple.Curry ( uncurryN )
26 import Data.Typeable ( Typeable )
27 import Database.Groundhog (
28 defaultMigrationLogger,
32 import Database.Groundhog.Core ( DefaultKey )
33 import Database.Groundhog.TH (
36 import System.Console.CmdArgs.Default ( Default(..) )
37 import Test.Tasty ( TestTree, testGroup )
38 import Test.Tasty.HUnit ( (@?=), testCase )
39 import Text.XML.HXT.Core (
61 import TSN.DbImport ( DbImport(..), ImportResult(..) )
62 import Xml ( ToFromXml(..), pickle_unpickle, unpickleable )
68 xml_casino_client_id :: Int,
69 xml_casino_name :: String,
70 xml_casino_line :: Maybe Float }
75 xml_home_team_id :: Int,
76 xml_home_rotation_number :: Int,
77 xml_home_abbr :: String,
78 xml_home_team_name :: String,
79 xml_home_casinos :: [OddsCasino] }
84 xml_away_team_id :: Int,
85 xml_away_rotation_number :: Int,
86 xml_away_abbr :: String,
87 xml_away_team_name :: String,
88 xml_away_casinos :: [OddsCasino] }
91 -- | Can't use a newtype with Groundhog.
93 OddsOverUnder [OddsCasino]
99 xml_game_date :: String, -- TODO
100 xml_game_time :: String, -- TODO
101 xml_game_away_team :: OddsAwayTeam,
102 xml_game_home_team :: OddsHomeTeam,
103 xml_game_over_under :: OddsOverUnder }
110 db_line_time :: String,
112 db_notes2 :: String }
116 xml_xml_file_id :: Int,
117 xml_heading :: String,
118 xml_category :: String,
121 xml_line_time :: String, -- The DTD goes crazy here.
122 xml_notes1 :: String,
123 xml_games1 :: [OddsGame],
124 xml_notes2 :: String,
125 xml_games2 :: [OddsGame],
126 xml_time_stamp :: String }
130 instance ToFromXml Message where
131 type Xml Message = MessageXml
132 type Container Message = ()
134 -- Use a record wildcard here so GHC doesn't complain that we never
135 -- used our named fields.
136 to_xml (Message {..}) =
150 -- We don't need the key argument (from_xml_fk) since the XML type
151 -- contains more information in this case.
152 from_xml (MessageXml _ _ _ d e f g _ i _ _) =
156 pickle_casino :: PU OddsCasino
159 xpWrap (from_tuple, to_tuple) $
161 (xpAttr "ClientID" xpInt)
162 (xpAttr "Name" xpText)
165 from_tuple = uncurryN OddsCasino
166 to_tuple (OddsCasino x y z) = (x, y, z)
168 instance XmlPickler OddsCasino where
169 xpickle = pickle_casino
172 pickle_home_team :: PU OddsHomeTeam
175 xpWrap (from_tuple, to_tuple) $
177 (xpElem "HomeTeamID" xpPrim)
178 (xpElem "HomeRotationNumber" xpPrim)
179 (xpElem "HomeAbbr" xpText)
180 (xpElem "HomeTeamName" xpText)
181 (xpList pickle_casino)
183 from_tuple = uncurryN OddsHomeTeam
184 to_tuple (OddsHomeTeam v w x y z) = (v, w, x, y, z)
187 instance XmlPickler OddsHomeTeam where
188 xpickle = pickle_home_team
191 pickle_away_team :: PU OddsAwayTeam
194 xpWrap (from_tuple, to_tuple) $
196 (xpElem "AwayTeamID" xpPrim)
197 (xpElem "AwayRotationNumber" xpPrim)
198 (xpElem "AwayAbbr" xpText)
199 (xpElem "AwayTeamName" xpText)
200 (xpList pickle_casino)
202 from_tuple = uncurryN OddsAwayTeam
203 to_tuple (OddsAwayTeam v w x y z) = (v, w, x, y, z)
206 instance XmlPickler OddsAwayTeam where
207 xpickle = pickle_away_team
210 pickle_over_under :: PU OddsOverUnder
212 xpElem "Over_Under" $
213 xpWrap (to_newtype, from_newtype) $
216 from_newtype (OddsOverUnder cs) = cs
217 to_newtype = OddsOverUnder
219 instance XmlPickler OddsOverUnder where
220 xpickle = pickle_over_under
223 pickle_game :: PU OddsGame
226 xpWrap (from_tuple, to_tuple) $
228 (xpElem "GameID" xpPrim)
229 (xpElem "Game_Date" xpText)
230 (xpElem "Game_Time" xpText)
235 from_tuple = uncurryN OddsGame
236 to_tuple (OddsGame u v w x y z) = (u,v,w,x,y,z)
238 instance XmlPickler OddsGame where
239 xpickle = pickle_game
242 pickle_message :: PU MessageXml
245 xpWrap (from_tuple, to_tuple) $
246 xp11Tuple (xpElem "XML_File_ID" xpPrim)
247 (xpElem "heading" xpText)
248 (xpElem "category" xpText)
249 (xpElem "sport" xpText)
250 (xpElem "Title" xpText)
251 (xpElem "Line_Time" xpText)
256 (xpElem "time_stamp" xpText)
258 from_tuple = uncurryN MessageXml
259 to_tuple m = (xml_xml_file_id m,
271 pickle_notes :: PU String
273 xpWrap (to_string, from_string) $
274 xpList (xpElem "Notes" xpText)
276 from_string :: String -> [String]
277 from_string = split "\n"
279 to_string :: [String] -> String
280 to_string = join "\n"
282 instance XmlPickler MessageXml where
283 xpickle = pickle_message
291 odds_tests :: TestTree
295 [ test_pickle_of_unpickle_is_identity,
296 test_unpickle_succeeds ]
299 -- | Warning, succeess of this test does not mean that unpickling
301 test_pickle_of_unpickle_is_identity :: TestTree
302 test_pickle_of_unpickle_is_identity =
303 testCase "pickle composed with unpickle is the identity" $ do
304 let path = "test/xml/Odds_XML.xml"
305 (expected :: [MessageXml], actual) <- pickle_unpickle "message" path
309 test_unpickle_succeeds :: TestTree
310 test_unpickle_succeeds =
311 testCase "unpickling succeeds" $ do
312 let path = "test/xml/Odds_XML.xml"
313 actual <- unpickleable path pickle_message