1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE StandaloneDeriving #-}
6 {-# LANGUAGE TemplateHaskell #-}
7 {-# LANGUAGE TypeFamilies #-}
9 -- | Parse TSN XML for the DTD "injuriesxml.dtd". Each document
10 -- contains a root element \<message\> that in turn contains zero or
13 -- The listings will be mapped to a database table called "injuries"
14 -- automatically. The root message is not retained.
16 module TSN.XML.Injuries (
21 import Data.Data ( Data )
22 import Data.Typeable ( Typeable )
23 import Database.Groundhog (
25 import Database.Groundhog.TH (
29 import Data.Tuple.Curry ( uncurryN )
30 import Test.Tasty ( TestTree, testGroup )
31 import Test.Tasty.HUnit ( (@?=), testCase )
32 import Text.XML.HXT.Core (
47 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
48 import TSN.XmlImport ( XmlImport(..) )
49 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
54 team_league :: Maybe String }
55 deriving (Data, Eq, Show, Typeable)
62 updated :: Maybe Bool }
65 instance FromXml Listing where
66 type Db Listing = Listing
69 instance XmlImport Listing
77 listings :: [Listing],
78 time_stamp :: String }
81 instance DbImport Message where
82 dbimport msg = mapM_ insert_xml (listings msg) >> return ImportSucceeded
84 dbmigrate _ = run_dbmigrate $ migrate (undefined :: Listing)
86 mkPersist defaultCodegenConfig [groundhog|
88 dbName: injuries_listings
94 - {name: team_name, dbName: team_name}
95 - {name: team_league, dbName: team_league}
96 - embedded: InjuriesTeam
103 pickle_injuries_team :: PU InjuriesTeam
104 pickle_injuries_team =
106 xpWrap (from_tuple, to_tuple) $
107 xpPair xpText (xpOption $ xpAttr "league" xpText)
109 from_tuple = uncurryN InjuriesTeam
110 to_tuple m = (team_name m, team_league m)
113 pickle_listing :: PU Listing
116 xpWrap (from_tuple, to_tuple) $
117 xp4Tuple pickle_injuries_team
118 (xpOption $ xpElem "teamno" xpInt)
119 (xpElem "injuries" xpText)
120 (xpOption $ xpElem "updated" xpPrim)
122 from_tuple = uncurryN Listing
123 to_tuple l = (team l, teamno l, injuries l, updated l)
126 pickle_message :: PU Message
129 xpWrap (from_tuple, to_tuple) $
130 xp6Tuple (xpElem "XML_File_ID" xpInt)
131 (xpElem "heading" xpText)
132 (xpElem "category" xpText)
133 (xpElem "sport" xpText)
134 (xpList pickle_listing)
135 (xpElem "time_stamp" xpText)
137 from_tuple = uncurryN Message
138 to_tuple m = (xml_file_id m,
148 injuries_tests :: TestTree
152 [ test_pickle_of_unpickle_is_identity,
153 test_unpickle_succeeds ]
156 -- | Warning, succeess of this test does not mean that unpickling
158 test_pickle_of_unpickle_is_identity :: TestTree
159 test_pickle_of_unpickle_is_identity =
160 testCase "pickle composed with unpickle is the identity" $ do
161 let path = "test/xml/injuriesxml.xml"
162 (expected, actual) <- pickle_unpickle pickle_message path
166 test_unpickle_succeeds :: TestTree
167 test_unpickle_succeeds =
168 testCase "unpickling succeeds" $ do
169 let path = "test/xml/injuriesxml.xml"
170 actual <- unpickleable path pickle_message