1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE QuasiQuotes #-}
5 {-# LANGUAGE RecordWildCards #-}
6 {-# LANGUAGE StandaloneDeriving #-}
7 {-# LANGUAGE TemplateHaskell #-}
8 {-# LANGUAGE TypeFamilies #-}
10 -- | Parse TSN XML for the DTD \"newsxml.dtd\". Each document contains
11 -- a root element \<message\> that contains an entire news item.
18 -- * WARNING: these are private but exported to silence warnings
19 News_NewsLocationConstructor(..),
20 News_NewsTeamConstructor(..),
22 NewsLocationConstructor(..),
23 NewsTeamConstructor(..) )
27 import Data.Data ( Data, constrFields, dataTypeConstrs, dataTypeOf )
28 import Data.Time.Clock ( UTCTime )
29 import Data.List.Utils ( join, split )
30 import Data.Tuple.Curry ( uncurryN )
31 import Data.Typeable ( Typeable )
32 import Database.Groundhog (
38 silentMigrationLogger )
39 import Database.Groundhog.Core ( DefaultKey )
40 import Database.Groundhog.Generic ( runDbConn )
41 import Database.Groundhog.Sqlite ( withSqliteConn )
42 import Database.Groundhog.TH (
46 import Test.Tasty ( TestTree, testGroup )
47 import Test.Tasty.HUnit ( (@?=), testCase )
48 import Text.XML.HXT.Core (
64 tsn_db_field_namer ) -- Used in a test
65 import TSN.Database ( insert_or_select )
66 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
67 import TSN.Picklers ( xp_time_stamp )
68 import TSN.XmlImport ( XmlImport(..) )
77 -- | The DTD to which this module corresponds. Used to invoke dbimport.
89 -- | The msg_id child of <message> contains an event_id attribute; we
90 -- embed it into the 'News' type. We (pointlessly) use the "db_"
91 -- prefix here so that the two names don't collide on "id" when
92 -- Groundhog is creating its fields using our field namer.
97 db_event_id :: Maybe Int }
98 deriving (Data, Eq, Show, Typeable)
101 -- | The XML representation of a news item (\<message\>).
105 xml_xml_file_id :: Int,
106 xml_heading :: String,
108 xml_category :: String,
110 xml_url :: Maybe String,
111 xml_teams :: [NewsTeam],
112 xml_locations :: [NewsLocation],
114 xml_editor :: Maybe String,
115 xml_text :: Maybe String, -- Text and continue seem to show up in pairs,
116 xml_continue :: Maybe String, -- either both present or both missing.
117 xml_time_stamp :: UTCTime }
121 -- | The database representation of a news item. We drop several
122 -- uninteresting fields from 'Message', and omit the list fields which
123 -- will be represented as join tables.
127 db_xml_file_id :: Int,
130 db_url :: Maybe String,
132 db_editor :: Maybe String,
133 db_text :: Maybe String,
134 db_continue :: Maybe String,
135 db_time_stamp :: UTCTime }
136 deriving (Data, Eq, Show, Typeable)
140 instance ToDb Message where
141 -- | The database representation of 'Message' is 'News'.
142 type Db Message = News
144 -- | Convert the XML representation 'Message' to the database
145 -- representation 'News'.
147 instance FromXml Message where
148 -- | We use a record wildcard so GHC doesn't complain that we never
149 -- used the field names.
151 from_xml Message{..} = News { db_xml_file_id = xml_xml_file_id,
153 db_sport = xml_sport,
156 db_editor = xml_editor,
158 db_continue = xml_continue,
159 db_time_stamp = xml_time_stamp }
161 -- | This lets us insert the XML representation 'Message' directly.
163 instance XmlImport Message
168 -- | The database type for teams as they show up in the news.
171 NewsTeam { team_name :: String }
178 -- | Mapping between News records and NewsTeam records in the
179 -- database. We don't name the fields because we don't use the names
180 -- explicitly; that means we have to give them nice database names
183 data News_NewsTeam = News_NewsTeam
185 (DefaultKey NewsTeam)
190 -- | The database type for locations as they show up in the news.
194 city :: Maybe String,
195 state :: Maybe String,
200 -- * News_NewsLocation
202 -- | Mapping between News records and NewsLocation records in the
203 -- database. We don't name the fields because we don't use the names
204 -- explicitly; that means we have to give them nice database names
207 data News_NewsLocation = News_NewsLocation
209 (DefaultKey NewsLocation)
217 -- | Define 'dbmigrate' and 'dbimport' for 'Message's. The import is
218 -- slightly non-generic because of our 'News_NewsTeam' and
219 -- 'News_NewsLocation' join tables.
221 instance DbImport Message where
224 migrate (undefined :: News)
225 migrate (undefined :: NewsTeam)
226 migrate (undefined :: News_NewsTeam)
227 migrate (undefined :: NewsLocation)
228 migrate (undefined :: News_NewsLocation)
230 dbimport message = do
231 -- Insert the message and acquire its primary key (unique ID)
232 news_id <- insert_xml message
234 -- And insert each one into its own table. We use insert_xml_or_select
235 -- because we know that most teams will already exist, and we
236 -- want to get back the id for the existing team when
237 -- there's a collision.
238 nt_ids <- mapM insert_or_select (xml_teams message)
240 -- Now that the teams have been inserted, create
241 -- news__news_team records mapping beween the two.
242 let news_news_teams = map (News_NewsTeam news_id) nt_ids
243 mapM_ insert_ news_news_teams
245 -- Do all of that over again for the NewsLocations.
246 loc_ids <- mapM insert_or_select (xml_locations message)
247 let news_news_locations = map (News_NewsLocation news_id) loc_ids
248 mapM_ insert_ news_news_locations
250 return ImportSucceeded
253 -- These types don't have special XML representations or field name
254 -- collisions so we use the defaultCodegenConfig and give their
255 -- fields nice simple names.
256 mkPersist defaultCodegenConfig [groundhog|
262 - name: unique_news_team
266 - entity: NewsLocation
267 dbName: news_locations
271 - name: unique_news_location
273 fields: [city, state, country]
278 -- These types have fields with e.g. db_ and xml_ prefixes, so we
279 -- use our own codegen to peel those off before naming the columns.
280 mkPersist tsn_codegen_config [groundhog|
287 # Prevent multiple imports of the same message.
288 fields: [db_xml_file_id]
292 - {name: msg_id, dbName: msg_id}
293 - {name: event_id, dbName: event_id}
302 - entity: News_NewsTeam
303 dbName: news__news_teams
305 - name: News_NewsTeam
307 - name: news_NewsTeam0 # Default created by mkNormalFieldName
311 - name: news_NewsTeam1 # Default created by mkNormalFieldName
312 dbName: news_teams_id
316 - entity: News_NewsLocation
317 dbName: news__news_locations
319 - name: News_NewsLocation
321 - name: news_NewsLocation0 # Default created by mkNormalFieldName
325 - name: news_NewsLocation1 # Default created by mkNormalFieldName
326 dbName: news_locations_id
336 -- | Convert a 'NewsTeam' to/from XML.
338 pickle_news_team :: PU NewsTeam
341 xpWrap (from_string, to_string) xpText
343 to_string :: NewsTeam -> String
344 to_string = team_name
346 from_string :: String -> NewsTeam
347 from_string = NewsTeam
350 -- | Convert a 'MsgId' to/from XML.
352 pickle_msg_id :: PU MsgId
355 xpWrap (from_tuple, to_tuple) $
356 xpPair xpInt (xpAttr "EventId" (xpOption xpInt))
358 from_tuple = uncurryN MsgId
359 to_tuple m = (db_msg_id m, db_event_id m)
362 -- | Convert a 'NewsLocation' to/from XML.
364 pickle_location :: PU NewsLocation
367 xpWrap (from_tuple, to_tuple) $
368 xpTriple (xpOption (xpElem "city" xpText))
369 (xpOption (xpElem "state" xpText))
370 (xpElem "country" xpText)
373 uncurryN NewsLocation
374 to_tuple l = (city l, state l, country l)
377 -- | Convert a 'Message' to/from XML.
379 pickle_message :: PU Message
382 xpWrap (from_tuple, to_tuple) $
383 xp13Tuple (xpElem "XML_File_ID" xpInt)
384 (xpElem "heading" xpText)
386 (xpElem "category" xpText)
387 (xpElem "sport" xpText)
388 (xpElem "url" $ xpOption xpText)
389 (xpList pickle_news_team)
390 (xpList pickle_location)
391 (xpElem "SMS" xpText)
392 (xpOption (xpElem "Editor" xpText))
393 (xpOption (xpElem "text" xpText))
395 (xpElem "time_stamp" xp_time_stamp)
397 from_tuple = uncurryN Message
398 to_tuple m = (xml_xml_file_id m, -- Verbose,
399 xml_heading m, -- but
400 xml_mid m, -- eliminates
401 xml_category m, -- GHC
402 xml_sport m, -- warnings
405 xml_locations m, -- .
412 -- | We combine all of the \<continue\> elements into one 'String'
413 -- while unpickling and do the reverse while pickling.
415 pickle_continue :: PU (Maybe String)
418 xpWrap (to_string, from_string) $
420 xpList (xpElem "P" xpText)
422 from_string :: String -> [String]
423 from_string = split "\n"
425 to_string :: [String] -> String
426 to_string = join "\n"
433 -- | A list of all tests for this module.
435 news_tests :: TestTree
439 [ test_news_fields_have_correct_names,
440 test_on_delete_cascade,
441 test_pickle_of_unpickle_is_identity,
442 test_unpickle_succeeds ]
445 -- | Make sure our codegen is producing the correct database names.
447 test_news_fields_have_correct_names :: TestTree
448 test_news_fields_have_correct_names =
449 testCase "news fields get correct database names" $
450 mapM_ check (zip actual expected)
452 -- This is cool, it uses the (derived) Data instance of
453 -- News.News to get its constructor names.
454 field_names :: [String]
456 constrFields . head $ dataTypeConstrs $ dataTypeOf (undefined :: News)
460 map (\x -> tsn_db_field_namer "herp" "derp" 8675309 x 90210) field_names
463 actual = ["xml_file_id",
472 check (x,y) = (x @?= y)
475 -- | If we unpickle something and then pickle it, we should wind up
476 -- with the same thing we started with. WARNING: success of this
477 -- test does not mean that unpickling succeeded.
479 test_pickle_of_unpickle_is_identity :: TestTree
480 test_pickle_of_unpickle_is_identity = testGroup "pickle-unpickle tests"
481 [ check "pickle composed with unpickle is the identity"
482 "test/xml/newsxml.xml",
484 check "pickle composed with unpickle is the identity (with Editor)"
485 "test/xml/newsxml-with-editor.xml" ]
487 check desc path = testCase desc $ do
488 (expected, actual) <- pickle_unpickle pickle_message path
492 -- | Make sure we can actually unpickle these things.
494 test_unpickle_succeeds :: TestTree
495 test_unpickle_succeeds = testGroup "unpickle tests"
496 [ check "unpickling succeeds"
497 "test/xml/newsxml.xml",
499 check "unpickling succeeds (with Editor)"
500 "test/xml/newsxml-with-editor.xml" ]
502 check desc path = testCase desc $ do
503 actual <- unpickleable path pickle_message
508 -- | Make sure everything gets deleted when we delete the top-level
511 test_on_delete_cascade :: TestTree
512 test_on_delete_cascade = testGroup "cascading delete tests"
513 [ check "deleting news deletes its children"
514 "test/xml/newsxml.xml"
515 4 -- 2 news_teams and 2 news_locations that should remain.
518 check desc path expected = testCase desc $ do
519 news <- unsafe_unpickle path pickle_message
520 let a = undefined :: News
521 let b = undefined :: NewsTeam
522 let c = undefined :: News_NewsTeam
523 let d = undefined :: NewsLocation
524 let e = undefined :: News_NewsLocation
525 actual <- withSqliteConn ":memory:" $ runDbConn $ do
526 runMigration silentMigrationLogger $ do
534 count_a <- countAll a
535 count_b <- countAll b
536 count_c <- countAll c
537 count_d <- countAll d
538 count_e <- countAll e
539 return $ count_a + count_b + count_c + count_d + count_e