X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FGameInfo.hs;h=8938200ec480e7765cd8d75732d3c1a635be7b9c;hb=b45f24209ce6f1eb3c24a5f01a71f5940a001c02;hp=375a77ad0efda3da6f365a4e61be6391911002a0;hpb=d80ec9748b20888a1aae1828e1b622c6476e1992;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/GameInfo.hs b/src/TSN/XML/GameInfo.hs index 375a77a..8938200 100644 --- a/src/TSN/XML/GameInfo.hs +++ b/src/TSN/XML/GameInfo.hs @@ -13,23 +13,36 @@ -- module TSN.XML.GameInfo ( dtds, + game_info_tests, parse_xml, -- * WARNING: these are private but exported to silence warnings GameInfoConstructor(..) ) where -- System imports. +import Data.Either ( rights ) import Data.Time.Clock ( UTCTime ) -import Database.Groundhog ( migrate ) +import Database.Groundhog ( + countAll, + migrate, + runMigration, + silentMigrationLogger ) +import Database.Groundhog.Generic ( runDbConn ) +import Database.Groundhog.Sqlite ( withSqliteConn ) import Database.Groundhog.TH ( groundhog, mkPersist ) +import Test.Tasty ( TestTree, testGroup ) +import Test.Tasty.HUnit ( (@?=), testCase ) import Text.XML.HXT.Core ( XmlTree ) import Text.XML.HXT.DOM.ShowXml ( xshow ) -- Local imports. import TSN.Codegen ( tsn_codegen_config ) -import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate ) +import TSN.DbImport ( + DbImport(..), + ImportResult(..), + run_dbmigrate ) import TSN.Parse ( parse_message, parse_xmlfid, @@ -37,9 +50,11 @@ import TSN.Parse ( import TSN.XmlImport ( XmlImport(..) ) import Xml ( FromXml(..), - ToDb(..) ) + ToDb(..), + unsafe_read_document ) --- | The DTDs for everything that we consider "Game Info." + +-- | The DTDs for everything that we consider \"Game Info.\" -- -- TODO: This is the list from the old implementation. We need to -- make sure that we are really receiving XML for these DTDs @@ -49,16 +64,14 @@ dtds :: [String] dtds = [ "CBASK_Lineup_XML.dtd", "cbaskpreviewxml.dtd", - "cflpreviewxml.dtd", -- missing DTD + "cflpreviewxml.dtd", "Matchup_NBA_NHL_XML.dtd", "mlbpreviewxml.dtd", "MLB_Gaming_Matchup_XML.dtd", - "MLB.dtd", -- missing DTD "MLB_Lineup_XML.dtd", "MLB_Matchup_XML.dtd", "MLS_Preview_XML.dtd", "NBA_Gaming_Matchup_XML.dtd", - "NBA.dtd", -- missing DTD "NBA_Playoff_Matchup_XML.dtd", "NBALineupXML.dtd", "nbapreviewxml.dtd", @@ -67,8 +80,7 @@ dtds = "NFL_NCAA_FB_Matchup_XML.dtd", "nhlpreviewxml.dtd", "recapxml.dtd", - "WorldBaseballPreviewXML.dtd" -- missing DTD - ] + "WorldBaseballPreviewXML.dtd" ] -- | XML representation of a GameInfo \. @@ -137,6 +149,10 @@ instance DbImport Message where insert_xml_ msg return ImportSucceeded + +-- | The database schema for GameInfo is trivial; all we need is for +-- the XML_File_ID to be unique. +-- mkPersist tsn_codegen_config [groundhog| - entity: GameInfo constructors: @@ -147,3 +163,78 @@ mkPersist tsn_codegen_config [groundhog| # Prevent multiple imports of the same message. fields: [db_xml_file_id] |] + + +-- +-- Tasty Tests +-- + +-- | A list of all tests for this module. +-- +game_info_tests :: TestTree +game_info_tests = + testGroup + "GameInfo tests" + [ test_parse_xml_succeeds, + test_dbimport_succeeds ] + + +-- | Sample XML documents for GameInfo types. +-- +game_info_test_files :: [FilePath] +game_info_test_files = + [ "test/xml/gameinfo/CBASK_Lineup_XML.xml", + "test/xml/gameinfo/cbaskpreviewxml.xml", + "test/xml/gameinfo/cflpreviewxml.xml", + "test/xml/gameinfo/Matchup_NBA_NHL_XML.xml", + "test/xml/gameinfo/MLB_Gaming_Matchup_XML.xml", + "test/xml/gameinfo/MLB_Lineup_XML.xml", + "test/xml/gameinfo/MLB_Matchup_XML.xml", + "test/xml/gameinfo/mlbpreviewxml.xml", + "test/xml/gameinfo/MLS_Preview_XML.xml", + "test/xml/gameinfo/NBA_Gaming_Matchup_XML.xml", + "test/xml/gameinfo/NBALineupXML.xml", + "test/xml/gameinfo/NBA_Playoff_Matchup_XML.xml", + "test/xml/gameinfo/NCAA_FB_Preview_XML.xml", + "test/xml/gameinfo/nbapreviewxml.xml", + "test/xml/gameinfo/nflpreviewxml.xml", + "test/xml/gameinfo/NFL_NCAA_FB_Matchup_XML.xml", + "test/xml/gameinfo/nhlpreviewxml.xml", + "test/xml/gameinfo/recapxml.xml", + "test/xml/gameinfo/WorldBaseballPreviewXML.xml" ] + + +-- | Make sure we can parse every element of 'game_info_test_files'. +-- +test_parse_xml_succeeds :: TestTree +test_parse_xml_succeeds = + testGroup "parse_xml" $ map check game_info_test_files + where + check t = testCase t $ do + x <- unsafe_read_document t + let result = parse_xml "dummy" x + let actual = case result of -- isRight appears in base-4.7 + Left _ -> False + Right _ -> True + let expected = True + actual @?= expected + + +-- | Ensure that each element of 'game_info_test_files' can be imported +-- by counting the total number of database records (after +-- importing) and comparing it against the length of +-- 'game_info_test_files'. +-- +test_dbimport_succeeds :: TestTree +test_dbimport_succeeds = testCase "dbimport succeeds" $ do + xmltrees <- mapM unsafe_read_document game_info_test_files + let msgs = rights $ map (parse_xml "dummy") xmltrees + actual <- withSqliteConn ":memory:" $ runDbConn $ do + runMigration silentMigrationLogger $ do + migrate (undefined :: GameInfo) + mapM_ dbimport msgs + countAll (undefined :: GameInfo) + + actual @?= expected + where + expected = length game_info_test_files