1
|
|
|
import datetime as dt |
2
|
|
|
|
3
|
|
|
from django.core.cache import cache |
4
|
|
|
from django.utils import translation |
5
|
|
|
from django.utils.timezone import now |
6
|
|
|
from parler import appsettings |
7
|
|
|
|
8
|
|
|
from .utils import AppTestCase, override_parler_settings |
9
|
|
|
from .testapp.models import SimpleModel, DateTimeModel |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class QueryCountTests(AppTestCase): |
13
|
|
|
""" |
14
|
|
|
Test model construction |
15
|
|
|
""" |
16
|
|
|
|
17
|
|
|
@classmethod |
18
|
|
|
def setUpClass(cls): |
19
|
|
|
super(QueryCountTests, cls).setUpClass() |
20
|
|
|
|
21
|
|
|
cls.country_list = ( |
22
|
|
|
'Mexico', |
23
|
|
|
'Monaco', |
24
|
|
|
'Morocco', |
25
|
|
|
'Netherlands', |
26
|
|
|
'Norway', |
27
|
|
|
'Poland', |
28
|
|
|
'Portugal', |
29
|
|
|
'Romania', |
30
|
|
|
'Russia', |
31
|
|
|
'South Africa', |
32
|
|
|
) |
33
|
|
|
|
34
|
|
|
for country in cls.country_list: |
35
|
|
|
SimpleModel.objects.create(_current_language=cls.conf_fallback, tr_title=country) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
DateTimeModel.objects.create(_current_language=cls.conf_fallback, |
39
|
|
|
tr_title=country, datetime=now()) |
40
|
|
|
|
41
|
|
|
#def setUp(self): |
42
|
|
|
# cache.clear() |
43
|
|
|
|
44
|
|
|
def assertNumTranslatedQueries(self, num, qs, language_code=None): |
45
|
|
|
# Use default language if available. |
46
|
|
|
if language_code is None: |
47
|
|
|
language_code = self.conf_fallback |
48
|
|
|
|
49
|
|
|
# Easier to understand then a oneline lambda |
50
|
|
|
# Using str(), not unicode() to be python 3 compatible. |
51
|
|
|
def test_qs(): |
52
|
|
|
for obj in qs: |
53
|
|
|
str(obj.tr_title) |
54
|
|
|
|
55
|
|
|
# Queryset is not set to a language, the individual models |
56
|
|
|
# will default to the currently active project language. |
57
|
|
|
with translation.override(language_code): |
58
|
|
|
self.assertNumQueries(num, test_qs) |
59
|
|
|
|
60
|
|
|
def test_uncached_queries(self): |
61
|
|
|
""" |
62
|
|
|
Test that uncached queries work, albeit slowly. |
63
|
|
|
""" |
64
|
|
|
with override_parler_settings(PARLER_ENABLE_CACHING=False): |
65
|
|
|
self.assertNumTranslatedQueries(1 + len(self.country_list), SimpleModel.objects.all()) |
66
|
|
|
|
67
|
|
|
def test_iteration_with_non_qs_methods(self): |
68
|
|
|
""" |
69
|
|
|
Test QuerySet methods that do not return QuerySets of models. |
70
|
|
|
""" |
71
|
|
|
# We have at least one object created in setUpClass. |
72
|
|
|
obj = DateTimeModel.objects.all()[0] |
73
|
|
|
self.assertEqual( |
74
|
|
|
obj, |
75
|
|
|
DateTimeModel.objects.language(self.conf_fallback).all()[0]) |
76
|
|
|
# Test iteration through QuerySet of non-model objects. |
77
|
|
|
self.assertIsInstance( |
78
|
|
|
DateTimeModel.objects.language(self.conf_fallback).dates( |
79
|
|
|
'datetime', 'day')[0], |
80
|
|
|
dt.date) |
81
|
|
|
|
82
|
|
|
def test_prefetch_queries(self): |
83
|
|
|
""" |
84
|
|
|
Test that .prefetch_related() works |
85
|
|
|
""" |
86
|
|
|
with override_parler_settings(PARLER_ENABLE_CACHING=False): |
87
|
|
|
self.assertNumTranslatedQueries(2, SimpleModel.objects.prefetch_related('translations')) |
88
|
|
|
|
89
|
|
|
def test_model_cache_queries(self): |
90
|
|
|
""" |
91
|
|
|
Test that the ``_translations_cache`` works. |
92
|
|
|
""" |
93
|
|
|
cache.clear() |
94
|
|
|
|
95
|
|
|
with override_parler_settings(PARLER_ENABLE_CACHING=False): |
96
|
|
|
qs = SimpleModel.objects.all() |
97
|
|
|
self.assertNumTranslatedQueries(1 + len(self.country_list), qs) |
98
|
|
|
self.assertNumTranslatedQueries(0, qs) # All should be cached on the QuerySet and object now. |
99
|
|
|
|
100
|
|
|
qs = SimpleModel.objects.prefetch_related('translations') |
101
|
|
|
self.assertNumTranslatedQueries(2, qs) |
102
|
|
|
self.assertNumTranslatedQueries(0, qs) # All should be cached on the QuerySet and object now. |
103
|
|
|
|