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 (
24 -- * WARNING: these are private but exported to silence warnings
25 InjuriesConstructor(..),
26 InjuriesListingConstructor(..) )
30 import Data.Data ( Data )
31 import Data.Time ( UTCTime )
32 import Data.Typeable ( Typeable )
33 import Database.Groundhog (
38 silentMigrationLogger )
39 import Database.Groundhog.Core ( DefaultKey )
40 import Database.Groundhog.Generic ( runDbConn )
41 import Database.Groundhog.TH (
44 import Database.Groundhog.Sqlite ( withSqliteConn )
45 import Data.Tuple.Curry ( uncurryN )
46 import Test.Tasty ( TestTree, testGroup )
47 import Test.Tasty.HUnit ( (@?=), testCase )
48 import Text.XML.HXT.Core (
63 import TSN.Codegen ( tsn_codegen_config )
64 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
65 import TSN.Picklers ( xp_time_stamp )
66 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
77 -- | The DTD to which this module corresponds. Used to invoke dbimport.
80 dtd = "injuriesxml.dtd"
88 -- | XML/Database representation of a team as they appear in the
89 -- injuries documents.
93 db_team_name :: String,
94 db_team_league :: Maybe String }
95 deriving (Data, Eq, Show, Typeable)
98 -- * InjuriesListing/InjuriesListingXml
100 -- | XML representation of the injury listings.
102 data InjuriesListingXml =
104 xml_team :: InjuriesTeam,
105 xml_teamno :: Maybe Int,
106 xml_injuries :: String,
107 xml_updated :: Maybe Bool }
110 -- | Database representation of a 'InjuriesListing'. It possesses a
111 -- foreign key to an 'Injuries' object so that we can easily delete
112 -- 'InjuriesListing's based on the parent message's time_stamp.
114 data InjuriesListing =
116 db_injuries_id :: DefaultKey Injuries,
117 db_team :: InjuriesTeam,
118 db_teamno :: Maybe Int,
119 db_injuries :: String,
120 db_updated :: Maybe Bool }
122 instance ToDb InjuriesListingXml where
123 -- | The DB analogue of a 'InjuriesListingXml' is a 'InjuriesListing'
124 type Db InjuriesListingXml = InjuriesListing
126 instance FromXmlFk InjuriesListingXml where
127 -- | Our foreign key points to an 'Injuries'.
128 type Parent InjuriesListingXml = Injuries
130 -- | To convert between a 'InjuriesListingXml' and a
131 -- 'InjuriesListing', we simply append the foreign key.
132 from_xml_fk fk InjuriesListingXml{..} =
136 db_teamno = xml_teamno,
137 db_injuries = xml_injuries,
138 db_updated = xml_updated }
140 -- | This allows us to insert the XML representation
141 -- 'InjuriesListingXml' directly.
143 instance XmlImportFk InjuriesListingXml
146 -- * Injuries/Message
148 -- | XML representation of an injuriesxml \<message\>.
152 xml_xml_file_id :: Int,
153 xml_heading :: String,
154 xml_category :: String,
156 xml_listings :: [InjuriesListingXml],
157 xml_time_stamp :: UTCTime }
160 -- | Database representation of a 'Message'.
164 db_xml_file_id :: Int,
166 db_time_stamp :: UTCTime }
168 instance ToDb Message where
169 -- | The database analogue of a 'Message' is an 'Injuries'.
170 type Db Message = Injuries
172 instance FromXml Message where
173 -- | To convert from XML to DB, we simply drop the fields we don't
176 from_xml Message{..} =
178 db_xml_file_id = xml_xml_file_id,
179 db_sport = xml_sport,
180 db_time_stamp = xml_time_stamp }
182 -- | This allows us to insert the XML representation 'Message'
185 instance XmlImport Message
192 instance DbImport Message where
195 migrate (undefined :: Injuries)
196 migrate (undefined :: InjuriesListing)
198 -- | We import a 'Message' by inserting all of its 'listings', but
199 -- the listings require a foreign key to the parent 'Message'.
202 msg_id <- insert_xml msg
204 -- Convert each XML listing to a DB one using the message id and
205 -- insert it (disregarding the result).
206 mapM_ (insert_xml_fk_ msg_id) (xml_listings msg)
208 return ImportSucceeded
211 mkPersist tsn_codegen_config [groundhog|
216 - name: unique_injuries
218 # Prevent multiple imports of the same message.
219 fields: [db_xml_file_id]
221 - entity: InjuriesListing
222 dbName: injuries_listings
224 - name: InjuriesListing
228 - {name: team_name, dbName: team_name}
229 - {name: team_league, dbName: team_league}
230 - name: db_injuries_id
234 - embedded: InjuriesTeam
237 - name: db_team_league
246 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
248 pickle_injuries_team :: PU InjuriesTeam
249 pickle_injuries_team =
251 xpWrap (from_tuple, to_tuple) $
252 xpPair xpText (xpAttrImplied "league" xpText)
254 from_tuple = uncurryN InjuriesTeam
255 to_tuple m = (db_team_name m, db_team_league m)
258 -- | A pickler for 'InjuriesListingXml's that can convert them to/from
261 pickle_listing :: PU InjuriesListingXml
264 xpWrap (from_tuple, to_tuple) $
265 xp4Tuple pickle_injuries_team
266 (xpOption $ xpElem "teamno" xpInt)
267 (xpElem "injuries" xpText)
268 (xpOption $ xpElem "updated" xpPrim)
270 from_tuple = uncurryN InjuriesListingXml
271 to_tuple l = (xml_team l, xml_teamno l, xml_injuries l, xml_updated l)
274 -- | A pickler for 'Message's that can convert them to/from XML.
276 pickle_message :: PU Message
279 xpWrap (from_tuple, to_tuple) $
280 xp6Tuple (xpElem "XML_File_ID" xpInt)
281 (xpElem "heading" xpText)
282 (xpElem "category" xpText)
283 (xpElem "sport" xpText)
284 (xpList pickle_listing)
285 (xpElem "time_stamp" xp_time_stamp)
287 from_tuple = uncurryN Message
288 to_tuple m = (xml_xml_file_id m,
300 -- | A list of all tests for this module.
302 injuries_tests :: TestTree
306 [ test_on_delete_cascade,
307 test_pickle_of_unpickle_is_identity,
308 test_unpickle_succeeds ]
311 -- | If we unpickle something and then pickle it, we should wind up
312 -- with the same thing we started with. WARNING: success of this
313 -- test does not mean that unpickling succeeded.
315 test_pickle_of_unpickle_is_identity :: TestTree
316 test_pickle_of_unpickle_is_identity =
317 testCase "pickle composed with unpickle is the identity" $ do
318 let path = "test/xml/injuriesxml.xml"
319 (expected, actual) <- pickle_unpickle pickle_message path
323 -- | Make sure we can actually unpickle these things.
325 test_unpickle_succeeds :: TestTree
326 test_unpickle_succeeds =
327 testCase "unpickling succeeds" $ do
328 let path = "test/xml/injuriesxml.xml"
329 actual <- unpickleable path pickle_message
334 -- | Make sure everything gets deleted when we delete the top-level
337 test_on_delete_cascade :: TestTree
338 test_on_delete_cascade =
339 testCase "deleting an injuries deletes its children" $ do
340 let path = "test/xml/injuriesxml.xml"
341 inj <- unsafe_unpickle path pickle_message
342 let a = undefined :: Injuries
343 let b = undefined :: InjuriesListing
344 actual <- withSqliteConn ":memory:" $ runDbConn $ do
345 runMigration silentMigrationLogger $ do
350 count_a <- countAll a
351 count_b <- countAll b
352 return $ count_a + count_b