1 {-# LANGUAGE DeriveGeneric #-}
2 {-# LANGUAGE DeriveDataTypeable #-}
3 {-# LANGUAGE FlexibleInstances #-}
5 {-# LANGUAGE QuasiQuotes #-}
6 {-# LANGUAGE RecordWildCards #-}
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 qualified GHC.Generics as GHC ( Generic )
45 import Database.Groundhog.Sqlite ( withSqliteConn )
46 import Data.Tuple.Curry ( uncurryN )
47 import Test.Tasty ( TestTree, testGroup )
48 import Test.Tasty.HUnit ( (@?=), testCase )
49 import Text.XML.HXT.Core (
64 import Generics ( Generic(..), to_tuple )
65 import TSN.Codegen ( tsn_codegen_config )
66 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
67 import TSN.Picklers ( xp_time_stamp )
68 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
80 -- | The DTD to which this module corresponds. Used to invoke dbimport.
83 dtd = "injuriesxml.dtd"
91 -- | XML/Database representation of a team as they appear in the
92 -- injuries documents.
96 db_team_name :: String,
97 db_team_league :: Maybe String }
98 deriving (Data, Eq, Show, Typeable)
101 -- * InjuriesListing/InjuriesListingXml
103 -- | XML representation of the injury listings.
105 data InjuriesListingXml =
107 xml_team :: InjuriesTeam,
108 xml_teamno :: Maybe String, -- ^ Can contain non-numerics, e.g. \"ZR2\"
109 xml_injuries :: String,
110 xml_updated :: Maybe Bool }
111 deriving (Eq, GHC.Generic, Show)
114 -- | For 'Generics.to_tuple'.
116 instance Generic InjuriesListingXml
119 -- | Database representation of a 'InjuriesListing'. It possesses a
120 -- foreign key to an 'Injuries' object so that we can easily delete
121 -- 'InjuriesListing's based on the parent message's time_stamp.
123 data InjuriesListing =
125 db_injuries_id :: DefaultKey Injuries,
126 db_team :: InjuriesTeam,
127 db_teamno :: Maybe String, -- ^ Can contain non-numerics, e.g. \"ZR2\"
128 db_injuries :: String,
129 db_updated :: Maybe Bool }
131 instance ToDb InjuriesListingXml where
132 -- | The DB analogue of a 'InjuriesListingXml' is a 'InjuriesListing'
133 type Db InjuriesListingXml = InjuriesListing
135 instance Child InjuriesListingXml where
136 -- | Our foreign key points to an 'Injuries'.
137 type Parent InjuriesListingXml = Injuries
139 instance FromXmlFk InjuriesListingXml where
140 -- | To convert between a 'InjuriesListingXml' and a
141 -- 'InjuriesListing', we simply append the foreign key.
142 from_xml_fk fk InjuriesListingXml{..} =
146 db_teamno = xml_teamno,
147 db_injuries = xml_injuries,
148 db_updated = xml_updated }
150 -- | This allows us to insert the XML representation
151 -- 'InjuriesListingXml' directly.
153 instance XmlImportFk InjuriesListingXml
156 -- * Injuries/Message
158 -- | XML representation of an injuriesxml \<message\>.
162 xml_xml_file_id :: Int,
163 xml_heading :: String,
164 xml_category :: String,
166 xml_listings :: [InjuriesListingXml],
167 xml_time_stamp :: UTCTime }
168 deriving (Eq, GHC.Generic, Show)
171 -- | For 'Generics.to_tuple'.
173 instance Generic Message
176 -- | Database representation of a 'Message'.
180 db_xml_file_id :: Int,
182 db_time_stamp :: UTCTime }
184 instance ToDb Message where
185 -- | The database analogue of a 'Message' is an 'Injuries'.
186 type Db Message = Injuries
188 instance FromXml Message where
189 -- | To convert from XML to DB, we simply drop the fields we don't
192 from_xml Message{..} =
194 db_xml_file_id = xml_xml_file_id,
195 db_sport = xml_sport,
196 db_time_stamp = xml_time_stamp }
198 -- | This allows us to insert the XML representation 'Message'
201 instance XmlImport Message
208 instance DbImport Message where
211 migrate (undefined :: Injuries)
212 migrate (undefined :: InjuriesListing)
214 -- | We import a 'Message' by inserting all of its 'listings', but
215 -- the listings require a foreign key to the parent 'Message'.
218 msg_id <- insert_xml msg
220 -- Convert each XML listing to a DB one using the message id and
221 -- insert it (disregarding the result).
222 mapM_ (insert_xml_fk_ msg_id) (xml_listings msg)
224 return ImportSucceeded
227 mkPersist tsn_codegen_config [groundhog|
232 - name: unique_injuries
234 # Prevent multiple imports of the same message.
235 fields: [db_xml_file_id]
237 - entity: InjuriesListing
238 dbName: injuries_listings
240 - name: InjuriesListing
244 - {name: team_name, dbName: team_name}
245 - {name: team_league, dbName: team_league}
246 - name: db_injuries_id
250 - embedded: InjuriesTeam
253 - name: db_team_league
262 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
264 pickle_injuries_team :: PU InjuriesTeam
265 pickle_injuries_team =
267 xpWrap (from_tuple, to_tuple') $
268 xpPair xpText (xpAttrImplied "league" xpText)
270 from_tuple = uncurryN InjuriesTeam
272 -- Pointless, but silences two unused field warnings.
273 to_tuple' InjuriesTeam{..} = (db_team_name, db_team_league)
275 -- | A pickler for 'InjuriesListingXml's that can convert them to/from
278 pickle_listing :: PU InjuriesListingXml
281 xpWrap (from_tuple, to_tuple) $
282 xp4Tuple pickle_injuries_team
283 (xpOption $ xpElem "teamno" xpText)
284 (xpElem "injuries" xpText)
285 (xpOption $ xpElem "updated" xpPrim)
287 from_tuple = uncurryN InjuriesListingXml
291 -- | A pickler for 'Message's that can convert them to/from XML.
293 pickle_message :: PU Message
296 xpWrap (from_tuple, to_tuple) $
297 xp6Tuple (xpElem "XML_File_ID" xpInt)
298 (xpElem "heading" xpText)
299 (xpElem "category" xpText)
300 (xpElem "sport" xpText)
301 (xpList pickle_listing)
302 (xpElem "time_stamp" xp_time_stamp)
304 from_tuple = uncurryN Message
311 -- | A list of all tests for this module.
313 injuries_tests :: TestTree
317 [ test_on_delete_cascade,
318 test_pickle_of_unpickle_is_identity,
319 test_unpickle_succeeds ]
322 -- | If we unpickle something and then pickle it, we should wind up
323 -- with the same thing we started with. WARNING: success of this
324 -- test does not mean that unpickling succeeded.
326 test_pickle_of_unpickle_is_identity :: TestTree
327 test_pickle_of_unpickle_is_identity =
328 testCase "pickle composed with unpickle is the identity" $ do
329 let path = "test/xml/injuriesxml.xml"
330 (expected, actual) <- pickle_unpickle pickle_message path
334 -- | Make sure we can actually unpickle these things.
336 test_unpickle_succeeds :: TestTree
337 test_unpickle_succeeds =
338 testCase "unpickling succeeds" $ do
339 let path = "test/xml/injuriesxml.xml"
340 actual <- unpickleable path pickle_message
345 -- | Make sure everything gets deleted when we delete the top-level
348 test_on_delete_cascade :: TestTree
349 test_on_delete_cascade =
350 testCase "deleting an injuries deletes its children" $ do
351 let path = "test/xml/injuriesxml.xml"
352 inj <- unsafe_unpickle path pickle_message
353 let a = undefined :: Injuries
354 let b = undefined :: InjuriesListing
355 actual <- withSqliteConn ":memory:" $ runDbConn $ do
356 runMigration silentMigrationLogger $ do
361 count_a <- countAll a
362 count_b <- countAll b
363 return $ count_a + count_b