1 {-# LANGUAGE ScopedTypeVariables #-}
3 -- | Handle documents defined by Heartbeat.dtd.
5 module TSN.XML.Heartbeat (
13 import Data.Time.Clock ( UTCTime )
14 import Data.Tuple.Curry ( uncurryN )
15 import Test.Tasty ( TestTree, testGroup )
16 import Test.Tasty.HUnit ( (@?=), testCase )
17 import Text.XML.HXT.Core (
28 import TSN.DbImport ( ImportResult(..) )
29 import TSN.Picklers ( xp_time_stamp )
30 import Xml ( pickle_unpickle, unpickleable )
33 -- | The DTD to which this module corresponds.
38 -- | The data structure that holds the XML representation of a
45 time_stamp :: UTCTime }
49 -- | A (un)pickler that turns a Heartbeat XML file into a 'Message'
52 pickle_message :: PU Message
55 xpWrap (from_tuple, to_tuple) $
56 xpTriple (xpElem "XML_File_ID" xpInt)
57 (xpElem "heading" xpText)
58 (xpElem "time_stamp" xp_time_stamp)
60 from_tuple = uncurryN Message
61 to_tuple m = (xml_file_id m,
66 -- | Verify (and report) the received heartbeat. We return
67 -- 'ImportSkipped' because we want to indicate that we processed the
68 -- file but there was nothing to import.
70 verify :: XmlTree -> IO ImportResult
72 let root_element = unpickleDoc pickle_message xml
73 return $ case root_element of
74 Nothing -> ImportFailed "Could not unpickle document to be verified."
75 Just _ -> ImportSkipped "Heartbeat received. Thump."
81 -- | A list of all tests for this module.
83 heartbeat_tests :: TestTree
87 [ test_pickle_of_unpickle_is_identity,
88 test_unpickle_succeeds ]
91 -- | If we unpickle something and then pickle it, we should wind up
92 -- with the same thing we started with. WARNING: success of this
93 -- test does not mean that unpickling succeeded.
95 test_pickle_of_unpickle_is_identity :: TestTree
96 test_pickle_of_unpickle_is_identity =
97 testCase "pickle composed with unpickle is the identity" $ do
98 let path = "test/xml/Heartbeat.xml"
99 (expected :: [Message], actual) <- pickle_unpickle pickle_message path
103 -- | Make sure we can unpickle the sample file.
105 test_unpickle_succeeds :: TestTree
106 test_unpickle_succeeds =
107 testCase "unpickling succeeds" $ do
108 let path = "test/xml/Heartbeat.xml"
109 actual <- unpickleable path pickle_message