1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE ScopedTypeVariables #-}
6 {-# LANGUAGE StandaloneDeriving #-}
7 {-# LANGUAGE TemplateHaskell #-}
8 {-# LANGUAGE TypeFamilies #-}
10 -- | Parse TSN XML for the DTD "injuriesxml.dtd". Each document
11 -- contains a root element \<message\> that in turn contains zero or
14 -- The listings will be mapped to a database table called "injuries"
15 -- automatically. The root message is not retained.
17 module TSN.XML.Injuries (
22 import Data.Data ( Data )
23 import Data.Typeable ( Typeable )
24 import Database.Groundhog (
26 import Database.Groundhog.TH (
30 import Data.Tuple.Curry ( uncurryN )
31 import Test.Tasty ( TestTree, testGroup )
32 import Test.Tasty.HUnit ( (@?=), testCase )
33 import Text.XML.HXT.Core (
49 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
50 import TSN.XmlImport ( XmlImport(..) )
51 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
56 team_league :: Maybe String }
57 deriving (Data, Eq, Show, Typeable)
64 updated :: Maybe Bool }
67 instance FromXml Listing where
68 type Db Listing = Listing
71 instance XmlImport Listing
79 listings :: [Listing],
80 time_stamp :: String }
83 instance DbImport Message where
84 dbimport msg = mapM_ insert_xml (listings msg) >> return ImportSucceeded
86 dbmigrate _ = run_dbmigrate $ migrate (undefined :: Listing)
88 mkPersist defaultCodegenConfig [groundhog|
96 - {name: team_name, dbName: team_name}
97 - {name: team_league, dbName: team_league}
98 - embedded: InjuriesTeam
105 pickle_injuries_team :: PU InjuriesTeam
106 pickle_injuries_team =
108 xpWrap (from_tuple, to_tuple) $
109 xpPair xpText (xpOption $ xpAttr "league" xpText)
111 from_tuple = uncurryN InjuriesTeam
112 to_tuple m = (team_name m, team_league m)
114 instance XmlPickler InjuriesTeam where
115 xpickle = pickle_injuries_team
117 pickle_listing :: PU Listing
120 xpWrap (from_tuple, to_tuple) $
121 xp4Tuple pickle_injuries_team
122 (xpOption $ xpElem "teamno" xpInt)
123 (xpElem "injuries" xpText)
124 (xpOption $ xpElem "updated" xpPrim)
126 from_tuple = uncurryN Listing
127 to_tuple l = (team l, teamno l, injuries l, updated l)
129 instance XmlPickler Listing where
130 xpickle = pickle_listing
133 pickle_message :: PU Message
136 xpWrap (from_tuple, to_tuple) $
137 xp6Tuple (xpElem "XML_File_ID" xpInt)
138 (xpElem "heading" xpText)
139 (xpElem "category" xpText)
140 (xpElem "sport" xpText)
141 (xpList pickle_listing)
142 (xpElem "time_stamp" xpText)
144 from_tuple = uncurryN Message
145 to_tuple m = (xml_file_id m,
152 instance XmlPickler Message where
153 xpickle = pickle_message
158 injuries_tests :: TestTree
162 [ test_pickle_of_unpickle_is_identity,
163 test_unpickle_succeeds ]
166 -- | Warning, succeess of this test does not mean that unpickling
168 test_pickle_of_unpickle_is_identity :: TestTree
169 test_pickle_of_unpickle_is_identity =
170 testCase "pickle composed with unpickle is the identity" $ do
171 let path = "test/xml/injuriesxml.xml"
172 (expected :: [Message], actual) <- pickle_unpickle "message" path
176 test_unpickle_succeeds :: TestTree
177 test_unpickle_succeeds =
178 testCase "unpickling succeeds" $ do
179 let path = "test/xml/injuriesxml.xml"
180 actual <- unpickleable path pickle_message