]> gitweb.michael.orlitzky.com - dead/htsn-import.git/commitdiff
Make the MostLapsLeading embedded fields of AutoRacingResults optional.
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 24 Sep 2014 01:57:51 +0000 (21:57 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 24 Sep 2014 01:57:51 +0000 (21:57 -0400)
Add a test case for the newly-optional fields and fix a shelltest.
Update the AutoRacingResultsXML dbschema diagram.

doc/dbschema/AutoRacingResultsXML.png
src/TSN/XML/AutoRacingResults.hs
test/shell/import-duplicates.test

index 9ea9386f0ae9fdc2fdcd5b2ef0b479f3dee6c42f..7f71ddc1d9911e6546291a7bf1b82d0ba63a4302 100644 (file)
Binary files a/doc/dbschema/AutoRacingResultsXML.png and b/doc/dbschema/AutoRacingResultsXML.png differ
index 539c4380dfa68bc9d8fdf763b2e043fd0129a1f8..e7ab014e50579c77fe9488639bd42549ce75f3b6 100644 (file)
@@ -46,6 +46,7 @@ import Text.XML.HXT.Core (
   xp11Tuple,
   xp13Tuple,
   xpAttr,
+  xpDefault,
   xpElem,
   xpInt,
   xpList,
@@ -259,17 +260,27 @@ instance XmlImportFk AutoRacingResultsListingXml
 --   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
@@ -306,7 +317,7 @@ data AutoRacingResultsRaceInformationXml =
     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)
 
 
@@ -345,8 +356,15 @@ instance FromXmlFk AutoRacingResultsRaceInformationXml where
       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 =
+        case xml_most_laps_leading of
+          Just mll -> mll
+          Nothing  -> MostLapsLeading Nothing Nothing Nothing
 
 -- | This allows us to insert the XML representation
 --   'AutoRacingResultsRaceInformationXml' directly.
@@ -507,24 +525,52 @@ pickle_message =
 
 
 -- | 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" $
@@ -544,7 +590,7 @@ pickle_race_information =
               (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.
@@ -589,7 +635,10 @@ test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
           "test/xml/AutoRacingResultsXML.xml",
 
     check "pickle composed with unpickle is the identity (fractional KPH)"
-          "test/xml/AutoRacingResultsXML-fractional-kph.xml" ]
+          "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
@@ -605,7 +654,10 @@ test_unpickle_succeeds = testGroup "unpickle tests"
           "test/xml/AutoRacingResultsXML.xml",
 
     check "unpickling succeeds (fractional KPH)"
-          "test/xml/AutoRacingResultsXML-fractional-kph.xml" ]
+          "test/xml/AutoRacingResultsXML-fractional-kph.xml",
+
+    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
@@ -623,7 +675,11 @@ test_on_delete_cascade = testGroup "cascading delete tests"
           "test/xml/AutoRacingResultsXML.xml",
 
     check "deleting auto_racing_results deletes its children (fractional KPH)"
-          "test/xml/AutoRacingResultsXML-fractional-kph.xml" ]
+          "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
index 33b1e78339b90b67a2f52e10520097d5f5405e60..53ab0bd5ef9eb47a59e31093e9b55aaf981cebbc 100644 (file)
@@ -16,15 +16,15 @@ rm -f shelltest.sqlite3
 # and a newsxml that aren't really supposed to import.
 find ./test/xml -maxdepth 1 -name '*.xml' | wc -l
 >>>
-30
+31
 >>>= 0
 
 # Run the imports again; we should get complaints about the duplicate
-# xml_file_ids. There are 2 errors for each violation, so we expect 2*26
+# xml_file_ids. There are 2 errors for each violation, so we expect 2*27
 # occurrences of the string 'ERROR'.
 ./dist/build/htsn-import/htsn-import -c 'shelltest.sqlite3' test/xml/*.xml 2>&1 | grep ERROR | wc -l
 >>>
-52
+54
 >>>= 0
 
 # Finally, clean up after ourselves.