test_functional.py 2.98 KB
Newer Older
Jamesie Pic's avatar
Jamesie Pic committed
1
2
import datetime
import copy
Arnaud Fabre's avatar
Arnaud Fabre committed
3

Jamesie Pic's avatar
Jamesie Pic committed
4
from django.core.urlresolvers import reverse
Jamesie Pic's avatar
Jamesie Pic committed
5
6
from django.test import TestCase, Client

Jamesie Pic's avatar
Jamesie Pic committed
7
8
from representatives.models import Representative
from representatives_positions.models import Position
Jamesie Pic's avatar
Jamesie Pic committed
9
10
11


class PositionTest(TestCase):
Jamesie Pic's avatar
Jamesie Pic committed
12
13
    fixtures = ['one_representative']

Jamesie Pic's avatar
Jamesie Pic committed
14
15
    def setUp(self):
        self.client = Client()
Jamesie Pic's avatar
Jamesie Pic committed
16
        self.create_url = reverse('representatives_positions:position-create')
Jamesie Pic's avatar
Jamesie Pic committed
17

Jamesie Pic's avatar
Jamesie Pic committed
18
        self.mep = Representative.objects.get(pk=160)
Jamesie Pic's avatar
Jamesie Pic committed
19
20
21
22
23
24
25
26
27

        self.fixture = {
            'datetime': '2015-12-11',
            'text': '%stext' % self.id(),
            'link': 'http://example.com/%slink' % self.id(),
            'representative': self.mep.pk,
        }

    def test_create_position(self):
Jamesie Pic's avatar
Jamesie Pic committed
28
29
        response = self.client.post(self.create_url, self.fixture)
        assert response.status_code == 302
30
31
        mep_url = reverse('representative-detail', args=(self.mep.slug,))
        expected = 'http://testserver%s' % mep_url
Jamesie Pic's avatar
Jamesie Pic committed
32
33
34
35
36
        assert response['Location'] == expected

        result = Position.objects.get(text='%stext' % self.id())
        assert result.datetime == datetime.date(2015, 12, 11)
        assert result.link == self.fixture['link']
Jamesie Pic's avatar
Jamesie Pic committed
37
        assert result.representative.pk == self.mep.pk
Jamesie Pic's avatar
Jamesie Pic committed
38
39
40
        assert result.published is False

    def test_position_publishing(self):
Jamesie Pic's avatar
Jamesie Pic committed
41
42
        response = self.client.post(self.create_url, self.fixture)
        assert response.status_code == 302
Jamesie Pic's avatar
Jamesie Pic committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        position = Position.objects.get(text='%stext' % self.id())

        get_response = self.client.get(position.get_absolute_url())
        assert get_response.status_code == 404

        position.published = True
        position.save()

        get_response = self.client.get(position.get_absolute_url())
        assert get_response.status_code == 200

    def test_create_position_without_field(self):
        for key in self.fixture.keys():
            fixture = copy.copy(self.fixture)
            fixture.pop(key)

Jamesie Pic's avatar
Jamesie Pic committed
59
60
61
            response = self.client.post(self.create_url, fixture)
            assert response.context['form'].is_valid() is False, \
                'Could submit form without %s' % key
Jamesie Pic's avatar
Jamesie Pic committed
62
63

    def test_position_detail(self):
Jamesie Pic's avatar
Jamesie Pic committed
64
65
66
67
68
69
70
        position = Position.objects.create(
            published=True,
            datetime=self.fixture['datetime'],
            text=self.fixture['text'],
            link=self.fixture['link'],
            representative=self.mep
        )
Jamesie Pic's avatar
Jamesie Pic committed
71

Jamesie Pic's avatar
Jamesie Pic committed
72
73
74
        # Trigger irrelevant queries that happen only once ie. constance before
        # testing actual page queries.
        self.client.get(position.get_absolute_url())
75
        with self.assertNumQueries(3):
Jamesie Pic's avatar
Jamesie Pic committed
76
77
            # One for position and rep and score
            # One for rep mandates
78
            # One for rep chamber
Jamesie Pic's avatar
Jamesie Pic committed
79
            response = self.client.get(position.get_absolute_url())
Jamesie Pic's avatar
Jamesie Pic committed
80
81

        assert 'Dec. 11, 2015' in response.content
Jamesie Pic's avatar
Jamesie Pic committed
82
83
        assert self.fixture['link'] in response.content
        assert self.fixture['text'] in response.content
Jamesie Pic's avatar
Jamesie Pic committed
84
        assert self.mep.full_name in response.content