Skip to content

Commit

Permalink
Bugfix AssertQueries.get_table_name() for M2M update query
Browse files Browse the repository at this point in the history
a M2M `set()` will result in e.g.: `INSERT OR IGNORE INTO...` and `f'Error parsing: {sql!r}'` will
be raised. Add support this, too.
  • Loading branch information
Jens Diemer committed Aug 22, 2023
1 parent 4911335 commit 5d163be
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bx_django_utils/test_utils/assert_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def get_table_name(query):
# transaction statements
return

table_names = re.findall(r'(FROM|INSERT INTO|UPDATE) \"(.+?)\"', sql)
table_names = re.findall(r'(FROM|INSERT[A-Z ]+?INTO|UPDATE) \"(.+?)\"', sql)
assert len(table_names) >= 1, f'Error parsing: {sql!r}'
# Use only the first table name (e.g.: ignore names in inner join)
table_name = table_names[0][1]
Expand Down
14 changes: 14 additions & 0 deletions bx_django_utils_tests/tests/test_assert_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ def test_assert_queries(self):
similar=True,
)

def test_get_table_name(self):
self.assertEqual(
AssertQueries.get_table_name(
query={'sql': 'INSERT INTO "table_name1" ("foo_id", "bar_id") VALUES (1, 2)'}
),
'table_name1',
)
self.assertEqual(
AssertQueries.get_table_name(
query={'sql': 'INSERT OR IGNORE INTO "table_name2" ("foo_id", "bar_id") VALUES (1, 2)'}
),
'table_name2',
)

def test_assert_table_names(self):
queries = self.get_instance()
queries.assert_table_names('auth_permission')
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = 'bx_django_utils'
version = "63"
version = "64"
description = 'Various Django utility functions'
homepage = "https://github.com/boxine/bx_django_utils/"
authors = [
Expand Down

0 comments on commit 5d163be

Please sign in to comment.