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 (
48 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
49 import TSN.XmlImport ( XmlImport(..) )
50 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
55 team_league :: Maybe String }
56 deriving (Data, Eq, Show, Typeable)
63 updated :: Maybe Bool }
66 instance FromXml Listing where
67 type Db Listing = Listing
70 instance XmlImport Listing
78 listings :: [Listing],
79 time_stamp :: String }
82 instance DbImport Message where
83 dbimport msg = mapM_ insert_xml (listings msg) >> return ImportSucceeded
85 dbmigrate _ = run_dbmigrate $ migrate (undefined :: Listing)
87 mkPersist defaultCodegenConfig [groundhog|
89 dbName: injuries_listings
95 - {name: team_name, dbName: team_name}
96 - {name: team_league, dbName: team_league}
97 - embedded: InjuriesTeam
104 pickle_injuries_team :: PU InjuriesTeam
105 pickle_injuries_team =
107 xpWrap (from_tuple, to_tuple) $
108 xpPair xpText (xpOption $ xpAttr "league" xpText)
110 from_tuple = uncurryN InjuriesTeam
111 to_tuple m = (team_name m, team_league m)
113 instance XmlPickler InjuriesTeam where
114 xpickle = pickle_injuries_team
116 pickle_listing :: PU Listing
119 xpWrap (from_tuple, to_tuple) $
120 xp4Tuple pickle_injuries_team
121 (xpOption $ xpElem "teamno" xpInt)
122 (xpElem "injuries" xpText)
123 (xpOption $ xpElem "updated" xpPrim)
125 from_tuple = uncurryN Listing
126 to_tuple l = (team l, teamno l, injuries l, updated l)
128 instance XmlPickler Listing where
129 xpickle = pickle_listing
132 pickle_message :: PU Message
135 xpWrap (from_tuple, to_tuple) $
136 xp6Tuple (xpElem "XML_File_ID" xpInt)
137 (xpElem "heading" xpText)
138 (xpElem "category" xpText)
139 (xpElem "sport" xpText)
140 (xpList pickle_listing)
141 (xpElem "time_stamp" xpText)
143 from_tuple = uncurryN Message
144 to_tuple m = (xml_file_id m,
151 instance XmlPickler Message where
152 xpickle = pickle_message
157 injuries_tests :: TestTree
161 [ test_pickle_of_unpickle_is_identity,
162 test_unpickle_succeeds ]
165 -- | Warning, succeess of this test does not mean that unpickling
167 test_pickle_of_unpickle_is_identity :: TestTree
168 test_pickle_of_unpickle_is_identity =
169 testCase "pickle composed with unpickle is the identity" $ do
170 let path = "test/xml/injuriesxml.xml"
171 (expected, actual) <- pickle_unpickle pickle_message path
175 test_unpickle_succeeds :: TestTree
176 test_unpickle_succeeds =
177 testCase "unpickling succeeds" $ do
178 let path = "test/xml/injuriesxml.xml"
179 actual <- unpickleable path pickle_message