(db_simple)
| 30 | |
| 31 | @pytest.mark.asyncio |
| 32 | async 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…