|
19 | 19 | from debug_toolbar import settings as dt_settings |
20 | 20 | from debug_toolbar.models import HistoryEntry |
21 | 21 | from debug_toolbar.panels.sql import SQLPanel, tracking |
| 22 | +from debug_toolbar.panels.sql.utils import reformat_sql |
22 | 23 |
|
23 | 24 | try: |
24 | 25 | import psycopg |
@@ -870,48 +871,42 @@ def test_explain_with_union(self): |
870 | 871 | self.assertTrue(query["is_select"]) |
871 | 872 |
|
872 | 873 | @override_settings( |
873 | | - DEBUG_TOOLBAR_CONFIG={"SQL_PRETTIFY_MAX_LENGTH": 100, "PRETTIFY_SQL": True} |
| 874 | + DEBUG_TOOLBAR_CONFIG={"PRETTIFY_SQL_MAX_LENGTH": 100, "PRETTIFY_SQL": True} |
874 | 875 | ) |
875 | | - def test_sql_prettify_max_length(self): |
| 876 | + def test_prettify_sql_max_length(self): |
876 | 877 | """ |
877 | 878 | Test that SQL formatting is skipped for queries exceeding the max length threshold. |
878 | 879 | """ |
879 | | - from debug_toolbar.panels.sql.utils import reformat_sql |
880 | | - |
881 | | - # Short SQL should be formatted normally |
| 880 | + # Short SQL should be formatted normally (includes <strong> tags for keywords) |
882 | 881 | short_sql = "SELECT * FROM auth_user WHERE id = 1" |
883 | 882 | result = reformat_sql(short_sql, with_toggle=True) |
884 | | - self.assertNotIn("SQL formatting skipped", result) |
885 | | - self.assertIn("SELECT", result) |
| 883 | + self.assertIn("<strong>", result) |
886 | 884 |
|
887 | | - # Long SQL should skip formatting and show a message |
| 885 | + # Long SQL should skip formatting (no <strong> tags, just escaped SQL) |
888 | 886 | long_sql = ( |
889 | 887 | "SELECT * FROM auth_user WHERE id IN (" |
890 | 888 | + ", ".join([f"'{i}'" for i in range(100)]) |
891 | 889 | + ")" |
892 | 890 | ) |
893 | 891 | result = reformat_sql(long_sql, with_toggle=True) |
894 | | - self.assertIn("SQL formatting skipped", result) |
895 | | - self.assertIn("exceeds threshold", result) |
| 892 | + self.assertNotIn("<strong>", result) |
| 893 | + self.assertIn("SELECT", result) |
896 | 894 |
|
897 | | - def test_sql_prettify_max_length_disabled(self): |
| 895 | + @override_settings( |
| 896 | + DEBUG_TOOLBAR_CONFIG={"PRETTIFY_SQL_MAX_LENGTH": 0, "PRETTIFY_SQL": True} |
| 897 | + ) |
| 898 | + def test_prettify_sql_max_length_disabled(self): |
898 | 899 | """ |
899 | | - Test that SQL_PRETTIFY_MAX_LENGTH=0 or None disables the length check. |
| 900 | + Test that PRETTIFY_SQL_MAX_LENGTH=0 disables the length check. |
900 | 901 | """ |
901 | | - from debug_toolbar.panels.sql.utils import reformat_sql |
902 | | - |
903 | 902 | long_sql = ( |
904 | 903 | "SELECT * FROM auth_user WHERE id IN (" |
905 | 904 | + ", ".join([f"'{i}'" for i in range(100)]) |
906 | 905 | + ")" |
907 | 906 | ) |
908 | | - |
909 | | - with override_settings( |
910 | | - DEBUG_TOOLBAR_CONFIG={"SQL_PRETTIFY_MAX_LENGTH": 0, "PRETTIFY_SQL": True} |
911 | | - ): |
912 | | - result = reformat_sql(long_sql, with_toggle=True) |
913 | | - # When max_length is 0 (falsy), formatting should not be skipped |
914 | | - self.assertNotIn("SQL formatting skipped", result) |
| 907 | + result = reformat_sql(long_sql, with_toggle=True) |
| 908 | + # When max_length is 0 (falsy), formatting should not be skipped |
| 909 | + self.assertIn("<strong>", result) |
915 | 910 |
|
916 | 911 |
|
917 | 912 | class SQLPanelMultiDBTestCase(BaseMultiDBTestCase): |
|
0 commit comments