From 4f4740dea3708683136334d82b3b866ae2aff743 Mon Sep 17 00:00:00 2001 From: Dan McKinley Date: Fri, 19 Jul 2024 17:05:20 -0700 Subject: [PATCH] multi-row inserts are always passed as a single list argument --- pugsql/compiler.py | 5 ++++- tests/test_postgres.py | 8 ++++++++ tests/test_pugsql.py | 5 +++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/pugsql/compiler.py b/pugsql/compiler.py index ceeb00c..7d5f35d 100644 --- a/pugsql/compiler.py +++ b/pugsql/compiler.py @@ -135,7 +135,10 @@ def transaction(self): def _execute(self, clause, *multiparams, **params): if getattr(self._locals, 'session', None): - return self._locals.session.execute(clause, multiparams or params) + if multiparams: + return self._locals.session.execute(clause, *multiparams) + else: + return self._locals.session.execute(clause, params) if not self.engine: raise NoConnectionError() diff --git a/tests/test_postgres.py b/tests/test_postgres.py index 6a53733..68a440d 100644 --- a/tests/test_postgres.py +++ b/tests/test_postgres.py @@ -13,6 +13,14 @@ def setUp(self): self.fixtures.connect(f'postgresql+pg8000://{user_pass}@127.0.0.1') self.fixtures.setup() + def test_multi_upsert_in_transaction(self): + with self.fixtures.transaction(): + self.fixtures.multi_upsert([ + { 'id': 65, 'foo': 'yyy' }, + { 'id': 99, 'foo': '99999' }]) + + self.assertEqual('yyy', self.fixtures.get_foo(id=65)) + def test_multi_upsert(self): self.fixtures.multi_upsert([ { 'id': 1, 'foo': 'abcd' }, diff --git a/tests/test_pugsql.py b/tests/test_pugsql.py index f4b7642..8bf6f7b 100644 --- a/tests/test_pugsql.py +++ b/tests/test_pugsql.py @@ -64,11 +64,12 @@ def test_where_in_multiple_parameters(self): def test_multi_insert(self): with self.fixtures.transaction() as t: - self.fixtures.insert_user( + self.fixtures.insert_user([ { 'username': 'joe' }, { 'username': 'paul' }, { 'username': 'topper' }, - { 'username': 'mick' }) + { 'username': 'mick' } + ]) sr = list(self.fixtures.search_users(username='topper')) self.assertEqual('topper', sr[0]['username'])