import Data.Data ( Data )
import Data.Time ( UTCTime )
import Data.Typeable ( Typeable )
-import Database.Groundhog ( migrate )
+import Database.Groundhog (
+ countAll,
+ executeRaw,
+ migrate,
+ runMigration,
+ silentMigrationLogger )
import Database.Groundhog.Core ( DefaultKey )
+import Database.Groundhog.Generic ( runDbConn )
import Database.Groundhog.TH (
groundhog,
mkPersist )
+import Database.Groundhog.Sqlite ( withSqliteConn )
import Data.Tuple.Curry ( uncurryN )
import Test.Tasty ( TestTree, testGroup )
import Test.Tasty.HUnit ( (@?=), testCase )
FromXmlFk(..),
ToDb(..),
pickle_unpickle,
- unpickleable )
+ unpickleable,
+ unsafe_unpickle )
--
injuries_tests =
testGroup
"Injuries tests"
- [ test_pickle_of_unpickle_is_identity,
+ [ test_on_delete_cascade,
+ test_pickle_of_unpickle_is_identity,
test_unpickle_succeeds ]
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
+ let path = "test/xml/injuriesxml.xml"
+ actual <- unpickleable path pickle_message
+ let expected = True
+ actual @?= expected
+
+
+-- | Make sure everything gets deleted when we delete the top-level
+-- record.
+--
+test_on_delete_cascade :: TestTree
+test_on_delete_cascade =
+ testCase "deleting an injuries deletes its children" $ do
+ let path = "test/xml/injuriesxml.xml"
+ inj <- unsafe_unpickle path pickle_message
+ let a = undefined :: Injuries
+ let b = undefined :: InjuriesListing
+ actual <- withSqliteConn ":memory:" $ runDbConn $ do
+ runMigration silentMigrationLogger $ do
+ migrate a
+ migrate b
+ _ <- dbimport inj
+ -- No idea how 'delete' works, so do this instead.
+ executeRaw False "DELETE FROM injuries;" []
+ count_a <- countAll a
+ count_b <- countAll b
+ return $ count_a + count_b
+ let expected = 0
+ actual @?= expected
import Data.Tuple.Curry ( uncurryN )
import Database.Groundhog (
DefaultKey,
- migrate )
+ countAll,
+ executeRaw,
+ migrate,
+ runMigration,
+ silentMigrationLogger )
+import Database.Groundhog.Generic ( runDbConn )
+import Database.Groundhog.Sqlite ( withSqliteConn )
import Database.Groundhog.TH (
groundhog,
mkPersist )
FromXmlFk(..),
ToDb(..),
pickle_unpickle,
- unpickleable )
+ unpickleable,
+ unsafe_unpickle )
--
injuries_detail_tests =
testGroup
"InjuriesDetail tests"
- [ test_pickle_of_unpickle_is_identity,
+ [ test_on_delete_cascade,
+ test_pickle_of_unpickle_is_identity,
test_unpickle_succeeds ]
actual <- unpickleable path pickle_message
let expected = True
actual @?= expected
+
+
+-- | Make sure everything gets deleted when we delete the top-level
+-- record.
+--
+test_on_delete_cascade :: TestTree
+test_on_delete_cascade = testGroup "cascading delete tests"
+ [ check "delete of injuries_detail deletes its children"
+ "test/xml/Injuries_Detail_XML.xml",
+
+ check "delete of injuries_detail deletes its children (non-int team_id)"
+ "test/xml/Injuries_Detail_XML-noninteger-team-id.xml" ]
+ where
+ check desc path = testCase desc $ do
+ inj <- unsafe_unpickle path pickle_message
+ let a = undefined :: InjuriesDetail
+ let b = undefined :: InjuriesDetailListing
+ let c = undefined :: InjuriesDetailListingPlayerListing
+ actual <- withSqliteConn ":memory:" $ runDbConn $ do
+ runMigration silentMigrationLogger $ do
+ migrate a
+ migrate b
+ migrate c
+ _ <- dbimport inj
+ -- No idea how 'delete' works, so do this instead.
+ executeRaw False "DELETE FROM injuries_detail;" []
+ count_a <- countAll a
+ count_b <- countAll b
+ count_c <- countAll c
+ return $ count_a + count_b + count_c
+ let expected = 0
+ actual @?= expected