{-# LANGUAGE BangPatterns #-}
+{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE QuasiQuotes #-}
where
import Control.Monad.IO.Class ( MonadIO, liftIO )
+import Data.Data ( Data, constrFields, dataTypeConstrs, dataTypeOf )
import Data.List.Utils ( join, split )
import Data.Tuple.Curry ( uncurryN )
+import Data.Typeable ( Typeable )
import Database.Groundhog (
defaultMigrationLogger,
insert,
xpWrap )
import Network.Services.TSN.Report ( report_error )
-import TSN.Codegen ( tsn_codegen_config )
+import TSN.Codegen (
+ tsn_codegen_config,
+ tsn_db_field_namer -- Used in a test.
+ )
import TSN.DbImport ( DbImport(..) )
import Xml ( ToFromXml(..), pickle_unpickle )
MsgId {
db_msg_id :: Int,
db_event_id :: Maybe Int }
- deriving (Eq, Show)
+ deriving (Data, Eq, Show, Typeable)
data MessageXml =
db_sms :: String,
db_text :: String,
db_continue :: String }
- deriving (Eq, Show)
+ deriving (Data, Eq, Show, Typeable)
instance ToFromXml Message where
type Xml Message = MessageXml
news_tests =
testGroup
"News tests"
- [ test_pickle_of_unpickle_is_identity ]
+ [ test_news_fields_have_correct_names,
+ test_pickle_of_unpickle_is_identity ]
test_pickle_of_unpickle_is_identity :: TestTree
let path = "test/xml/newsxml.xml"
(expected :: [MessageXml], actual) <- pickle_unpickle "message" path
actual @?= expected
+
+
+test_news_fields_have_correct_names :: TestTree
+test_news_fields_have_correct_names =
+ testCase "news fields get correct database names" $ do
+ mapM_ check (zip actual expected)
+ where
+ -- This is cool, it uses the (derived) Data instance of
+ -- News.Message to get its constructor names.
+ field_names :: [String]
+ field_names =
+ constrFields . head $ dataTypeConstrs $ dataTypeOf (undefined :: Message)
+
+ expected :: [String]
+ expected =
+ map (\x -> tsn_db_field_namer "herp" "derp" 8675309 x 90210) field_names
+
+ actual :: [String]
+ actual = ["mid", "sport", "url", "sms", "text", "continue"]
+
+ check (x,y) = (x @?= y)