Ticket #17143: 17143_test.diff

File 17143_test.diff, 2.1 KB (added by calvinspealman, 3 years ago)

Turned the example into a test case

  • new file tests/modeltests/test_17143/models.py

    diff --git a/tests/modeltests/test_17143/__init__.py b/tests/modeltests/test_17143/__init__.py
    new file mode 100644
    index 0000000..e69de29
    diff --git a/tests/modeltests/test_17143/models.py b/tests/modeltests/test_17143/models.py
    new file mode 100644
    index 0000000..dd19a87
    - +  
     1# coding: utf-8
     2"""
     31. Bare-bones model
     4
     5This is a basic model with only two non-primary-key fields.
     6"""
     7from django.db import models
     8
     9
     10init_effect = []
     11
     12
     13class A(models.Model):
     14    foo = models.CharField(max_length=1, default='N')
     15   
     16    def __init__(self, *args, **kwargs):
     17        super(A, self).__init__(*args, **kwargs)
     18       
     19        init_effect.append(self.foo)
     20   
     21class B(A):
     22    bar = models.CharField(max_length=1, default='X')
  • new file tests/modeltests/test_17143/tests.py

    diff --git a/tests/modeltests/test_17143/tests.py b/tests/modeltests/test_17143/tests.py
    new file mode 100644
    index 0000000..8bde29b
    - +  
     1from __future__ import absolute_import
     2
     3from datetime import datetime
     4
     5from django.core.exceptions import ObjectDoesNotExist
     6from django.db.models.fields import FieldDoesNotExist
     7from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
     8
     9from .models import A, B, init_effect
     10
     11
     12class ModelTest(TestCase):
     13
     14    def setUp(self):
     15        self.b = B.objects.create(foo='Y', bar='Z')
     16
     17    def tearDown(self):
     18        init_effect[:] = []
     19
     20    def test_17143_1(self):
     21        self.assertEqual(init_effect, ['Y'])
     22
     23    def test_17143_2(self):
     24        a = A.objects.get()
     25        self.assertEqual(init_effect, ['Y'] * 2)
     26
     27    def test_17143_3(self):
     28        b = B.objects.get()
     29        self.assertEqual(init_effect, ['Y'] * 2)
     30
     31    def test_17143_4(self):
     32        a = A.objects.get()
     33        b = a.b
     34        self.assertEqual(b.foo, a.foo)
     35        self.assertEqual(init_effect, ['Y'] * 3)
     36
     37    def test_17143_5(self):
     38        (a,) = A.objects.select_related('b')
     39        self.assertEqual(a.id, self.b.id)
     40        self.assertEqual(a.b.foo, 'Y')
     41        self.assertEqual(init_effect, ['Y'] * 3)
Back to Top