|
6 | 6 | from pathlib import Path |
7 | 7 |
|
8 | 8 | import aiopg.sa |
| 9 | +import asyncpg |
| 10 | +import sqlalchemy as sa |
9 | 11 | import yaml |
10 | 12 | from aiohttp.test_utils import TestServer |
11 | 13 | from simcore_service_webserver.application_settings import ( |
|
18 | 20 | is_service_enabled, |
19 | 21 | is_service_responsive, |
20 | 22 | ) |
| 23 | +from simcore_service_webserver.login.storage import AsyncpgStorage, get_plugin_storage |
21 | 24 | from sqlalchemy.ext.asyncio import AsyncEngine |
22 | 25 |
|
23 | 26 |
|
24 | | -def test_engines_in_app_state(web_server: TestServer): |
| 27 | +async def test_all_pg_engines_in_app(web_server: TestServer): |
25 | 28 | app = web_server.app |
26 | 29 | assert app |
27 | | - settings: ApplicationSettings = get_application_settings(app) |
28 | 30 |
|
29 | | - # same as host |
| 31 | + settings: ApplicationSettings = get_application_settings(app) |
30 | 32 | assert settings.WEBSERVER_DB |
31 | 33 | assert settings.WEBSERVER_DB.POSTGRES_CLIENT_NAME |
32 | 34 |
|
33 | | - engine: AsyncEngine = get_asyncpg_engine(app) |
34 | | - assert engine |
35 | | - assert isinstance(engine, AsyncEngine) |
36 | | - |
| 35 | + # (1) aiopg engine (deprecated) |
37 | 36 | aiopg_engine = get_aiopg_engine(app) |
38 | 37 | assert aiopg_engine |
39 | 38 | assert isinstance(aiopg_engine, aiopg.sa.Engine) |
40 | 39 |
|
| 40 | + # (2) asyncpg engine via sqlalchemy.ext.asyncio (new) |
| 41 | + asyncpg_engine: AsyncEngine = get_asyncpg_engine(app) |
| 42 | + assert asyncpg_engine |
| 43 | + assert isinstance(asyncpg_engine, AsyncEngine) |
| 44 | + |
| 45 | + # (3) low-level asyncpg Pool (deprecated) |
| 46 | + # Will be replaced by (2) |
| 47 | + login_storage: AsyncpgStorage = get_plugin_storage(app) |
| 48 | + assert login_storage.pool |
| 49 | + assert isinstance(login_storage.pool, asyncpg.Pool) |
| 50 | + |
| 51 | + # they ALL point to the SAME database |
| 52 | + assert aiopg_engine.dsn |
| 53 | + assert asyncpg_engine.url |
| 54 | + |
| 55 | + query = sa.text('SELECT "version_num" FROM "alembic_version"') |
| 56 | + async with login_storage.pool.acquire() as conn: |
| 57 | + result_pool = await conn.fetchval(str(query)) |
| 58 | + |
| 59 | + async with asyncpg_engine.connect() as conn: |
| 60 | + result_asyncpg = (await conn.execute(query)).scalar_one_or_none() |
| 61 | + |
| 62 | + async with aiopg_engine.acquire() as conn: |
| 63 | + result_aiopg = await (await conn.execute(query)).scalar() |
| 64 | + |
| 65 | + assert result_pool == result_asyncpg |
| 66 | + assert result_pool == result_aiopg |
| 67 | + |
41 | 68 |
|
42 | 69 | def test_uses_same_postgres_version( |
43 | 70 | docker_compose_file: Path, osparc_simcore_root_dir: Path |
|
0 commit comments