93 lines
3.4 KiB
Python
93 lines
3.4 KiB
Python
import datetime
|
|
import json
|
|
from unittest import mock
|
|
|
|
from requests.exceptions import ConnectionError
|
|
from requests.models import Response
|
|
|
|
from chrono.agendas.models import Agenda
|
|
from chrono.utils.date import get_weekday_index
|
|
from chrono.utils.lingo import CheckType, get_agenda_check_types
|
|
|
|
|
|
def test_get_weekday_index():
|
|
for date in (
|
|
datetime.date(2021, 11, 1), # month starting a Monday
|
|
datetime.date(2021, 12, 1), # month starting a Wednesday
|
|
datetime.date(2021, 5, 1), # month starting a Sunday
|
|
):
|
|
assert get_weekday_index(date) == 1
|
|
assert get_weekday_index(date.replace(day=3)) == 1
|
|
assert get_weekday_index(date.replace(day=7)) == 1
|
|
assert get_weekday_index(date.replace(day=8)) == 2
|
|
assert get_weekday_index(date.replace(day=10)) == 2
|
|
assert get_weekday_index(date.replace(day=14)) == 2
|
|
assert get_weekday_index(date.replace(day=15)) == 3
|
|
assert get_weekday_index(date.replace(day=21)) == 3
|
|
assert get_weekday_index(date.replace(day=22)) == 4
|
|
assert get_weekday_index(date.replace(day=28)) == 4
|
|
assert get_weekday_index(date.replace(day=29)) == 5
|
|
assert get_weekday_index(date.replace(day=30)) == 5
|
|
|
|
|
|
CHECK_TYPES_DATA = [
|
|
{'id': 'bar-reason', 'kind': 'presence', 'text': 'Bar reason'},
|
|
{'id': 'foo-reason', 'kind': 'absence', 'text': 'Foo reason'},
|
|
]
|
|
|
|
|
|
class MockedRequestResponse(mock.Mock):
|
|
status_code = 200
|
|
|
|
def json(self):
|
|
return json.loads(self.content)
|
|
|
|
|
|
def test_get_agenda_check_types_no_service(settings):
|
|
agenda = Agenda(slug='foo')
|
|
|
|
settings.KNOWN_SERVICES = {}
|
|
assert get_agenda_check_types(agenda) == []
|
|
|
|
settings.KNOWN_SERVICES = {'other': []}
|
|
assert get_agenda_check_types(agenda) == []
|
|
|
|
|
|
def test_get_agenda_check_types():
|
|
agenda = Agenda(slug='foo')
|
|
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.side_effect = ConnectionError()
|
|
assert get_agenda_check_types(agenda) == []
|
|
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
mock_resp = Response()
|
|
mock_resp.status_code = 500
|
|
requests_get.return_value = mock_resp
|
|
assert get_agenda_check_types(agenda) == []
|
|
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
mock_resp = Response()
|
|
mock_resp.status_code = 404
|
|
requests_get.return_value = mock_resp
|
|
assert get_agenda_check_types(agenda) == []
|
|
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps({'foo': 'bar'}))
|
|
assert get_agenda_check_types(agenda) == []
|
|
|
|
data = {'data': []}
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
assert get_agenda_check_types(agenda) == []
|
|
assert requests_get.call_args_list[0][0] == ('api/agenda/foo/check-types/',)
|
|
assert requests_get.call_args_list[0][1]['remote_service']['url'] == 'http://lingo.example.org'
|
|
|
|
data = {'data': CHECK_TYPES_DATA}
|
|
with mock.patch('requests.Session.get') as requests_get:
|
|
requests_get.return_value = MockedRequestResponse(content=json.dumps(data))
|
|
assert get_agenda_check_types(agenda) == [
|
|
CheckType(slug='bar-reason', label='Bar reason', kind='presence'),
|
|
CheckType(slug='foo-reason', label='Foo reason', kind='absence'),
|
|
]
|