-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathtest_integration.py
414 lines (366 loc) · 14.8 KB
/
test_integration.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
from datetime import datetime
import unittest
import django
from django.core.management import call_command
from django.test import TestCase, TransactionTestCase
if django.VERSION < (4, 0):
from django.utils.translation import ugettext_lazy as _
else:
from django.utils.translation import gettext_lazy as _
from six import StringIO
from elasticsearch.exceptions import NotFoundError
from elasticsearch.dsl import Index as DSLIndex
from django_elasticsearch_dsl.test import ESTestCase, is_es_online
from tests import ES_MAJOR_VERSION
from .documents import (
ad_index,
AdDocument,
car_index,
CarDocument,
CarWithPrepareDocument,
ArticleDocument,
ArticleWithSlugAsIdDocument,
index_settings
)
from .models import Car, Manufacturer, Ad, Category, Article, COUNTRIES
@unittest.skipUnless(is_es_online(), 'Elasticsearch is offline')
class IntegrationTestCase(ESTestCase, TransactionTestCase):
def setUp(self):
super(IntegrationTestCase, self).setUp()
self.manufacturer = Manufacturer(
name="Peugeot", created=datetime(1900, 10, 9, 0, 0),
country_code="FR", logo='logo.jpg'
)
self.manufacturer.save()
self.car1 = Car(
name="508", launched=datetime(2010, 9, 9, 0, 0),
manufacturer=self.manufacturer
)
self.car1.save()
self.car2 = Car(
name="208", launched=datetime(2010, 10, 9, 0, 0),
manufacturer=self.manufacturer
)
self.car2.save()
self.category1 = Category(
title="Category 1", slug="category-1", icon="icon.jpeg"
)
self.category1.save()
self.car2.categories.add(self.category1)
self.car2.save()
self.car3 = Car(name="308", launched=datetime(2010, 11, 9, 0, 0))
self.car3.save()
self.category2 = Category(title="Category 2", slug="category-2")
self.category2.save()
self.car3.categories.add(self.category1, self.category2)
self.car3.save()
self.ad1 = Ad(
title=_("Ad number 1"), url="www.ad1.com",
description="My super ad description 1",
car=self.car1
)
self.ad1.save()
self.ad2 = Ad(
title="Ad number 2", url="www.ad2.com",
description="My super ad descriptio 2",
car=self.car1
)
self.ad2.save()
self.car1.save()
def test_get_doc_with_relationships(self):
s = CarDocument.search().query("match", name=self.car2.name)
result = s.execute()
self.assertEqual(len(result), 1)
car2_doc = result[0]
self.assertEqual(car2_doc.ads, [])
self.assertEqual(car2_doc.name, self.car2.name)
self.assertEqual(int(car2_doc.meta.id), self.car2.pk)
self.assertEqual(car2_doc.launched, self.car2.launched)
self.assertEqual(car2_doc.manufacturer.name,
self.car2.manufacturer.name)
self.assertEqual(car2_doc.manufacturer.country,
COUNTRIES[self.manufacturer.country_code])
s = CarDocument.search().query("match", name=self.car3.name)
result = s.execute()
car3_doc = result[0]
self.assertEqual(car3_doc.manufacturer, {})
self.assertEqual(car3_doc.name, self.car3.name)
self.assertEqual(int(car3_doc.meta.id), self.car3.pk)
def test_get_doc_with_reverse_relationships(self):
s = CarDocument.search().query("match", name=self.car1.name)
result = s.execute()
self.assertEqual(len(result), 1)
car1_doc = result[0]
self.assertEqual(car1_doc.ads, [
{
'title': self.ad1.title,
'description': self.ad1.description,
'pk': self.ad1.pk,
},
{
'title': self.ad2.title,
'description': self.ad2.description,
'pk': self.ad2.pk,
},
])
self.assertEqual(car1_doc.name, self.car1.name)
self.assertEqual(int(car1_doc.meta.id), self.car1.pk)
def test_get_doc_with_many_to_many_relationships(self):
s = CarDocument.search().query("match", name=self.car3.name)
result = s.execute()
self.assertEqual(len(result), 1)
car1_doc = result[0]
self.assertEqual(car1_doc.categories, [
{
'title': self.category1.title,
'slug': self.category1.slug,
'icon': self.category1.icon.url,
},
{
'title': self.category2.title,
'slug': self.category2.slug,
'icon': '',
}
])
def test_doc_to_dict(self):
s = CarDocument.search().query("match", name=self.car2.name)
result = s.execute()
self.assertEqual(len(result), 1)
car2_doc = result[0]
self.assertEqual(car2_doc.to_dict(), {
'type': self.car2.type,
'launched': self.car2.launched,
'name': self.car2.name,
'manufacturer': {
'name': self.manufacturer.name,
'country': COUNTRIES[self.manufacturer.country_code],
},
'categories': [{
'title': self.category1.title,
'slug': self.category1.slug,
'icon': self.category1.icon.url,
}]
})
s = CarDocument.search().query("match", name=self.car3.name)
result = s.execute()
self.assertEqual(len(result), 1)
car3_doc = result[0]
self.assertEqual(car3_doc.to_dict(), {
'type': self.car3.type,
'launched': self.car3.launched,
'name': self.car3.name,
'categories': [
{
'title': self.category1.title,
'slug': self.category1.slug,
'icon': self.category1.icon.url,
},
{
'title': self.category2.title,
'slug': self.category2.slug,
'icon': '',
}
]
})
def test_index_to_dict(self):
self.maxDiff = None
index_dict = car_index.to_dict()
text_type = 'string' if ES_MAJOR_VERSION == 2 else 'text'
test_index = DSLIndex('test_index').settings(**index_settings)
test_index.document(CarDocument)
index_dict = test_index.to_dict()
self.assertEqual(index_dict['settings'], {
'number_of_shards': 1,
'number_of_replicas': 0,
'analysis': {
'analyzer': {
'html_strip': {
'tokenizer': 'standard',
'filter': ['lowercase',
'stop', 'snowball'],
'type': 'custom',
'char_filter': ['html_strip']
}
}
}
})
self.assertEqual(index_dict['mappings'], {
'properties': {
'ads': {
'type': 'nested',
'properties': {
'description': {
'type': text_type, 'analyzer':
'html_strip'
},
'pk': {'type': 'integer'},
'title': {'type': text_type}
},
},
'categories': {
'type': 'nested',
'properties': {
'title': {'type': text_type},
'slug': {'type': text_type},
'icon': {'type': text_type}
},
},
'manufacturer': {
'type': 'object',
'properties': {
'country': {'type': text_type},
'name': {'type': text_type}
},
},
'name': {'type': text_type},
'launched': {'type': 'date'},
'type': {'type': text_type}
}
})
def test_related_docs_are_updated(self):
# test foreignkey relation
self.manufacturer.name = 'Citroen'
self.manufacturer.save()
s = CarDocument.search().query("match", name=self.car2.name)
car2_doc = s.execute()[0]
self.assertEqual(car2_doc.manufacturer.name, 'Citroen')
self.assertEqual(len(car2_doc.ads), 0)
ad3 = Ad.objects.create(
title=_("Ad number 3"), url="www.ad3.com",
description="My super ad description 3",
car=self.car2
)
s = CarDocument.search().query("match", name=self.car2.name)
car2_doc = s.execute()[0]
self.assertEqual(len(car2_doc.ads), 1)
ad3.delete()
s = CarDocument.search().query("match", name=self.car2.name)
car2_doc = s.execute()[0]
self.assertEqual(len(car2_doc.ads), 0)
self.manufacturer.delete()
s = CarDocument.search().query("match", name=self.car2.name)
car2_doc = s.execute()[0]
self.assertEqual(car2_doc.manufacturer, {})
def test_m2m_related_docs_are_updated(self):
# test m2m add
category = Category(
title="Category", slug="category", icon="icon.jpeg"
)
category.save()
self.car2.categories.add(category)
s = CarDocument.search().query("match", name=self.car2.name)
car2_doc = s.execute()[0]
self.assertEqual(len(car2_doc.categories), 2)
# test m2m deletion
self.car2.categories.remove(category)
s = CarDocument.search().query("match", name=self.car2.name)
car2_doc = s.execute()[0]
self.assertEqual(len(car2_doc.categories), 1)
self.category1.car_set.clear()
s = CarDocument.search().query("match", name=self.car2.name)
car2_doc = s.execute()[0]
self.assertEqual(len(car2_doc.categories), 0)
def test_related_docs_with_prepare_are_updated(self):
s = CarWithPrepareDocument.search().query("match", name=self.car2.name)
self.assertEqual(s.execute()[0].manufacturer.name, 'Peugeot')
self.assertEqual(s.execute()[0].manufacturer_short.name, 'Peugeot')
self.manufacturer.name = 'Citroen'
self.manufacturer.save()
s = CarWithPrepareDocument.search().query("match", name=self.car2.name)
self.assertEqual(s.execute()[0].manufacturer.name, 'Citroen')
self.assertEqual(s.execute()[0].manufacturer_short.name, 'Citroen')
self.manufacturer.delete()
s = CarWithPrepareDocument.search().query("match", name=self.car2.name)
self.assertEqual(s.execute()[0].manufacturer, {})
def test_delete_create_populate_commands(self):
out = StringIO()
self.assertTrue(ad_index.exists())
self.assertTrue(car_index.exists())
call_command('search_index', action='delete',
force=True, stdout=out, models=['tests.ad'])
self.assertFalse(ad_index.exists())
self.assertTrue(car_index.exists())
call_command('search_index', action='create',
models=['tests.ad'], stdout=out)
self.assertTrue(ad_index.exists())
call_command('search_index', action='populate',
models=['tests.ad'], stdout=out)
result = AdDocument().search().execute()
self.assertEqual(len(result), 2)
def test_rebuild_command(self):
out = StringIO()
result = AdDocument().search().execute()
self.assertEqual(len(result), 2)
Ad(title="Ad title 3").save()
call_command('search_index', action='populate',
force=True, stdout=out, models=['tests.ad'])
result = AdDocument().search().execute()
self.assertEqual(len(result), 3)
def test_filter_queryset(self):
Ad(title="Nothing that match", car=self.car1).save()
qs = AdDocument().search().query(
'match', title="Ad number 2").filter_queryset(Ad.objects)
self.assertEqual(qs.count(), 2)
self.assertEqual(list(qs), [self.ad2, self.ad1])
qs = AdDocument().search().query(
'match', title="Ad number 2"
).filter_queryset(Ad.objects.filter(url="www.ad2.com"))
self.assertEqual(qs.count(), 1)
self.assertEqual(list(qs), [self.ad2])
with self.assertRaisesMessage(TypeError, 'Unexpected queryset model'):
AdDocument().search().query(
'match', title="Ad number 2").filter_queryset(Category.objects)
def test_to_queryset(self):
Ad(title="Nothing that match", car=self.car1).save()
qs = AdDocument().search().query(
'match', title="Ad number 2").to_queryset()
self.assertEqual(qs.count(), 2)
self.assertEqual(list(qs), [self.ad2, self.ad1])
def test_queryset_iterator_queries(self):
ad3 = Ad(title="Ad 3", car=self.car1)
ad3.save()
with self.assertNumQueries(1):
AdDocument().update(Ad.objects.all())
doc = AdDocument()
with self.assertNumQueries(1):
doc.update(Ad.objects.all().order_by('-id'))
self.assertEqual(
set(int(instance.meta.id) for instance in
doc.search().query('match', title="Ad")),
set([ad3.pk, self.ad1.pk, self.ad2.pk])
)
def test_default_document_id(self):
obj_id = 12458
article_slug = "some-article"
article = Article(
id=obj_id,
slug=article_slug,
)
# saving should create two documents (in the two indices): one with the
# Django object's id as the ES doc _id, and the other with the slug
# as the ES _id
article.save()
# assert that the document's id is the id of the Django object
try:
es_obj = ArticleDocument.get(id=obj_id)
except NotFoundError:
self.fail("document with _id {} not found").format(obj_id)
self.assertEqual(es_obj.slug, article.slug)
def test_custom_document_id(self):
article_slug = "my-very-first-article"
article = Article(
slug=article_slug,
)
# saving should create two documents (in the two indices): one with the
# Django object's id as the ES doc _id, and the other with the slug
# as the ES _id
article.save()
# assert that the document's id is its the slug
try:
es_obj = ArticleWithSlugAsIdDocument.get(id=article_slug)
except NotFoundError:
self.fail(
"document with _id '{}' not found: "
"using a custom id is broken".format(article_slug)
)
self.assertEqual(es_obj.slug, article.slug)