X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTSN%2FXML%2FInjuries.hs;h=94baa19758ad1cbf961c8a2f0a690f3763ba1538;hb=bd75961380ef07c2ba0ab5412e4326a197fd40c5;hp=bea54d3c029deea97f395c4b444d538d9b24ead9;hpb=778250be6038d4c637e761e132f074e9bb206eaa;p=dead%2Fhtsn-import.git diff --git a/src/TSN/XML/Injuries.hs b/src/TSN/XML/Injuries.hs index bea54d3..94baa19 100644 --- a/src/TSN/XML/Injuries.hs +++ b/src/TSN/XML/Injuries.hs @@ -2,7 +2,6 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE QuasiQuotes #-} -{-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} @@ -11,18 +10,23 @@ -- contains a root element \ that in turn contains zero or -- more \s. -- --- The listings will be mapped to a database table called "injuries" --- automatically. The root message is not retained. +-- The listings will be mapped to a database table called +-- \"injuries_listings\" automatically. The root message is not +-- retained. -- module TSN.XML.Injuries ( - Listing, - Message( listings ), - injuries_tests ) + pickle_message, + -- * Tests + injuries_tests, + -- * WARNING: these are private but exported to silence warnings + ListingConstructor(..) ) where +-- System imports. import Data.Data ( Data ) import Data.Typeable ( Typeable ) -import Database.Groundhog() +import Database.Groundhog ( + migrate ) import Database.Groundhog.TH ( defaultCodegenConfig, groundhog, @@ -32,10 +36,9 @@ import Test.Tasty ( TestTree, testGroup ) import Test.Tasty.HUnit ( (@?=), testCase ) import Text.XML.HXT.Core ( PU, - XmlPickler(..), xp4Tuple, xp6Tuple, - xpAttr, + xpAttrImplied, xpElem, xpInt, xpList, @@ -45,17 +48,23 @@ import Text.XML.HXT.Core ( xpText, xpWrap ) +-- Local imports. +import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate ) +import TSN.XmlImport ( XmlImport(..) ) +import Xml ( FromXml(..), pickle_unpickle, unpickleable ) -import TSN.DbImport ( DbImport(..), import_generic ) -import Xml ( pickle_unpickle, unpickleable ) - - +-- | XML/Database representation of a team as they appear in the +-- injuries documents. +-- data InjuriesTeam = InjuriesTeam { team_name :: String, team_league :: Maybe String } deriving (Data, Eq, Show, Typeable) + +-- | XML/Database representation of the injury listings. +-- data Listing = Listing { team :: InjuriesTeam, @@ -64,6 +73,23 @@ data Listing = updated :: Maybe Bool } deriving (Eq, Show) + +instance FromXml Listing where + -- | The DB analogue of a 'Listing' is... itself! + type Db Listing = Listing + + -- | To convert between a 'Listing' and a 'Listing', we do nothing. + from_xml = id + +-- | This lets us call 'insert_xml' on a 'Listing' without having to +-- explicitly convert it to its database analogue first. +-- +instance XmlImport Listing + + +-- | XML representation of an injuriesxml \. This is only +-- used for (un)pickling; 'Message's are not saved to the database. +-- data Message = Message { xml_file_id :: Int, @@ -71,13 +97,20 @@ data Message = category :: String, sport :: String, listings :: [Listing], - time_stamp :: String } + time_stamp :: String -- ^ Slightly lax, but we don't save it, so who cares. + } deriving (Eq, Show) +instance DbImport Message where + -- | We import a 'Message' by inserting all of its 'listings'. + -- + dbimport msg = mapM_ insert_xml (listings msg) >> return ImportSucceeded + + dbmigrate _ = run_dbmigrate $ migrate (undefined :: Listing) mkPersist defaultCodegenConfig [groundhog| - entity: Listing - dbName: injuries + dbName: injuries_listings constructors: - name: Listing fields: @@ -92,18 +125,20 @@ mkPersist defaultCodegenConfig [groundhog| |] +-- | A pickler for 'InjuriesTeam's that can convert them to/from XML. +-- pickle_injuries_team :: PU InjuriesTeam pickle_injuries_team = xpElem "team" $ xpWrap (from_tuple, to_tuple) $ - xpPair xpText (xpAttr "league" (xpOption xpText)) + xpPair xpText (xpAttrImplied "league" xpText) where from_tuple = uncurryN InjuriesTeam to_tuple m = (team_name m, team_league m) -instance XmlPickler InjuriesTeam where - xpickle = pickle_injuries_team +-- | A pickler for 'Listings's that can convert them to/from XML. +-- pickle_listing :: PU Listing pickle_listing = xpElem "listing" $ @@ -116,10 +151,9 @@ pickle_listing = from_tuple = uncurryN Listing to_tuple l = (team l, teamno l, injuries l, updated l) -instance XmlPickler Listing where - xpickle = pickle_listing - +-- | A pickler for 'Message's that can convert them to/from XML. +-- pickle_message :: PU Message pickle_message = xpElem "message" $ @@ -139,15 +173,13 @@ pickle_message = listings m, time_stamp m) -instance XmlPickler Message where - xpickle = pickle_message - +-- +-- Tasty Tests +-- -instance DbImport Listing where - dbimport = import_generic listings - --- * Tasty Tests +-- | A list of all tests for this module. +-- injuries_tests :: TestTree injuries_tests = testGroup @@ -156,16 +188,20 @@ injuries_tests = test_unpickle_succeeds ] --- | Warning, succeess of this test does not mean that unpickling --- succeeded. +-- | 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/injuriesxml.xml" - (expected :: [Message], actual) <- pickle_unpickle "message" path + (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