Completed
Pull Request — master (#118)
by
unknown
01:07
created

test_iteration_with_non_qs_methods()   A

Complexity

Conditions 1

Size

Total Lines 10

Duplication

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