Tests schema ArrayField

class SchemaTests(TransactionTestCase)

 1 import datetime
 2 import itertools
 3 import unittest
 4 from copy import copy
 5 from unittest import mock
 6
 7 from django.core.management.color import no_style
 8 from django.db import (
 9     DatabaseError, DataError, IntegrityError, OperationalError, connection,
10 )
11 from django.db.models import (
12     CASCADE, PROTECT, AutoField, BigAutoField, BigIntegerField, BinaryField,
13     BooleanField, CharField, CheckConstraint, DateField, DateTimeField,
14     ForeignKey, ForeignObject, Index, IntegerField, ManyToManyField, Model,
15     OneToOneField, PositiveIntegerField, Q, SlugField, SmallAutoField,
16     SmallIntegerField, TextField, TimeField, UniqueConstraint, UUIDField,
17 )
18 from django.db.transaction import TransactionManagementError, atomic
19 from django.test import (
20     TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
21 )
22 from django.test.utils import CaptureQueriesContext, isolate_apps
23 from django.utils import timezone
24
25 from .fields import (
26     CustomManyToManyField, InheritedManyToManyField, MediumBlobField,
27 )
28 from .models import (
29     Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex,
30     AuthorWithDefaultHeight, AuthorWithEvenLongerName, AuthorWithIndexedName,
31     AuthorWithIndexedNameAndBirthday, AuthorWithUniqueName,
32     AuthorWithUniqueNameAndBirthday, Book, BookForeignObj, BookWeak,
33     BookWithLongName, BookWithO2O, BookWithoutAuthor, BookWithSlug, IntegerPK,
34     Node, Note, NoteRename, Tag, TagIndexed, TagM2MTest, TagUniqueRename,
35     Thing, UniqueTest, new_apps,
36 )
37
38
39 class SchemaTests(TransactionTestCase):
40     """
41     Tests for the schema-alteration code.
42
43     Be aware that these tests are more liable than most to false results,
44     as sometimes the code to check if a test has worked is almost as complex
45     as the code it is testing.
46     """
47
48     available_apps = []
49
50     models = [
51         Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex,
52         AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, BookWeak,
53         BookWithLongName, BookWithO2O, BookWithSlug, IntegerPK, Node, Note,
54         Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest,
55     ]

test_alter_field_with_custom_db_type

 1 @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
 2 def test_alter_field_with_custom_db_type(self):
 3     from django.contrib.postgres.fields import ArrayField
 4
 5     class Foo(Model):
 6         field = ArrayField(CharField(max_length=255))
 7
 8         class Meta:
 9             app_label = 'schema'
10
11     with connection.schema_editor() as editor:
12         editor.create_model(Foo)
13     self.isolated_local_models = [Foo]
14     old_field = Foo._meta.get_field('field')
15     new_field = ArrayField(CharField(max_length=16))
16     new_field.set_attributes_from_name('field')
17     new_field.model = Foo
18     with connection.schema_editor() as editor:
19         editor.alter_field(Foo, old_field, new_field, strict=True)

test_alter_array_field_decrease_base_field_length

 1 @isolate_apps('schema')
 2 @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
 3 def test_alter_array_field_decrease_base_field_length(self):
 4     from django.contrib.postgres.fields import ArrayField
 5
 6     class ArrayModel(Model):
 7         field = ArrayField(CharField(max_length=16))
 8
 9         class Meta:
10             app_label = 'schema'
11
12     with connection.schema_editor() as editor:
13         editor.create_model(ArrayModel)
14     self.isolated_local_models = [ArrayModel]
15     ArrayModel.objects.create(field=['x' * 16])
16     old_field = ArrayModel._meta.get_field('field')
17     new_field = ArrayField(CharField(max_length=15))
18     new_field.set_attributes_from_name('field')
19     new_field.model = ArrayModel
20     with connection.schema_editor() as editor:
21         msg = 'value too long for type character varying(15)'
22         with self.assertRaisesMessage(DataError, msg):
23             editor.alter_field(ArrayModel, old_field, new_field, strict=True)

test_alter_array_field_decrease_nested_base_field_length

 1 @isolate_apps('schema')
 2 @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
 3 def test_alter_array_field_decrease_nested_base_field_length(self):
 4     from django.contrib.postgres.fields import ArrayField
 5
 6     class ArrayModel(Model):
 7         field = ArrayField(ArrayField(CharField(max_length=16)))
 8
 9         class Meta:
10             app_label = 'schema'
11
12     with connection.schema_editor() as editor:
13         editor.create_model(ArrayModel)
14     self.isolated_local_models = [ArrayModel]
15     ArrayModel.objects.create(field=[['x' * 16]])
16     old_field = ArrayModel._meta.get_field('field')
17     new_field = ArrayField(ArrayField(CharField(max_length=15)))
18     new_field.set_attributes_from_name('field')
19     new_field.model = ArrayModel
20     with connection.schema_editor() as editor:
21         msg = 'value too long for type character varying(15)'
22         with self.assertRaisesMessage(DataError, msg):
23             editor.alter_field(ArrayModel, old_field, new_field, strict=True)