X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FGameInfo.hs;h=2b5e1adb9ed3b45ad803a26479811088ab5099e6;hb=6eb1c7477c2d4d3cace6d1b865a5efbec21300a7;hp=b3ac3f6e51a844fbc4f4261f38d0af7d15f8ae2b;hpb=dcaa338a8e638ff20890f949fd116fab0228e050;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/GameInfo.hs b/src/TSN/XML/GameInfo.hs index b3ac3f6..2b5e1ad 100644 --- a/src/TSN/XML/GameInfo.hs +++ b/src/TSN/XML/GameInfo.hs @@ -1,3 +1,10 @@ +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE GADTs #-} +{-# LANGUAGE QuasiQuotes #-} +{-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE TypeFamilies #-} + -- | GameInfo represents a collection of DTDs that we don't really -- handle but want to make available. The raw XML gets stored in the -- database along with the XML_File_ID, but we don't parse any of it. @@ -5,10 +12,47 @@ -- See also: TSN.XML.SportInfo -- module TSN.XML.GameInfo ( - dtds ) + dtds, + game_info_tests, + parse_xml, + -- * WARNING: these are private but exported to silence warnings + GameInfoConstructor(..) ) where --- | The DTDs for everything that we consider "Game Info." +-- System imports. +import Data.Either ( rights ) +import Data.String.Utils ( replace ) +import Data.Time.Clock ( UTCTime ) +import Database.Groundhog ( + countAll, + insert_, + migrate, + runMigration, + silentMigrationLogger ) +import Database.Groundhog.Generic ( runDbConn ) +import Database.Groundhog.Sqlite ( withSqliteConn ) +import Database.Groundhog.TH ( + defaultCodegenConfig, + 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.DbImport ( + DbImport(..), + ImportResult(..), + run_dbmigrate ) +import TSN.Parse ( + parse_message, + parse_xmlfid, + parse_xml_time_stamp ) +import Xml ( unsafe_read_document ) + + +-- | 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 @@ -22,12 +66,10 @@ dtds = "Matchup_NBA_NHL_XML.dtd", "mlbpreviewxml.dtd", "MLB_Gaming_Matchup_XML.dtd", - "MLB.dtd", "MLB_Lineup_XML.dtd", "MLB_Matchup_XML.dtd", "MLS_Preview_XML.dtd", "NBA_Gaming_Matchup_XML.dtd", - "NBA.dtd", "NBA_Playoff_Matchup_XML.dtd", "NBALineupXML.dtd", "nbapreviewxml.dtd", @@ -37,3 +79,137 @@ dtds = "nhlpreviewxml.dtd", "recapxml.dtd", "WorldBaseballPreviewXML.dtd" ] + + +-- | This serves as both the database and XML representation of a +-- GameInfo \. +-- +data GameInfo = + GameInfo { + dtd :: String, + xml_file_id :: Int, + time_stamp :: UTCTime, + xml :: String } + deriving (Eq, Show) + + +-- | Attempt to parse a 'GameInfo' from an 'XmlTree'. If we cannot, +-- we fail with an error message. +-- +parse_xml :: String -> XmlTree -> Either String GameInfo +parse_xml dtdname xmltree = do + xmlfid <- parse_xmlfid xmltree + timestamp <- parse_xml_time_stamp xmltree + message <- parse_message xmltree + return $ GameInfo dtdname (fromInteger xmlfid) timestamp (xshow [message]) + +-- +-- Database code +-- + +instance DbImport GameInfo where + dbmigrate _ = + run_dbmigrate $ migrate (undefined :: GameInfo) + + -- | We import a 'GameInfo' by inserting the whole thing at + -- once. Nothing fancy going on here. + dbimport msg = do + insert_ msg + return ImportSucceeded + + +-- | The database schema for GameInfo is trivial; all we need is for +-- the XML_File_ID to be unique. +-- +mkPersist defaultCodegenConfig [groundhog| +- entity: GameInfo + dbName: game_info + constructors: + - name: GameInfo + uniques: + - name: unique_game_info + type: constraint + # Prevent multiple imports of the same message. + fields: [xml_file_id] +|] + + +-- +-- Tasty Tests +-- + +-- | A list of all tests for this module. +-- +game_info_tests :: TestTree +game_info_tests = + testGroup + "GameInfo tests" + [ test_accessors, + test_parse_xml_succeeds, + test_dbimport_succeeds ] + + +-- | Make sure the accessors work and that we can parse one file. Ok, +-- so the real point of this is to make the unused fields (dtd, xml, +-- ...) warning go away without having to mangle the groundhog code. +-- +test_accessors :: TestTree +test_accessors = testCase "we can access a parsed game_info" $ do + xmltree <- unsafe_read_document "test/xml/gameinfo/recapxml.xml" + let Right t = parse_xml "recapxml.dtd" xmltree + let a1 = dtd t + let ex1 = "recapxml.dtd" + let a2 = xml_file_id t + let ex2 = 21201550 + let a3 = show $ time_stamp t + let ex3 = "2014-05-31 15:13:00 UTC" + let a4 = take 9 (xml t) + let ex4 = "" + let actual = (a1,a2,a3,a4) + let expected = (ex1,ex2,ex3,ex4) + actual @?= expected + + +-- | Sample XML documents for GameInfo types. +-- +game_info_test_files :: [FilePath] +game_info_test_files = + map (change_suffix . add_path) dtds + where + add_path = ("test/xml/gameinfo/" ++ ) + change_suffix = replace ".dtd" ".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 $ + migrate (undefined :: GameInfo) + mapM_ dbimport msgs + countAll (undefined :: GameInfo) + + actual @?= expected + where + expected = length game_info_test_files