Ticket #28405: test.diff

File test.diff, 3.0 KB (added by Jon Dufresne, 2 years ago)

An example unit test

  • tests/postgres_tests/migrations/0002_create_test_models.py

    diff --git a/tests/postgres_tests/migrations/0002_create_test_models.py b/tests/postgres_tests/migrations/0002_create_test_models.py
    index 1cb6fe6..4fbe68b 100644
    a b class Migration(migrations.Migration): 
    150150            bases=None,
    151151        ),
    152152        migrations.CreateModel(
     153            name='CITestModel2',
     154            fields=[
     155                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
     156                ('name', CICharField(max_length=255, unique=True)),
     157            ],
     158            options={
     159                'required_db_vendor': 'postgresql',
     160            },
     161            bases=None,
     162        ),
     163        migrations.CreateModel(
    153164            name='Line',
    154165            fields=[
    155166                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
  • tests/postgres_tests/models.py

    diff --git a/tests/postgres_tests/models.py b/tests/postgres_tests/models.py
    index fcfd85f..e7df73f 100644
    a b class CITestModel(PostgreSQLModel): 
    112112        return self.name
    113113
    114114
     115class CITestModel2(PostgreSQLModel):
     116    name = CICharField(max_length=255, unique=True)
     117
     118    def __str__(self):
     119        return self.name
     120
     121
    115122class Line(PostgreSQLModel):
    116123    scene = models.ForeignKey('Scene', models.CASCADE)
    117124    character = models.ForeignKey('Character', models.CASCADE)
  • tests/postgres_tests/test_citext.py

    diff --git a/tests/postgres_tests/test_citext.py b/tests/postgres_tests/test_citext.py
    index 0a7012b..f4ec8d7 100644
    a b modifiers to enforce use of an index. 
    55"""
    66from django.db import IntegrityError
    77from django.test.utils import modify_settings
     8from django.forms.models import modelformset_factory
    89
    910from . import PostgreSQLTestCase
    10 from .models import CITestModel
     11from .models import CITestModel, CITestModel2
    1112
    1213
    1314@modify_settings(INSTALLED_APPS={'append': 'django.contrib.postgres'})
    class CITextTestCase(PostgreSQLTestCase): 
    4243        instance = CITestModel.objects.get()
    4344        self.assertEqual(instance.array_field, self.john.array_field)
    4445        self.assertTrue(CITestModel.objects.filter(array_field__contains=['joe']).exists())
     46
     47    def test_duplicate_formset_mixed_case(self):
     48        FormSet = modelformset_factory(CITestModel2, extra=2, fields=['name'])
     49        data = {
     50            'form-TOTAL_FORMS': 2,
     51            'form-INITIAL_FORMS': 0,
     52            'form-0-name': 'frank',
     53            'form-1-name': 'FRANK',
     54        }
     55        formset = FormSet(data)
     56        self.assertEquals(
     57            formset.non_form_errors(),
     58            ['Please correct the duplicate data for name.'],
     59        )
     60        self.assertEquals(
     61            formset.errors,
     62            [{}, {'__all__': ['Please correct the duplicate values below.']}],
     63        )
Back to Top