]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/Odds.hs
Make the Odds game_time optional for when the <Game_Time> element is "TBA".
[dead/htsn-import.git] / src / TSN / XML / Odds.hs
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE GADTs #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE TemplateHaskell #-}
7 {-# LANGUAGE TypeFamilies #-}
8
9 -- | Parse TSN XML for the DTD \"Odds_XML.dtd\". Each document
10 -- contains a root element \<message\> that contains a bunch of
11 -- other... disorganized... information.
12 --
13 module TSN.XML.Odds (
14 dtd,
15 pickle_message,
16 -- * Tests
17 odds_tests,
18 -- * WARNING: these are private but exported to silence warnings
19 OddsCasinoConstructor(..),
20 OddsConstructor(..),
21 OddsGameConstructor(..),
22 OddsGameLineConstructor(..) )
23 where
24
25 -- System imports.
26 import Control.Applicative ( (<$>) )
27 import Control.Monad ( forM_, join )
28 import Data.Time ( UTCTime(..) )
29 import Data.Tuple.Curry ( uncurryN )
30 import Database.Groundhog (
31 (=.),
32 (==.),
33 countAll,
34 deleteAll,
35 insert_,
36 migrate,
37 runMigration,
38 silentMigrationLogger,
39 update )
40 import Database.Groundhog.Core ( DefaultKey )
41 import Database.Groundhog.Generic ( runDbConn )
42 import Database.Groundhog.Sqlite ( withSqliteConn )
43 import Database.Groundhog.TH (
44 groundhog,
45 mkPersist )
46 import Test.Tasty ( TestTree, testGroup )
47 import Test.Tasty.HUnit ( (@?=), testCase )
48 import Text.Read ( readMaybe )
49 import Text.XML.HXT.Core (
50 PU,
51 xp6Tuple,
52 xp8Tuple,
53 xpAttr,
54 xpElem,
55 xpInt,
56 xpList,
57 xpOption,
58 xpPair,
59 xpText,
60 xpTriple,
61 xpWrap )
62
63 -- Local imports.
64 import TSN.Codegen (
65 tsn_codegen_config )
66 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
67 import TSN.Picklers ( xp_date_padded, xp_tba_time, xp_time_stamp )
68 import TSN.Team ( FromXmlFkTeams(..), Team(..) )
69 import TSN.XmlImport ( XmlImport(..), XmlImportFkTeams(..) )
70 import Xml (
71 Child(..),
72 FromXml(..),
73 ToDb(..),
74 pickle_unpickle,
75 unpickleable,
76 unsafe_unpickle )
77
78
79 -- | The DTD to which this module corresponds. Used to invoke dbimport.
80 --
81 dtd :: String
82 dtd = "Odds_XML.dtd"
83
84
85 --
86 -- DB/XML data types
87 --
88
89 -- * OddsGameCasino/OddsGameCasinoXml
90
91
92 -- | The casinos should have their own table, but the lines don't
93 -- belong in that table (there is a separate table for
94 -- 'OddsGameLine' which associates the two).
95 --
96 -- We drop the \"Game\" prefix because the casinos really aren't
97 -- children of the games; the XML just makes it seem that way.
98 --
99 data OddsCasino =
100 OddsCasino {
101 casino_client_id :: Int,
102 casino_name :: String }
103 deriving (Eq, Show)
104
105
106 -- | The home/away lines are 'Double's, but the over/under lines are
107 -- textual. If we want to use one data type for both, we have to go
108 -- with a 'String' and then attempt to 'read' a 'Double' later when we
109 -- go to insert the thing.
110 --
111 data OddsGameCasinoXml =
112 OddsGameCasinoXml {
113 xml_casino_client_id :: Int,
114 xml_casino_name :: String,
115 xml_casino_line :: Maybe String }
116 deriving (Eq, Show)
117
118
119 -- | Try to get a 'Double' out of the 'xml_casino_line' which is a
120 -- priori textual (because it might be an over/under line).
121 --
122 home_away_line :: OddsGameCasinoXml -> Maybe Double
123 home_away_line = join . (fmap readMaybe) . xml_casino_line
124
125
126
127 instance ToDb OddsGameCasinoXml where
128 -- | The database representation of an 'OddsGameCasinoXml' is an
129 -- 'OddsCasino'.
130 --
131 type Db OddsGameCasinoXml = OddsCasino
132
133
134 instance FromXml OddsGameCasinoXml where
135 -- | We convert from XML to the database by dropping the line field.
136 --
137 from_xml OddsGameCasinoXml{..} =
138 OddsCasino {
139 casino_client_id = xml_casino_client_id,
140 casino_name = xml_casino_name }
141
142
143 -- | This allows us to insert the XML representation 'OddsGameCasinoXml'
144 -- directly.
145 --
146 instance XmlImport OddsGameCasinoXml
147
148
149 -- * OddsGameTeamXml / OddsGameTeamStarterXml
150
151 -- | The XML representation of a \"starter\". It contains both an ID
152 -- and a name. The ID does not appear to be optional, but the name
153 -- can be absent. When the name is absent, the ID has always been
154 -- set to \"0\". This occurs even though the entire starter element
155 -- is optional (see 'OddsGameTeamXml' below).
156 --
157 data OddsGameTeamStarterXml =
158 OddsGameTeamStarterXml {
159 xml_starter_id :: Int,
160 xml_starter_name :: Maybe String }
161 deriving (Eq, Show)
162
163
164 -- | The XML representation of a \<HomeTeam\> or \<AwayTeam\>, as
165 -- found in \<Game\>s. We can't use the 'Team' representation
166 -- directly because there are some other fields we need to parse.
167 --
168 data OddsGameTeamXml =
169 OddsGameTeamXml {
170 xml_team_id :: String, -- ^ The home/away team IDs
171 -- are three characters but
172 -- Postgres imposes no
173 -- performance penalty on
174 -- lengthless text fields,
175 -- so we ignore the probable
176 -- upper bound of three
177 -- characters.
178 xml_team_rotation_number :: Maybe Int,
179 xml_team_abbr :: String,
180 xml_team_name :: String,
181 xml_team_starter :: Maybe OddsGameTeamStarterXml,
182 xml_team_casinos :: [OddsGameCasinoXml] }
183 deriving (Eq, Show)
184
185 instance ToDb OddsGameTeamXml where
186 -- | The database representation of an 'OddsGameTeamXml' is an
187 -- 'OddsGameTeam'.
188 --
189 type Db OddsGameTeamXml = Team
190
191 instance FromXml OddsGameTeamXml where
192 -- | We convert from XML to the database by dropping the lines and
193 -- rotation number (which are specific to the games, not the teams
194 -- themselves).
195 --
196 from_xml OddsGameTeamXml{..} =
197 Team {
198 team_id = xml_team_id,
199 abbreviation = Just xml_team_abbr,
200 name = Just xml_team_name }
201
202 -- | This allows us to insert the XML representation
203 -- 'OddsGameTeamXml' directly.
204 --
205 instance XmlImport OddsGameTeamXml where
206
207
208
209
210 -- * OddsGameOverUnderXml
211
212 -- | XML representation of the over/under. A wrapper around a bunch of
213 -- casino elements.
214 --
215 newtype OddsGameOverUnderXml =
216 OddsGameOverUnderXml { xml_casinos :: [OddsGameCasinoXml] }
217 deriving (Eq, Show)
218
219
220 -- * OddsGameLine
221
222 -- | This database representation of the casino lines can't be
223 -- constructed from the one in the XML. The casinos within
224 -- Game-\>HomeTeam, Game-\>AwayTeam, and Game-\>Over_Under are all more or
225 -- less the same. We don't need a bajillion different tables to
226 -- store that, just one tying the casino/game pair to the three
227 -- lines.
228 --
229 -- The one small difference between the over/under casinos and the
230 -- home/away ones is that the home/away lines are all 'Double's, but
231 -- the over/under lines appear to be textual.
232 --
233 data OddsGameLine =
234 OddsGameLine {
235 ogl_odds_games_id :: DefaultKey OddsGame,
236 ogl_odds_casinos_id :: DefaultKey OddsCasino,
237 ogl_over_under :: Maybe String,
238 ogl_away_line :: Maybe Double,
239 ogl_home_line :: Maybe Double }
240
241
242 -- * OddsGame/OddsGameXml
243
244 -- | Database representation of a game. We retain the rotation number
245 -- of the home/away teams, since those are specific to the game and
246 -- not the teams.
247 --
248 data OddsGame =
249 OddsGame {
250 db_odds_id :: DefaultKey Odds,
251 db_away_team_id :: DefaultKey Team,
252 db_home_team_id :: DefaultKey Team,
253 db_game_id :: Int,
254 db_game_time :: Maybe UTCTime, -- ^ Contains both the date and time.
255 db_away_team_rotation_number :: Maybe Int,
256 db_home_team_rotation_number :: Maybe Int,
257 db_away_team_starter_id :: Maybe Int,
258 db_away_team_starter_name :: Maybe String,
259 db_home_team_starter_id :: Maybe Int,
260 db_home_team_starter_name :: Maybe String }
261
262
263 -- | XML representation of an 'OddsGame'.
264 --
265 data OddsGameXml =
266 OddsGameXml {
267 xml_game_id :: Int,
268 xml_game_date :: UTCTime, -- ^ Contains only the date
269 xml_game_time :: Maybe UTCTime, -- ^ Contains only the time
270 xml_away_team :: OddsGameTeamXml,
271 xml_home_team :: OddsGameTeamXml,
272 xml_over_under :: OddsGameOverUnderXml }
273 deriving (Eq, Show)
274
275 -- | Pseudo-field that lets us get the 'OddsGameCasinoXml's out of
276 -- xml_over_under.
277 --
278 xml_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
279 xml_over_under_casinos = xml_casinos . xml_over_under
280
281
282 instance ToDb OddsGameXml where
283 -- | The database representation of an 'OddsGameXml' is an
284 -- 'OddsGame'.
285 --
286 type Db OddsGameXml = OddsGame
287
288
289 instance Child OddsGameXml where
290 -- | Each 'OddsGameXml' is contained in an 'Odds'. In other words
291 -- the foreign key for 'OddsGame' points to an 'Odds'.
292 --
293 type Parent OddsGameXml = Odds
294
295
296 instance FromXmlFkTeams OddsGameXml where
297 -- | To convert from the XML representation to the database one, we
298 -- drop the casino lines, but retain the home/away rotation
299 -- numbers and the starters. The foreign keys to 'Odds' and the
300 -- home/away teams are passed in.
301 --
302 from_xml_fk_teams fk fk_away fk_home OddsGameXml{..} =
303 OddsGame {
304 db_odds_id = fk,
305 db_away_team_id = fk_away,
306 db_home_team_id = fk_home,
307 db_game_id = xml_game_id,
308
309 db_game_time = make_game_time xml_game_date xml_game_time,
310
311 db_away_team_rotation_number =
312 (xml_team_rotation_number xml_away_team),
313
314 db_home_team_rotation_number =
315 (xml_team_rotation_number xml_home_team),
316
317 db_away_team_starter_id =
318 (xml_starter_id <$> xml_team_starter xml_away_team),
319
320 -- Sometimes the starter element is present but the name isn't,
321 -- so we combine the two maybes with join.
322 db_away_team_starter_name = join
323 (xml_starter_name <$> xml_team_starter xml_away_team),
324
325 db_home_team_starter_id =
326 (xml_starter_id <$> xml_team_starter xml_home_team),
327
328 -- Sometimes the starter element is present but the name isn't,
329 -- so we combine the two maybes with join.
330 db_home_team_starter_name = join
331 (xml_starter_name <$> xml_team_starter xml_home_team) }
332 where
333 -- | Construct the database game time from the XML \<Game_Date\>
334 -- and \<Game_Time\> elements. The \<Game_Time\> elements
335 -- sometimes have a value of \"TBA\"; in that case, we don't
336 -- want to pretend that we know the time by setting it to
337 -- e.g. midnight, so instead we make the entire date/time
338 -- Nothing.
339 make_game_time :: UTCTime -> Maybe UTCTime -> Maybe UTCTime
340 make_game_time _ Nothing = Nothing
341 make_game_time d (Just t) = Just $ UTCTime (utctDay d) (utctDayTime t)
342
343
344 -- | This lets us insert the XML representation 'OddsGameXml' directly.
345 --
346 instance XmlImportFkTeams OddsGameXml
347
348
349 -- * OddsGameWithNotes
350
351 -- | This is our best guess at what occurs in the Odds_XML
352 -- documents. It looks like each consecutive set of games can
353 -- optionally have some notes appear before it. Each \"note\" comes
354 -- as its own \<Notes\>...\</Notes\> element.
355 --
356 -- The notes are ignored completely in the database; we only bother
357 -- with them to ensure that we're (un)pickling correctly.
358 --
359 -- We can't group the notes with a \"set\" of 'OddsGame's, because
360 -- that leads to ambiguity in parsing. Since we're going to ignore
361 -- the notes anyway, we just stick them with an arbitrary
362 -- game. C'est la vie.
363 --
364 -- We have to take the same approach with the league. The
365 -- \<League_Name\> elements are sitting outside of the games, and
366 -- are presumably supposed to be interpreted in \"chronological\"
367 -- order; i.e. the current league stays the same until we see
368 -- another \<League_Name\> element. Unfortunately, that's not how
369 -- XML works. So we're forced to ignore the league in the database
370 -- and pull the same trick, pairing them with games.
371 --
372 data OddsGameWithNotes =
373 OddsGameWithNotes {
374 league :: Maybe String,
375 notes :: [String],
376 game :: OddsGameXml }
377 deriving (Eq, Show)
378
379
380 -- * Odds/Message
381
382 -- | Database representation of a 'Message'.
383 --
384 data Odds =
385 Odds {
386 db_xml_file_id :: Int,
387 db_sport :: String,
388 db_title :: String,
389 db_line_time :: String, -- ^ We don't parse these as a 'UTCTime'
390 -- because their timezones are ambiguous
391 -- (and the date is less than useful when
392 -- it might be off by an hour).
393 db_time_stamp :: UTCTime }
394
395
396 -- | The XML representation of 'Odds'.
397 --
398 data Message =
399 Message {
400 xml_xml_file_id :: Int,
401 xml_heading :: String,
402 xml_category :: String,
403 xml_sport :: String,
404 xml_title :: String,
405 xml_line_time :: String,
406 xml_games_with_notes :: [OddsGameWithNotes],
407 xml_time_stamp :: UTCTime }
408 deriving (Eq, Show)
409
410 -- | Pseudo-field that lets us get the 'OddsGame's out of
411 -- 'xml_games_with_notes'.
412 --
413 xml_games :: Message -> [OddsGameXml]
414 xml_games m = map game (xml_games_with_notes m)
415
416
417 instance ToDb Message where
418 -- | The database representation of a 'Message' is 'Odds'.
419 --
420 type Db Message = Odds
421
422 instance FromXml Message where
423 -- | To convert from the XML representation to the database one, we
424 -- just drop a bunch of fields.
425 --
426 from_xml Message{..} =
427 Odds {
428 db_xml_file_id = xml_xml_file_id,
429 db_sport = xml_sport,
430 db_title = xml_title,
431 db_line_time = xml_line_time,
432 db_time_stamp = xml_time_stamp }
433
434 -- | This lets us insert the XML representation 'Message' directly.
435 --
436 instance XmlImport Message
437
438
439 --
440 -- Database code
441 --
442
443 -- Groundhog database schema. This must come before the DbImport
444 -- instance definition. Don't know why.
445 mkPersist tsn_codegen_config [groundhog|
446 - entity: Odds
447 constructors:
448 - name: Odds
449 uniques:
450 - name: unique_odds
451 type: constraint
452 # Prevent multiple imports of the same message.
453 fields: [db_xml_file_id]
454
455 - entity: OddsCasino
456 dbName: odds_casinos
457 constructors:
458 - name: OddsCasino
459 uniques:
460 - name: unique_odds_casinos
461 type: constraint
462 fields: [casino_client_id]
463
464 - entity: OddsGame
465 dbName: odds_games
466 constructors:
467 - name: OddsGame
468 fields:
469 - name: db_odds_id
470 reference:
471 onDelete: cascade
472 - name: db_away_team_id
473 reference:
474 onDelete: cascade
475 - name: db_home_team_id
476 reference:
477 onDelete: cascade
478
479 - entity: OddsGameLine
480 dbName: odds_games_lines
481 constructors:
482 - name: OddsGameLine
483 fields:
484 - name: ogl_odds_games_id
485 reference:
486 onDelete: cascade
487 - name: ogl_odds_casinos_id
488 reference:
489 onDelete: cascade
490
491 |]
492
493 instance DbImport Message where
494 dbmigrate _=
495 run_dbmigrate $ do
496 migrate (undefined :: Team)
497 migrate (undefined :: Odds)
498 migrate (undefined :: OddsCasino)
499 migrate (undefined :: OddsGame)
500 migrate (undefined :: OddsGameLine)
501
502 dbimport m = do
503 -- Insert the root "odds" element and acquire its primary key (id).
504 odds_id <- insert_xml m
505
506 forM_ (xml_games m) $ \game -> do
507 -- First we insert the home and away teams.
508 away_team_id <- insert_xml_or_select (xml_away_team game)
509 home_team_id <- insert_xml_or_select (xml_home_team game)
510
511 -- Now insert the game, keyed to the "odds" and its teams.
512 game_id <- insert_xml_fk_teams odds_id away_team_id home_team_id game
513
514 -- Finally, we insert the lines. The over/under entries for this
515 -- game and the lines for the casinos all wind up in the same
516 -- table, odds_games_lines. We can insert the over/under entries
517 -- freely with empty away/home lines:
518 forM_ (xml_over_under_casinos game) $ \c -> do
519 -- Start by inderting the casino.
520 ou_casino_id <- insert_xml_or_select c
521
522 -- Now add the over/under entry with the casino's id.
523 let ogl = OddsGameLine {
524 ogl_odds_games_id = game_id,
525 ogl_odds_casinos_id = ou_casino_id,
526 ogl_over_under = (xml_casino_line c),
527 ogl_away_line = Nothing,
528 ogl_home_line = Nothing }
529
530 insert_ ogl
531
532 -- ...but then when we insert the home/away team lines, we
533 -- prefer to update the existing entry rather than overwrite it
534 -- or add a new record.
535 forM_ (xml_team_casinos $ xml_away_team game) $ \c -> do
536 -- insert, or more likely retrieve the existing, casino
537 a_casino_id <- insert_xml_or_select c
538
539 -- Get a Maybe Double instead of the Maybe String that's in there.
540 let away_line = home_away_line c
541
542 -- Unconditionally update that casino's away team line with ours.
543 update [Ogl_Away_Line =. away_line] $ -- WHERE
544 Ogl_Odds_Casinos_Id ==. a_casino_id
545
546 -- Repeat all that for the home team.
547 forM_ (xml_team_casinos $ xml_home_team game) $ \c ->do
548 h_casino_id <- insert_xml_or_select c
549 let home_line = home_away_line c
550 update [Ogl_Home_Line =. home_line] $ -- WHERE
551 Ogl_Odds_Casinos_Id ==. h_casino_id
552
553 return game_id
554
555 return ImportSucceeded
556
557
558 --
559 -- Pickling
560 --
561
562 -- | Pickler for an 'OddsGame' optionally preceded by some notes.
563 --
564 pickle_game_with_notes :: PU OddsGameWithNotes
565 pickle_game_with_notes =
566 xpWrap (from_pair, to_pair) $
567 xpTriple
568 (xpOption $ xpElem "League_Name" xpText)
569 (xpList $ xpElem "Notes" xpText)
570 pickle_game
571 where
572 from_pair = uncurryN OddsGameWithNotes
573 to_pair OddsGameWithNotes{..} = (league, notes, game)
574
575
576 -- | Pickler for an 'OddsGameCasinoXml'.
577 --
578 pickle_casino :: PU OddsGameCasinoXml
579 pickle_casino =
580 xpElem "Casino" $
581 xpWrap (from_tuple, to_tuple) $
582 xpTriple
583 (xpAttr "ClientID" xpInt)
584 (xpAttr "Name" xpText)
585 (xpOption xpText)
586 where
587 from_tuple = uncurryN OddsGameCasinoXml
588 -- Use record wildcards to avoid unused field warnings.
589 to_tuple OddsGameCasinoXml{..} = (xml_casino_client_id,
590 xml_casino_name,
591 xml_casino_line)
592
593
594 -- | Pickler for an 'OddsGameTeamXml'.
595 --
596 pickle_home_team :: PU OddsGameTeamXml
597 pickle_home_team =
598 xpElem "HomeTeam" $
599 xpWrap (from_tuple, to_tuple) $
600 xp6Tuple
601 (xpElem "HomeTeamID" xpText)
602 (xpElem "HomeRotationNumber" (xpOption xpInt))
603 (xpElem "HomeAbbr" xpText)
604 (xpElem "HomeTeamName" xpText)
605 (xpOption pickle_home_starter)
606 (xpList pickle_casino)
607 where
608 from_tuple = uncurryN OddsGameTeamXml
609
610 -- Use record wildcards to avoid unused field warnings.
611 to_tuple OddsGameTeamXml{..} = (xml_team_id,
612 xml_team_rotation_number,
613 xml_team_abbr,
614 xml_team_name,
615 xml_team_starter,
616 xml_team_casinos)
617
618
619 -- | Portion of the 'OddsGameTeamStarterXml' pickler that is not
620 -- specific to the home/away teams.
621 --
622 pickle_starter :: PU OddsGameTeamStarterXml
623 pickle_starter =
624 xpWrap (from_tuple, to_tuple) $
625 xpPair (xpAttr "ID" xpInt) (xpOption xpText)
626 where
627 from_tuple = uncurry OddsGameTeamStarterXml
628 to_tuple OddsGameTeamStarterXml{..} = (xml_starter_id,
629 xml_starter_name)
630
631 -- | Pickler for an home team 'OddsGameTeamStarterXml'
632 --
633 pickle_home_starter :: PU OddsGameTeamStarterXml
634 pickle_home_starter = xpElem "HStarter" pickle_starter
635
636
637 -- | Pickler for an away team 'OddsGameTeamStarterXml'
638 --
639 pickle_away_starter :: PU OddsGameTeamStarterXml
640 pickle_away_starter = xpElem "AStarter" pickle_starter
641
642
643
644 -- | Pickler for an 'OddsGameTeamXml'.
645 --
646 pickle_away_team :: PU OddsGameTeamXml
647 pickle_away_team =
648 xpElem "AwayTeam" $
649 xpWrap (from_tuple, to_tuple) $
650 xp6Tuple
651 (xpElem "AwayTeamID" xpText)
652 (xpElem "AwayRotationNumber" (xpOption xpInt))
653 (xpElem "AwayAbbr" xpText)
654 (xpElem "AwayTeamName" xpText)
655 (xpOption pickle_away_starter)
656 (xpList pickle_casino)
657 where
658 from_tuple = uncurryN OddsGameTeamXml
659
660 -- Use record wildcards to avoid unused field warnings.
661 to_tuple OddsGameTeamXml{..} = (xml_team_id,
662 xml_team_rotation_number,
663 xml_team_abbr,
664 xml_team_name,
665 xml_team_starter,
666 xml_team_casinos)
667
668
669
670 -- | Pickler for an 'OddsGameOverUnderXml'.
671 --
672 pickle_over_under :: PU OddsGameOverUnderXml
673 pickle_over_under =
674 xpElem "Over_Under" $
675 xpWrap (to_newtype, from_newtype) $
676 xpList pickle_casino
677 where
678 from_newtype (OddsGameOverUnderXml cs) = cs
679 to_newtype = OddsGameOverUnderXml
680
681
682 -- | Pickler for an 'OddsGameXml'.
683 --
684 pickle_game :: PU OddsGameXml
685 pickle_game =
686 xpElem "Game" $
687 xpWrap (from_tuple, to_tuple) $
688 xp6Tuple
689 (xpElem "GameID" xpInt)
690 (xpElem "Game_Date" xp_date_padded)
691 (xpElem "Game_Time" xp_tba_time)
692 pickle_away_team
693 pickle_home_team
694 pickle_over_under
695 where
696 from_tuple = uncurryN OddsGameXml
697 -- Use record wildcards to avoid unused field warnings.
698 to_tuple OddsGameXml{..} = (xml_game_id,
699 xml_game_date,
700 xml_game_time,
701 xml_away_team,
702 xml_home_team,
703 xml_over_under)
704
705
706 -- | Pickler for the top-level 'Message'.
707 --
708 pickle_message :: PU Message
709 pickle_message =
710 xpElem "message" $
711 xpWrap (from_tuple, to_tuple) $
712 xp8Tuple (xpElem "XML_File_ID" xpInt)
713 (xpElem "heading" xpText)
714 (xpElem "category" xpText)
715 (xpElem "sport" xpText)
716 (xpElem "Title" xpText)
717 (xpElem "Line_Time" xpText)
718 (xpList pickle_game_with_notes)
719 (xpElem "time_stamp" xp_time_stamp)
720 where
721 from_tuple = uncurryN Message
722 to_tuple m = (xml_xml_file_id m,
723 xml_heading m,
724 xml_category m,
725 xml_sport m,
726 xml_title m,
727 xml_line_time m,
728 xml_games_with_notes m,
729 xml_time_stamp m)
730
731
732 --
733 -- Tasty Tests
734 --
735
736 -- | A list of all tests for this module.
737 --
738 odds_tests :: TestTree
739 odds_tests =
740 testGroup
741 "Odds tests"
742 [ test_on_delete_cascade,
743 test_pickle_of_unpickle_is_identity,
744 test_unpickle_succeeds ]
745
746
747 -- | If we unpickle something and then pickle it, we should wind up
748 -- with the same thing we started with. WARNING: success of this
749 -- test does not mean that unpickling succeeded.
750 --
751 test_pickle_of_unpickle_is_identity :: TestTree
752 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
753 [ check "pickle composed with unpickle is the identity"
754 "test/xml/Odds_XML.xml",
755
756 check "pickle composed with unpickle is the identity (non-int team_id)"
757 "test/xml/Odds_XML-noninteger-team-id.xml",
758
759 check "pickle composed with unpickle is the identity (positive(+) line)"
760 "test/xml/Odds_XML-positive-line.xml",
761
762 check "pickle composed with unpickle is the identity (large file)"
763 "test/xml/Odds_XML-largefile.xml",
764
765 check "pickle composed with unpickle is the identity (league name)"
766 "test/xml/Odds_XML-league-name.xml",
767
768 check "pickle composed with unpickle is the identity (missing starters)"
769 "test/xml/Odds_XML-missing-starters.xml",
770
771 check "pickle composed with unpickle is the identity (TBA game time)"
772 "test/xml/Odds_XML-tba-game-time.xml"]
773 where
774 check desc path = testCase desc $ do
775 (expected, actual) <- pickle_unpickle pickle_message path
776 actual @?= expected
777
778
779 -- | Make sure we can actually unpickle these things.
780 --
781 test_unpickle_succeeds :: TestTree
782 test_unpickle_succeeds = testGroup "unpickle tests"
783 [ check "unpickling succeeds"
784 "test/xml/Odds_XML.xml",
785
786 check "unpickling succeeds (non-int team_id)"
787 "test/xml/Odds_XML-noninteger-team-id.xml",
788
789 check "unpickling succeeds (positive(+) line)"
790 "test/xml/Odds_XML-positive-line.xml",
791
792 check "unpickling succeeds (large file)"
793 "test/xml/Odds_XML-largefile.xml",
794
795 check "unpickling succeeds (league name)"
796 "test/xml/Odds_XML-league-name.xml",
797
798 check "unpickling succeeds (missing starters)"
799 "test/xml/Odds_XML-missing-starters.xml",
800
801 check "unpickling succeeds (TBA game time)"
802 "test/xml/Odds_XML-tba-game-time.xml" ]
803 where
804 check desc path = testCase desc $ do
805 actual <- unpickleable path pickle_message
806 let expected = True
807 actual @?= expected
808
809
810 -- | Make sure everything gets deleted when we delete the top-level
811 -- record. The casinos and teams should be left behind.
812 --
813 test_on_delete_cascade :: TestTree
814 test_on_delete_cascade = testGroup "cascading delete tests"
815 [ check "deleting odds deletes its children"
816 "test/xml/Odds_XML.xml"
817 13 -- 5 casinos, 8 teams
818 ,
819
820 check "deleting odds deletes its children (non-int team_id)"
821 "test/xml/Odds_XML-noninteger-team-id.xml"
822 51 -- 5 casinos, 46 teams
823 ,
824
825 check "deleting odds deleted its children (positive(+) line)"
826 "test/xml/Odds_XML-positive-line.xml"
827 17 -- 5 casinos, 12 teams
828 ,
829
830 check "deleting odds deleted its children (large file)"
831 "test/xml/Odds_XML-largefile.xml"
832 189 -- 5 casinos, 184 teams
833 ,
834 check "deleting odds deleted its children (league name)"
835 "test/xml/Odds_XML-league-name.xml"
836 35 -- 5 casinos, 30 teams
837 ,
838 check "deleting odds deleted its children (missing starters)"
839 "test/xml/Odds_XML-missing-starters.xml"
840 7 -- 5 casinos, 2 teams
841 ,
842 check "deleting odds deleted its children (TBA game time)"
843 "test/xml/Odds_XML-tba-game-time.xml"
844 119 -- 5 casinos, 114 teams
845 ]
846 where
847 check desc path expected = testCase desc $ do
848 odds <- unsafe_unpickle path pickle_message
849 let a = undefined :: Team
850 let b = undefined :: Odds
851 let c = undefined :: OddsCasino
852 let d = undefined :: OddsGame
853 let e = undefined :: OddsGameLine
854 actual <- withSqliteConn ":memory:" $ runDbConn $ do
855 runMigration silentMigrationLogger $ do
856 migrate a
857 migrate b
858 migrate c
859 migrate d
860 migrate e
861 _ <- dbimport odds
862 deleteAll b
863 count_a <- countAll a
864 count_b <- countAll b
865 count_c <- countAll c
866 count_d <- countAll d
867 count_e <- countAll e
868 return $ sum [count_a, count_b, count_c,
869 count_d, count_e ]
870 actual @?= expected