diff --git a/tests/sql/postgres/upsert_foo.sql b/tests/sql/postgres/upsert_foo.sql new file mode 100644 index 0000000..836c1d0 --- /dev/null +++ b/tests/sql/postgres/upsert_foo.sql @@ -0,0 +1,4 @@ +-- :name upsert_foo :affected +insert into test.test (id, foo) +values (:id, :foo) +on conflict (id) do update set foo = excluded.foo diff --git a/tests/test_postgres.py b/tests/test_postgres.py index 71a66e3..6a53733 100644 --- a/tests/test_postgres.py +++ b/tests/test_postgres.py @@ -36,3 +36,27 @@ def test_where_in(self): ids = [r['id'] for r in self.fixtures.where_in(foo=('abcd', '99999',))] self.assertEqual({ 1, 2 }, set(ids)) + + def test_transaction(self): + with self.fixtures.transaction(): + self.fixtures.upsert_foo(id=1, foo='abcd') + self.assertEqual('abcd', self.fixtures.get_foo(id=1)) + + def test_nested_transactions(self): + with self.fixtures.transaction(): + self.fixtures.upsert_foo(id=1, foo='abcd') + + with self.fixtures.transaction(): + self.fixtures.upsert_foo(id=1, foo='bar') + + self.assertEqual('bar', self.fixtures.get_foo(id=1)) + + def test_rolling_back_nested_transactions(self): + with self.fixtures.transaction(): + self.fixtures.upsert_foo(id=1, foo='abcd') + + with self.fixtures.transaction() as t: + self.fixtures.upsert_foo(id=1, foo='bar') + t.rollback() + + self.assertEqual('abcd', self.fixtures.get_foo(id=1))