test_representatives_list.py 5.37 KB
Newer Older
1
# -*- coding: utf-8 -*-
2

Jamesie Pic's avatar
Jamesie Pic committed
3
4
from django.test import TestCase

5
6
from responsediff.response import Response

7

Jamesie Pic's avatar
Jamesie Pic committed
8
from .base import UrlGetTestMixin
9
10
from representatives.models import Representative
from ..views.representative_mixin import RepresentativeViewMixin
Jamesie Pic's avatar
Jamesie Pic committed
11
12
13
14
15
16


class RepresentativeListTest(UrlGetTestMixin, TestCase):
    fixtures = ['smaller_sample.json']
    url = '/legislature/representative/'

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    def test_prefetch_profile(self):
        test = RepresentativeViewMixin()
        reps = test.prefetch_for_representative_country_and_main_mandate(
            Representative.objects.order_by('pk'))

        with self.assertNumQueries(2):
            # Cast to list to avoid [index] to cast a select with an offset
            # below !
            reps = [test.add_representative_country_and_main_mandate(r)
                    for r in reps]

            assert reps[0].country.code == 'GB'
            assert reps[0].main_mandate.pk == 3318

            assert reps[1].country.code == 'FI'
            assert reps[1].main_mandate.pk == 5545

34
35
    def functional_test(self, page, paginate_by, active_only, display,
                        search=''):
Jamesie Pic's avatar
Jamesie Pic committed
36
37
38
        url = '%s?page=%s&search=%s' % (self.url, page, search)

        # Cancel out one-time queries (session)
39
40
        self.client.get('%s&paginate_by=%s&display=%s&active_only=%s' %
            (url, paginate_by, display, active_only))
Jamesie Pic's avatar
Jamesie Pic committed
41

Nicolas Joyard's avatar
Nicolas Joyard committed
42
        with self.assertNumQueries(5):
Jamesie Pic's avatar
Jamesie Pic committed
43
44
            """
            - A count for pagination
Nicolas Joyard's avatar
Nicolas Joyard committed
45
46
            - One query for chambers (filters)
            - One query for countries (filters)
Jamesie Pic's avatar
Jamesie Pic committed
47
            - One query for representative + score
Jamesie Pic's avatar
Jamesie Pic committed
48
            - One query for mandates (including country + main_mandate)
Jamesie Pic's avatar
Jamesie Pic committed
49
            """
Jamesie Pic's avatar
Jamesie Pic committed
50
51
            self.response = self.client.get(url)

52
53
        expected = Response.for_test(self)
        expected.assertNoDiff(self.response)
Jamesie Pic's avatar
Jamesie Pic committed
54

55
56
    def filter_test(self, num_queries, search='', country='', chamber='',
                    group=''):
57
58
59
60
61
62
63
64
65
66
67
68
        url = '%s?search=%s&country=%s&chamber=%s&group=%s' % (self.url,
            search, country, chamber, group)

        # Cancel out one-time queries (session)
        self.client.get('%s&paginate_by=12&active_only=1' % url)

        with self.assertNumQueries(num_queries):
            self.response = self.client.get(url)

        expected = Response.for_test(self)
        expected.assertNoDiff(self.response)

Nicolas Joyard's avatar
Nicolas Joyard committed
69
70
71
72
73
74
75
76
77
78
79
80
    def sorting_test(self, num_queries, field='', dir=''):
        url = '%s?sort_by=%s&sort_dir=%s' % (self.url, field, dir)

        # Cancel out one-time queries (session)
        self.client.get('%s&paginate_by=12&active_only=1' % url)

        with self.assertNumQueries(num_queries):
            self.response = self.client.get(self.url)

        expected = Response.for_test(self)
        expected.assertNoDiff(self.response)

81
82
    def test_page1_paginateby12_active_displaylist(self):
        self.functional_test(1, 12, 1, 'list')
Jamesie Pic's avatar
Jamesie Pic committed
83

84
85
86
87
88
89
90
91
    def test_page1_paginateby12_all_displaylist(self):
        self.functional_test(1, 12, 0, 'list')

    def test_page1_paginateby24_active_displaygrid(self):
        self.functional_test(1, 24, 1, 'grid')

    def test_page1_paginateby24_all_displaygrid(self):
        self.functional_test(1, 24, 0, 'grid')
Jamesie Pic's avatar
Jamesie Pic committed
92
93

    def test_page2_paginateby24_displaylist(self):
94
        self.functional_test(2, 24, 1, 'list')
Jamesie Pic's avatar
Jamesie Pic committed
95
96

    def test_page1_paginateby12_displaylist_searchjoly(self):
97
        self.functional_test(1, 12, 1, 'list', 'joly')
Jamesie Pic's avatar
Jamesie Pic committed
98
99

    def test_page2_paginateby12_displaylist(self):
100
        self.functional_test(2, 12, 1, 'list')
101
102

    def test_filter_search(self):
103
104
105
106
107
108
109
110
        """
        - A count for pagination
        - One query for representative + score
        - One query for mandates (including country + main_mandate)
        - One query for chambers (filters)
        - One query for countries (filters)
        """
        self.filter_test(5, 'am')
111
112

    def test_filter_country(self):
113
114
115
116
117
        """
        5 same queries as test_filter_search plus:
        - One query for the country
        """
        self.filter_test(6, '', 110)
118
119

    def test_filter_chamber(self):
120
121
122
123
124
        """
        5 same queries as test_filter_search plus:
        - One query for the chamber
        """
        self.filter_test(6, '', '', 1)
125
126

    def test_filter_group(self):
127
128
129
130
131
132
        """
        5 same queries as test_filter_search plus:
        - One query for the group
        - One query to display the group name (DAL select2 widget)
        """
        self.filter_test(7, '', '', '', 17)
133
134

    def test_filter_multiple(self):
135
136
137
138
139
140
141
142
        """
        5 same queries as test_filter_search plus:
        - One query for the country
        - One query for the chamber
        - One query for the group
        - One query to display the group name (DAL select2 widget)
        """
        self.filter_test(9, 'e', 110, 1, 17)
143
144

    def test_filter_notfound(self):
145
146
147
148
149
150
151
        """
        Same queries as test_filter_search minus those two :
        - One query for representative + score
        - One query for mandates (including country + main_mandate)
        (as the first count query returns 0)
        """
        self.filter_test(3, 'non-existing-rep-name')
Nicolas Joyard's avatar
Nicolas Joyard committed
152
153
154
155
156
157
158
159
160
161

    def test_sorting(self):
        """
        - A count for pagination
        - One query for chambers (filters)
        - One query for countries (filters)
        - One query for representative + score
        - One query for mandates (including country + main_mandate)
        """
        self.sorting_test(5, 'score', 'desc')