]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/XML/Injuries.hs
Fixing compiler warnings by exporting stuff that shouldn't be exported (woo).
[dead/htsn-import.git] / src / TSN / XML / Injuries.hs
index 53e24196c238c9c3ac2b8668287647fbe4e7b0ff..8002799c62e0e26f4bb7217740ac4651aaa0235f 100644 (file)
@@ -1,7 +1,7 @@
+{-# LANGUAGE DeriveDataTypeable #-}
 {-# LANGUAGE FlexibleInstances #-}
 {-# LANGUAGE GADTs #-}
 {-# LANGUAGE QuasiQuotes #-}
-{-# LANGUAGE ScopedTypeVariables #-}
 {-# LANGUAGE StandaloneDeriving #-}
 {-# LANGUAGE TemplateHaskell #-}
 {-# LANGUAGE TypeFamilies #-}
 --   automatically. The root message is not retained.
 --
 module TSN.XML.Injuries (
-  Listing,
-  Message( listings ),
-  injuries_tests )
+  injuries_tests,
+  pickle_message,
+  -- * WARNING: these are private but exported to silence warnings
+  ListingConstructor(..) )
 where
 
-import Data.Tuple.Curry ( uncurryN )
-import Database.Groundhog()
+import Data.Data ( Data )
+import Data.Typeable ( Typeable )
+import Database.Groundhog (
+  migrate )
 import Database.Groundhog.TH (
   defaultCodegenConfig,
   groundhog,
   mkPersist )
+import Data.Tuple.Curry ( uncurryN )
 import Test.Tasty ( TestTree, testGroup )
 import Test.Tasty.HUnit ( (@?=), testCase )
 import Text.XML.HXT.Core (
   PU,
-  XmlPickler(..),
   xp4Tuple,
   xp6Tuple,
+  xpAttr,
   xpElem,
   xpInt,
   xpList,
+  xpOption,
+  xpPair,
   xpPrim,
   xpText,
   xpWrap )
 
 
-import TSN.DbImport ( DbImport(..), import_generic )
-import Xml ( pickle_unpickle )
+import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
+import TSN.XmlImport ( XmlImport(..) )
+import Xml ( FromXml(..), pickle_unpickle, unpickleable )
 
+data InjuriesTeam =
+  InjuriesTeam {
+    team_name :: String,
+    team_league :: Maybe String }
+  deriving (Data, Eq, Show, Typeable)
 
 data Listing =
   Listing {
-    team :: String,
-    teamno :: Int,
+    team :: InjuriesTeam,
+    teamno :: Maybe Int,
     injuries :: String,
-    updated :: Bool }
+    updated :: Maybe Bool }
   deriving (Eq, Show)
 
+instance FromXml Listing where
+  type Db Listing = Listing
+  from_xml = id
+
+instance XmlImport Listing
+
 data Message =
   Message {
     xml_file_id :: Int,
@@ -62,28 +80,50 @@ data Message =
     time_stamp :: String }
   deriving (Eq, Show)
 
+instance DbImport Message where
+  dbimport msg = mapM_ insert_xml (listings msg) >> return ImportSucceeded
+
+  dbmigrate _ = run_dbmigrate $ migrate (undefined :: Listing)
 
 mkPersist defaultCodegenConfig [groundhog|
 - entity: Listing
-  dbName: injuries
+  dbName: injuries_listings
+  constructors:
+    - name: Listing
+      fields:
+        - name: team
+          embeddedType:
+            - {name: team_name, dbName: team_name}
+            - {name: team_league, dbName: team_league}
+- embedded: InjuriesTeam
+  fields:
+    - name: team_name
+    - name: team_league
 |]
 
 
+pickle_injuries_team :: PU InjuriesTeam
+pickle_injuries_team =
+  xpElem "team" $
+    xpWrap (from_tuple, to_tuple) $
+    xpPair xpText (xpOption $ xpAttr "league" xpText)
+  where
+    from_tuple = uncurryN InjuriesTeam
+    to_tuple m = (team_name m, team_league m)
+
+
 pickle_listing :: PU Listing
 pickle_listing =
   xpElem "listing" $
     xpWrap (from_tuple, to_tuple) $
-    xp4Tuple (xpElem "team" xpText)
-             (xpElem "teamno" xpInt)
+    xp4Tuple pickle_injuries_team
+             (xpOption $ xpElem "teamno" xpInt)
              (xpElem "injuries" xpText)
-             (xpElem "updated" xpPrim)
+             (xpOption $ xpElem "updated" xpPrim)
   where
     from_tuple = uncurryN Listing
     to_tuple l = (team l, teamno l, injuries l, updated l)
 
-instance XmlPickler Listing where
-  xpickle = pickle_listing
-
 
 pickle_message :: PU Message
 pickle_message =
@@ -104,25 +144,31 @@ pickle_message =
                   listings m,
                   time_stamp m)
 
-instance XmlPickler Message where
-  xpickle = pickle_message
-
 
 
-instance DbImport Listing where
-  dbimport = import_generic listings
-
 -- * Tasty Tests
 injuries_tests :: TestTree
 injuries_tests =
   testGroup
     "Injuries tests"
-    [ test_pickle_of_unpickle_is_identity ]
+    [ test_pickle_of_unpickle_is_identity,
+      test_unpickle_succeeds ]
 
 
+-- | Warning, succeess of this test does not mean that unpickling
+--   succeeded.
 test_pickle_of_unpickle_is_identity :: TestTree
 test_pickle_of_unpickle_is_identity =
   testCase "pickle composed with unpickle is the identity" $ do
     let path = "test/xml/injuriesxml.xml"
-    (expected :: [Message], actual) <- pickle_unpickle "message" path
+    (expected, actual) <- pickle_unpickle pickle_message path
     actual @?= expected
+
+
+test_unpickle_succeeds :: TestTree
+test_unpickle_succeeds =
+  testCase "unpickling succeeds" $ do
+  let path = "test/xml/injuriesxml.xml"
+  actual <- unpickleable path pickle_message
+  let expected = True
+  actual @?= expected