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
refactor: TT-179 count method moved to TimeEntryCosmosDBRepository
  • Loading branch information
kellycastrof committed Mar 10, 2021
commit 4a9431a1f1e5c0faf0a6e709dda1b5294b2fd7f2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,6 @@ migration_status.csv

# Mac
.DS_Store

# windows env variables
.env.bat
44 changes: 0 additions & 44 deletions commons/data_access_layer/cosmos_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,50 +277,6 @@ def find_all(
function_mapper = self.get_mapper_or_dict(mapper)
return list(map(function_mapper, result))

def count(
self,
event_context: EventContext,
conditions: dict = None,
custom_sql_conditions: List[str] = None,
custom_params: dict = None,
visible_only=True,
):
conditions = conditions if conditions else {}
custom_sql_conditions = (
custom_sql_conditions if custom_sql_conditions else []
)
custom_params = custom_params if custom_params else {}
partition_key_value = self.find_partition_key_value(event_context)
params = [
{"name": "@partition_key_value", "value": partition_key_value},
]
params.extend(self.generate_params(conditions))
params.extend(custom_params)
query_str = """
SELECT VALUE COUNT(1) FROM c
WHERE c.{partition_key_attribute}=@partition_key_value
{conditions_clause}
{visibility_condition}
{custom_sql_conditions_clause}
""".format(
partition_key_attribute=self.partition_key_attribute,
visibility_condition=self.create_sql_condition_for_visibility(
visible_only
),
conditions_clause=self.create_sql_where_conditions(conditions),
custom_sql_conditions_clause=self.create_custom_sql_conditions(
custom_sql_conditions
),
)

flask.current_app.logger.debug(query_str)
result = self.container.query_items(
query=query_str,
parameters=params,
partition_key=partition_key_value,
)
return result.next()

def partial_update(
self,
id: str,
Expand Down
9 changes: 0 additions & 9 deletions tests/commons/data_access_layer/cosmos_db_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,6 @@ def test_find_all_with_offset(
assert result_after_the_second_item == result_all_items[2:]


def test_count(
cosmos_db_repository: CosmosDBRepository, event_context: EventContext
):
counter = cosmos_db_repository.count(event_context)
print('test counter: ', counter)

assert counter == 10


@pytest.mark.parametrize(
'mapper,expected_type', [(None, dict), (dict, dict), (Person, Person)]
)
Expand Down
41 changes: 33 additions & 8 deletions time_tracker_api/time_entries/time_entries_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
create_in_condition,
add_user_email_to_time_entries,
)

import flask
from flask_restplus import abort
from flask_restplus._http import HTTPStatus
from utils.azure_users import AzureConnection
Expand Down Expand Up @@ -91,6 +91,7 @@ def count(
conditions: dict = None,
custom_sql_conditions: List[str] = None,
date_range: dict = None,
visible_only=True,
**kwargs,
):
conditions = conditions if conditions else {}
Expand All @@ -104,14 +105,38 @@ def count(
)

custom_params = self.generate_params(date_range)
counter = CosmosDBRepository.count(
self,
event_context=event_context,
conditions=conditions,
custom_sql_conditions=custom_sql_conditions,
custom_params=custom_params,
partition_key_value = self.find_partition_key_value(event_context)
params = [
{"name": "@partition_key_value", "value": partition_key_value},
]
params.extend(self.generate_params(conditions))
params.extend(custom_params)

query_str = """
SELECT VALUE COUNT(1) FROM c
WHERE c.{partition_key_attribute}=@partition_key_value
{conditions_clause}
{visibility_condition}
{custom_sql_conditions_clause}
""".format(
partition_key_attribute=self.partition_key_attribute,
visibility_condition=self.create_sql_condition_for_visibility(
visible_only
),
conditions_clause=self.create_sql_where_conditions(conditions),
custom_sql_conditions_clause=self.create_custom_sql_conditions(
custom_sql_conditions
),
)
return counter

flask.current_app.logger.debug(query_str)
result = self.container.query_items(
query=query_str,
parameters=params,
partition_key=partition_key_value,
)

return result.next()

def find_all(
self,
Expand Down