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 (
23 import Data.Data ( Data )
24 import Data.Typeable ( Typeable )
25 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(..), import_generic )
50 import Xml ( pickle_unpickle, unpickleable )
56 team_league :: Maybe String }
57 deriving (Data, Eq, Show, Typeable)
64 updated :: Maybe Bool }
73 listings :: [Listing],
74 time_stamp :: String }
78 mkPersist defaultCodegenConfig [groundhog|
86 - {name: team_name, dbName: team_name}
87 - {name: team_league, dbName: team_league}
88 - embedded: InjuriesTeam
95 pickle_injuries_team :: PU InjuriesTeam
96 pickle_injuries_team =
98 xpWrap (from_tuple, to_tuple) $
99 xpPair xpText (xpAttr "league" (xpOption xpText))
101 from_tuple = uncurryN InjuriesTeam
102 to_tuple m = (team_name m, team_league m)
104 instance XmlPickler InjuriesTeam where
105 xpickle = pickle_injuries_team
107 pickle_listing :: PU Listing
110 xpWrap (from_tuple, to_tuple) $
111 xp4Tuple pickle_injuries_team
112 (xpOption $ xpElem "teamno" xpInt)
113 (xpElem "injuries" xpText)
114 (xpOption $ xpElem "updated" xpPrim)
116 from_tuple = uncurryN Listing
117 to_tuple l = (team l, teamno l, injuries l, updated l)
119 instance XmlPickler Listing where
120 xpickle = pickle_listing
123 pickle_message :: PU Message
126 xpWrap (from_tuple, to_tuple) $
127 xp6Tuple (xpElem "XML_File_ID" xpInt)
128 (xpElem "heading" xpText)
129 (xpElem "category" xpText)
130 (xpElem "sport" xpText)
131 (xpList pickle_listing)
132 (xpElem "time_stamp" xpText)
134 from_tuple = uncurryN Message
135 to_tuple m = (xml_file_id m,
142 instance XmlPickler Message where
143 xpickle = pickle_message
147 instance DbImport Listing where
148 dbimport = import_generic listings
151 injuries_tests :: TestTree
155 [ test_pickle_of_unpickle_is_identity,
156 test_unpickle_succeeds ]
159 -- | Warning, succeess of this test does not mean that unpickling
161 test_pickle_of_unpickle_is_identity :: TestTree
162 test_pickle_of_unpickle_is_identity =
163 testCase "pickle composed with unpickle is the identity" $ do
164 let path = "test/xml/injuriesxml.xml"
165 (expected :: [Message], actual) <- pickle_unpickle "message" path
169 test_unpickle_succeeds :: TestTree
170 test_unpickle_succeeds =
171 testCase "unpickling succeeds" $ do
172 let path = "test/xml/injuriesxml.xml"
173 actual <- unpickleable path pickle_message