test_semester_dates_validate.py 943 Bytes
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
3
4
5
6
7
8
9
10
11
12
13
from django.test import TestCase
from backend.models.university import UniversitySemestersDatesSerializer
import pytest
from rest_framework.validators import ValidationError

semesters = ['spring_begin', 'spring_end', 'autumn_begin', 'autumn_end']


class SemesterDatesTestCase(TestCase):

    def test_validation(self):

        def build(l):
14
15
16
17
            tmp = {sem: val for sem, val in zip(semesters, l)}
            tmp['comment'] = ""
            tmp['usefull_links'] = []
            return tmp
Florent Chehab's avatar
Florent Chehab committed
18
19
20
21
22
23
24
25
26
27
28

        def _test_attrs_error(attrs):
            with pytest.raises(ValidationError):
                self.ser.my_validate(attrs)

        self.ser = UniversitySemestersDatesSerializer()
        _test_attrs_error(build([None] * 4))
        _test_attrs_error(build([None, 3, None, None]))
        _test_attrs_error(build([2, 3, 3, 2]))
        self.ser.my_validate(build([2, 3, None, None]))
        self.ser.my_validate(build([2, 3, 2, 3]))