Skip to content

Commit ff4a4d6

Browse files
committed
fix: update test of filter per month and year
1 parent f001ece commit ff4a4d6

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

commons/data_access_layer/cosmos_db.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import uuid
44
from datetime import datetime
5-
from typing import Callable, List, Tuple
5+
from typing import Callable, List, Dict
66

77
import azure.cosmos.cosmos_client as cosmos_client
88
import azure.cosmos.exceptions as exceptions
@@ -326,7 +326,7 @@ def get_current_month() -> int:
326326
def get_date_range_of_month(
327327
year: int,
328328
month: int
329-
) -> Tuple[datetime, datetime]:
329+
) -> Dict[str, str]:
330330
first_day_of_month = 1
331331
start_date = datetime(year=year, month=month, day=first_day_of_month)
332332

@@ -340,4 +340,8 @@ def get_date_range_of_month(
340340
second=59,
341341
microsecond=999999
342342
)
343-
return start_date, end_date
343+
344+
return {
345+
'start_date': datetime_str(start_date),
346+
'end_date': datetime_str(end_date)
347+
}

tests/time_tracker_api/time_entries/time_entries_namespace_test.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from commons.data_access_layer.cosmos_db import current_datetime, \
1111
current_datetime_str, get_date_range_of_month, get_current_month, \
1212
get_current_year, datetime_str
13+
from commons.data_access_layer.database import EventContext
1314
from time_tracker_api.time_entries.time_entries_model import TimeEntriesCosmosDBDao
1415

1516
fake = Faker()
@@ -449,7 +450,6 @@ def test_create_with_valid_uuid_format_should_return_created(client: FlaskClient
449450
def test_find_all_is_called_with_generated_dates(client: FlaskClient,
450451
mocker: MockFixture,
451452
valid_header: dict,
452-
tenant_id: str,
453453
owner_id: str,
454454
url: str,
455455
month: int,
@@ -459,23 +459,15 @@ def test_find_all_is_called_with_generated_dates(client: FlaskClient,
459459
'find_all',
460460
return_value=fake_time_entry)
461461

462-
response = client.get(url,
463-
headers=valid_header,
464-
follow_redirects=True)
465-
466-
start_date, end_date = get_date_range_of_month(year, month)
467-
custom_args = {
468-
'start_date': datetime_str(start_date),
469-
'end_date': datetime_str(end_date)
470-
}
462+
response = client.get(url, headers=valid_header, follow_redirects=True)
471463

464+
date_range = get_date_range_of_month(year, month)
472465
conditions = {
473466
'owner_id': owner_id
474467
}
475468

476469
assert HTTPStatus.OK == response.status_code
477470
assert json.loads(response.data) is not None
478-
repository_find_all_mock.assert_called_once_with(partition_key_value=tenant_id,
479-
conditions=conditions,
480-
custom_args=custom_args)
481-
471+
repository_find_all_mock.assert_called_once_with(ANY,
472+
conditions=conditions,
473+
date_range=date_range)

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -221,25 +221,7 @@ def get_all(self, conditions: dict = {}) -> list:
221221
event_ctx = self.create_event_context("read-many")
222222
conditions.update({"owner_id": event_ctx.user_id})
223223

224-
if 'month' and 'year' in conditions:
225-
month = int(conditions.get("month"))
226-
year = int(conditions.get("year"))
227-
conditions.pop('month')
228-
conditions.pop('year')
229-
elif 'month' in conditions:
230-
month = int(conditions.get("month"))
231-
year = get_current_year()
232-
conditions.pop('month')
233-
else:
234-
month = get_current_month()
235-
year = get_current_year()
236-
237-
start_date, end_date = get_date_range_of_month(year, month)
238-
239-
date_range = {
240-
'start_date': start_date.isoformat(),
241-
'end_date': end_date.isoformat(),
242-
}
224+
date_range = self.handle_date_filter_args(args=conditions)
243225
return self.repository.find_all(event_ctx,
244226
conditions=conditions,
245227
date_range=date_range)
@@ -277,6 +259,22 @@ def find_running(self):
277259
event_ctx = self.create_event_context("find_running")
278260
return self.repository.find_running(event_ctx.tenant_id, event_ctx.user_id)
279261

262+
@staticmethod
263+
def handle_date_filter_args(args: dict) -> dict:
264+
if 'month' and 'year' in args:
265+
month = int(args.get("month"))
266+
year = int(args.get("year"))
267+
args.pop('month')
268+
args.pop('year')
269+
elif 'month' in args:
270+
month = int(args.get("month"))
271+
year = get_current_year()
272+
args.pop('month')
273+
else:
274+
month = get_current_month()
275+
year = get_current_year()
276+
return get_date_range_of_month(year, month)
277+
280278

281279
def create_dao() -> TimeEntriesDao:
282280
repository = TimeEntryCosmosDBRepository()

0 commit comments

Comments
 (0)