]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
add better tests for [ticket:2750]
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Jun 2013 15:21:19 +0000 (11:21 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Jun 2013 15:21:19 +0000 (11:21 -0400)
test/orm/inheritance/test_basic.py

index f313a34ffd29e4af97952e3deb6e2bc5b59395af..14d43e0c790b55b5ebb33a47ae981283ae2ccc21 100644 (file)
@@ -616,7 +616,10 @@ class PolymorphicAttributeManagementTest(fixtures.MappedTest):
 
         assert isinstance(sess.query(A).first(), C)
 
-    def test_assignment(self):
+    def test_valid_assignment_upwards(self):
+        """test that we can assign 'd' to a B, since B/D
+        both involve the same set of tables.
+        """
         D, B = self.classes.D, self.classes.B
 
         sess = Session()
@@ -627,8 +630,11 @@ class PolymorphicAttributeManagementTest(fixtures.MappedTest):
         sess.close()
         assert isinstance(sess.query(B).first(), D)
 
-    def test_invalid_assignment(self):
-        C, B = self.classes.C, self.classes.B
+    def test_invalid_assignment_downwards(self):
+        """test that we warn on assign of 'b' to a C, since this adds
+        a row to the C table we'd never load.
+        """
+        C = self.classes.C
 
         sess = Session()
         c1 = C()
@@ -642,6 +648,42 @@ class PolymorphicAttributeManagementTest(fixtures.MappedTest):
             sess.flush
         )
 
+    def test_invalid_assignment_upwards(self):
+        """test that we warn on assign of 'c' to a B, since we will have a
+        "C" row that has no joined row, which will cause object
+        deleted errors.
+        """
+        B = self.classes.B
+
+        sess = Session()
+        b1 = B()
+        b1.class_name = 'c'
+        sess.add(b1)
+        assert_raises_message(
+            sa_exc.SAWarning,
+            "Flushing object %s with incompatible "
+            "polymorphic identity 'c'; the object may not "
+            "refresh and/or load correctly" % instance_str(b1),
+            sess.flush
+        )
+
+    def test_entirely_oob_assignment(self):
+        """test warn on an unknown polymorphic identity.
+        """
+        B = self.classes.B
+
+        sess = Session()
+        b1 = B()
+        b1.class_name = 'xyz'
+        sess.add(b1)
+        assert_raises_message(
+            sa_exc.SAWarning,
+            "Flushing object %s with incompatible "
+            "polymorphic identity 'xyz'; the object may not "
+            "refresh and/or load correctly" % instance_str(b1),
+            sess.flush
+        )
+
 class CascadeTest(fixtures.MappedTest):
     """that cascades on polymorphic relationships continue
     cascading along the path of the instance's mapper, not