Skip to content

Commit 9a71d4a

Browse files
committed
feat: add test find_all is called with generated dates
1 parent b494a75 commit 9a71d4a

File tree

3 files changed

+80
-30
lines changed

3 files changed

+80
-30
lines changed

commons/data_access_layer/cosmos_db.py

Lines changed: 34 additions & 1 deletion
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
5+
from typing import Callable, List, Tuple
66

77
import azure.cosmos.cosmos_client as cosmos_client
88
import azure.cosmos.exceptions as exceptions
@@ -292,3 +292,36 @@ def generate_uuid4() -> str:
292292
def init_app(app: Flask) -> None:
293293
global cosmos_helper
294294
cosmos_helper = CosmosDBFacade.from_flask_config(app)
295+
296+
297+
def get_last_day_of_month(year: int, month: int) -> int:
298+
from calendar import monthrange
299+
return monthrange(year=year, month=month)[1]
300+
301+
302+
def get_current_year() -> int:
303+
return datetime.now().year
304+
305+
306+
def get_current_month() -> int:
307+
return datetime.now().month
308+
309+
310+
def get_date_range_of_month(
311+
year: int,
312+
month: int
313+
) -> Tuple[datetime, datetime]:
314+
first_day_of_month = 1
315+
start_date = datetime(year=year, month=month, day=first_day_of_month)
316+
317+
last_day_of_month = get_last_day_of_month(year=year, month=month)
318+
end_date = datetime(
319+
year=year,
320+
month=month,
321+
day=last_day_of_month,
322+
hour=23,
323+
minute=59,
324+
second=59,
325+
microsecond=999999
326+
)
327+
return start_date, end_date

tests/time_tracker_api/time_entries/time_entries_namespace_test.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from flask_restplus._http import HTTPStatus
88
from pytest_mock import MockFixture, pytest
99

10-
from commons.data_access_layer.cosmos_db import current_datetime, current_datetime_str
10+
from commons.data_access_layer.cosmos_db import current_datetime, \
11+
current_datetime_str, get_date_range_of_month, get_current_month, \
12+
get_current_year, datetime_str
1113

1214
fake = Faker()
1315

@@ -470,3 +472,44 @@ def test_create_with_valid_uuid_format_should_return_created(client: FlaskClient
470472
assert HTTPStatus.CREATED == response.status_code
471473
repository_container_create_item_mock.assert_called()
472474

475+
476+
@pytest.mark.parametrize(
477+
'url,month,year',
478+
[
479+
('/time-entries?month=4&year=2020', 4, 2020),
480+
('/time-entries?month=4', 4, get_current_year()),
481+
('/time-entries', get_current_month(), get_current_year())
482+
]
483+
)
484+
def test_find_all_is_called_with_generated_dates(client: FlaskClient,
485+
mocker: MockFixture,
486+
valid_header: dict,
487+
tenant_id: str,
488+
owner_id: str,
489+
url: str,
490+
month: int,
491+
year: int):
492+
from time_tracker_api.time_entries.time_entries_namespace import time_entries_dao
493+
repository_find_all_mock = mocker.patch.object(time_entries_dao.repository,
494+
'find_all',
495+
return_value=fake_time_entry)
496+
497+
response = client.get(url,
498+
headers=valid_header,
499+
follow_redirects=True)
500+
501+
start_date, end_date = get_date_range_of_month(year, month)
502+
custom_args = {
503+
'start_date': datetime_str(start_date),
504+
'end_date': datetime_str(end_date)
505+
}
506+
507+
conditions = {
508+
'owner_id': owner_id
509+
}
510+
511+
assert HTTPStatus.OK == response.status_code
512+
assert json.loads(response.data) is not None
513+
repository_find_all_mock.assert_called_once_with(partition_key_value=tenant_id,
514+
conditions=conditions,
515+
custom_args=custom_args)

time_tracker_api/time_entries/time_entries_model.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import abc
22
from dataclasses import dataclass, field
3-
from typing import List, Callable, Tuple
4-
from datetime import datetime
3+
from typing import List, Callable
54

65
from azure.cosmos import PartitionKey
76
from flask_restplus._http import HTTPStatus
87

98
from commons.data_access_layer.cosmos_db import CosmosDBDao, CosmosDBRepository, CustomError, current_datetime_str, \
10-
CosmosDBModel
9+
CosmosDBModel, get_date_range_of_month, get_current_year, get_current_month
1110
from commons.data_access_layer.database import CRUDDao
1211
from time_tracker_api.security import current_user_id
1312

@@ -187,31 +186,6 @@ def validate_data(self, data):
187186
description="There is another time entry in that date range")
188187

189188

190-
def get_last_day_of_month(year: int, month: int) -> int:
191-
from calendar import monthrange
192-
return monthrange(year=year, month=month)[1]
193-
194-
195-
def get_current_year() -> int:
196-
return datetime.now().year
197-
198-
199-
def get_current_month() -> int:
200-
return datetime.now().month
201-
202-
203-
def get_date_range_of_month(
204-
year: int,
205-
month: int
206-
) -> Tuple[datetime, datetime]:
207-
first_day_of_month = 1
208-
start_date = datetime(year=year, month=month, day=first_day_of_month)
209-
210-
# TODO : fix bound as this would exclude the last day
211-
last_day_of_month = get_last_day_of_month(year=year, month=month)
212-
end_date = datetime(year=year, month=month, day=last_day_of_month)
213-
return start_date, end_date
214-
215189

216190
class TimeEntriesCosmosDBDao(TimeEntriesDao, CosmosDBDao):
217191
def __init__(self, repository):

0 commit comments

Comments
 (0)