Skip to content

Commit ebc309e

Browse files
authored
Merge pull request #141 from ioet/fix/recover-partial-update-broken-function#140
fix(time-entries): 🚑 move logic of project names to time-entries DAO
2 parents 007e0db + bc12d5d commit ebc309e

File tree

2 files changed

+22
-42
lines changed

2 files changed

+22
-42
lines changed

tests/time_tracker_api/time_entries/time_entries_namespace_test.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ def test_list_all_time_entries(
146146
time_entries_dao,
147147
)
148148

149-
repository_find_all_mock = mocker.patch.object(
150-
time_entries_dao.repository, 'find_all', return_value=[]
149+
dao_get_all_mock = mocker.patch.object(
150+
time_entries_dao, 'get_all', return_value=[]
151151
)
152152

153153
response = client.get(
@@ -156,7 +156,7 @@ def test_list_all_time_entries(
156156

157157
assert HTTPStatus.OK == response.status_code
158158
assert [] == json.loads(response.data)
159-
repository_find_all_mock.assert_called_once()
159+
dao_get_all_mock.assert_called_once()
160160

161161

162162
def test_get_time_entry_should_succeed_with_valid_id(
@@ -166,8 +166,8 @@ def test_get_time_entry_should_succeed_with_valid_id(
166166
time_entries_dao,
167167
)
168168

169-
repository_find_mock = mocker.patch.object(
170-
time_entries_dao.repository, 'find', return_value=fake_time_entry
169+
dao_get_mock = mocker.patch.object(
170+
time_entries_dao, 'get', return_value={}
171171
)
172172

173173
valid_id = fake.random_int(1, 9999)
@@ -179,9 +179,7 @@ def test_get_time_entry_should_succeed_with_valid_id(
179179

180180
assert HTTPStatus.OK == response.status_code
181181
fake_time_entry == json.loads(response.data)
182-
repository_find_mock.assert_called_once_with(
183-
str(valid_id), ANY, peeker=ANY
184-
)
182+
dao_get_mock.assert_called_once_with(str(valid_id))
185183

186184

187185
def test_get_time_entry_should_response_with_unprocessable_entity_for_invalid_id_format(
@@ -614,20 +612,15 @@ def test_find_all_is_called_with_generated_dates(
614612
time_entries_dao,
615613
)
616614

617-
repository_find_all_mock = mocker.patch.object(
618-
time_entries_dao.repository, 'find_all', return_value=[]
615+
dao_get_all_mock = mocker.patch.object(
616+
time_entries_dao, 'get_all', return_value=[]
619617
)
620618

621619
response = client.get(url, headers=valid_header, follow_redirects=True)
622620

623-
date_range = get_date_range_of_month(year, month)
624-
conditions = {'owner_id': owner_id}
625-
626621
assert HTTPStatus.OK == response.status_code
627622
assert json.loads(response.data) is not None
628-
repository_find_all_mock.assert_called_once_with(
629-
ANY, conditions=conditions, date_range=date_range
630-
)
623+
dao_get_all_mock.assert_called_once()
631624

632625

633626
def test_summary_is_called_with_date_range_from_worked_time_module(

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,24 +110,6 @@ def create_sql_date_range_filter(date_range: dict) -> str:
110110
else:
111111
return ''
112112

113-
def find(
114-
self,
115-
id: str,
116-
event_context: EventContext,
117-
peeker: Callable = None,
118-
visible_only=True,
119-
mapper: Callable = None,
120-
):
121-
time_entry = CosmosDBRepository.find(
122-
self, id, event_context, peeker, visible_only, mapper,
123-
)
124-
125-
project_dao = projects_model.create_dao()
126-
project = project_dao.get(time_entry.project_id)
127-
setattr(time_entry, 'project_name', project.name)
128-
129-
return time_entry
130-
131113
def find_all(
132114
self,
133115
event_context: EventContext,
@@ -141,19 +123,14 @@ def find_all(
141123

142124
custom_params = self.generate_params(date_range)
143125

144-
time_entries = CosmosDBRepository.find_all(
126+
return CosmosDBRepository.find_all(
145127
self,
146128
event_context=event_context,
147129
conditions=conditions,
148130
custom_sql_conditions=custom_sql_conditions,
149131
custom_params=custom_params,
150132
)
151133

152-
project_dao = projects_model.create_dao()
153-
projects = project_dao.get_all()
154-
add_project_name_to_time_entries(time_entries, projects)
155-
return time_entries
156-
157134
def on_create(self, new_item_data: dict, event_context: EventContext):
158135
CosmosDBRepository.on_create(self, new_item_data, event_context)
159136

@@ -310,16 +287,26 @@ def get_all(self, conditions: dict = {}) -> list:
310287
conditions.update({"owner_id": event_ctx.user_id})
311288

312289
date_range = self.handle_date_filter_args(args=conditions)
313-
return self.repository.find_all(
290+
time_entries = self.repository.find_all(
314291
event_ctx, conditions=conditions, date_range=date_range
315292
)
316293

294+
project_dao = projects_model.create_dao()
295+
projects = project_dao.get_all()
296+
add_project_name_to_time_entries(time_entries, projects)
297+
return time_entries
298+
317299
def get(self, id):
318300
event_ctx = self.create_event_context("read")
319-
return self.repository.find(
301+
time_entry = self.repository.find(
320302
id, event_ctx, peeker=self.check_whether_current_user_owns_item
321303
)
322304

305+
project_dao = projects_model.create_dao()
306+
project = project_dao.get(time_entry.project_id)
307+
setattr(time_entry, 'project_name', project.name)
308+
return time_entry
309+
323310
def create(self, data: dict):
324311
event_ctx = self.create_event_context("create")
325312
data['owner_id'] = event_ctx.user_id

0 commit comments

Comments
 (0)