Skip to content

Commit 9a9134e

Browse files
committed
improved display format
1 parent 807f6af commit 9a9134e

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ warn_return_any = false # [no-any-return]
112112
disable_error_code = [
113113
"operator",
114114
"assignment",
115+
"union-attr",
115116
]
116117

117118

src/capture_db_queries/printers.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,23 @@ class PrinterSql(AbcPrinter):
8787

8888
single_sql_template = (
8989
'Queries count: {queries_count} | '
90-
'Execution time: {execution_time_per_iter:.3f}s | Vendor: {vendor}'
90+
'Execution time: {execution_time_per_iter:.6f}s | Vendor: {vendor}\n'
9191
)
9292
several_sql_template = (
9393
'Tests count: {current_iteration} | '
9494
'Total queries count: {queries_count} | '
95-
'Total execution time: {sum_all_execution_times:.2f}s | '
96-
'Median time one test is: {median_all_execution_times:.3f}s | '
97-
'Vendor: {vendor}'
95+
'Total execution time: {sum_all_execution_times:.5f}s | '
96+
'Median time one test is: {median_all_execution_times:.6f}s | '
97+
'Vendor: {vendor}\n'
9898
)
9999
iteration_sql_template = (
100100
'Test №{current_iteration} | '
101101
'Queries count: {queries_count_per_iter} | '
102-
'Execution time: {execution_time_per_iter:.2f}s'
102+
'Execution time: {execution_time_per_iter:.6f}s'
103103
)
104104
assert_msg_template = '{queries_count} not less than or equal to {assert_q_count} queries'
105105

106-
sql_template = '№[{ordinal_num}] time=[{time:.3f}]{explain}\n{sql}'
106+
sql_template = '№[{ordinal_num}] time=[{time:.6f}]{explain}\n{sql}'
107107

108108
def print_single_sql(self, dto: SinglePrintDTO) -> str:
109109
return self.print_sql(
@@ -125,10 +125,14 @@ def print_several_sql(self, dto: SeveralPrintDTO) -> str:
125125
'median_all_execution_times': dto.median_all_execution_times,
126126
}
127127
)
128+
if self.verbose and not self.advanced_verb and not self.queries:
129+
print('\n')
128130
return self.print_sql(self.several_sql_template, dto.queries_log, **format_kwargs)
129131

130132
def iteration_print(self, dto: IterationPrintDTO) -> None:
131133
if self.advanced_verb:
134+
if dto.current_iteration == 1:
135+
print('\n')
132136
print(
133137
self.iteration_sql_template.format(
134138
current_iteration=dto.current_iteration,
@@ -155,17 +159,18 @@ def format_sql(self, queries_log: QueriesLog) -> QueriesLog:
155159

156160
def filter_queries(self, queries_log: QueriesLog) -> QueriesLog:
157161
if _EXCLUDE:
158-
return deque(
159-
query
160-
for query in queries_log
161-
if query['sql'].upper() not in self.EXCLUDE # type: ignore[union-attr]
162-
)
162+
return deque(query for query in queries_log if query['sql'].upper() not in self.EXCLUDE)
163163
return queries_log
164164

165165
def format_explain(self, queries_log: QueriesLog) -> QueriesLog:
166166
for query in queries_log:
167167
if 'explain' in query:
168-
query['explain'] = f' explain=[{query["explain"]!r}]'
168+
explain_count = len(query['explain'].split('\n'))
169+
if explain_count > 1:
170+
explain = f' explain=[\n{query["explain"]}\n]'
171+
else:
172+
explain = f' explain=[{query["explain"]}]'
173+
query['explain'] = explain
169174
return queries_log
170175

171176
def build_output_string(self, queries_log: QueriesLog) -> str:
@@ -178,4 +183,4 @@ def build_output_string(self, queries_log: QueriesLog) -> str:
178183
)
179184
for ordinal_num, query in enumerate(queries_log, start=1)
180185
)
181-
return f'\n\n\n{formatted_queries}\n\n'
186+
return f'\n\n{formatted_queries}\n\n'

src/capture_db_queries/wrappers.py

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class ExplainExecutionWrapper(BaseExecutionWrapper):
5858
5959
https://docs.djangoproject.com/en/5.1/topics/db/instrumentation/#connection-execute-wrapper
6060
https://docs.djangoproject.com/en/5.1/ref/models/querysets/#explain
61+
from django_extensions.management.debug_cursor import monkey_patch_cursordebugwrapper
6162
"""
6263

6364
# Inspired from

tests/test_decorators.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ def test_capture_queries_loop(self, intercept_output: StringIO) -> None:
269269
output = intercept_output.getvalue()
270270

271271
assert re.match(
272-
f'Tests count: 100 | Total queries count: 200 | Total execution time: {ANYNUM}s | Median time one test is: {ANYNUM}s\n', # noqa: E501
272+
f'\n\nTests count: 100 | Total queries count: 200 | Total execution time: {ANYNUM}s | Median time one test is: {ANYNUM}s\n', # noqa: E501
273273
output,
274274
), 'incorrect output'
275275

@@ -284,7 +284,7 @@ def _() -> None:
284284
output = intercept_output.getvalue()
285285

286286
assert re.match(
287-
f'Tests count: 100 | Total queries count: 200 | Total execution time: {ANYNUM}s | Median time one test is: {ANYNUM}s\n', # noqa: E501
287+
f'\n\nTests count: 100 | Total queries count: 200 | Total execution time: {ANYNUM}s | Median time one test is: {ANYNUM}s\n', # noqa: E501
288288
output,
289289
), 'incorrect output'
290290

tests/test_printers.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def setup_method(self, method: Callable[..., Any]) -> None:
3737
maxlen=9000,
3838
)
3939
self.queries_log_output = (
40-
'\n\n\n№[1] time=[0.094]\nSELECT "tests_reporter"."id",\n '
40+
'\n\n№[1] time=[0.094000]\nSELECT "tests_reporter"."id",\n '
4141
'"tests_reporter"."full_name"\n FROM "tests_reporter"\n '
42-
'WHERE "tests_reporter"."id" = %s\n\n\n№[2] time=[0.109]\nSELECT "tests_article"."id",\n ' # noqa: E501
42+
'WHERE "tests_reporter"."id" = %s\n\n\n№[2] time=[0.109000]\nSELECT "tests_article"."id",\n ' # noqa: E501
4343
'"tests_article"."pub_date",\n '
4444
'"tests_article"."headline",\n '
4545
'"tests_article"."content",\n '
@@ -48,11 +48,11 @@ def setup_method(self, method: Callable[..., Any]) -> None:
4848
)
4949

5050
self.assert_msg = '4 not less than or equal to 2 queries'
51-
self.iter_output = 'Test №1 | Queries count: 2 | Execution time: 0.74s\n'
52-
self.single_output = 'Queries count: 2 | Execution time: 0.743s | Vendor: fake_vendor\n'
51+
self.iter_output = '\n\nTest №1 | Queries count: 2 | Execution time: 0.743167s\n'
52+
self.single_output = 'Queries count: 2 | Execution time: 0.743167s | Vendor: fake_vendor\n\n'
5353
self.several_output = (
54-
'Tests count: 1 | Total queries count: 2 | '
55-
'Total execution time: 1.49s | Median time one test is: 0.743s | Vendor: fake_vendor\n'
54+
'\n\nTests count: 1 | Total queries count: 2 | Total execution time: 1.48633s | '
55+
'Median time one test is: 0.743167s | Vendor: fake_vendor\n\n'
5656
)
5757

5858
self.iter_dto = IterationPrintDTO(

0 commit comments

Comments
 (0)