diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py
index 51f439c..d796370 100644
a
|
b
|
class AggregationTests(TestCase):
|
625 | 625 | lambda: Book.objects.annotate(mean_age=Avg('authors__age')).annotate(Avg('mean_age')) |
626 | 626 | ) |
627 | 627 | |
| 628 | def test_m2m_overwrite(self): |
| 629 | "Regression for #14373: don't let annotate/aggregate lose m2m data." |
| 630 | list(Book.objects.all().annotate(authors=Count('authors'))) |
| 631 | self.assertEqual(sorted(a.name for a in Book.objects.get(pk=1).authors.all()), |
| 632 | ['Adrian Holovaty', 'Jacob Kaplan-Moss']) |
| 633 | |
628 | 634 | if run_stddev_tests(): |
629 | 635 | def test_stddev(self): |
630 | 636 | self.assertEqual( |
diff --git a/tests/regressiontests/m2m_regress/models.py b/tests/regressiontests/m2m_regress/models.py
index 5453d39..b9e1733 100644
a
|
b
|
class User(models.Model):
|
57 | 57 | name = models.CharField(max_length=30) |
58 | 58 | friends = models.ManyToManyField(auth.User) |
59 | 59 | |
| 60 | # Regression for #14373 -- Assigning an integer as the value of an m2m related |
| 61 | # attribute should raise ValueError, not clear all relationships. |
| 62 | |
| 63 | class Platform(models.Model): |
| 64 | name = models.CharField(max_length=25) |
| 65 | tags = models.ManyToManyField(Tag) |
| 66 | |
60 | 67 | __test__ = {"regressions": """ |
61 | 68 | # Multiple m2m references to the same model or a different model must be |
62 | 69 | # distinguished when accessing the relations through an instance attribute. |
diff --git a/tests/regressiontests/m2m_regress/tests.py b/tests/regressiontests/m2m_regress/tests.py
new file mode 100644
index 0000000..0618e34
-
|
+
|
|
| 1 | from django.test import TestCase |
| 2 | |
| 3 | from regressiontests.m2m_regress.models import Platform, Tag |
| 4 | |
| 5 | class M2MAssignmentTests(TestCase): |
| 6 | """ |
| 7 | Regression tests for #14373. Assigning nonsense to an m2m attribute |
| 8 | shouldn't silently lose data. |
| 9 | |
| 10 | """ |
| 11 | def setUp(self): |
| 12 | self.platform = Platform.objects.create(name='BSD') |
| 13 | self.platform.tags = [Tag.objects.create(name='Net'), |
| 14 | Tag.objects.create(name='Free')] |
| 15 | |
| 16 | def test_assigning_integer(self): |
| 17 | try: |
| 18 | self.platform.tags = 7 |
| 19 | except TypeError: |
| 20 | pass |
| 21 | else: |
| 22 | self.fail("Assignment of integer to m2m attribute should have raised TypeError") |
| 23 | self.assertEqual(sorted(t.name for t in self.platform.tags.all()), |
| 24 | ['Free', 'Net']) |