Skip to content

Commit

Permalink
Move attendances mock tests / test data to new files to keep tests re…
Browse files Browse the repository at this point in the history
…adable once more tests are added
  • Loading branch information
Philip Flohr committed Nov 30, 2020
1 parent 18bc524 commit 9de540f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 77 deletions.
48 changes: 1 addition & 47 deletions tests/mock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,50 +888,4 @@
"""
json_dict_empty_response = json.loads(json_string_empty_response)

json_string_attendance_rms = """
{
"success": true,
"data": [{
"id": 33479712,
"type": "AttendancePeriod",
"attributes": {
"employee": 2116366,
"date": "1985-03-20",
"start_time": "11:00",
"end_time": "12:30",
"break": 60,
"comment": "release day! GNU Emacs Version 13 is available as free software now *yay*",
"is_holiday": false,
"is_on_time_off": false
}
}, {
"id": 33479612,
"type": "AttendancePeriod",
"attributes": {
"employee": 2116366,
"date": "1985-03-19",
"start_time": "10:30",
"end_time": "22:00",
"break": 120,
"comment": "just a couple more parentheses...",
"is_holiday": false,
"is_on_time_off": false
}
}, {
"id": 33479602,
"type": "AttendancePeriod",
"attributes": {
"employee": 2116366,
"date": "1985-03-18",
"start_time": "10:00",
"end_time": "20:00",
"break": 90,
"comment": "working on GNU Emacs",
"is_holiday": false,
"is_on_time_off": false
}
}
]
}
"""
json_dict_attendance_rms = json.loads(json_string_attendance_rms)

31 changes: 1 addition & 30 deletions tests/test_mock_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from datetime import date, timedelta
from datetime import date
from typing import Any, Dict

import pytest
Expand Down Expand Up @@ -147,35 +147,6 @@ def test_get_absence_types():
assert source_dict == target_dict


@responses.activate
def test_get_attendance():
# mock the get absences endpoint (with different array offsets)
responses.add(
responses.GET, re.compile('https://api.personio.de/v1/company/attendances?.*offset=0.*'),
status=200, json=json_dict_attendance_rms, adding_headers={'Authorization': 'Bearer foo'})
responses.add(
responses.GET, re.compile('https://api.personio.de/v1/company/attendances?.*offset=3.*'),
status=200, json=json_dict_empty_response, adding_headers={'Authorization': 'Bearer bar'})
# configure personio & get absences for alan
personio = mock_personio()
attendances = personio.get_attendances(2116366)
# validate
assert len(attendances) == 3
selection = [a for a in attendances if "release" in a.comment.lower()]
assert len(selection) == 1
release = selection[0]
assert "free software" in release.comment
assert release.date == date(1985, 3, 20)
assert release.start_time == timedelta(seconds=11*60*60)
assert release.end_time == timedelta(seconds=12.5*60*60)
assert release.break_duration == 60
assert release.employee_id == 2116366
# validate serialization
source_dict = json_dict_attendance_rms['data'][0]
target_dict = release.to_dict()
compare_labeled_attributes(source_dict, target_dict)


def mock_personio():
# mock the authentication endpoint, or all no requests will get through
resp_json = {'success': True, 'data': {'token': 'dummy_token'}}
Expand Down
37 changes: 37 additions & 0 deletions tests/test_mock_api_attendances.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import responses
import re

from datetime import timedelta, date

from tests.mock_data import json_dict_empty_response
from tests.test_mock_api import compare_labeled_attributes, mock_personio
from tests.test_mock_api_attendances_data import *


@responses.activate
def test_get_attendance():
# mock the get absences endpoint (with different array offsets)
responses.add(
responses.GET, re.compile('https://api.personio.de/v1/company/attendances?.*offset=0.*'),
status=200, json=json_dict_attendance_rms, adding_headers={'Authorization': 'Bearer foo'})
responses.add(
responses.GET, re.compile('https://api.personio.de/v1/company/attendances?.*offset=3.*'),
status=200, json=json_dict_empty_response, adding_headers={'Authorization': 'Bearer bar'})
# configure personio & get absences for alan
personio = mock_personio()
attendances = personio.get_attendances(2116366)
# validate
assert len(attendances) == 3
selection = [a for a in attendances if "release" in a.comment.lower()]
assert len(selection) == 1
release = selection[0]
assert "free software" in release.comment
assert release.date == date(1985, 3, 20)
assert release.start_time == timedelta(seconds=11*60*60)
assert release.end_time == timedelta(seconds=12.5*60*60)
assert release.break_duration == 60
assert release.employee_id == 2116366
# validate serialization
source_dict = json_dict_attendance_rms['data'][0]
target_dict = release.to_dict()
compare_labeled_attributes(source_dict, target_dict)
49 changes: 49 additions & 0 deletions tests/test_mock_api_attendances_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json

json_string_attendance_rms = """
{
"success": true,
"data": [{
"id": 33479712,
"type": "AttendancePeriod",
"attributes": {
"employee": 2116366,
"date": "1985-03-20",
"start_time": "11:00",
"end_time": "12:30",
"break": 60,
"comment": "release day! GNU Emacs Version 13 is available as free software now *yay*",
"is_holiday": false,
"is_on_time_off": false
}
}, {
"id": 33479612,
"type": "AttendancePeriod",
"attributes": {
"employee": 2116366,
"date": "1985-03-19",
"start_time": "10:30",
"end_time": "22:00",
"break": 120,
"comment": "just a couple more parentheses...",
"is_holiday": false,
"is_on_time_off": false
}
}, {
"id": 33479602,
"type": "AttendancePeriod",
"attributes": {
"employee": 2116366,
"date": "1985-03-18",
"start_time": "10:00",
"end_time": "20:00",
"break": 90,
"comment": "working on GNU Emacs",
"is_holiday": false,
"is_on_time_off": false
}
}
]
}
"""
json_dict_attendance_rms = json.loads(json_string_attendance_rms)

0 comments on commit 9de540f

Please sign in to comment.