]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/TSN/XML/Weather.hs
Move the weird weatherxml example out of schemagen/ and under doc/.
[dead/htsn-import.git] / src / TSN / XML / Weather.hs
1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE GADTs #-}
3 {-# LANGUAGE QuasiQuotes #-}
4 {-# LANGUAGE RecordWildCards #-}
5 {-# LANGUAGE TemplateHaskell #-}
6 {-# LANGUAGE TypeFamilies #-}
7
8 -- | Parse TSN XML for the DTD \"weatherxml.dtd\". Each document
9 -- contains a bunch of forecasts, which each contain zero or more
10 -- leagues, which in turn (each) contain a bunch of listings.
11 --
12 module TSN.XML.Weather (
13 dtd,
14 pickle_message,
15 -- * Tests
16 weather_tests,
17 -- * WARNING: these are private but exported to silence warnings
18 WeatherConstructor(..),
19 WeatherForecastConstructor(..),
20 WeatherForecastListingConstructor(..) )
21 where
22
23 -- System imports.
24 import Control.Monad ( forM_ )
25 import Data.Time ( UTCTime )
26 import Data.Tuple.Curry ( uncurryN )
27 import Database.Groundhog (
28 countAll,
29 deleteAll,
30 insert_,
31 migrate,
32 runMigration,
33 silentMigrationLogger )
34 import Database.Groundhog.Core ( DefaultKey )
35 import Database.Groundhog.Generic ( runDbConn )
36 import Database.Groundhog.Sqlite ( withSqliteConn )
37 import Database.Groundhog.TH (
38 groundhog,
39 mkPersist )
40 import Test.Tasty ( TestTree, testGroup )
41 import Test.Tasty.HUnit ( (@?=), testCase )
42 import Text.XML.HXT.Core (
43 PU,
44 xp7Tuple,
45 xpAttr,
46 xpElem,
47 xpInt,
48 xpList,
49 xpOption,
50 xpPair,
51 xpText,
52 xpWrap )
53
54 -- Local imports.
55 import TSN.Codegen (
56 tsn_codegen_config )
57 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
58 import TSN.Picklers ( xp_gamedate, xp_time_stamp )
59 import TSN.XmlImport ( XmlImport(..), XmlImportFk(..) )
60 import Xml (
61 Child(..),
62 FromXml(..),
63 FromXmlFk(..),
64 ToDb(..),
65 pickle_unpickle,
66 unpickleable,
67 unsafe_unpickle )
68
69
70
71 -- | The DTD to which this module corresponds. Used to invoke dbimport.
72 --
73 dtd :: String
74 dtd = "weatherxml.dtd"
75
76
77 --
78 -- DB/XML Data types
79 --
80
81 -- * WeatherForecastListing/WeatherForecastListingXml
82
83 -- | XML representation of a weather forecast listing.
84 --
85 data WeatherForecastListingXml =
86 WeatherForecastListingXml {
87 xml_teams :: String,
88 xml_weather :: String }
89 deriving (Eq, Show)
90
91
92 -- | Database representation of a weather forecast listing. The
93 -- 'db_league_name' field should come from the containing \<league\>
94 -- element which is not stored in the database.
95 --
96 data WeatherForecastListing =
97 WeatherForecastListing {
98 db_weather_forecasts_id :: DefaultKey WeatherForecast,
99 db_league_name :: Maybe String,
100 db_teams :: String,
101 db_weather :: String }
102
103
104 -- | We don't make 'WeatherForecastListingXml' an instance of
105 -- 'FromXmlFk' because it needs some additional information, namely
106 -- the league name from its containing \<league\> element.
107 --
108 -- When supplied with a forecast id and a league name, this will
109 -- turn an XML listing into a database one.
110 --
111 from_xml_fk_league :: DefaultKey WeatherForecast
112 -> (Maybe String)
113 -> WeatherForecastListingXml
114 -> WeatherForecastListing
115 from_xml_fk_league fk ln WeatherForecastListingXml{..} =
116 WeatherForecastListing {
117 db_weather_forecasts_id = fk,
118 db_league_name = ln,
119 db_teams = xml_teams,
120 db_weather = xml_weather }
121
122
123 -- * WeatherLeague
124
125 -- | XML representation of a league, as they appear in the weather
126 -- documents. There is no associated database representation because
127 -- the league element really adds no information besides its own
128 -- (usually empty) name. The leagues contain listings, so we
129 -- associate the league name with each listing instead.
130 --
131 data WeatherLeague =
132 WeatherLeague {
133 league_name :: Maybe String,
134 listings :: [WeatherForecastListingXml] }
135 deriving (Eq, Show)
136
137
138 -- * WeatherForecast/WeatherForecastXml
139
140 -- | Database representation of a weather forecast.
141 --
142 data WeatherForecast =
143 WeatherForecast {
144 db_weather_id :: DefaultKey Weather,
145 db_game_date :: UTCTime }
146
147
148 -- | XML representation of a weather forecast.
149 --
150 data WeatherForecastXml =
151 WeatherForecastXml {
152 xml_game_date :: UTCTime,
153 xml_leagues :: [WeatherLeague] }
154 deriving (Eq, Show)
155
156
157 instance ToDb WeatherForecastXml where
158 -- | The database representation of a 'WeatherForecastXml' is a
159 -- 'WeatherForecast'.
160 --
161 type Db WeatherForecastXml = WeatherForecast
162
163
164 instance Child WeatherForecastXml where
165 -- | The database type containing a 'WeatherForecastXml' is
166 -- 'Weather'.
167 type Parent WeatherForecastXml = Weather
168
169
170 instance FromXmlFk WeatherForecastXml where
171
172 -- | To convert a 'WeatherForecastXml' into a 'WeatherForecast', we
173 -- just copy everything verbatim.
174 --
175 from_xml_fk fk WeatherForecastXml{..} =
176 WeatherForecast {
177 db_weather_id = fk,
178 db_game_date = xml_game_date }
179
180
181 -- | This allows us to call 'insert_xml' on an 'WeatherForecastXml'
182 -- without first converting it to the database representation.
183 --
184 instance XmlImportFk WeatherForecastXml
185
186
187 -- * Weather/Message
188
189 -- | The database representation of a weather message.
190 --
191 data Weather =
192 Weather {
193 db_xml_file_id :: Int,
194 db_sport :: String,
195 db_title :: String,
196 db_time_stamp :: UTCTime }
197
198
199 -- | The XML representation of a weather message.
200 --
201 data Message =
202 Message {
203 xml_xml_file_id :: Int,
204 xml_heading :: String,
205 xml_category :: String,
206 xml_sport :: String,
207 xml_title :: String,
208 xml_forecasts :: [WeatherForecastXml],
209 xml_time_stamp :: UTCTime }
210 deriving (Eq, Show)
211
212 instance ToDb Message where
213 -- | The database representation of 'Message' is 'Weather'.
214 --
215 type Db Message = Weather
216
217 instance FromXml Message where
218 -- | To get a 'Weather' from a 'Message', we drop a bunch of
219 -- unwanted fields.
220 --
221 from_xml Message{..} =
222 Weather {
223 db_xml_file_id = xml_xml_file_id,
224 db_sport = xml_sport,
225 db_title = xml_title,
226 db_time_stamp = xml_time_stamp }
227
228 -- | This allows us to insert the XML representation 'Message'
229 -- directly.
230 --
231 instance XmlImport Message
232
233
234 --
235 -- Database stuff
236 --
237
238 mkPersist tsn_codegen_config [groundhog|
239 - entity: Weather
240 constructors:
241 - name: Weather
242 uniques:
243 - name: unique_weather
244 type: constraint
245 # Prevent multiple imports of the same message.
246 fields: [db_xml_file_id]
247
248 - entity: WeatherForecast
249 dbName: weather_forecasts
250 constructors:
251 - name: WeatherForecast
252 fields:
253 - name: db_weather_id
254 reference:
255 onDelete: cascade
256
257 - entity: WeatherForecastListing
258 dbName: weather_forecast_listings
259 constructors:
260 - name: WeatherForecastListing
261 fields:
262 - name: db_weather_forecasts_id
263 reference:
264 onDelete: cascade
265
266 |]
267
268
269 instance DbImport Message where
270 dbmigrate _ =
271 run_dbmigrate $ do
272 migrate (undefined :: Weather)
273 migrate (undefined :: WeatherForecast)
274 migrate (undefined :: WeatherForecastListing)
275
276 dbimport m = do
277 -- First we insert the top-level weather record.
278 weather_id <- insert_xml m
279
280 -- Next insert all of the forecasts, one at a time.
281 forM_ (xml_forecasts m) $ \forecast -> do
282 forecast_id <- insert_xml_fk weather_id forecast
283
284 -- With the forecast id in hand, loop through this forecast's
285 -- leagues...
286 forM_ (xml_leagues forecast) $ \league -> do
287 -- Construct the function that converts an XML listing to a
288 -- database one.
289 let todb = from_xml_fk_league forecast_id (league_name league)
290
291 -- Now use it to convert all of the XML listings.
292 let db_listings = map todb (listings league)
293
294 -- And finally, insert those DB listings.
295 mapM_ insert_ db_listings
296
297 return ImportSucceeded
298
299
300 ---
301 --- Pickling
302 ---
303
304 -- | Pickler to convert a 'WeatherForecastListingXml' to/from XML.
305 --
306 pickle_listing :: PU WeatherForecastListingXml
307 pickle_listing =
308 xpElem "listing" $
309 xpWrap (from_pair, to_pair) $
310 xpPair
311 (xpElem "teams" xpText)
312 (xpElem "weather" xpText)
313 where
314 from_pair = uncurry WeatherForecastListingXml
315 to_pair WeatherForecastListingXml{..} = (xml_teams, xml_weather)
316
317
318 -- | Pickler to convert a 'WeatherLeague' to/from XML.
319 --
320 pickle_league :: PU WeatherLeague
321 pickle_league =
322 xpElem "league" $
323 xpWrap (from_pair, to_pair) $
324 xpPair
325 (xpAttr "name" $ xpOption xpText)
326 (xpList pickle_listing)
327 where
328 from_pair = uncurry WeatherLeague
329 to_pair WeatherLeague{..} = (league_name, listings)
330
331
332 -- | Pickler to convert a 'WeatherForecastXml' to/from XML.
333 --
334 pickle_forecast :: PU WeatherForecastXml
335 pickle_forecast =
336 xpElem "forecast" $
337 xpWrap (from_pair, to_pair) $
338 xpPair
339 (xpAttr "gamedate" xp_gamedate)
340 (xpList pickle_league)
341 where
342 from_pair = uncurry WeatherForecastXml
343 to_pair WeatherForecastXml{..} = (xml_game_date,
344 xml_leagues)
345
346
347
348 -- | Pickler to convert a 'Message' to/from XML.
349 --
350 pickle_message :: PU Message
351 pickle_message =
352 xpElem "message" $
353 xpWrap (from_tuple, to_tuple) $
354 xp7Tuple
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)
362 where
363 from_tuple = uncurryN Message
364 to_tuple Message{..} = (xml_xml_file_id,
365 xml_heading,
366 xml_category,
367 xml_sport,
368 xml_title,
369 xml_forecasts,
370 xml_time_stamp)
371
372 --
373 -- Tasty tests
374 --
375
376 weather_tests :: TestTree
377 weather_tests =
378 testGroup
379 "Weather tests"
380 [ test_on_delete_cascade,
381 test_pickle_of_unpickle_is_identity,
382 test_unpickle_succeeds ]
383
384
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.
388 --
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
394 actual @?= expected
395
396
397 -- | Make sure we can actually unpickle these things.
398 --
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
404 let expected = True
405 actual @?= expected
406
407
408 -- | Make sure everything gets deleted when we delete the top-level
409 -- record.
410 --
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
421 migrate a
422 migrate b
423 migrate c
424 _ <- dbimport weather
425 deleteAll a
426 count_a <- countAll a
427 count_b <- countAll b
428 count_c <- countAll c
429 return $ count_a + count_b + count_c
430 let expected = 0
431 actual @?= expected