1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
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
14 -- \"injuries_listings\" automatically. The root message is retained
15 -- so that we can easily delete its associated listings based on its
18 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 (
37 silentMigrationLogger )
38 import Database.Groundhog.Core ( DefaultKey )
39 import Database.Groundhog.Generic ( runDbConn )
40 import Database.Groundhog.TH (
43 import Database.Groundhog.Sqlite ( withSqliteConn )
44 import Data.Tuple.Curry ( uncurryN )
45 import Test.Tasty ( TestTree, testGroup )
46 import Test.Tasty.HUnit ( (@?=), testCase )
47 import Text.XML.HXT.Core (
62 import TSN.Codegen ( tsn_codegen_config )
63 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
64 import TSN.Picklers ( xp_time_stamp )
65 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 Child InjuriesListingXml where
127 -- | Our foreign key points to an 'Injuries'.
128 type Parent InjuriesListingXml = Injuries
130 instance FromXmlFk InjuriesListingXml where
131 -- | To convert between a 'InjuriesListingXml' and a
132 -- 'InjuriesListing', we simply append the foreign key.
133 from_xml_fk fk InjuriesListingXml{..} =
137 db_teamno = xml_teamno,
138 db_injuries = xml_injuries,
139 db_updated = xml_updated }
141 -- | This allows us to insert the XML representation
142 -- 'InjuriesListingXml' directly.
144 instance XmlImportFk InjuriesListingXml
147 -- * Injuries/Message
149 -- | XML representation of an injuriesxml \<message\>.
153 xml_xml_file_id :: Int,
154 xml_heading :: String,
155 xml_category :: String,
157 xml_listings :: [InjuriesListingXml],
158 xml_time_stamp :: UTCTime }
161 -- | Database representation of a 'Message'.
165 db_xml_file_id :: Int,
167 db_time_stamp :: UTCTime }
169 instance ToDb Message where
170 -- | The database analogue of a 'Message' is an 'Injuries'.
171 type Db Message = Injuries
173 instance FromXml Message where
174 -- | To convert from XML to DB, we simply drop the fields we don't
177 from_xml Message{..} =
179 db_xml_file_id = xml_xml_file_id,
180 db_sport = xml_sport,
181 db_time_stamp = xml_time_stamp }
183 -- | This allows us to insert the XML representation 'Message'
186 instance XmlImport Message
193 instance DbImport Message where
196 migrate (undefined :: Injuries)
197 migrate (undefined :: InjuriesListing)
199 -- | We import a 'Message' by inserting all of its 'listings', but
200 -- the listings require a foreign key to the parent 'Message'.
203 msg_id <- insert_xml msg
205 -- Convert each XML listing to a DB one using the message id and
206 -- insert it (disregarding the result).
207 mapM_ (insert_xml_fk_ msg_id) (xml_listings msg)
209 return ImportSucceeded
212 mkPersist tsn_codegen_config [groundhog|
217 - name: unique_injuries
219 # Prevent multiple imports of the same message.
220 fields: [db_xml_file_id]
222 - entity: InjuriesListing
223 dbName: injuries_listings
225 - name: InjuriesListing
229 - {name: team_name, dbName: team_name}
230 - {name: team_league, dbName: team_league}
231 - name: db_injuries_id
235 - embedded: InjuriesTeam
238 - name: db_team_league
247 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
249 pickle_injuries_team :: PU InjuriesTeam
250 pickle_injuries_team =
252 xpWrap (from_tuple, to_tuple) $
253 xpPair xpText (xpAttrImplied "league" xpText)
255 from_tuple = uncurryN InjuriesTeam
256 to_tuple m = (db_team_name m, db_team_league m)
259 -- | A pickler for 'InjuriesListingXml's that can convert them to/from
262 pickle_listing :: PU InjuriesListingXml
265 xpWrap (from_tuple, to_tuple) $
266 xp4Tuple pickle_injuries_team
267 (xpOption $ xpElem "teamno" xpInt)
268 (xpElem "injuries" xpText)
269 (xpOption $ xpElem "updated" xpPrim)
271 from_tuple = uncurryN InjuriesListingXml
272 to_tuple l = (xml_team l, xml_teamno l, xml_injuries l, xml_updated l)
275 -- | A pickler for 'Message's that can convert them to/from XML.
277 pickle_message :: PU Message
280 xpWrap (from_tuple, to_tuple) $
281 xp6Tuple (xpElem "XML_File_ID" xpInt)
282 (xpElem "heading" xpText)
283 (xpElem "category" xpText)
284 (xpElem "sport" xpText)
285 (xpList pickle_listing)
286 (xpElem "time_stamp" xp_time_stamp)
288 from_tuple = uncurryN Message
289 to_tuple m = (xml_xml_file_id m,
301 -- | A list of all tests for this module.
303 injuries_tests :: TestTree
307 [ test_on_delete_cascade,
308 test_pickle_of_unpickle_is_identity,
309 test_unpickle_succeeds ]
312 -- | If we unpickle something and then pickle it, we should wind up
313 -- with the same thing we started with. WARNING: success of this
314 -- test does not mean that unpickling succeeded.
316 test_pickle_of_unpickle_is_identity :: TestTree
317 test_pickle_of_unpickle_is_identity =
318 testCase "pickle composed with unpickle is the identity" $ do
319 let path = "test/xml/injuriesxml.xml"
320 (expected, actual) <- pickle_unpickle pickle_message path
324 -- | Make sure we can actually unpickle these things.
326 test_unpickle_succeeds :: TestTree
327 test_unpickle_succeeds =
328 testCase "unpickling succeeds" $ do
329 let path = "test/xml/injuriesxml.xml"
330 actual <- unpickleable path pickle_message
335 -- | Make sure everything gets deleted when we delete the top-level
338 test_on_delete_cascade :: TestTree
339 test_on_delete_cascade =
340 testCase "deleting an injuries deletes its children" $ do
341 let path = "test/xml/injuriesxml.xml"
342 inj <- unsafe_unpickle path pickle_message
343 let a = undefined :: Injuries
344 let b = undefined :: InjuriesListing
345 actual <- withSqliteConn ":memory:" $ runDbConn $ do
346 runMigration silentMigrationLogger $ do
351 count_a <- countAll a
352 count_b <- countAll b
353 return $ count_a + count_b