|
1
|
|
|
from unittest import mock |
|
2
|
|
|
|
|
3
|
|
|
from django.db import connection |
|
4
|
|
|
from django.conf import settings |
|
5
|
|
|
from django.test import TestCase |
|
6
|
|
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor |
|
7
|
|
|
|
|
8
|
|
|
from localized_fields import LocalizedField, get_language_codes |
|
9
|
|
|
|
|
10
|
|
|
from .fake_model import define_fake_model |
|
11
|
|
|
from django.apps import apps |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
class DBBackendTestCase(TestCase): |
|
15
|
|
|
"""Tests the custom database back-end.""" |
|
16
|
|
|
|
|
17
|
|
|
@staticmethod |
|
18
|
|
|
def test_hstore_extension_enabled(): |
|
19
|
|
|
"""Tests whether the `hstore` extension was |
|
20
|
|
|
enabled automatically.""" |
|
21
|
|
|
|
|
22
|
|
|
with connection.cursor() as cursor: |
|
23
|
|
|
cursor.execute(( |
|
24
|
|
|
'SELECT count(*) FROM pg_extension ' |
|
25
|
|
|
'WHERE extname = \'hstore\'' |
|
26
|
|
|
)) |
|
27
|
|
|
|
|
28
|
|
|
assert cursor.fetchone()[0] == 1 |
|
29
|
|
|
|
|
30
|
|
|
@classmethod |
|
31
|
|
|
def test_migration_create_drop_model(cls): |
|
32
|
|
|
"""Tests whether models containing a :see:LocalizedField |
|
33
|
|
|
with a `uniqueness` constraint get created properly, |
|
34
|
|
|
with the contraints in the database.""" |
|
35
|
|
|
|
|
36
|
|
|
model = define_fake_model('NewModel', { |
|
37
|
|
|
'title': LocalizedField(uniqueness=get_language_codes()) |
|
38
|
|
|
}) |
|
39
|
|
|
|
|
40
|
|
|
# create the model in db and verify the indexes are being created |
|
41
|
|
|
with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute: |
|
42
|
|
|
with connection.schema_editor() as schema_editor: |
|
43
|
|
|
schema_editor.create_model(model) |
|
44
|
|
|
|
|
45
|
|
|
create_index_calls = [ |
|
46
|
|
|
call for call in execute.mock_calls if 'CREATE UNIQUE INDEX' in str(call) |
|
47
|
|
|
] |
|
48
|
|
|
|
|
49
|
|
|
assert len(create_index_calls) == len(settings.LANGUAGES) |
|
50
|
|
|
|
|
51
|
|
|
# delete the model in the db and verify the indexes are being deleted |
|
52
|
|
|
with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute: |
|
53
|
|
|
with connection.schema_editor() as schema_editor: |
|
54
|
|
|
schema_editor.delete_model(model) |
|
55
|
|
|
|
|
56
|
|
|
drop_index_calls = [ |
|
57
|
|
|
call for call in execute.mock_calls if 'DROP INDEX' in str(call) |
|
58
|
|
|
] |
|
59
|
|
|
|
|
60
|
|
|
assert len(drop_index_calls) == len(settings.LANGUAGES) |
|
61
|
|
|
|
|
62
|
|
|
@classmethod |
|
63
|
|
|
def test_migration_alter_field(cls): |
|
64
|
|
|
"""Tests whether the back-end correctly removes and |
|
65
|
|
|
adds `uniqueness` constraints when altering a :see:LocalizedField.""" |
|
66
|
|
|
|
|
67
|
|
|
define_fake_model('ExistingModel', { |
|
68
|
|
|
'title': LocalizedField(uniqueness=get_language_codes()) |
|
69
|
|
|
}) |
|
70
|
|
|
|
|
71
|
|
|
app_config = apps.get_app_config('tests') |
|
72
|
|
|
|
|
73
|
|
|
with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute: |
|
74
|
|
|
with connection.schema_editor() as schema_editor: |
|
75
|
|
|
dynmodel = app_config.get_model('ExistingModel') |
|
76
|
|
|
schema_editor.alter_field( |
|
77
|
|
|
dynmodel, |
|
78
|
|
|
dynmodel._meta.fields[1], |
|
79
|
|
|
dynmodel._meta.fields[1] |
|
80
|
|
|
) |
|
81
|
|
|
|
|
82
|
|
|
index_calls = [ |
|
83
|
|
|
call for call in execute.mock_calls |
|
84
|
|
|
if 'INDEX' in str(call) and 'title' in str(call) |
|
85
|
|
|
] |
|
86
|
|
|
|
|
87
|
|
|
assert len(index_calls) == len(settings.LANGUAGES) * 2 |
|
88
|
|
|
|