base.py 1.47 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
3
4
5
import re
import os.path

from django.test import Client

Nicolas Joyard's avatar
Nicolas Joyard committed
6
7
from responsediff.response import Response

Jamesie Pic's avatar
Jamesie Pic committed
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

class UrlGetTestMixin(object):
    url = None

    def setUp(self):
        self.client = Client()

        if not hasattr(type(self), 'response'):
            # Do it once and for all, note that this also caches content types
            # so the contenttype query used by taggit won't be counted in
            # test_num_queries.
            self.__class__.response = self.client.get(self.url)
        self.response = self.__class__.response

    def assertHtmlInResult(self, expected):
        compare = re.sub('[\s"\']', '', expected)
        result = re.sub('[\s"\']', '', self.response.content)
        self.assertIn(compare, result)

    def assertExpectedHtmlInResult(self):
        """
        For test_votes_display, it is:
        /positions/tests/test_representatives_detail_test_votes_display_expected.html
        """
        expected = os.path.join(
            os.path.dirname(__file__),
            type(self).__name__,
            '%s.html' % self._testMethodName
        )

        with open(expected, 'r') as f:
            self.assertHtmlInResult(f.read())
Nicolas Joyard's avatar
Nicolas Joyard committed
40
41
42
43
44
45
46
47
48
49
50
51
52


class ResponseDiffMixin(object):

    def responsediff_test(self, url, numQueries):
        # Setup session variables
        self.client.get(url)

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

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