MCPcopy Index your code
hub / github.com/MagicStack/asyncpg / test_transaction_nested

Method test_transaction_nested

tests/test_transaction.py:47–111  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

45 ''')
46
47 async def test_transaction_nested(self):
48 self.assertIsNone(self.con._top_xact)
49 self.assertFalse(self.con.is_in_transaction())
50
51 tr = self.con.transaction()
52
53 self.assertIsNone(self.con._top_xact)
54 self.assertFalse(self.con.is_in_transaction())
55
56 with self.assertRaises(ZeroDivisionError):
57 async with tr:
58 self.assertIs(self.con._top_xact, tr)
59 self.assertTrue(self.con.is_in_transaction())
60
61 await self.con.execute('''
62 CREATE TABLE mytab (a int);
63 ''')
64
65 async with self.con.transaction():
66 self.assertIs(self.con._top_xact, tr)
67 self.assertTrue(self.con.is_in_transaction())
68
69 await self.con.execute('''
70 INSERT INTO mytab (a) VALUES (1), (2);
71 ''')
72
73 self.assertIs(self.con._top_xact, tr)
74 self.assertTrue(self.con.is_in_transaction())
75
76 with self.assertRaises(ZeroDivisionError):
77 in_tr = self.con.transaction()
78 async with in_tr:
79
80 self.assertIs(self.con._top_xact, tr)
81 self.assertTrue(self.con.is_in_transaction())
82
83 await self.con.execute('''
84 INSERT INTO mytab (a) VALUES (3), (4);
85 ''')
86
87 1 / 0
88
89 st = await self.con.prepare('SELECT * FROM mytab;')
90
91 recs = []
92 async for rec in st.cursor():
93 recs.append(rec)
94
95 self.assertEqual(len(recs), 2)
96 self.assertEqual(recs[0][0], 1)
97 self.assertEqual(recs[1][0], 2)
98
99 self.assertIs(self.con._top_xact, tr)
100 self.assertTrue(self.con.is_in_transaction())
101
102 1 / 0
103
104 self.assertIs(self.con._top_xact, None)

Callers

nothing calls this directly

Calls 5

is_in_transactionMethod · 0.80
transactionMethod · 0.80
prepareMethod · 0.80
executeMethod · 0.45
cursorMethod · 0.45

Tested by

no test coverage detected