Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions tests/time_tracker_api/time_entries/time_entries_repository_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import pytest

from utils.azure_users import AzureConnection, AzureUser
from time_tracker_api.time_entries.time_entries_repository import (
TimeEntryCosmosDBModel,
TimeEntryCosmosDBRepository,
)
from time_tracker_api.projects.projects_model import (
ProjectCosmosDBDao,
ProjectCosmosDBModel,
)
from time_tracker_api.activities.activities_model import (
ActivityCosmosDBDao,
ActivityCosmosDBModel,
)
from flask_restplus._http import HTTPStatus
from werkzeug.exceptions import HTTPException

time_entry_data = {
'id': 'id',
'start_date': '2021-03-22T10:00:00.000Z',
'end_date': "2021-03-22T11:00:00.000Z",
'description': 'do some testing',
'tenant_id': 'tenant_id',
'project_id': 'project_id1',
'activity_id': 'activity_id1',
'technologies': ['python', 'pytest'],
'owner_id': 'id',
}

project_data = {
'customer_id': 'dsakldh12ASD',
'id': 'project_id1',
'name': 'project_name',
'description': 'do some testing',
'project_type_id': "id2",
'tenant_id': 'tenantid1',
}

activity_data = {
'id': 'activity_id1',
'name': 'activity',
'description': 'testing',
"tenant_id": 'nomatter',
}


def test_add_complementary_info_when_there_are_time_entries(
mocker,
time_entry_repository: TimeEntryCosmosDBRepository,
):
projects_db_get_all_mock = mocker.patch.object(
ProjectCosmosDBDao, 'get_all'
)
activities_db_get_all_mock = mocker.patch.object(
ActivityCosmosDBDao, 'get_all'
)
users_mock = mocker.patch.object(AzureConnection, 'users')

expected_project = ProjectCosmosDBModel(project_data)
expected_activity = ActivityCosmosDBModel(activity_data)
expected_time_entry_in = TimeEntryCosmosDBModel(time_entry_data)
expected_user = AzureUser('email1', [], 'id', 'name', ['admin'])
setattr(expected_project, 'customer_name', 'customer_name')

projects_db_get_all_mock.return_value = [expected_project]
activities_db_get_all_mock.return_value = [expected_activity]
users_mock.return_value = [expected_user]

expected_time_entry_out = time_entry_repository.add_complementary_info(
[expected_time_entry_in], max_count=None, exist_conditions=True
)

assert isinstance(expected_time_entry_out[0], TimeEntryCosmosDBModel)
assert (
expected_time_entry_out[0].__dict__['project_name']
== expected_project.__dict__['name']
)
assert (
expected_time_entry_out[0].__dict__['customer_id']
== expected_project.__dict__['customer_id']
)
assert (
expected_time_entry_out[0].__dict__['customer_name']
== expected_project.__dict__['customer_name']
)
assert (
expected_time_entry_out[0].__dict__['activity_name']
== expected_activity.__dict__['name']
)


def test_add_complementary_info_when_there_are_not_time_entries(
time_entry_repository: TimeEntryCosmosDBRepository,
):
with pytest.raises(HTTPException) as http_error:
time_entry_repository.add_complementary_info(
time_entries=None, exist_conditions=False
)
status_code = http_error.value.code
message = http_error.value.data.get('message')

assert message == 'Time entry not found'
assert status_code == HTTPStatus.NOT_FOUND
2 changes: 1 addition & 1 deletion time_tracker_api/time_entries/time_entries_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def add_complementary_info(

users = AzureConnection().users()
add_user_email_to_time_entries(time_entries, users)
elif not time_entries and exist_conditions:
elif not time_entries and not exist_conditions:
abort(HTTPStatus.NOT_FOUND, "Time entry not found")
return time_entries

Expand Down