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