Skip to content

Commit 14c82d0

Browse files
authored
refactor: TT-179 count method moved to TimeEntryCosmosDBRepository (#264)
1 parent 29721f0 commit 14c82d0

File tree

4 files changed

+36
-61
lines changed

4 files changed

+36
-61
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ migration_status.csv
3636

3737
# Mac
3838
.DS_Store
39+
40+
# windows env variables
41+
.env.bat

commons/data_access_layer/cosmos_db.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -277,50 +277,6 @@ def find_all(
277277
function_mapper = self.get_mapper_or_dict(mapper)
278278
return list(map(function_mapper, result))
279279

280-
def count(
281-
self,
282-
event_context: EventContext,
283-
conditions: dict = None,
284-
custom_sql_conditions: List[str] = None,
285-
custom_params: dict = None,
286-
visible_only=True,
287-
):
288-
conditions = conditions if conditions else {}
289-
custom_sql_conditions = (
290-
custom_sql_conditions if custom_sql_conditions else []
291-
)
292-
custom_params = custom_params if custom_params else {}
293-
partition_key_value = self.find_partition_key_value(event_context)
294-
params = [
295-
{"name": "@partition_key_value", "value": partition_key_value},
296-
]
297-
params.extend(self.generate_params(conditions))
298-
params.extend(custom_params)
299-
query_str = """
300-
SELECT VALUE COUNT(1) FROM c
301-
WHERE c.{partition_key_attribute}=@partition_key_value
302-
{conditions_clause}
303-
{visibility_condition}
304-
{custom_sql_conditions_clause}
305-
""".format(
306-
partition_key_attribute=self.partition_key_attribute,
307-
visibility_condition=self.create_sql_condition_for_visibility(
308-
visible_only
309-
),
310-
conditions_clause=self.create_sql_where_conditions(conditions),
311-
custom_sql_conditions_clause=self.create_custom_sql_conditions(
312-
custom_sql_conditions
313-
),
314-
)
315-
316-
flask.current_app.logger.debug(query_str)
317-
result = self.container.query_items(
318-
query=query_str,
319-
parameters=params,
320-
partition_key=partition_key_value,
321-
)
322-
return result.next()
323-
324280
def partial_update(
325281
self,
326282
id: str,

tests/commons/data_access_layer/cosmos_db_test.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,15 +317,6 @@ def test_find_all_with_offset(
317317
assert result_after_the_second_item == result_all_items[2:]
318318

319319

320-
def test_count(
321-
cosmos_db_repository: CosmosDBRepository, event_context: EventContext
322-
):
323-
counter = cosmos_db_repository.count(event_context)
324-
print('test counter: ', counter)
325-
326-
assert counter == 10
327-
328-
329320
@pytest.mark.parametrize(
330321
'mapper,expected_type', [(None, dict), (dict, dict), (Person, Person)]
331322
)

time_tracker_api/time_entries/time_entries_repository.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
create_in_condition,
1919
add_user_email_to_time_entries,
2020
)
21-
21+
import flask
2222
from flask_restplus import abort
2323
from flask_restplus._http import HTTPStatus
2424
from utils.azure_users import AzureConnection
@@ -91,6 +91,7 @@ def count(
9191
conditions: dict = None,
9292
custom_sql_conditions: List[str] = None,
9393
date_range: dict = None,
94+
visible_only=True,
9495
**kwargs,
9596
):
9697
conditions = conditions if conditions else {}
@@ -104,14 +105,38 @@ def count(
104105
)
105106

106107
custom_params = self.generate_params(date_range)
107-
counter = CosmosDBRepository.count(
108-
self,
109-
event_context=event_context,
110-
conditions=conditions,
111-
custom_sql_conditions=custom_sql_conditions,
112-
custom_params=custom_params,
108+
partition_key_value = self.find_partition_key_value(event_context)
109+
params = [
110+
{"name": "@partition_key_value", "value": partition_key_value},
111+
]
112+
params.extend(self.generate_params(conditions))
113+
params.extend(custom_params)
114+
115+
query_str = """
116+
SELECT VALUE COUNT(1) FROM c
117+
WHERE c.{partition_key_attribute}=@partition_key_value
118+
{conditions_clause}
119+
{visibility_condition}
120+
{custom_sql_conditions_clause}
121+
""".format(
122+
partition_key_attribute=self.partition_key_attribute,
123+
visibility_condition=self.create_sql_condition_for_visibility(
124+
visible_only
125+
),
126+
conditions_clause=self.create_sql_where_conditions(conditions),
127+
custom_sql_conditions_clause=self.create_custom_sql_conditions(
128+
custom_sql_conditions
129+
),
113130
)
114-
return counter
131+
132+
flask.current_app.logger.debug(query_str)
133+
result = self.container.query_items(
134+
query=query_str,
135+
parameters=params,
136+
partition_key=partition_key_value,
137+
)
138+
139+
return result.next()
115140

116141
def find_all(
117142
self,

0 commit comments

Comments
 (0)