summaryrefslogtreecommitdiff
path: root/tests/model_fields
diff options
context:
space:
mode:
authorDaniyal <abbasi.daniyal98@gmail.com>2021-03-24 11:15:08 +0530
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-07-15 11:43:33 +0200
commitf479df7f8d03ab767bb5e5d655243191087d6432 (patch)
treebf6f248ad7938a0d9f77ea616a59fba2d5a8befb /tests/model_fields
parent08f077888548a951f01b454d0db08d9407f7f0aa (diff)
downloaddjango-f479df7f8d03ab767bb5e5d655243191087d6432.tar.gz
Refs #32508 -- Raised Type/ValueError instead of using "assert" in django.db.models.
Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
Diffstat (limited to 'tests/model_fields')
-rw-r--r--tests/model_fields/test_foreignkey.py9
-rw-r--r--tests/model_fields/test_manytomanyfield.py28
2 files changed, 37 insertions, 0 deletions
diff --git a/tests/model_fields/test_foreignkey.py b/tests/model_fields/test_foreignkey.py
index d30cca9b5c..0cd6d62a55 100644
--- a/tests/model_fields/test_foreignkey.py
+++ b/tests/model_fields/test_foreignkey.py
@@ -147,3 +147,12 @@ class ForeignKeyTests(TestCase):
)
with self.assertRaisesMessage(FieldError, msg):
Related._meta.get_field('child').related_fields
+
+ def test_invalid_to_parameter(self):
+ msg = (
+ "ForeignKey(1) is invalid. First parameter to ForeignKey must be "
+ "either a model, a model name, or the string 'self'"
+ )
+ with self.assertRaisesMessage(TypeError, msg):
+ class MyModel(models.Model):
+ child = models.ForeignKey(1, models.CASCADE)
diff --git a/tests/model_fields/test_manytomanyfield.py b/tests/model_fields/test_manytomanyfield.py
index 5724fe9384..0ddf57ceaf 100644
--- a/tests/model_fields/test_manytomanyfield.py
+++ b/tests/model_fields/test_manytomanyfield.py
@@ -59,6 +59,34 @@ class ManyToManyFieldTests(SimpleTestCase):
assert_app_model_resolved('model_fields')
assert_app_model_resolved('tests')
+ def test_invalid_to_parameter(self):
+ msg = (
+ "ManyToManyField(1) is invalid. First parameter to "
+ "ManyToManyField must be either a model, a model name, or the "
+ "string 'self'"
+ )
+ with self.assertRaisesMessage(TypeError, msg):
+ class MyModel(models.Model):
+ m2m = models.ManyToManyField(1)
+
+ @isolate_apps('model_fields')
+ def test_through_db_table_mutually_exclusive(self):
+ class Child(models.Model):
+ pass
+
+ class Through(models.Model):
+ referred = models.ForeignKey(Child, on_delete=models.CASCADE)
+ referent = models.ForeignKey(Child, on_delete=models.CASCADE)
+
+ msg = 'Cannot specify a db_table if an intermediary model is used.'
+ with self.assertRaisesMessage(ValueError, msg):
+ class MyModel(models.Model):
+ m2m = models.ManyToManyField(
+ Child,
+ through='Through',
+ db_table='custom_name',
+ )
+
class ManyToManyFieldDBTests(TestCase):