Ticket #14373: 14373_r13978_initial_tests.diff

File 14373_r13978_initial_tests.diff, 2.7 KB (added by Carl Meyer, 14 years ago)
  • tests/regressiontests/aggregation_regress/tests.py

    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):  
    625625            lambda: Book.objects.annotate(mean_age=Avg('authors__age')).annotate(Avg('mean_age'))
    626626        )
    627627
     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
    628634    if run_stddev_tests():
    629635        def test_stddev(self):
    630636            self.assertEqual(
  • tests/regressiontests/m2m_regress/models.py

    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):  
    5757    name = models.CharField(max_length=30)
    5858    friends = models.ManyToManyField(auth.User)
    5959
     60# Regression for #14373 -- Assigning an integer as the value of an m2m related
     61# attribute should raise ValueError, not clear all relationships.
     62
     63class Platform(models.Model):
     64    name = models.CharField(max_length=25)
     65    tags = models.ManyToManyField(Tag)
     66
    6067__test__ = {"regressions": """
    6168# Multiple m2m references to the same model or a different model must be
    6269# distinguished when accessing the relations through an instance attribute.
  • new file tests/regressiontests/m2m_regress/tests.py

    diff --git a/tests/regressiontests/m2m_regress/tests.py b/tests/regressiontests/m2m_regress/tests.py
    new file mode 100644
    index 0000000..0618e34
    - +  
     1from django.test import TestCase
     2
     3from regressiontests.m2m_regress.models import Platform, Tag
     4
     5class 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'])
Back to Top