|
3 | 3 | # pylint: disable=unused-variable |
4 | 4 | # pylint: disable=too-many-arguments |
5 | 5 |
|
6 | | - |
7 | 6 | import pytest |
8 | | -import sqlalchemy as sa |
9 | | -from servicelib.common_aiopg_utils import DataSourceName, create_pg_engine |
10 | | -from simcore_service_webserver.constants import APP_AIOPG_ENGINE_KEY |
11 | | -from simcore_service_webserver.groups._classifiers_service import ( |
| 7 | +from pytest_simcore.helpers.faker_factories import random_group_classifier |
| 8 | +from pytest_simcore.helpers.postgres_tools import insert_and_get_row_lifespan |
| 9 | +from simcore_postgres_database.models.classifiers import group_classifiers |
| 10 | +from simcore_service_webserver.groups._classifiers_repository import ( |
12 | 11 | GroupClassifierRepository, |
13 | 12 | ) |
14 | | -from sqlalchemy.sql import text |
| 13 | +from sqlalchemy.ext.asyncio import AsyncEngine |
15 | 14 |
|
16 | 15 |
|
17 | 16 | @pytest.fixture |
18 | | -def inject_tables(postgres_db: sa.engine.Engine): |
19 | | - stmt = text( |
20 | | - """\ |
21 | | - INSERT INTO "group_classifiers" ("id", "bundle", "created", "modified", "gid", "uses_scicrunch") VALUES |
22 | | - (2, '{"vcs_ref": "asdfasdf", "vcs_url": "https://foo.classifiers.git", "build_date": "2021-01-20T15:19:30Z", "classifiers": {"project::dak": {"url": null, "logo": null, "aliases": [], "related": [], "markdown": "", "released": null, "classifier": "project::dak", "created_by": "Nicolas Chavannes", "github_url": null, "display_name": "DAK", "wikipedia_url": null, "short_description": null}, "organization::zmt": {"url": "https://zmt.swiss/", "logo": null, "aliases": ["Zurich MedTech AG"], "related": [], "markdown": "Zurich MedTech AG (ZMT) offers tools and best practices for targeted life sciences applications to simulate, analyze, and predict complex and dynamic biological processes and interactions. ZMT is a member of Zurich43", "released": null, "classifier": "organization::zmt", "created_by": "crespo", "github_url": null, "display_name": "ZMT", "wikipedia_url": null, "short_description": "ZMT is a member of Zurich43"}}, "collections": {"jupyterlab-math": {"items": ["crespo/osparc-demo"], "markdown": "Curated collection of repositories with examples of notebooks to run in jupyter-python-octave-math service", "created_by": "crespo", "display_name": "jupyterlab-math"}}}', '2021-03-04 23:17:43.373258', '2021-03-04 23:17:43.373258', 1, '0'); |
23 | | - """ |
| 17 | +async def group_classifier_in_db(asyncpg_engine: AsyncEngine): |
| 18 | + """Pre-populate group_classifiers table with test data.""" |
| 19 | + data = random_group_classifier( |
| 20 | + gid=1, |
24 | 21 | ) |
25 | | - with postgres_db.connect() as conn: |
26 | | - conn.execute(stmt) |
| 22 | + |
| 23 | + async with insert_and_get_row_lifespan( |
| 24 | + asyncpg_engine, |
| 25 | + table=group_classifiers, |
| 26 | + values=data, |
| 27 | + pk_col=group_classifiers.c.id, |
| 28 | + pk_value=data.get("id"), |
| 29 | + ) as row: |
| 30 | + yield row |
27 | 31 |
|
28 | 32 |
|
29 | 33 | @pytest.fixture |
30 | | -async def app(postgres_dsn: dict, inject_tables): |
31 | | - dsn = DataSourceName( |
32 | | - user=postgres_dsn["user"], |
33 | | - password=postgres_dsn["password"], |
34 | | - database=postgres_dsn["database"], |
35 | | - host=postgres_dsn["host"], |
36 | | - port=postgres_dsn["port"], |
| 34 | +def group_classifier_repository( |
| 35 | + asyncpg_engine: AsyncEngine, |
| 36 | +) -> GroupClassifierRepository: |
| 37 | + """Create GroupClassifierRepository instance.""" |
| 38 | + return GroupClassifierRepository(engine=asyncpg_engine) |
| 39 | + |
| 40 | + |
| 41 | +async def test_get_classifiers_from_bundle_returns_bundle( |
| 42 | + group_classifier_repository: GroupClassifierRepository, |
| 43 | + group_classifier_in_db: dict, |
| 44 | +): |
| 45 | + """Test get_classifiers_from_bundle returns the stored bundle.""" |
| 46 | + # Act |
| 47 | + bundle = await group_classifier_repository.get_classifiers_from_bundle( |
| 48 | + gid=group_classifier_in_db["gid"] |
37 | 49 | ) |
38 | 50 |
|
39 | | - async with create_pg_engine(dsn) as engine: |
40 | | - fake_app = {APP_AIOPG_ENGINE_KEY: engine} |
41 | | - yield fake_app |
| 51 | + # Assert |
| 52 | + assert bundle is not None |
| 53 | + assert bundle["vcs_ref"] == "asdfasdf" |
| 54 | + assert bundle["vcs_url"] == "https://foo.classifiers.git" |
| 55 | + assert "classifiers" in bundle |
| 56 | + assert "project::dak" in bundle["classifiers"] |
| 57 | + assert bundle["classifiers"]["project::dak"]["display_name"] == "DAK" |
| 58 | + |
| 59 | + |
| 60 | +async def test_group_uses_scicrunch_returns_false( |
| 61 | + group_classifier_repository: GroupClassifierRepository, |
| 62 | + group_classifier_in_db: dict, |
| 63 | +): |
| 64 | + """Test group_uses_scicrunch returns False for non-scicrunch group.""" |
| 65 | + # Act |
| 66 | + uses_scicrunch = await group_classifier_repository.group_uses_scicrunch( |
| 67 | + gid=group_classifier_in_db["gid"] |
| 68 | + ) |
| 69 | + |
| 70 | + # Assert |
| 71 | + assert uses_scicrunch is False |
| 72 | + |
42 | 73 |
|
| 74 | +async def test_get_classifiers_from_bundle_returns_none_for_missing_gid( |
| 75 | + group_classifier_repository: GroupClassifierRepository, |
| 76 | +): |
| 77 | + """Test get_classifiers_from_bundle returns None for non-existent gid.""" |
| 78 | + # Act |
| 79 | + bundle = await group_classifier_repository.get_classifiers_from_bundle(gid=999999) |
43 | 80 |
|
44 | | -async def test_classfiers_from_bundle(app): |
45 | | - repo = GroupClassifierRepository.create_from_app(app) |
| 81 | + # Assert |
| 82 | + assert bundle is None |
46 | 83 |
|
47 | | - assert not await repo.group_uses_scicrunch(gid=1) |
48 | 84 |
|
49 | | - bundle = await repo.get_classifiers_from_bundle(gid=1) |
50 | | - assert bundle |
| 85 | +async def test_group_uses_scicrunch_returns_false_for_missing_gid( |
| 86 | + group_classifier_repository: GroupClassifierRepository, |
| 87 | +): |
| 88 | + """Test group_uses_scicrunch returns False for non-existent gid.""" |
| 89 | + # Act |
| 90 | + uses_scicrunch = await group_classifier_repository.group_uses_scicrunch(gid=999999) |
51 | 91 |
|
52 | | - # Prunes extras and excludes unset and nones |
53 | | - assert bundle["classifiers"]["project::dak"] == { |
54 | | - "classifier": "project::dak", |
55 | | - "display_name": "DAK", |
56 | | - } |
| 92 | + # Assert |
| 93 | + assert uses_scicrunch is False |
0 commit comments