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 qualified Data.Vector.HFixed as H ( HVector, cons, convert )
34 import Database.Groundhog (
39 silentMigrationLogger )
40 import Database.Groundhog.Core ( DefaultKey )
41 import Database.Groundhog.Generic ( runDbConn )
42 import Database.Groundhog.TH (
45 import qualified GHC.Generics as GHC ( Generic )
46 import Database.Groundhog.Sqlite ( withSqliteConn )
47 import Data.Tuple.Curry ( uncurryN )
48 import Test.Tasty ( TestTree, testGroup )
49 import Test.Tasty.HUnit ( (@?=), testCase )
50 import Text.XML.HXT.Core (
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. The leading
104 -- underscores prevent unused field warnings.
106 data InjuriesListingXml =
108 _xml_team :: InjuriesTeam,
109 _xml_teamno :: Maybe String, -- ^ Can contain non-numerics, e.g. \"ZR2\"
110 _xml_injuries :: String,
111 _xml_updated :: Maybe Bool }
112 deriving (Eq, GHC.Generic, Show)
115 -- | For 'H.convert'.
117 instance H.HVector InjuriesListingXml
120 -- | Database representation of a 'InjuriesListing'. It possesses a
121 -- foreign key to an 'Injuries' object so that we can easily delete
122 -- 'InjuriesListing's based on the parent message's time_stamp.
123 -- The leading underscores prevent unused field warnings.
125 data InjuriesListing =
127 _db_injuries_id :: DefaultKey Injuries,
128 _db_team :: InjuriesTeam,
129 _db_teamno :: Maybe String, -- ^ Can contain non-numerics, e.g. \"ZR2\"
130 _db_injuries :: String,
131 _db_updated :: Maybe Bool }
132 deriving ( GHC.Generic )
136 instance H.HVector InjuriesListing
138 instance ToDb InjuriesListingXml where
139 -- | The DB analogue of a 'InjuriesListingXml' is a 'InjuriesListing'
140 type Db InjuriesListingXml = InjuriesListing
142 instance Child InjuriesListingXml where
143 -- | Our foreign key points to an 'Injuries'.
144 type Parent InjuriesListingXml = Injuries
146 instance FromXmlFk InjuriesListingXml where
147 -- | To convert between a 'InjuriesListingXml' and a
148 -- 'InjuriesListing', we simply append the foreign key.
151 -- | This allows us to insert the XML representation
152 -- 'InjuriesListingXml' directly.
154 instance XmlImportFk InjuriesListingXml
157 -- * Injuries/Message
159 -- | XML representation of an injuriesxml \<message\>.
163 xml_xml_file_id :: Int,
164 xml_heading :: String,
165 xml_category :: String,
167 xml_listings :: [InjuriesListingXml],
168 xml_time_stamp :: UTCTime }
169 deriving (Eq, GHC.Generic, Show)
172 -- | For 'H.HVector'.
174 instance H.HVector Message
177 -- | Database representation of a 'Message'.
181 db_xml_file_id :: Int,
183 db_time_stamp :: UTCTime }
185 instance ToDb Message where
186 -- | The database analogue of a 'Message' is an 'Injuries'.
187 type Db Message = Injuries
189 instance FromXml Message where
190 -- | To convert from XML to DB, we simply drop the fields we don't
193 from_xml Message{..} =
195 db_xml_file_id = xml_xml_file_id,
196 db_sport = xml_sport,
197 db_time_stamp = xml_time_stamp }
199 -- | This allows us to insert the XML representation 'Message'
202 instance XmlImport Message
209 instance DbImport Message where
212 migrate (undefined :: Injuries)
213 migrate (undefined :: InjuriesListing)
215 -- | We import a 'Message' by inserting all of its 'listings', but
216 -- the listings require a foreign key to the parent 'Message'.
219 msg_id <- insert_xml msg
221 -- Convert each XML listing to a DB one using the message id and
222 -- insert it (disregarding the result).
223 mapM_ (insert_xml_fk_ msg_id) (xml_listings msg)
225 return ImportSucceeded
228 mkPersist tsn_codegen_config [groundhog|
233 - name: unique_injuries
235 # Prevent multiple imports of the same message.
236 fields: [db_xml_file_id]
238 - entity: InjuriesListing
239 dbName: injuries_listings
241 - name: InjuriesListing
245 - {name: team_name, dbName: team_name}
246 - {name: team_league, dbName: team_league}
247 - name: _db_injuries_id
251 - embedded: InjuriesTeam
254 - name: db_team_league
263 -- | A pickler for 'InjuriesTeam's that can convert them to/from XML.
265 pickle_injuries_team :: PU InjuriesTeam
266 pickle_injuries_team =
268 xpWrap (from_tuple, to_tuple') $
269 xpPair xpText (xpAttrImplied "league" xpText)
271 from_tuple = uncurryN InjuriesTeam
273 -- Pointless, but silences two unused field warnings.
274 to_tuple' InjuriesTeam{..} = (db_team_name, db_team_league)
276 -- | A pickler for 'InjuriesListingXml's that can convert them to/from
279 pickle_listing :: PU InjuriesListingXml
282 xpWrap (from_tuple, H.convert) $
283 xp4Tuple pickle_injuries_team
284 (xpOption $ xpElem "teamno" xpText)
285 (xpElem "injuries" xpText)
286 (xpOption $ xpElem "updated" xpPrim)
288 from_tuple = uncurryN InjuriesListingXml
292 -- | A pickler for 'Message's that can convert them to/from XML.
294 pickle_message :: PU Message
297 xpWrap (from_tuple, H.convert) $
298 xp6Tuple (xpElem "XML_File_ID" xpInt)
299 (xpElem "heading" xpText)
300 (xpElem "category" xpText)
301 (xpElem "sport" xpText)
302 (xpList pickle_listing)
303 (xpElem "time_stamp" xp_time_stamp)
305 from_tuple = uncurryN Message
312 -- | A list of all tests for this module.
314 injuries_tests :: TestTree
318 [ test_on_delete_cascade,
319 test_pickle_of_unpickle_is_identity,
320 test_unpickle_succeeds ]
323 -- | If we unpickle something and then pickle it, we should wind up
324 -- with the same thing we started with. WARNING: success of this
325 -- test does not mean that unpickling succeeded.
327 test_pickle_of_unpickle_is_identity :: TestTree
328 test_pickle_of_unpickle_is_identity =
329 testCase "pickle composed with unpickle is the identity" $ do
330 let path = "test/xml/injuriesxml.xml"
331 (expected, actual) <- pickle_unpickle pickle_message path
335 -- | Make sure we can actually unpickle these things.
337 test_unpickle_succeeds :: TestTree
338 test_unpickle_succeeds =
339 testCase "unpickling succeeds" $ do
340 let path = "test/xml/injuriesxml.xml"
341 actual <- unpickleable path pickle_message
346 -- | Make sure everything gets deleted when we delete the top-level
349 test_on_delete_cascade :: TestTree
350 test_on_delete_cascade =
351 testCase "deleting an injuries deletes its children" $ do
352 let path = "test/xml/injuriesxml.xml"
353 inj <- unsafe_unpickle path pickle_message
354 let a = undefined :: Injuries
355 let b = undefined :: InjuriesListing
356 actual <- withSqliteConn ":memory:" $ runDbConn $ do
357 runMigration silentMigrationLogger $ do
362 count_a <- countAll a
363 count_b <- countAll b
364 return $ count_a + count_b