forked from bbmokhtari/django-translations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_models.py
266 lines (223 loc) · 8.12 KB
/
test_models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from django.test import TestCase
from django.contrib.contenttypes.models import ContentType
from django.db import utils
from translations.models import Translation
from sample.models import Timezone, Continent, City
from sample.utils import create_samples
class TranslationTest(TestCase):
"""Tests for `Translation`."""
def test_content_type_none(self):
europe = Continent.objects.create(name='Europe', code='EU')
with self.assertRaises(utils.IntegrityError) as error:
Translation.objects.create(
content_type=None,
object_id=europe.pk,
field='name',
language='de',
text='Europa',
)
self.assertEqual(
error.exception.args[0],
('NOT NULL constraint failed: translations_translation' +
'.content_type_id'),
)
def test_object_id_none(self):
continent_ct = ContentType.objects.get_for_model(Continent)
with self.assertRaises(utils.IntegrityError) as error:
Translation.objects.create(
content_type=continent_ct,
object_id=None,
field='name',
language='de',
text='Europa',
)
self.assertEqual(
error.exception.args[0],
'NOT NULL constraint failed: translations_translation.object_id',
)
def test_content_object_none(self):
with self.assertRaises(utils.IntegrityError) as error:
Translation.objects.create(
content_object=None,
field='name',
language='de',
text='Europa',
)
self.assertEqual(
error.exception.args[0],
'NOT NULL constraint failed: translations_translation.object_id',
)
def test_field_none(self):
europe = Continent.objects.create(name='Europe', code='EU')
continent_ct = ContentType.objects.get_for_model(Continent)
with self.assertRaises(utils.IntegrityError) as error:
Translation.objects.create(
content_type=continent_ct,
object_id=europe.pk,
field=None,
language='de',
text='Europa',
)
self.assertEqual(
error.exception.args[0],
'NOT NULL constraint failed: translations_translation.field',
)
def test_language_none(self):
europe = Continent.objects.create(name='Europe', code='EU')
continent_ct = ContentType.objects.get_for_model(Continent)
with self.assertRaises(utils.IntegrityError) as error:
Translation.objects.create(
content_type=continent_ct,
object_id=europe.pk,
field='name',
language=None,
text='Europa',
)
self.assertEqual(
error.exception.args[0],
'NOT NULL constraint failed: translations_translation.language',
)
def test_text_none(self):
europe = Continent.objects.create(name='Europe', code='EU')
continent_ct = ContentType.objects.get_for_model(Continent)
with self.assertRaises(utils.IntegrityError) as error:
Translation.objects.create(
content_type=continent_ct,
object_id=europe.pk,
field='name',
language='de',
text=None,
)
self.assertEqual(
error.exception.args[0],
'NOT NULL constraint failed: translations_translation.text',
)
def test_str(self):
europe = Continent.objects.create(name='Europe', code='EU')
continent_ct = ContentType.objects.get_for_model(Continent)
translation = Translation.objects.create(
content_type=continent_ct,
object_id=europe.pk,
field='name',
language='de',
text='Europa'
)
self.assertEqual(
str(translation),
'Europe: Europa'
)
def test_uniqueness(self):
europe = Continent.objects.create(name='Europe', code='EU')
continent_ct = ContentType.objects.get_for_model(Continent)
Translation.objects.create(
content_type=continent_ct,
object_id=europe.pk,
field='name',
language='de',
text='Europa'
)
with self.assertRaises(utils.IntegrityError) as error:
Translation.objects.create(
content_type=continent_ct,
object_id=europe.pk,
field='name',
language='de',
text='Europa'
)
self.assertEqual(
error.exception.args[0],
('UNIQUE constraint failed: ' +
'translations_translation.content_type_id, ' +
'translations_translation.object_id, ' +
'translations_translation.field, ' +
'translations_translation.language'),
)
class TranslatableTest(TestCase):
"""Tests for `Translatable`."""
def test_one_translations_rel(self):
create_samples(
continent_names=['europe'],
continent_fields=['name', 'denonym'],
langs=['de']
)
europe = Continent.objects.get(code='EU')
self.assertQuerysetEqual(
europe.translations.order_by('id'),
[
'<Translation: Europe: Europa>',
'<Translation: European: Europäisch>',
]
)
def test_two_translations_rel(self):
create_samples(
continent_names=['europe', 'asia'],
continent_fields=['name', 'denonym'],
langs=['de']
)
europe = Continent.objects.get(code='EU')
asia = Continent.objects.get(code='AS')
self.assertQuerysetEqual(
europe.translations.order_by('id'),
[
'<Translation: Europe: Europa>',
'<Translation: European: Europäisch>',
]
)
self.assertQuerysetEqual(
asia.translations.order_by('id'),
[
'<Translation: Asia: Asien>',
'<Translation: Asian: Asiatisch>',
]
)
def test_get_translatable_fields_automatic(self):
self.assertListEqual(
City.get_translatable_fields(),
[
City._meta.get_field('name'),
City._meta.get_field('denonym'),
]
)
def test_get_translatable_fields_empty(self):
self.assertListEqual(
Timezone.get_translatable_fields(),
[]
)
def test_get_translatable_fields_explicit(self):
self.assertListEqual(
Continent.get_translatable_fields(),
[
Continent._meta.get_field('name'),
Continent._meta.get_field('denonym'),
]
)
def test_get_translatable_fields_names_automatic(self):
self.assertListEqual(
City._get_translatable_fields_names(),
['name', 'denonym']
)
def test_get_translatable_fields_names_empty(self):
self.assertListEqual(
Timezone._get_translatable_fields_names(),
[]
)
def test_get_translatable_fields_names_explicit(self):
self.assertListEqual(
Continent._get_translatable_fields_names(),
['name', 'denonym']
)
def test_get_translatable_fields_choices_automatic(self):
self.assertListEqual(
City._get_translatable_fields_choices(),
[(None, '---------'), ('name', 'Name'), ('denonym', 'Denonym')]
)
def test_get_translatable_fields_choices_empty(self):
self.assertListEqual(
Timezone._get_translatable_fields_choices(),
[(None, '---------')]
)
def test_get_translatable_fields_choices_explicit(self):
self.assertListEqual(
Continent._get_translatable_fields_choices(),
[(None, '---------'), ('name', 'Name'), ('denonym', 'Denonym')]
)