1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
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
15 -- \"injuries_listings\" automatically. The root message is retained
16 -- so that we can easily delete its associated listings based on its
19 module TSN.XML.Injuries (
23 -- * WARNING: these are private but exported to silence warnings
24 InjuriesConstructor(..),
25 InjuriesListingConstructor(..) )
29 import Data.Data ( Data )
30 import Data.Time ( UTCTime )
31 import Data.Typeable ( Typeable )
32 import Database.Groundhog ( migrate )
33 import Database.Groundhog.Core ( DefaultKey )
34 import Database.Groundhog.TH (
37 import Data.Tuple.Curry ( uncurryN )
38 import Test.Tasty ( TestTree, testGroup )
39 import Test.Tasty.HUnit ( (@?=), testCase )
40 import Text.XML.HXT.Core (
55 import TSN.Codegen ( tsn_codegen_config )
56 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
57 import TSN.Picklers ( xp_time_stamp )
58 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
66 -- | XML/Database representation of a team as they appear in the
67 -- injuries documents.
71 db_team_name :: String,
72 db_team_league :: Maybe String }
73 deriving (Data, Eq, Show, Typeable)
76 -- | XML/Database representation of the injury listings.
78 data InjuriesListingXml =
80 xml_team :: InjuriesTeam,
81 xml_teamno :: Maybe Int,
82 xml_injuries :: String,
83 xml_updated :: Maybe Bool }
86 -- | Database representation of a 'InjuriesListing'. It possesses a
87 -- foreign key to an 'Injuries' object so that we can easily delete
88 -- 'InjuriesListing's based on the parent message's time_stamp.
90 data InjuriesListing =
92 db_injuries_id :: DefaultKey Injuries,
93 db_team :: InjuriesTeam,
94 db_teamno :: Maybe Int,
95 db_injuries :: String,
96 db_updated :: Maybe Bool }
98 instance ToDb InjuriesListingXml where
99 -- | The DB analogue of a 'InjuriesListingXml' is a 'InjuriesListing'
100 type Db InjuriesListingXml = InjuriesListing
102 instance FromXmlFk InjuriesListingXml where
103 -- | Our foreign key points to an 'Injuries'.
104 type Parent InjuriesListingXml = Injuries
106 -- | To convert between a 'InjuriesListingXml' and a
107 -- 'InjuriesListing', we simply append the foreign key.
108 from_xml_fk fk InjuriesListingXml{..} =
112 db_teamno = xml_teamno,
113 db_injuries = xml_injuries,
114 db_updated = xml_updated }
116 instance XmlImportFk InjuriesListingXml
118 -- | XML representation of an injuriesxml \<message\>.
122 xml_xml_file_id :: Int,
123 xml_heading :: String,
124 xml_category :: String,
126 xml_listings :: [InjuriesListingXml],
127 xml_time_stamp :: UTCTime }
130 -- | Database representation of a 'Message'. We really only care about
136 db_time_stamp :: UTCTime }
138 instance ToDb Message where
139 -- | The database analogue of a 'Message' is an 'Injuries'.
140 type Db Message = Injuries
142 instance FromXml Message where
143 -- | To convert from XML to DB, we simply drop the fields we don't
146 from_xml Message{..} =
148 db_sport = xml_sport,
149 db_time_stamp = xml_time_stamp }
151 instance XmlImport Message
154 instance DbImport Message where
157 migrate (undefined :: Injuries)
158 migrate (undefined :: InjuriesListing)
160 -- | We import a 'Message' by inserting all of its 'listings'.
163 msg_id <- insert_xml msg
165 -- Convert each XML listing to a DB one using the message id and
166 -- insert it (disregarding the result).
167 mapM_ (insert_xml_fk_ msg_id) (xml_listings msg)
169 return ImportSucceeded
172 mkPersist tsn_codegen_config [groundhog|
175 - entity: InjuriesListing
176 dbName: injuries_listings
178 - name: InjuriesListing
182 - {name: team_name, dbName: team_name}
183 - {name: team_league, dbName: team_league}
184 - name: db_injuries_id
188 - embedded: InjuriesTeam
191 - name: db_team_league
195 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
197 pickle_injuries_team :: PU InjuriesTeam
198 pickle_injuries_team =
200 xpWrap (from_tuple, to_tuple) $
201 xpPair xpText (xpAttrImplied "league" xpText)
203 from_tuple = uncurryN InjuriesTeam
204 to_tuple m = (db_team_name m, db_team_league m)
207 -- | A pickler for 'InjuriesListingXml's that can convert them to/from XML.
209 pickle_listing :: PU InjuriesListingXml
212 xpWrap (from_tuple, to_tuple) $
213 xp4Tuple pickle_injuries_team
214 (xpOption $ xpElem "teamno" xpInt)
215 (xpElem "injuries" xpText)
216 (xpOption $ xpElem "updated" xpPrim)
218 from_tuple = uncurryN InjuriesListingXml
219 to_tuple l = (xml_team l, xml_teamno l, xml_injuries l, xml_updated l)
222 -- | A pickler for 'Message's that can convert them to/from XML.
224 pickle_message :: PU Message
227 xpWrap (from_tuple, to_tuple) $
228 xp6Tuple (xpElem "XML_File_ID" xpInt)
229 (xpElem "heading" xpText)
230 (xpElem "category" xpText)
231 (xpElem "sport" xpText)
232 (xpList pickle_listing)
233 (xpElem "time_stamp" xp_time_stamp)
235 from_tuple = uncurryN Message
236 to_tuple m = (xml_xml_file_id m,
248 -- | A list of all tests for this module.
250 injuries_tests :: TestTree
254 [ test_pickle_of_unpickle_is_identity,
255 test_unpickle_succeeds ]
258 -- | If we unpickle something and then pickle it, we should wind up
259 -- with the same thing we started with. WARNING: success of this
260 -- test does not mean that unpickling succeeded.
262 test_pickle_of_unpickle_is_identity :: TestTree
263 test_pickle_of_unpickle_is_identity =
264 testCase "pickle composed with unpickle is the identity" $ do
265 let path = "test/xml/injuriesxml.xml"
266 (expected, actual) <- pickle_unpickle pickle_message path
270 -- | Make sure we can actually unpickle these things.
272 test_unpickle_succeeds :: TestTree
273 test_unpickle_succeeds =
274 testCase "unpickling succeeds" $ do
275 let path = "test/xml/injuriesxml.xml"
276 actual <- unpickleable path pickle_message