test_position_form.py 2.72 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
13
14
15
    create_url = RepresentativeBaseTest.base_url % 'none'
    position_fixture = {
        'position-representative': 1,
        '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
28
29
30
31
32
33

    def test_select_representative(self):
        self.selector_test(
            '#add-position-form #id_position-representative option[selected]',
            RepresentativeBaseTest.base_url % 'none'
        )

    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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

    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)
        assert position.representative.pk == \
            self.position_fixture['position-representative']
        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)
        fixture.pop('position-representative')

        response = self.client.post(self.create_url, fixture)
53
54
        self.assertResponseDiffEmpty(response,
            '#add-position-form .has-error .form-control')
Nicolas Joyard's avatar
Nicolas Joyard committed
55
56
57
58
59
60
61
        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)
62
63
        self.assertResponseDiffEmpty(response,
            '#add-position-form .has-error .form-control')
Nicolas Joyard's avatar
Nicolas Joyard committed
64
65
66
67
68
69
70
        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)
71
72
        self.assertResponseDiffEmpty(response,
            '#add-position-form .has-error .form-control')
Nicolas Joyard's avatar
Nicolas Joyard committed
73
        assert response.context['position_form'].is_valid() is False