test_position_form.py 2.52 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1
2
3
import copy
import datetime

Nicolas Joyard's avatar
Nicolas Joyard committed
4
5
from .base import BaseTest, RepresentativeBaseTest, ThemeBaseTest

Nicolas Joyard's avatar
Nicolas Joyard committed
6
7
from representatives_positions.models import Position

Nicolas Joyard's avatar
Nicolas Joyard committed
8
9
10

class PositionFormTest(BaseTest):
    url = '/'
Nicolas Joyard's avatar
Nicolas Joyard committed
11
12
    create_url = RepresentativeBaseTest.base_url % 'none'
    position_fixture = {
13
        'position-representatives': 1,
Nicolas Joyard's avatar
Nicolas Joyard committed
14
15
        'position-datetime': '2016-09-01',
        'position-link': 'http://example.com/test',
16
17
18
        'position-kind': 'other',
        'position-score': '0',
        'position-title': 'position test title',
Nicolas Joyard's avatar
Nicolas Joyard committed
19
20
21
        'position-text': 'position test text',
        'position-themes': '1'
    }
Nicolas Joyard's avatar
Nicolas Joyard committed
22
23
24
25
26
27

    def test_select_theme(self):
        self.selector_test(
            '#add-position-form #id_position-themes input[checked]',
            ThemeBaseTest.base_url % 'none'
        )
Nicolas Joyard's avatar
Nicolas Joyard committed
28
29
30
31
32
33
34
35

    def test_create_position(self):
        response = self.client.post(self.create_url, self.position_fixture)

        self.assertResponseDiffEmpty(response, '#add-position-success')
        position = Position.objects.get(text='position test text')

        assert position.datetime == datetime.date(2016, 9, 1)
36
37
        assert position.representatives.all()[0].pk == \
            self.position_fixture['position-representatives']
Nicolas Joyard's avatar
Nicolas Joyard committed
38
39
40
41
42
43
        assert position.link == self.position_fixture['position-link']
        assert ''.join(['%s' % t.pk for t in position.themes.all()]) == '1'
        assert position.published is False

    def test_create_position_without_representative(self):
        fixture = copy.copy(self.position_fixture)
44
        fixture.pop('position-representatives')
Nicolas Joyard's avatar
Nicolas Joyard committed
45
46

        response = self.client.post(self.create_url, fixture)
47
48
        self.assertResponseDiffEmpty(response,
            '#add-position-form .has-error .form-control')
Nicolas Joyard's avatar
Nicolas Joyard committed
49
50
51
52
53
54
55
        assert response.context['position_form'].is_valid() is False

    def test_create_position_without_datetime(self):
        fixture = copy.copy(self.position_fixture)
        fixture.pop('position-datetime')

        response = self.client.post(self.create_url, fixture)
56
57
        self.assertResponseDiffEmpty(response,
            '#add-position-form .has-error .form-control')
Nicolas Joyard's avatar
Nicolas Joyard committed
58
59
60
61
62
63
64
        assert response.context['position_form'].is_valid() is False

    def test_create_position_without_link(self):
        fixture = copy.copy(self.position_fixture)
        fixture.pop('position-link')

        response = self.client.post(self.create_url, fixture)
65
66
        self.assertResponseDiffEmpty(response,
            '#add-position-form .has-error .form-control')
Nicolas Joyard's avatar
Nicolas Joyard committed
67
        assert response.context['position_form'].is_valid() is False