Ticket #14394: 14394_r13984_testonly.diff

File 14394_r13984_testonly.diff, 1.9 KB (added by carljm, 4 years ago)
  • tests/regressiontests/m2m_regress/models.py

    diff --git a/tests/regressiontests/m2m_regress/models.py b/tests/regressiontests/m2m_regress/models.py
    index 5453d39..8e0b00d 100644
    a b class User(models.Model): 
    5757    name = models.CharField(max_length=30)
    5858    friends = models.ManyToManyField(auth.User)
    5959
     60# Assigning an integer as the value of an m2m related attribute should not
     61# 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..cb85e47
    - +  
     1from django.test import TestCase
     2
     3from regressiontests.m2m_regress.models import Platform, Tag
     4
     5class M2MAssignmentTests(TestCase):
     6    """
     7    Assigning nonsense to an m2m attribute shouldn't lose data.
     8
     9    """
     10    def setUp(self):
     11        self.platform = Platform.objects.create(name='BSD')
     12        self.platform.tags = [Tag.objects.create(name='Net'),
     13                              Tag.objects.create(name='Free')]
     14
     15    def test_assigning_integer(self):
     16        try:
     17            self.platform.tags = 7
     18        except TypeError:
     19            pass
     20        else:
     21            self.fail("Assignment of integer to m2m attribute should have raised TypeError")
     22        # ensure we're getting actual data from the database, not anything cached
     23        platform = Platform.objects.get(pk=self.platform.pk)
     24        self.assertEqual(sorted(t.name for t in platform.tags.all()),
     25                         ['Free', 'Net'])
Back to Top