Skip to content

Commit c6fac7f

Browse files
authored
fix: TT-261 handle response Not found (#299)
* fix: TT-261 handle response Not found and added unit testing of methond add_complementary_info * fix: TT-261 refactor code based on PR reviews * fix: TT-261 refactor code based on PR reviews * fix: TT-261 added a second test about the methond add_complementary_info * fix: TT-261 corection about of the code smell
1 parent 9f0f50d commit c6fac7f

File tree

2 files changed

+105
-1
lines changed

2 files changed

+105
-1
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import pytest
2+
3+
from utils.azure_users import AzureConnection, AzureUser
4+
from time_tracker_api.time_entries.time_entries_repository import (
5+
TimeEntryCosmosDBModel,
6+
TimeEntryCosmosDBRepository,
7+
)
8+
from time_tracker_api.projects.projects_model import (
9+
ProjectCosmosDBDao,
10+
ProjectCosmosDBModel,
11+
)
12+
from time_tracker_api.activities.activities_model import (
13+
ActivityCosmosDBDao,
14+
ActivityCosmosDBModel,
15+
)
16+
from flask_restplus._http import HTTPStatus
17+
from werkzeug.exceptions import HTTPException
18+
19+
time_entry_data = {
20+
'id': 'id',
21+
'start_date': '2021-03-22T10:00:00.000Z',
22+
'end_date': "2021-03-22T11:00:00.000Z",
23+
'description': 'do some testing',
24+
'tenant_id': 'tenant_id',
25+
'project_id': 'project_id1',
26+
'activity_id': 'activity_id1',
27+
'technologies': ['python', 'pytest'],
28+
'owner_id': 'id',
29+
}
30+
31+
project_data = {
32+
'customer_id': 'dsakldh12ASD',
33+
'id': 'project_id1',
34+
'name': 'project_name',
35+
'description': 'do some testing',
36+
'project_type_id': "id2",
37+
'tenant_id': 'tenantid1',
38+
}
39+
40+
activity_data = {
41+
'id': 'activity_id1',
42+
'name': 'activity',
43+
'description': 'testing',
44+
"tenant_id": 'nomatter',
45+
}
46+
47+
48+
def test_add_complementary_info_when_there_are_time_entries(
49+
mocker,
50+
time_entry_repository: TimeEntryCosmosDBRepository,
51+
):
52+
projects_db_get_all_mock = mocker.patch.object(
53+
ProjectCosmosDBDao, 'get_all'
54+
)
55+
activities_db_get_all_mock = mocker.patch.object(
56+
ActivityCosmosDBDao, 'get_all'
57+
)
58+
users_mock = mocker.patch.object(AzureConnection, 'users')
59+
60+
expected_project = ProjectCosmosDBModel(project_data)
61+
expected_activity = ActivityCosmosDBModel(activity_data)
62+
expected_time_entry_in = TimeEntryCosmosDBModel(time_entry_data)
63+
expected_user = AzureUser('email1', [], 'id', 'name', ['admin'])
64+
setattr(expected_project, 'customer_name', 'customer_name')
65+
66+
projects_db_get_all_mock.return_value = [expected_project]
67+
activities_db_get_all_mock.return_value = [expected_activity]
68+
users_mock.return_value = [expected_user]
69+
70+
expected_time_entry_out = time_entry_repository.add_complementary_info(
71+
[expected_time_entry_in], max_count=None, exist_conditions=True
72+
)
73+
74+
assert isinstance(expected_time_entry_out[0], TimeEntryCosmosDBModel)
75+
assert (
76+
expected_time_entry_out[0].__dict__['project_name']
77+
== expected_project.__dict__['name']
78+
)
79+
assert (
80+
expected_time_entry_out[0].__dict__['customer_id']
81+
== expected_project.__dict__['customer_id']
82+
)
83+
assert (
84+
expected_time_entry_out[0].__dict__['customer_name']
85+
== expected_project.__dict__['customer_name']
86+
)
87+
assert (
88+
expected_time_entry_out[0].__dict__['activity_name']
89+
== expected_activity.__dict__['name']
90+
)
91+
92+
93+
def test_add_complementary_info_when_there_are_not_time_entries(
94+
time_entry_repository: TimeEntryCosmosDBRepository,
95+
):
96+
with pytest.raises(HTTPException) as http_error:
97+
time_entry_repository.add_complementary_info(
98+
time_entries=None, exist_conditions=False
99+
)
100+
status_code = http_error.value.code
101+
message = http_error.value.data.get('message')
102+
103+
assert message == 'Time entry not found'
104+
assert status_code == HTTPStatus.NOT_FOUND

time_tracker_api/time_entries/time_entries_repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def add_complementary_info(
119119

120120
users = AzureConnection().users()
121121
add_user_email_to_time_entries(time_entries, users)
122-
elif not time_entries and exist_conditions:
122+
elif not time_entries and not exist_conditions:
123123
abort(HTTPStatus.NOT_FOUND, "Time entry not found")
124124
return time_entries
125125

0 commit comments

Comments
 (0)