Skip to content
Snippets Groups Projects
Commit cf83bed8 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Bastien Abadie
Browse files

Ignore comments in assertExactQueries

parent bdfeff47
No related branches found
No related tags found
1 merge request!1308Ignore comments in assertExactQueries
import re
from pathlib import Path
from typing import Any, Callable, Iterable, Mapping, Optional, Union
......@@ -29,12 +30,25 @@ class _AssertExactQueriesContext(CaptureQueriesContext):
super().__init__(connection)
def _format(self, query: str) -> str:
return sqlparse.format(
query,
reindent=True,
use_space_around_operators=True,
indent_width=4,
).strip()
"""
Format one or more SQL queries.
Does not allow more than two newlines at once (at most one empty line).
This still allows separating with empty lines in the expected SQL file,
but prevents duplicate empty lines from appearing;
sqlparse.format duplicates an empty line when it encounters one, causing assertion errors.
"""
return re.sub(
r'\n\n+',
'\n\n',
sqlparse.format(
query,
reindent=True,
strip_comments=True,
use_space_around_operators=True,
indent_width=4,
).strip()
)
def __exit__(self, exc_type, exc_value, traceback):
super().__exit__(exc_type, exc_value, traceback)
......@@ -64,7 +78,7 @@ class _AssertExactQueriesContext(CaptureQueriesContext):
)
# Ignore newlines in the expected SQL, otherwise the reformatting might have duplicate line breaks
expected_sql = self.path.read_text().strip().replace('\n', ' ')
expected_sql = self.path.read_text().strip()
# Apply str.format on the SQL when needed to handle variable IDs
if self.params:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment