From 4a9431a1f1e5c0faf0a6e709dda1b5294b2fd7f2 Mon Sep 17 00:00:00 2001 From: kelly Date: Wed, 10 Mar 2021 15:57:13 -0500 Subject: [PATCH] refactor: TT-179 count method moved to TimeEntryCosmosDBRepository --- .gitignore | 3 ++ commons/data_access_layer/cosmos_db.py | 44 ------------------- .../data_access_layer/cosmos_db_test.py | 9 ---- .../time_entries/time_entries_repository.py | 41 +++++++++++++---- 4 files changed, 36 insertions(+), 61 deletions(-) diff --git a/.gitignore b/.gitignore index 368d5048..b4e0b071 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,6 @@ migration_status.csv # Mac .DS_Store + +# windows env variables +.env.bat \ No newline at end of file diff --git a/commons/data_access_layer/cosmos_db.py b/commons/data_access_layer/cosmos_db.py index 9c08952c..c1b9cfee 100644 --- a/commons/data_access_layer/cosmos_db.py +++ b/commons/data_access_layer/cosmos_db.py @@ -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, diff --git a/tests/commons/data_access_layer/cosmos_db_test.py b/tests/commons/data_access_layer/cosmos_db_test.py index 06e79f85..c7a04eaf 100644 --- a/tests/commons/data_access_layer/cosmos_db_test.py +++ b/tests/commons/data_access_layer/cosmos_db_test.py @@ -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)] ) diff --git a/time_tracker_api/time_entries/time_entries_repository.py b/time_tracker_api/time_entries/time_entries_repository.py index 41883bb1..b86a6ae3 100644 --- a/time_tracker_api/time_entries/time_entries_repository.py +++ b/time_tracker_api/time_entries/time_entries_repository.py @@ -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 @@ -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 {} @@ -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,