|
1 | 1 | """ Tests for the configure-tables command. """ |
2 | | -from collections.abc import MutableMapping |
| 2 | +import re |
| 3 | +from collections.abc import MutableMapping, Sized |
3 | 4 | from typing import Any |
4 | 5 |
|
5 | 6 | from sqlalchemy import select |
@@ -452,7 +453,7 @@ def test_sanity_checks_errors_only(self) -> None: |
452 | 453 |
|
453 | 454 |
|
454 | 455 | class TrickyTests(ConfigureTablesTests): |
455 | | - """Testing configure-tables with the instrument.sql database.""" |
| 456 | + """Testing configure-tables with arguments that could cause SQL errors.""" |
456 | 457 |
|
457 | 458 | dump_file_path = "tricky.sql" |
458 | 459 | database_name = "tricky" |
@@ -536,11 +537,28 @@ def test_repeated_field_does_not_throw_exception(self) -> None: |
536 | 537 | "Failed to display", "/".join(m for (m, _a, _kw) in tc.messages) |
537 | 538 | ) |
538 | 539 |
|
| 540 | + def assert_message_correctly_formatted( |
| 541 | + self, m: str, a: Sized, kw: dict[str, Any] |
| 542 | + ) -> None: |
| 543 | + """ |
| 544 | + Assert that the ``{...}`` interpolations in ``m`` match the arguments given. |
| 545 | +
|
| 546 | + :param m: The message. |
| 547 | + :param a: List of positional arguments. |
| 548 | + :param kw: Dict of keyword arguments. |
| 549 | + """ |
| 550 | + interpolations = set(re.findall(r"\{([A-Za-z0-9_]+)\}", m)) |
| 551 | + positions = set(range(len(a))) |
| 552 | + keywords = set(kw.keys()) |
| 553 | + self.assertSetEqual(interpolations, positions | keywords) |
| 554 | + |
539 | 555 | def test_sql_error_does_not_throw_exception(self) -> None: |
540 | 556 | """ |
541 | 557 | Select with a SQL error. |
542 | 558 | """ |
543 | 559 | with self._get_cmd({}) as tc: |
544 | 560 | tc.reset() |
545 | 561 | tc.do_select("+++") |
546 | | - self.assertIn("SQL query", "/".join(m for (m, a, kw) in tc.messages)) |
| 562 | + self.assertIn("SQL query", "/".join(m for (m, _a, _kw) in tc.messages)) |
| 563 | + for m, a, kw in tc.messages: |
| 564 | + self.assert_message_correctly_formatted(m, a, kw) |
0 commit comments