-- System imports.
import Control.Monad ( forM_ )
import Data.Data ( Data )
+import Data.Maybe ( fromMaybe )
import Data.Time ( UTCTime(..) )
import Data.Tuple.Curry ( uncurryN )
import Data.Typeable ( Typeable )
xp11Tuple,
xp13Tuple,
xpAttr,
+ xpDefault,
xpElem,
xpInt,
xpList,
-- Local imports.
import TSN.Codegen ( tsn_codegen_config )
import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
-import TSN.Picklers ( xp_earnings, xp_datetime, xp_time_stamp )
+import TSN.Picklers (
+ xp_earnings,
+ xp_fracpart_only_double,
+ xp_datetime,
+ xp_time_stamp )
import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
import Xml (
Child(..),
-- the \"db_\" prefix since our field namer is going to strip of
-- everything before the first underscore.
--
+-- We make the three fields optional because the entire
+-- \<Most_Laps_Leading\> is apparently optional (although it is
+-- usually present). A 'Nothing' in the XML should get turned into
+-- three 'Nothing's in the DB.
+--
data MostLapsLeading =
MostLapsLeading {
- db_most_laps_leading_driver_id :: Int,
- db_most_laps_leading_driver :: String,
- db_most_laps_leading_number_of_laps :: Int }
+ db_most_laps_leading_driver_id :: Maybe Int,
+ db_most_laps_leading_driver :: Maybe String,
+ db_most_laps_leading_number_of_laps :: Maybe Int }
deriving (Data, Eq, Show, Typeable)
-- | Database representation of a \<Race_Information\> contained
-- within a \<message\>.
--
+-- The 'db_most_laps_leading' field is not optional because when we
+-- convert from our XML representation, a missing 'MostLapsLeading'
+-- will be replaced with a 'MostLapsLeading' with three missing
+-- fields.
+--
data AutoRacingResultsRaceInformation =
AutoRacingResultsRaceInformation {
-- Note the apostrophe to disambiguate it from the
xml_cautions :: Maybe String,
xml_lead_changes :: Maybe String,
xml_lap_leaders :: Maybe String,
- xml_most_laps_leading :: MostLapsLeading }
+ xml_most_laps_leading :: Maybe MostLapsLeading }
deriving (Eq,Show)
db_cautions = xml_cautions,
db_lead_changes = xml_lead_changes,
db_lap_leaders = xml_lap_leaders,
- db_most_laps_leading = xml_most_laps_leading }
+ db_most_laps_leading = most_laps_leading }
+ where
+ -- If we didn't get a \<Most_Laps_Leading\>, indicate that in
+ -- the database with an (embedded) 'MostLapsLeading' with three
+ -- missing fields.
+ most_laps_leading =
+ fromMaybe (MostLapsLeading Nothing Nothing Nothing)
+ xml_most_laps_leading
-- | This allows us to insert the XML representation
-- | Pickler for the \<Most_Laps_Leading\> child of a
--- \<Race_Information\>.
+-- \<Race_Information\>. This is complicated by the fact that the
+-- three fields we're trying to parse are not actually optional;
+-- only the entire \<Most_Laps_Leading\> is. So we always wrap what
+-- we parse in a 'Just', and when converting from the DB to XML,
+-- we'll drop the entire element if any of its fields are missing
+-- (which they never should be).
--
-pickle_most_laps_leading :: PU MostLapsLeading
+pickle_most_laps_leading :: PU (Maybe MostLapsLeading)
pickle_most_laps_leading =
xpElem "Most_Laps_Leading" $
xpWrap (from_tuple, to_tuple) $
- xpTriple (xpElem "DriverID" xpInt)
- (xpElem "Driver" xpText)
- (xpElem "NumberOfLaps" xpInt)
+ xpTriple (xpOption $ xpElem "DriverID" xpInt)
+ (xpOption $ xpElem "Driver" xpText)
+ (xpOption $ 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)
+ from_tuple :: (Maybe Int, Maybe String, Maybe Int) -> Maybe MostLapsLeading
+ from_tuple (Just x, Just y, Just z) =
+ Just $ MostLapsLeading (Just x) (Just y) (Just z)
+ from_tuple _ = Nothing
+
+ -- Sure had to go out of my way to avoid the warnings about unused
+ -- db_most_laps_foo fields here.
+ to_tuple :: Maybe MostLapsLeading -> (Maybe Int, Maybe String, Maybe Int)
+ to_tuple Nothing = (Nothing, Nothing, Nothing)
+ to_tuple (Just (MostLapsLeading Nothing _ _)) = (Nothing, Nothing, Nothing)
+ to_tuple (Just (MostLapsLeading _ Nothing _)) = (Nothing, Nothing, Nothing)
+ to_tuple (Just (MostLapsLeading _ _ Nothing)) = (Nothing, Nothing, Nothing)
+ to_tuple (Just 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\>.
--
+-- There's so much voodoo going on here. We have a double-layered
+-- Maybe on top of the MostLapsLeading. When unpickling, we return a
+-- Nothing (i.e. a Maybe MostLapsLeading) if any of its fields are
+-- missing. But if the entire element is missing, unpickling
+-- fails. 'xpOption' doesn't fix this because it would give us a
+-- Maybe (Maybe MostLapsLeading). But we can use 'xpDefault' with a
+-- default of (Nothing :: Maybe MostLapsLeading) to stick one in
+-- there if unpicking a (Maybe MostLapsLeading) fails because
+-- \<Most_Laps_Leading\> is missing.
+--
+-- Clear as mud, I know.
+--
pickle_race_information :: PU AutoRacingResultsRaceInformationXml
pickle_race_information =
xpElem "Race_Information" $
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 "TrackLength" $
+ xpPair xpText
+ (xpAttr "KPH" xp_fracpart_only_double) )
(xpElem "Laps" xpInt)
(xpOption $ xpElem "AverageSpeedMPH" xpPrim)
(xpOption $ xpElem "AverageSpeedKPH" xpPrim)
(xpOption $ xpElem "Cautions" xpText)
(xpOption $ xpElem "LeadChanges" xpText)
(xpOption $ xpElem "LapLeaders" xpText)
- pickle_most_laps_leading
+ (xpDefault Nothing pickle_most_laps_leading)
where
-- Derp. Since the first two are paired, we have to
-- manually unpack the bazillion arguments.
xml_most_laps_leading m)
--
--- Tasty Tests
+-- * Tasty Tests
--
-- | A list of all tests for this module.
-- 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
+test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
+ [ check "pickle composed with unpickle is the identity"
+ "test/xml/AutoRacingResultsXML.xml",
+
+ check "pickle composed with unpickle is the identity (fractional KPH)"
+ "test/xml/AutoRacingResultsXML-fractional-kph.xml",
+
+ check "pickle composed with unpickle is the identity (No Most_Laps_Leading)"
+ "test/xml/AutoRacingResultsXML-no-most-laps-leading.xml"]
+ where
+ check desc path = testCase desc $ do
+ (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
+test_unpickle_succeeds = testGroup "unpickle tests"
+ [ check "unpickling succeeds"
+ "test/xml/AutoRacingResultsXML.xml",
+
+ check "unpickling succeeds (fractional KPH)"
+ "test/xml/AutoRacingResultsXML-fractional-kph.xml",
- let expected = True
- actual @?= expected
+ check "unpickling succeeds (no Most_Laps_Leading)"
+ "test/xml/AutoRacingResultsXML-no-most-laps-leading.xml" ]
+ where
+ check desc path = testCase desc $ do
+ actual <- unpickleable path pickle_message
+ let expected = True
+ actual @?= expected
-- 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
+test_on_delete_cascade = testGroup "cascading delete tests"
+ [ check "deleting auto_racing_results deletes its children"
+ "test/xml/AutoRacingResultsXML.xml",
+
+ check "deleting auto_racing_results deletes its children (fractional KPH)"
+ "test/xml/AutoRacingResultsXML-fractional-kph.xml",
+
+ check ("deleting auto_racing_results deletes its children " ++
+ "(No Most_Laps_Leading)")
+ "test/xml/AutoRacingResultsXML-no-most-laps-leading.xml" ]
+ where
+ check desc path = testCase desc $ do
+ 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