MCPcopy
hub / github.com/tortoise/tortoise-orm / test_schema

Function test_schema

tests/backends/test_postgres.py:32–75  ·  view source on GitHub ↗
(db_simple)

Source from the content-addressed store, hash-verified

30
31@pytest.mark.asyncio
32async def test_schema(db_simple):
33 db_config, is_asyncpg, is_psycopg = _get_db_config()
34 if not is_asyncpg and not is_psycopg:
35 pytest.skip("PostgreSQL only")
36
37 if is_asyncpg:
38 from asyncpg.exceptions import InvalidSchemaNameError
39 else:
40 from psycopg.errors import InvalidSchemaName as InvalidSchemaNameError
41
42 if Tortoise._inited:
43 await Tortoise._drop_databases()
44
45 try:
46 db_config["connections"]["models"]["credentials"]["schema"] = "mytestschema"
47 await Tortoise.init(db_config, _create_db=True)
48
49 with pytest.raises(InvalidSchemaNameError):
50 await Tortoise.generate_schemas()
51
52 conn = connections.get("models")
53 await conn.execute_script("CREATE SCHEMA mytestschema;")
54 await Tortoise.generate_schemas()
55
56 tournament = await Tournament.create(name="Test")
57 await connections.close_all()
58
59 del db_config["connections"]["models"]["credentials"]["schema"]
60 await Tortoise.init(db_config)
61
62 with pytest.raises(OperationalError):
63 await Tournament.filter(name="Test").first()
64
65 conn = connections.get("models")
66 _, res = await conn.execute_query(
67 "SELECT id, name FROM mytestschema.tournament WHERE name='Test' LIMIT 1"
68 )
69
70 assert len(res) == 1
71 assert tournament.id == res[0]["id"]
72 assert tournament.name == res[0]["name"]
73 finally:
74 if Tortoise._inited:
75 await Tortoise._drop_databases()
76
77
78@pytest.mark.asyncio

Callers

nothing calls this directly

Calls 11

_drop_databasesMethod · 0.80
close_allMethod · 0.80
_get_db_configFunction · 0.70
initMethod · 0.45
generate_schemasMethod · 0.45
getMethod · 0.45
execute_scriptMethod · 0.45
createMethod · 0.45
firstMethod · 0.45
filterMethod · 0.45
execute_queryMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…