1 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE RecordWildCards #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
8 -- | Parse TSN XML for the DTD \"weatherxml.dtd\". Each document
9 -- contains a bunch of forecasts, which each contain one league, and
10 -- that league contains a bunch of listings.
12 module TSN.XML.Weather (
17 -- * WARNING: these are private but exported to silence warnings
18 WeatherConstructor(..),
19 WeatherForecastConstructor(..),
20 WeatherForecastListingConstructor(..) )
24 import Control.Monad ( forM_ )
25 import Data.Time ( UTCTime )
26 import Data.Tuple.Curry ( uncurryN )
27 import Database.Groundhog (
32 silentMigrationLogger )
33 import Database.Groundhog.Core ( DefaultKey )
34 import Database.Groundhog.Generic ( runDbConn )
35 import Database.Groundhog.Sqlite ( withSqliteConn )
36 import Database.Groundhog.TH (
39 import Test.Tasty ( TestTree, testGroup )
40 import Test.Tasty.HUnit ( (@?=), testCase )
41 import Text.XML.HXT.Core (
56 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
57 import TSN.Picklers ( xp_gamedate, xp_time_stamp )
58 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
70 -- | The DTD to which this module corresponds. Used to invoke dbimport.
73 dtd = "weatherxml.dtd"
80 -- * WeatherForecastListing/WeatherForecastListingXml
82 -- | XML representation of a weather forecast listing.
84 data WeatherForecastListingXml =
85 WeatherForecastListingXml {
87 xml_weather :: String }
90 -- | Database representation of a weather forecast listing.
92 data WeatherForecastListing =
93 WeatherForecastListing {
94 db_weather_forecasts_id :: DefaultKey WeatherForecast,
96 db_weather :: String }
99 -- | The database analogue of a 'WeatherForecastListingXml' is
100 -- 'WeatherForecastListing'.
102 instance ToDb WeatherForecastListingXml where
103 type Db WeatherForecastListingXml = WeatherForecastListing
106 instance Child WeatherForecastListingXml where
107 -- | Each 'WeatherForecastListingXml' is contained in a
108 -- 'WeatherForecast'.
110 type Parent WeatherForecastListingXml = WeatherForecast
113 -- | This is needed to define the 'XmlImportFk' instance for
114 -- 'WeatherForecastListing'.
116 instance FromXmlFk WeatherForecastListingXml where
117 from_xml_fk fk WeatherForecastListingXml{..} =
118 WeatherForecastListing {
119 db_weather_forecasts_id = fk,
120 db_teams = xml_teams,
121 db_weather = xml_weather }
123 -- | This allows us to insert the XML representation
124 -- 'WeatherForecastListingXml' directly.
126 instance XmlImportFk WeatherForecastListingXml
131 -- | XML representation of a league, as they appear in the weather
132 -- documents. There is no associated database representation because
133 -- the league element really adds no information besides its own
134 -- (usually empty) name. Since there's exactly one league per
135 -- forecast, we just store the league_name in the database
136 -- representation of a forecast.
140 league_name :: Maybe String,
141 listings :: [WeatherForecastListingXml] }
145 -- * WeatherForecast/WeatherForecastXml
147 -- | Database representation of a weather forecast.
149 data WeatherForecast =
151 db_weather_id :: DefaultKey Weather,
152 db_game_date :: UTCTime,
153 db_league_name :: Maybe String }
155 -- | XML representation of a weather forecast. It would have been
156 -- cleaner to omit the 'WeatherLeague' middleman, but having it
157 -- simplifies parsing.
159 data WeatherForecastXml =
161 xml_game_date :: UTCTime,
162 xml_league :: WeatherLeague }
165 instance ToDb WeatherForecastXml where
166 -- | The database representation of a 'WeatherForecastXml' is a
167 -- 'WeatherForecast'.
169 type Db WeatherForecastXml = WeatherForecast
172 instance Child WeatherForecastXml where
173 -- | The database type containing a 'WeatherForecastXml' is
175 type Parent WeatherForecastXml = Weather
178 instance FromXmlFk WeatherForecastXml where
180 -- | To convert a 'WeatherForecastXml' into a 'WeatherForecast', we
181 -- replace the 'WeatherLeague' with its name.
183 from_xml_fk fk WeatherForecastXml{..} =
186 db_game_date = xml_game_date,
187 db_league_name = (league_name xml_league) }
190 -- | This allows us to call 'insert_xml' on an 'WeatherForecastXml'
191 -- without first converting it to the database representation.
193 instance XmlImportFk WeatherForecastXml
198 -- | The database representation of a weather message.
202 db_xml_file_id :: Int,
205 db_time_stamp :: UTCTime }
208 -- | The XML representation of a weather message.
212 xml_xml_file_id :: Int,
213 xml_heading :: String,
214 xml_category :: String,
217 xml_forecasts :: [WeatherForecastXml],
218 xml_time_stamp :: UTCTime }
221 instance ToDb Message where
222 -- | The database representation of 'Message' is 'Weather'.
224 type Db Message = Weather
226 instance FromXml Message where
227 -- | To get a 'Weather' from a 'Message', we drop a bunch of
230 from_xml Message{..} =
232 db_xml_file_id = xml_xml_file_id,
233 db_sport = xml_sport,
234 db_title = xml_title,
235 db_time_stamp = xml_time_stamp }
237 -- | This allows us to insert the XML representation 'Message'
240 instance XmlImport Message
247 mkPersist tsn_codegen_config [groundhog|
252 - name: unique_weather
254 # Prevent multiple imports of the same message.
255 fields: [db_xml_file_id]
257 - entity: WeatherForecast
258 dbName: weather_forecasts
260 - name: WeatherForecast
262 - name: db_weather_id
266 - entity: WeatherForecastListing
267 dbName: weather_forecast_listings
269 - name: WeatherForecastListing
271 - name: db_weather_forecasts_id
278 instance DbImport Message where
281 migrate (undefined :: Weather)
282 migrate (undefined :: WeatherForecast)
283 migrate (undefined :: WeatherForecastListing)
286 -- The weather database schema has a nice linear structure. First
287 -- we insert the top-level weather record.
288 weather_id <- insert_xml m
290 -- Next insert all of the forecasts, one at a time.
291 forM_ (xml_forecasts m) $ \forecast -> do
292 forecast_id <- insert_xml_fk weather_id forecast
294 -- Insert all of this forecast's listings.
295 mapM_ (insert_xml_fk_ forecast_id) (listings $ xml_league forecast)
297 return ImportSucceeded
304 -- | Pickler to convert a 'WeatherForecastListingXml' to/from XML.
306 pickle_listing :: PU WeatherForecastListingXml
309 xpWrap (from_pair, to_pair) $
311 (xpElem "teams" xpText)
312 (xpElem "weather" xpText)
314 from_pair = uncurry WeatherForecastListingXml
315 to_pair WeatherForecastListingXml{..} = (xml_teams, xml_weather)
318 -- | Pickler to convert a 'WeatherLeague' to/from XML.
320 pickle_league :: PU WeatherLeague
323 xpWrap (from_pair, to_pair) $
325 (xpAttr "name" $ xpOption xpText)
326 (xpList pickle_listing)
328 from_pair = uncurry WeatherLeague
329 to_pair WeatherLeague{..} = (league_name, listings)
332 -- | Pickler to convert a 'WeatherForecastXml' to/from XML.
334 pickle_forecast :: PU WeatherForecastXml
337 xpWrap (from_pair, to_pair) $
339 (xpAttr "gamedate" xp_gamedate)
342 from_pair = uncurry WeatherForecastXml
343 to_pair WeatherForecastXml{..} = (xml_game_date,
348 -- | Pickler to convert a 'Message' to/from XML.
350 pickle_message :: PU Message
353 xpWrap (from_tuple, to_tuple) $
355 (xpElem "XML_File_ID" xpInt)
356 (xpElem "heading" xpText)
357 (xpElem "category" xpText)
358 (xpElem "sport" xpText)
359 (xpElem "title" xpText)
360 (xpList pickle_forecast)
361 (xpElem "time_stamp" xp_time_stamp)
363 from_tuple = uncurryN Message
364 to_tuple Message{..} = (xml_xml_file_id,
376 weather_tests :: TestTree
380 [ test_on_delete_cascade,
381 test_pickle_of_unpickle_is_identity,
382 test_unpickle_succeeds ]
385 -- | If we unpickle something and then pickle it, we should wind up
386 -- with the same thing we started with. WARNING: success of this
387 -- test does not mean that unpickling succeeded.
389 test_pickle_of_unpickle_is_identity :: TestTree
390 test_pickle_of_unpickle_is_identity =
391 testCase "pickle composed with unpickle is the identity" $ do
392 let path = "test/xml/weatherxml.xml"
393 (expected, actual) <- pickle_unpickle pickle_message path
397 -- | Make sure we can actually unpickle these things.
399 test_unpickle_succeeds :: TestTree
400 test_unpickle_succeeds =
401 testCase "unpickling succeeds" $ do
402 let path = "test/xml/weatherxml.xml"
403 actual <- unpickleable path pickle_message
408 -- | Make sure everything gets deleted when we delete the top-level
411 test_on_delete_cascade :: TestTree
412 test_on_delete_cascade =
413 testCase "deleting weather deletes its children" $ do
414 let path = "test/xml/weatherxml.xml"
415 weather <- unsafe_unpickle path pickle_message
416 let a = undefined :: Weather
417 let b = undefined :: WeatherForecast
418 let c = undefined :: WeatherForecastListing
419 actual <- withSqliteConn ":memory:" $ runDbConn $ do
420 runMigration silentMigrationLogger $ do
424 _ <- dbimport weather
426 count_a <- countAll a
427 count_b <- countAll b
428 count_c <- countAll c
429 return $ count_a + count_b + count_c