+
+
+---
+--- Pickling
+---
+
+-- | Pickler for the \<Listing\>s contained within \<message\>s.
+--
+pickle_listing :: PU AutoRacingResultsListingXml
+pickle_listing =
+ xpElem "Listing" $
+ xpWrap (from_tuple, to_tuple) $
+ xp13Tuple (xpElem "FinishPosition" xpInt)
+ (xpElem "StartingPosition" xpInt)
+ (xpElem "CarNumber" xpInt)
+ (xpElem "DriverID" xpInt)
+ (xpElem "Driver" xpText)
+ (xpElem "CarMake" xpText)
+ (xpElem "Points" xpInt)
+ (xpElem "Laps_Completed" xpInt)
+ (xpElem "Laps_Leading" xpInt)
+ (xpElem "Status" $ xpOption xpText)
+ (xpOption $ xpElem "DNF" xpPrim)
+ (xpOption $ xpElem "NC" xpPrim)
+ (xpElem "Earnings" xp_earnings)
+ where
+ from_tuple = uncurryN AutoRacingResultsListingXml
+ to_tuple m = (xml_finish_position m,
+ xml_starting_position m,
+ xml_car_number m,
+ xml_driver_id m,
+ xml_driver m,
+ xml_car_make m,
+ xml_points m,
+ xml_laps_completed m,
+ xml_laps_leading m,
+ xml_status m,
+ xml_dnf m,
+ xml_nc m,
+ xml_earnings m)
+
+
+-- | Pickler for the top-level 'Message'.
+--
+pickle_message :: PU Message
+pickle_message =
+ xpElem "message" $
+ xpWrap (from_tuple, to_tuple) $
+ xp13Tuple (xpElem "XML_File_ID" xpInt)
+ (xpElem "heading" xpText)
+ (xpElem "category" xpText)
+ (xpElem "sport" xpText)
+ (xpElem "RaceID" xpInt)
+ (xpElem "RaceDate" xp_datetime)
+ (xpElem "Title" xpText)
+ (xpElem "Track_Location" xpText)
+ (xpElem "Laps_Remaining" xpInt)
+ (xpElem "Checkered_Flag" xpPrim)
+ (xpList pickle_listing)
+ pickle_race_information
+ (xpElem "time_stamp" xp_time_stamp)
+ where
+ from_tuple = uncurryN Message
+ to_tuple m = (xml_xml_file_id m,
+ xml_heading m,
+ xml_category m,
+ xml_sport m,
+ xml_race_id m,
+ xml_race_date m,
+ xml_title m,
+ xml_track_location m,
+ xml_laps_remaining m,
+ xml_checkered_flag m,
+ xml_listings m,
+ xml_race_information m,
+ xml_time_stamp m)
+
+
+-- | Pickler for the \<Most_Laps_Leading\> child of a
+-- \<Race_Information\>.
+--
+pickle_most_laps_leading :: PU MostLapsLeading
+pickle_most_laps_leading =
+ xpElem "Most_Laps_Leading" $
+ xpWrap (from_tuple, to_tuple) $
+ xpTriple (xpElem "DriverID" xpInt)
+ (xpElem "Driver" xpText)
+ (xpElem "NumberOfLaps" xpInt)
+ where
+ from_tuple = uncurryN MostLapsLeading
+ to_tuple m = (db_most_laps_leading_driver_id m,
+ db_most_laps_leading_driver m,
+ db_most_laps_leading_number_of_laps m)
+
+
+-- | Pickler for the \<Race_Information\> child of \<message\>.
+--
+pickle_race_information :: PU AutoRacingResultsRaceInformationXml
+pickle_race_information =
+ xpElem "Race_Information" $
+ xpWrap (from_tuple, to_tuple) $
+ xp11Tuple (-- I can't think of another way to get both the
+ -- TrackLength and its KPH attribute. So we shove them
+ -- both in a 2-tuple. This should probably be an embedded type!
+ xpElem "TrackLength" $ xpPair xpText (xpAttr "KPH" xpPrim) )
+ (xpElem "Laps" xpInt)
+ (xpOption $ xpElem "AverageSpeedMPH" xpPrim)
+ (xpOption $ xpElem "AverageSpeedKPH" xpPrim)
+ (xpOption $ xpElem "AverageSpeed" xpPrim)
+ (xpOption $ xpElem "TimeOfRace" xpText)
+ (xpOption $ xpElem "MarginOfVictory" xpText)
+ (xpOption $ xpElem "Cautions" xpText)
+ (xpOption $ xpElem "LeadChanges" xpText)
+ (xpOption $ xpElem "LapLeaders" xpText)
+ pickle_most_laps_leading
+ where
+ -- Derp. Since the first two are paired, we have to
+ -- manually unpack the bazillion arguments.
+ from_tuple ((x1,x2),x3,x4,x5,x6,x7,x8,x9,x10,x11,x12) =
+ AutoRacingResultsRaceInformationXml
+ x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12
+
+ -- And here we have to re-pair the first two.
+ to_tuple m = ((xml_track_length m, xml_track_length_kph m),
+ xml_laps m,
+ xml_average_speed_mph m,
+ xml_average_speed_kph m,
+ xml_average_speed m,
+ xml_time_of_race m,
+ xml_margin_of_victory m,
+ xml_cautions m,
+ xml_lead_changes m,
+ xml_lap_leaders m,
+ xml_most_laps_leading m)
+
+--
+-- Tasty Tests
+--
+
+-- | A list of all tests for this module.
+--
+auto_racing_results_tests :: TestTree
+auto_racing_results_tests =
+ testGroup
+ "AutoRacingResults tests"
+ [ test_on_delete_cascade,
+ test_pickle_of_unpickle_is_identity,
+ test_unpickle_succeeds ]
+
+-- | If we unpickle something and then pickle it, we should wind up
+-- with the same thing we started with. WARNING: success of this
+-- test does not mean that unpickling succeeded.
+--
+test_pickle_of_unpickle_is_identity :: TestTree
+test_pickle_of_unpickle_is_identity =
+ testCase "pickle composed with unpickle is the identity" $ do
+ let path = "test/xml/AutoRacingResultsXML.xml"
+ (expected, actual) <- pickle_unpickle pickle_message path
+ actual @?= expected
+
+
+
+-- | Make sure we can actually unpickle these things.
+--
+test_unpickle_succeeds :: TestTree
+test_unpickle_succeeds =
+ testCase "unpickling succeeds" $ do
+ let path = "test/xml/AutoRacingResultsXML.xml"
+ actual <- unpickleable path pickle_message
+
+ let expected = True
+ actual @?= expected
+
+
+
+-- | Make sure everything gets deleted when we delete the top-level
+-- record.
+--
+test_on_delete_cascade :: TestTree
+test_on_delete_cascade =
+ testCase "deleting auto_racing_results deletes its children" $ do
+ let path = "test/xml/AutoRacingResultsXML.xml"
+ results <- unsafe_unpickle path pickle_message
+ let a = undefined :: AutoRacingResults
+ let b = undefined :: AutoRacingResultsListing
+ let c = undefined :: AutoRacingResultsRaceInformation
+
+ actual <- withSqliteConn ":memory:" $ runDbConn $ do
+ runMigration silentMigrationLogger $ do
+ migrate a
+ migrate b
+ migrate c
+ _ <- dbimport results
+ deleteAll a
+ count_a <- countAll a
+ count_b <- countAll b
+ count_c <- countAll c
+ return $ sum [count_a, count_b, count_c]
+ let expected = 0
+ actual @?= expected