forked from piccolo-orm/piccolo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_nested_transaction.py
More file actions
73 lines (53 loc) · 2.11 KB
/
test_nested_transaction.py
File metadata and controls
73 lines (53 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import asyncio
from unittest import TestCase
from piccolo.columns.column_types import Varchar
from piccolo.engine.exceptions import TransactionError
from piccolo.engine.sqlite import SQLiteEngine
from piccolo.table import Table
from ..base import sqlite_only, DBTestCase
from ..example_app.tables import Manager
ENGINE_1 = SQLiteEngine(path="engine1.sqlite")
ENGINE_2 = SQLiteEngine(path="engine2.sqlite")
class Musician(Table, db=ENGINE_1):
name = Varchar(length=100)
class Roadie(Table, db=ENGINE_2):
name = Varchar(length=100)
@sqlite_only
class TestDifferentDB(TestCase):
def setUp(self):
ENGINE_1.remove_db_file()
ENGINE_2.remove_db_file()
def tearDown(self):
ENGINE_1.remove_db_file()
ENGINE_2.remove_db_file()
async def run_nested(self):
"""
Make sure nested transactions which reference different databases work
as expected.
"""
async with Musician._meta.db.transaction():
await Musician.create_table().run()
await Musician(name="Bob").save().run()
async with Roadie._meta.db.transaction():
await Roadie.create_table().run()
await Roadie(name="Dave").save().run()
self.assertTrue(await Musician.table_exists().run())
musician = await Musician.select("name").first().run()
self.assertTrue(musician["name"] == "Bob")
self.assertTrue(await Roadie.table_exists().run())
roadie = await Roadie.select("name").first().run()
self.assertTrue(roadie["name"] == "Dave")
def test_nested(self):
asyncio.run(self.run_nested())
class TestSameDB(DBTestCase):
async def run_nested(self):
"""
Nested transactions currently aren't permitted in a connection.
"""
with self.assertRaises(TransactionError):
async with Manager._meta.db.transaction():
await Manager(name="Bob").save().run()
async with Manager._meta.db.transaction():
await Manager(name="Dave").save().run()
def test_nested(self):
asyncio.run(self.run_nested())