Skip to content

Commit 99c7bb6

Browse files
committed
✨ [Tests] Add random_group_classifier factory and related tests for GroupClassifierRepository
1 parent 35c6278 commit 99c7bb6

File tree

2 files changed

+133
-34
lines changed

2 files changed

+133
-34
lines changed

packages/pytest-simcore/src/pytest_simcore/helpers/faker_factories.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,65 @@ def random_service_consume_filetype(
629629

630630
data.update(overrides)
631631
return data
632+
633+
634+
def random_group_classifier(
635+
*,
636+
gid: int,
637+
fake: Faker = DEFAULT_FAKER,
638+
**overrides,
639+
) -> dict[str, Any]:
640+
from simcore_postgres_database.models.classifiers import group_classifiers
641+
642+
data = {
643+
"gid": gid,
644+
"bundle": {
645+
"vcs_ref": "asdfasdf",
646+
"vcs_url": "https://foo.classifiers.git",
647+
"build_date": "2021-01-20T15:19:30Z",
648+
"classifiers": {
649+
"project::dak": {
650+
"url": None,
651+
"logo": None,
652+
"aliases": [],
653+
"related": [],
654+
"markdown": "",
655+
"released": None,
656+
"classifier": "project::dak",
657+
"created_by": "Nicolas Chavannes",
658+
"github_url": None,
659+
"display_name": "DAK",
660+
"wikipedia_url": None,
661+
"short_description": None,
662+
},
663+
"organization::zmt": {
664+
"url": "https://zmt.swiss/",
665+
"logo": None,
666+
"aliases": ["Zurich MedTech AG"],
667+
"related": [],
668+
"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",
669+
"released": None,
670+
"classifier": "organization::zmt",
671+
"created_by": "crespo",
672+
"github_url": None,
673+
"display_name": "ZMT",
674+
"wikipedia_url": None,
675+
"short_description": "ZMT is a member of Zurich43",
676+
},
677+
},
678+
"collections": {
679+
"jupyterlab-math": {
680+
"items": ["crespo/osparc-demo"],
681+
"markdown": "Curated collection of repositories with examples of notebooks to run in jupyter-python-octave-math service",
682+
"created_by": "crespo",
683+
"display_name": "jupyterlab-math",
684+
}
685+
},
686+
},
687+
"uses_scicrunch": False,
688+
}
689+
690+
assert set(data.keys()).issubset({c.name for c in group_classifiers.columns})
691+
692+
data.update(overrides)
693+
return data

services/web/server/tests/unit/with_dbs/01/test_groups_classifiers_repository.py

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,91 @@
33
# pylint: disable=unused-variable
44
# pylint: disable=too-many-arguments
55

6-
76
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 (
1211
GroupClassifierRepository,
1312
)
14-
from sqlalchemy.sql import text
13+
from sqlalchemy.ext.asyncio import AsyncEngine
1514

1615

1716
@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,
2421
)
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
2731

2832

2933
@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"]
3749
)
3850

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+
4273

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)
4380

44-
async def test_classfiers_from_bundle(app):
45-
repo = GroupClassifierRepository.create_from_app(app)
81+
# Assert
82+
assert bundle is None
4683

47-
assert not await repo.group_uses_scicrunch(gid=1)
4884

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)
5191

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

Comments
 (0)