diff --git a/arkindex/project/tests/__init__.py b/arkindex/project/tests/__init__.py index 85311cace3233e2586264ed7d64cd1986cb55ac6..97e1c50d07979f206f6257d7a2be34636af78fe1 100644 --- a/arkindex/project/tests/__init__.py +++ b/arkindex/project/tests/__init__.py @@ -1,3 +1,4 @@ +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: