Skip to content

Commit 498a643

Browse files
committed
feat: TT-243 Refactor code based on PR reviews
1 parent bad6ee2 commit 498a643

File tree

5 files changed

+41
-153
lines changed

5 files changed

+41
-153
lines changed

tests/time_tracker_api/time_entries/time_entries_query_builder_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,37 @@ def test_build_with_add_sql_interception_with_date_range_condition():
9494
OR (c.end_date BETWEEN @start_date AND @end_date))
9595
OR ((@start_date BETWEEN c.start_date AND c.end_date)
9696
OR (@end_date BETWEEN c.start_date AND c.end_date)))
97+
AND c.start_date!= @end_date
98+
AND c.end_date!= @start_date
9799
"""
98100

99101
builder_query = time_entry_query_builder.get_query()
100102

101103
assert remove_white_spaces(builder_query) == remove_white_spaces(
102104
expected_query
103105
)
106+
107+
108+
def test_add_sql_is_running_time_entry_condition_should_update_where_conditions_list():
109+
query_builder = (
110+
TimeEntryQueryBuilder().add_sql_is_running_time_entry_condition()
111+
)
112+
113+
assert len(query_builder.where_conditions) == 1
114+
115+
116+
@pytest.mark.parametrize(
117+
"expected_condition,expected_params",
118+
[("c.id!=@ignore_id", {"name": "@ignore_id", "value": "nomatter"})],
119+
)
120+
def test_add_sql_ignore_id_condition_should_update_where_conditions_list(
121+
expected_condition, expected_params
122+
):
123+
query_builder = TimeEntryQueryBuilder().add_sql_ignore_id_condition(
124+
'nomatter'
125+
)
126+
127+
assert len(query_builder.where_conditions) == 1
128+
assert len(query_builder.parameters) == 1
129+
assert query_builder.where_conditions[0].strip() == expected_condition
130+
assert query_builder.parameters[0] == expected_params

tests/utils/query_builder_test.py

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -90,88 +90,6 @@ def test_add_sql_where_equal_condition_with_None_should_not_update_lists():
9090
assert query_builder.parameters == []
9191

9292

93-
@pytest.mark.parametrize(
94-
"data,expected_where_list,expected_params",
95-
[
96-
([], [], []),
97-
(
98-
[
99-
{
100-
"field_name": "end_date",
101-
"compare_field_name": "start_date",
102-
"compare_field_value": "nomatter",
103-
},
104-
{
105-
"field_name": "start_date",
106-
"compare_field_name": "end_date",
107-
"compare_field_value": "nomatter",
108-
},
109-
],
110-
[
111-
"c.end_date != @start_date",
112-
"c.start_date != @end_date",
113-
],
114-
[
115-
{'name': '@start_date', 'value': 'nomatter'},
116-
{'name': '@end_date', 'value': 'nomatter'},
117-
],
118-
),
119-
],
120-
)
121-
def test_add_sql_where_not_equal_condition_should_update_where_conditions_list(
122-
data, expected_where_list, expected_params
123-
):
124-
query_builder = CosmosDBQueryBuilder().add_sql_where_not_equal_condition(
125-
data
126-
)
127-
128-
assert len(query_builder.where_conditions) == len(expected_where_list)
129-
assert len(query_builder.parameters) == len(expected_params)
130-
assert query_builder.where_conditions == expected_where_list
131-
assert query_builder.parameters == expected_params
132-
133-
134-
def test_add_sql_where_not_equal_condition_with_none_data_should_not_update_lists():
135-
query_builder = CosmosDBQueryBuilder().add_sql_where_not_equal_condition(
136-
None
137-
)
138-
139-
assert len(query_builder.where_conditions) == 0
140-
assert len(query_builder.parameters) == 0
141-
assert query_builder.parameters == []
142-
assert query_builder.where_conditions == []
143-
144-
145-
@pytest.mark.parametrize(
146-
"data,expected_params",
147-
[
148-
(
149-
[
150-
{
151-
"field_name": "end_date",
152-
"compare_field_name": "start_date",
153-
"compare_field_value": "nomatter",
154-
}
155-
],
156-
[
157-
{'name': '@start_date', 'value': 'nomatter'},
158-
],
159-
)
160-
],
161-
)
162-
def test_add_sql_where_not_equal_conditions_should_not_update_parameters_if_already_exists(
163-
data, expected_params
164-
):
165-
query_builder = CosmosDBQueryBuilder()
166-
query_builder.parameters = [
167-
{'name': '@start_date', 'value': 'nomatter'},
168-
]
169-
170-
query_builder.add_sql_where_not_equal_condition(data)
171-
172-
assert len(query_builder.parameters) == len(expected_params)
173-
174-
17593
@pytest.mark.parametrize(
17694
"visibility_bool,expected_where_list",
17795
[(True, ['NOT IS_DEFINED(c.deleted)']), (False, [])],
@@ -413,30 +331,3 @@ def test_add_sql_not_in_condition(
413331
)
414332
assert len(query_builder.where_conditions) == len(expected_not_in_list)
415333
assert query_builder.where_conditions == expected_not_in_list
416-
417-
418-
def test_add_sql_non_existent_attribute_condition_should_update_where_conditions_list():
419-
query_builder = (
420-
CosmosDBQueryBuilder().add_sql_non_existent_attribute_condition('name')
421-
)
422-
expected_condition = "(NOT IS_DEFINED(c.name) OR c.name = null)"
423-
424-
assert len(query_builder.where_conditions) == 1
425-
assert query_builder.where_conditions[0].strip() == expected_condition
426-
427-
428-
@pytest.mark.parametrize(
429-
"expected_condition,expected_params",
430-
[("c.id!=@ignore_id", {"name": "@ignore_id", "value": "nomatter"})],
431-
)
432-
def test_add_sql_ignore_id_condition_should_update_where_conditions_list(
433-
expected_condition, expected_params
434-
):
435-
query_builder = CosmosDBQueryBuilder().add_sql_ignore_id_condition(
436-
'nomatter'
437-
)
438-
439-
assert len(query_builder.where_conditions) == 1
440-
assert len(query_builder.parameters) == 1
441-
assert query_builder.where_conditions[0].strip() == expected_condition
442-
assert query_builder.parameters[0] == expected_params

time_tracker_api/time_entries/time_entries_query_builder.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def add_sql_interception_with_date_range_condition(
3030
OR (c.end_date BETWEEN @start_date AND @end_date))
3131
OR ((@start_date BETWEEN c.start_date AND c.end_date)
3232
OR (@end_date BETWEEN c.start_date AND c.end_date)))
33+
AND c.start_date!= @end_date
34+
AND c.end_date!= @start_date
3335
"""
3436
self.where_conditions.append(condition)
3537
self.parameters.extend(
@@ -39,3 +41,14 @@ def add_sql_interception_with_date_range_condition(
3941
]
4042
)
4143
return self
44+
45+
def add_sql_is_running_time_entry_condition(self):
46+
condition = "(NOT IS_DEFINED(c.end_date) OR c.end_date = null)"
47+
self.where_conditions.append(condition)
48+
return self
49+
50+
def add_sql_ignore_id_condition(self, id: str = None):
51+
if id:
52+
self.where_conditions.append("c.id!=@ignore_id")
53+
self.parameters.append({'name': '@ignore_id', 'value': id})
54+
return self

time_tracker_api/time_entries/time_entries_repository.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -242,25 +242,11 @@ def find_interception_with_date_range(
242242
}
243243
end_date = end_date or current_datetime_str()
244244

245-
not_equal_conditions = [
246-
{
247-
"field_name": "start_date",
248-
"compare_field_name": "end_date",
249-
"compare_field_value": end_date,
250-
},
251-
{
252-
"field_name": "end_date",
253-
"compare_field_name": "start_date",
254-
"compare_field_value": start_date,
255-
},
256-
]
257-
258245
query_builder = (
259246
TimeEntryQueryBuilder()
260247
.add_sql_interception_with_date_range_condition(
261248
start_date, end_date
262249
)
263-
.add_sql_where_not_equal_condition(not_equal_conditions)
264250
.add_sql_where_equal_condition(conditions)
265251
.add_sql_ignore_id_condition(ignore_id)
266252
.add_sql_visibility_condition(visible_only)
@@ -290,7 +276,7 @@ def find_running(
290276

291277
query_builder = (
292278
TimeEntryQueryBuilder()
293-
.add_sql_non_existent_attribute_condition('end_date')
279+
.add_sql_is_running_time_entry_condition()
294280
.add_sql_where_equal_condition(conditions)
295281
.add_sql_visibility_condition(True)
296282
.add_sql_offset_condition(0)

utils/query_builder.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,6 @@ def add_sql_where_equal_condition(self, data: dict = None):
4242
self.parameters.append({'name': f'@{k}', 'value': v})
4343
return self
4444

45-
def add_sql_where_not_equal_condition(self, conditions: list = None):
46-
if conditions:
47-
for condition_values in conditions:
48-
field_name = condition_values['field_name']
49-
compare_field_name = condition_values['compare_field_name']
50-
compare_field_value = condition_values['compare_field_value']
51-
condition = f"c.{field_name} != @{compare_field_name}"
52-
self.where_conditions.append(condition)
53-
parameter = {
54-
'name': f'@{compare_field_name}',
55-
'value': compare_field_value,
56-
}
57-
if parameter not in self.parameters:
58-
self.parameters.append(parameter)
59-
return self
60-
6145
def add_sql_visibility_condition(self, visible_only: bool):
6246
if visible_only:
6347
self.where_conditions.append('NOT IS_DEFINED(c.deleted)')
@@ -85,19 +69,6 @@ def add_sql_not_in_condition(
8569
self.where_conditions.append(f"c.{attribute} NOT IN {ids_values}")
8670
return self
8771

88-
def add_sql_non_existent_attribute_condition(self, attribute: str):
89-
condition = f"""
90-
(NOT IS_DEFINED(c.{attribute}) OR c.{attribute} = null)
91-
"""
92-
self.where_conditions.append(condition)
93-
return self
94-
95-
def add_sql_ignore_id_condition(self, id: str = None):
96-
if id:
97-
self.where_conditions.append("c.id!=@ignore_id")
98-
self.parameters.append({'name': '@ignore_id', 'value': id})
99-
return self
100-
10172
def __build_select(self):
10273
if len(self.select_conditions) < 1:
10374
self.select_conditions.append("*")

0 commit comments

Comments
 (0)