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 (
25 defaultMigrationLogger,
28 import Database.Groundhog.TH (
32 import Data.Tuple.Curry ( uncurryN )
33 import Test.Tasty ( TestTree, testGroup )
34 import Test.Tasty.HUnit ( (@?=), testCase )
35 import Text.XML.HXT.Core (
51 import TSN.DbImport ( DbImport(..), ImportResult(..) )
52 import TSN.XmlImport ( XmlImport(..) )
53 import Xml ( FromXml(..), pickle_unpickle, unpickleable )
58 team_league :: Maybe String }
59 deriving (Data, Eq, Show, Typeable)
66 updated :: Maybe Bool }
69 instance FromXml Listing where
70 type Db Listing = Listing
73 instance XmlImport Listing
81 listings :: [Listing],
82 time_stamp :: String }
85 instance DbImport Message where
86 dbimport msg = mapM_ insert_xml (listings msg) >> return ImportSucceeded
89 runMigration defaultMigrationLogger $ migrate (undefined :: Listing)
91 mkPersist defaultCodegenConfig [groundhog|
99 - {name: team_name, dbName: team_name}
100 - {name: team_league, dbName: team_league}
101 - embedded: InjuriesTeam
108 pickle_injuries_team :: PU InjuriesTeam
109 pickle_injuries_team =
111 xpWrap (from_tuple, to_tuple) $
112 xpPair xpText (xpOption $ xpAttr "league" xpText)
114 from_tuple = uncurryN InjuriesTeam
115 to_tuple m = (team_name m, team_league m)
117 instance XmlPickler InjuriesTeam where
118 xpickle = pickle_injuries_team
120 pickle_listing :: PU Listing
123 xpWrap (from_tuple, to_tuple) $
124 xp4Tuple pickle_injuries_team
125 (xpOption $ xpElem "teamno" xpInt)
126 (xpElem "injuries" xpText)
127 (xpOption $ xpElem "updated" xpPrim)
129 from_tuple = uncurryN Listing
130 to_tuple l = (team l, teamno l, injuries l, updated l)
132 instance XmlPickler Listing where
133 xpickle = pickle_listing
136 pickle_message :: PU Message
139 xpWrap (from_tuple, to_tuple) $
140 xp6Tuple (xpElem "XML_File_ID" xpInt)
141 (xpElem "heading" xpText)
142 (xpElem "category" xpText)
143 (xpElem "sport" xpText)
144 (xpList pickle_listing)
145 (xpElem "time_stamp" xpText)
147 from_tuple = uncurryN Message
148 to_tuple m = (xml_file_id m,
155 instance XmlPickler Message where
156 xpickle = pickle_message
161 injuries_tests :: TestTree
165 [ test_pickle_of_unpickle_is_identity,
166 test_unpickle_succeeds ]
169 -- | Warning, succeess of this test does not mean that unpickling
171 test_pickle_of_unpickle_is_identity :: TestTree
172 test_pickle_of_unpickle_is_identity =
173 testCase "pickle composed with unpickle is the identity" $ do
174 let path = "test/xml/injuriesxml.xml"
175 (expected :: [Message], actual) <- pickle_unpickle "message" path
179 test_unpickle_succeeds :: TestTree
180 test_unpickle_succeeds =
181 testCase "unpickling succeeds" $ do
182 let path = "test/xml/injuriesxml.xml"
183 actual <- unpickleable path pickle_message