Skip to content

Commit 9e9fd0c

Browse files
PabloPablo
authored andcommitted
fix: TT-220 Remove custom_sql_params from methods and replace for new arguments and function calls
1 parent 7400b90 commit 9e9fd0c

File tree

9 files changed

+139
-163
lines changed

9 files changed

+139
-163
lines changed

commons/data_access_layer/cosmos_db.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,6 @@ def create_sql_where_conditions(
173173
else:
174174
return ""
175175

176-
@staticmethod
177-
def create_custom_sql_conditions(custom_sql_conditions: List[str]) -> str:
178-
if len(custom_sql_conditions) > 0:
179-
return "AND {custom_sql_conditions_clause}".format(
180-
custom_sql_conditions_clause=" AND ".join(
181-
custom_sql_conditions
182-
)
183-
)
184-
else:
185-
return ''
186-
187176
@staticmethod
188177
def generate_params(conditions: dict) -> list:
189178
result = []
@@ -217,6 +206,16 @@ def attach_context(data: dict, event_context: EventContext):
217206
"session_id": event_context.session_id,
218207
}
219208

209+
@staticmethod
210+
def create_sql_date_range_filter(date_range: dict) -> str:
211+
if 'start_date' and 'end_date' in date_range:
212+
return """
213+
AND ((c.start_date BETWEEN @start_date AND @end_date) OR
214+
(c.end_date BETWEEN @start_date AND @end_date))
215+
"""
216+
else:
217+
return ''
218+
220219
def create(
221220
self, data: dict, event_context: EventContext, mapper: Callable = None
222221
):
@@ -251,19 +250,13 @@ def find_all(
251250
self,
252251
event_context: EventContext,
253252
conditions: dict = None,
254-
custom_sql_conditions: List[str] = None,
255-
custom_params: dict = None,
253+
date_range: dict = None,
256254
max_count=None,
257255
offset=0,
258256
visible_only=True,
259257
mapper: Callable = None,
260258
):
261259
conditions = conditions if conditions else {}
262-
custom_sql_conditions = (
263-
custom_sql_conditions if custom_sql_conditions else []
264-
)
265-
custom_params = custom_params if custom_params else {}
266-
267260
partition_key_value = self.find_partition_key_value(event_context)
268261
max_count = self.get_page_size_or(max_count)
269262
params = [
@@ -277,15 +270,20 @@ def find_all(
277270
status_value = conditions.get('status')
278271
conditions.pop('status')
279272

273+
date_range = date_range if date_range else {}
274+
date_range_params = (
275+
self.generate_params(date_range) if date_range else []
276+
)
280277
params.extend(self.generate_params(conditions))
281-
params.extend(custom_params)
278+
params.extend(date_range_params)
279+
282280
query_str = """
283281
SELECT * FROM c
284282
WHERE c.{partition_key_attribute}=@partition_key_value
285283
{conditions_clause}
286-
{visibility_condition}
287284
{active_condition}
288-
{custom_sql_conditions_clause}
285+
{date_range_sql_condition}
286+
{visibility_condition}
289287
{order_clause}
290288
OFFSET @offset LIMIT @max_count
291289
""".format(
@@ -295,11 +293,12 @@ def find_all(
295293
),
296294
active_condition=self.create_sql_active_condition(status_value),
297295
conditions_clause=self.create_sql_where_conditions(conditions),
298-
custom_sql_conditions_clause=self.create_custom_sql_conditions(
299-
custom_sql_conditions
296+
date_range_sql_condition=self.create_sql_date_range_filter(
297+
date_range
300298
),
301299
order_clause=self.create_sql_order_clause(),
302300
)
301+
303302
result = self.container.query_items(
304303
query=query_str,
305304
parameters=params,

tests/time_tracker_api/projects/projects_model_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@patch(
1212
'time_tracker_api.projects.projects_model.ProjectCosmosDBRepository.find_partition_key_value'
1313
)
14-
def test_find_all_new_version(
14+
def test_find_all_projects_new_version(
1515
find_partition_key_value_mock,
1616
event_context: EventContext,
1717
project_repository: ProjectCosmosDBRepository,

tests/time_tracker_api/time_entries/time_entries_model_test.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -278,23 +278,11 @@ def test_updated_item_without_deleted_key_should_call_validate_data(
278278
@patch(
279279
'commons.data_access_layer.cosmos_db.CosmosDBRepository.generate_params'
280280
)
281-
@patch(
282-
'commons.data_access_layer.cosmos_db.CosmosDBRepository.create_sql_condition_for_visibility'
283-
)
284-
@patch(
285-
'commons.data_access_layer.cosmos_db.CosmosDBRepository.create_sql_where_conditions'
286-
)
287-
@patch(
288-
'commons.data_access_layer.cosmos_db.CosmosDBRepository.create_custom_sql_conditions'
289-
)
290281
@patch(
291282
'time_tracker_api.time_entries.time_entries_repository.TimeEntryCosmosDBRepository.add_complementary_info'
292283
)
293-
def test_find_all_v2(
284+
def test_find_all_time_entries_new_version(
294285
add_complementary_info_mock,
295-
create_custom_sql_conditions_mock,
296-
create_sql_where_conditions_mock,
297-
create_sql_condition_for_visibility_mock,
298286
generate_params_mock,
299287
get_page_size_or_mock,
300288
find_partition_key_value_mock,
@@ -319,25 +307,20 @@ def test_find_all_v2(
319307

320308
result = time_entry_repository.find_all(
321309
conditions={"user_id": "*"},
322-
custom_sql_conditions=[],
323310
event_context=event_context,
324311
date_range={
325312
'start_date': "2021-03-22T10:00:00.000Z",
326313
'end_date': "2021-03-22T11:00:00.000Z",
327314
},
328-
custom_params={},
329315
)
330316

331317
find_partition_key_value_mock.assert_called_once()
332318
get_page_size_or_mock.assert_called_once()
319+
333320
assert len(result) == 1
334321
time_entry = result[0]
335322
assert time_entry == expected_item
336323

337-
create_sql_condition_for_visibility_mock.assert_called_once()
338-
create_sql_where_conditions_mock.assert_called_once()
339-
create_custom_sql_conditions_mock.assert_called_once()
340-
341324

342325
@patch(
343326
'time_tracker_api.time_entries.time_entries_repository.TimeEntryCosmosDBRepository.find_partition_key_value'

tests/time_tracker_api/time_entries/time_entries_namespace_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def test_get_time_entry_should_succeed_with_valid_id(
197197
Mock(),
198198
)
199199
@patch(
200-
'time_tracker_api.time_entries.time_entries_dao.TimeEntriesCosmosDBDao.build_custom_query',
200+
'time_tracker_api.time_entries.time_entries_dao.TimeEntriesCosmosDBDao.get_owner_ids',
201201
Mock(),
202202
)
203203
@patch(
@@ -278,7 +278,7 @@ def test_get_time_entries_by_type_of_user_when_is_user_tester(
278278
Mock(),
279279
)
280280
@patch(
281-
'time_tracker_api.time_entries.time_entries_dao.TimeEntriesCosmosDBDao.build_custom_query',
281+
'time_tracker_api.time_entries.time_entries_dao.TimeEntriesCosmosDBDao.get_owner_ids',
282282
Mock(),
283283
)
284284
@patch(

tests/utils/query_builder_test.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ def test__build_order_by(
303303

304304
assert orderBy_condition == expected_order_by_condition
305305

306+
306307
@pytest.mark.parametrize(
307308
"attribute,ids_list,expected_not_in_list",
308309
[
@@ -313,8 +314,11 @@ def test__build_order_by(
313314
("id", ["id"], ["c.id NOT IN ('id')"]),
314315
("id", ["id1", "id2"], ["c.id NOT IN ('id1', 'id2')"]),
315316
("owner_id", ["id1", "id2"], ["c.owner_id NOT IN ('id1', 'id2')"]),
316-
("customer_id", ["id1", "id2"], [
317-
"c.customer_id NOT IN ('id1', 'id2')"]),
317+
(
318+
"customer_id",
319+
["id1", "id2"],
320+
["c.customer_id NOT IN ('id1', 'id2')"],
321+
),
318322
],
319323
)
320324
def test_add_sql_not_in_condition(
@@ -325,7 +329,29 @@ def test_add_sql_not_in_condition(
325329
query_builder = CosmosDBQueryBuilder().add_sql_not_in_condition(
326330
attribute, ids_list
327331
)
328-
assert len(query_builder.where_conditions) == len(
329-
expected_not_in_list
330-
)
331-
assert query_builder.where_conditions == expected_not_in_list
332+
assert len(query_builder.where_conditions) == len(expected_not_in_list)
333+
assert query_builder.where_conditions == expected_not_in_list
334+
335+
336+
@pytest.mark.parametrize(
337+
"date_range,expected_value,expected_expression",
338+
[
339+
(
340+
{
341+
'start_date': '2021-05-09T00:00:00-05:00',
342+
'end_date': '2021-05-15T23:59:59-05:00',
343+
},
344+
{
345+
'start_date': '2021-05-09T00:00:00-05:00',
346+
'end_date': '2021-05-15T23:59:59-05:00',
347+
},
348+
"((c.start_date BETWEEN @start_date AND @end_date) OR (c.end_date BETWEEN @start_date AND @end_date))",
349+
)
350+
],
351+
)
352+
def test_add_date_range(date_range, expected_value, expected_expression):
353+
query_builder = CosmosDBQueryBuilder().add_date_range(date_range)
354+
result = query_builder._CosmosDBQueryBuilder__build_date_range()
355+
356+
assert query_builder.date_range == expected_value
357+
assert result == expected_expression

time_tracker_api/time_entries/time_entries_dao.py

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
add_project_info_to_time_entries,
99
add_activity_name_to_time_entries,
1010
create_custom_query_from_str,
11+
create_list_from_str,
1112
)
1213
from utils.time import (
1314
datetime_str,
@@ -71,19 +72,15 @@ def check_time_entry_is_not_started(self, data):
7172
"The specified time entry is already running",
7273
)
7374

74-
def build_custom_query(self, is_admin: bool, conditions: dict = None):
75+
def get_owner_ids(self, is_admin: bool, conditions: dict = None):
7576
custom_query = []
7677
if "user_id" in conditions:
7778
if is_admin:
7879
conditions.pop("owner_id")
7980
custom_query = (
8081
[]
8182
if conditions.get("user_id") == "*"
82-
else [
83-
create_custom_query_from_str(
84-
conditions.get("user_id"), "c.owner_id"
85-
)
86-
]
83+
else create_list_from_str(conditions.get("user_id"))
8784
)
8885
conditions.pop("user_id")
8986
else:
@@ -96,7 +93,7 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
9693
event_ctx = self.create_event_context("read-many")
9794
conditions.update({"owner_id": event_ctx.user_id})
9895
is_complete_query = conditions.get("user_id") == '*'
99-
custom_query = self.build_custom_query(
96+
owner_ids = self.get_owner_ids(
10097
is_admin=event_ctx.is_admin,
10198
conditions=conditions,
10299
)
@@ -108,13 +105,6 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
108105
event_ctx.user_id
109106
)
110107

111-
custom_query.append(
112-
TimeEntryCosmosDBRepository.create_sql_date_range_filter(
113-
date_range
114-
)
115-
)
116-
custom_params = CosmosDBRepository.generate_params(date_range)
117-
118108
test_user_ids = (
119109
azure_connection.get_test_user_ids()
120110
if not current_user_is_tester and is_complete_query
@@ -123,11 +113,10 @@ def get_all(self, conditions: dict = None, **kwargs) -> list:
123113

124114
time_entries_list = self.repository.find_all(
125115
conditions=conditions,
126-
custom_sql_conditions=custom_query,
127116
test_user_ids=test_user_ids,
117+
owner_ids=owner_ids,
128118
date_range=date_range,
129119
max_count=limit,
130-
custom_params=custom_params,
131120
event_context=event_ctx,
132121
)
133122

@@ -138,10 +127,7 @@ def get_lastest_entries_by_project(
138127
) -> list:
139128
event_ctx = self.create_event_context("read-many")
140129
conditions.update({"owner_id": event_ctx.user_id})
141-
custom_query = self.build_custom_query(
142-
is_admin=event_ctx.is_admin,
143-
conditions=conditions,
144-
)
130+
145131
date_range = self.handle_date_filter_args(args=conditions)
146132

147133
project_dao = projects_model.create_dao()
@@ -161,13 +147,12 @@ def get_lastest_entries_by_project(
161147
latest = self.repository.find_all_entries(
162148
event_ctx,
163149
conditions=conditions,
164-
custom_sql_conditions=custom_query,
165150
date_range=date_range,
166151
max_count=limit,
167152
)
168153

169154
if len(latest) > 0:
170-
result.append(latest[0])
155+
self.append = result.append(latest[0])
171156

172157
add_activity_name_to_time_entries(result, activities)
173158
add_project_info_to_time_entries(result, projects)
@@ -180,19 +165,19 @@ def get_all_paginated(self, conditions: dict = None, **kwargs) -> list:
180165
get_all_conditions.pop("start")
181166
event_ctx = self.create_event_context("read-many")
182167
get_all_conditions.update({"owner_id": event_ctx.user_id})
183-
custom_query = self.build_custom_query(
168+
owner_ids = self.get_owner_ids(
184169
is_admin=event_ctx.is_admin,
185170
conditions=get_all_conditions,
186171
)
187172
date_range = self.handle_date_filter_args(args=get_all_conditions)
188173
records_total = self.repository.count(
189174
event_ctx,
190175
conditions=get_all_conditions,
191-
custom_sql_conditions=custom_query,
176+
owner_ids=owner_ids,
192177
date_range=date_range,
193178
)
194179
conditions.update({"owner_id": event_ctx.user_id})
195-
custom_query = self.build_custom_query(
180+
owner_ids = self.get_owner_ids(
196181
is_admin=event_ctx.is_admin,
197182
conditions=conditions,
198183
)
@@ -203,9 +188,9 @@ def get_all_paginated(self, conditions: dict = None, **kwargs) -> list:
203188
conditions.pop("start", None)
204189

205190
time_entries = self.repository.find_all(
206-
event_ctx,
191+
event_context=event_ctx,
207192
conditions=conditions,
208-
custom_sql_conditions=custom_query,
193+
owner_ids=owner_ids,
209194
date_range=date_range,
210195
max_count=length,
211196
offset=start,

0 commit comments

Comments
 (0)