Skip to content

Commit 03c5f08

Browse files
adding db test
1 parent 28c86ff commit 03c5f08

File tree

8 files changed

+127
-61
lines changed

8 files changed

+127
-61
lines changed

services/web/server/src/simcore_service_webserver/application.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .garbage_collector.plugin import setup_garbage_collector
2727
from .groups.plugin import setup_groups
2828
from .invitations.plugin import setup_invitations
29+
from .licenses.plugin import setup_licenses
2930
from .login.plugin import setup_login
3031
from .long_running_tasks import setup_long_running_tasks
3132
from .meta_modeling.plugin import setup_meta_modeling
@@ -143,6 +144,9 @@ def create_application() -> web.Application:
143144
setup_scicrunch(app)
144145
setup_tags(app)
145146

147+
# licenses
148+
setup_licenses(app)
149+
146150
setup_announcements(app)
147151
setup_publications(app)
148152
setup_studies_dispatcher(app)

services/web/server/src/simcore_service_webserver/application_settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class ApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
271271
WEBSERVER_DB_LISTENER: bool = True
272272
WEBSERVER_FOLDERS: bool = True
273273
WEBSERVER_GROUPS: bool = True
274+
WEBSERVER_LICENSES: bool = True
274275
WEBSERVER_META_MODELING: bool = True
275276
WEBSERVER_NOTIFICATIONS: bool = Field(default=True)
276277
WEBSERVER_PRODUCTS: bool = True

services/web/server/src/simcore_service_webserver/licenses/_license_goods_api.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import logging
44

5-
import _license_goods_db
65
from aiohttp import web
76
from models_library.api_schemas_webserver.license_goods import (
87
LicenseGoodGet,
@@ -14,6 +13,7 @@
1413
from models_library.users import UserID
1514
from pydantic import NonNegativeInt
1615

16+
from . import _license_goods_db
1717
from ._models import LicenseGoodsBodyParams
1818

1919
_logger = logging.getLogger(__name__)
@@ -29,7 +29,14 @@ async def get_license_good(
2929
license_good_db = await _license_goods_db.get(
3030
app, license_good_id=license_good_id, product_name=product_name
3131
)
32-
return LicenseGoodGet.model_construct(**license_good_db.model_dump())
32+
return LicenseGoodGet(
33+
license_good_id=license_good_db.license_good_id,
34+
name=license_good_db.name,
35+
license_resource_type=license_good_db.license_resource_type,
36+
pricing_plan_id=license_good_db.pricing_plan_id,
37+
created_at=license_good_db.created,
38+
modified_at=license_good_db.modified,
39+
)
3340

3441

3542
async def list_license_goods(
@@ -45,7 +52,14 @@ async def list_license_goods(
4552
)
4653
return LicenseGoodGetPage(
4754
items=[
48-
LicenseGoodGet.model_construct(**license_good_db.model_dump())
55+
LicenseGoodGet(
56+
license_good_id=license_good_db.license_good_id,
57+
name=license_good_db.name,
58+
license_resource_type=license_good_db.license_resource_type,
59+
pricing_plan_id=license_good_db.pricing_plan_id,
60+
created_at=license_good_db.created,
61+
modified_at=license_good_db.modified,
62+
)
4963
for license_good_db in license_good_db_list
5064
],
5165
total=total_count,
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
11
# mypy: disable-error-code=truthy-function
2-
from ._license_goods_api import check_user_workspace_access, get_workspace
32

4-
assert get_workspace # nosec
5-
assert check_user_workspace_access # nosec
6-
7-
__all__: tuple[str, ...] = (
8-
"get_workspace",
9-
"check_user_workspace_access",
10-
)
3+
__all__: tuple[str, ...] = ()

services/web/server/src/simcore_service_webserver/licenses/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
@app_module_setup(
1616
__name__,
1717
ModuleCategory.ADDON,
18-
settings_name="WEBSERVER_LICENSE",
18+
settings_name="WEBSERVER_LICENSES",
1919
depends=["simcore_service_webserver.rest"],
2020
logger=_logger,
2121
)
22-
def setup_workspaces(app: web.Application):
23-
assert app[APP_SETTINGS_KEY].WEBSERVER_LICENSE # nosec
22+
def setup_licenses(app: web.Application):
23+
assert app[APP_SETTINGS_KEY].WEBSERVER_LICENSES # nosec
2424

2525
# routes
2626
app.router.add_routes(_license_goods_handlers.routes)
Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
1+
from collections.abc import AsyncIterator
2+
13
# pylint:disable=unused-variable
24
# pylint:disable=unused-argument
35
# pylint:disable=redefined-outer-name
4-
from collections.abc import Iterator
5-
66
import pytest
7-
import sqlalchemy as sa
8-
from simcore_postgres_database.models.projects import projects
9-
from simcore_postgres_database.models.workspaces import workspaces
7+
from aiohttp.test_utils import TestClient
8+
from simcore_postgres_database.models.resource_tracker_pricing_plans import (
9+
resource_tracker_pricing_plans,
10+
)
11+
from simcore_postgres_database.utils_repos import transaction_context
12+
from simcore_service_webserver.db.plugin import get_asyncpg_engine
1013

1114

1215
@pytest.fixture
13-
def workspaces_clean_db(postgres_db: sa.engine.Engine) -> Iterator[None]:
14-
with postgres_db.connect() as con:
15-
yield
16-
con.execute(workspaces.delete())
17-
con.execute(projects.delete())
16+
async def pricing_plan_id(
17+
client: TestClient,
18+
osparc_product_name: str,
19+
) -> AsyncIterator[int]:
20+
assert client.app
21+
22+
async with transaction_context(get_asyncpg_engine(client.app)) as conn:
23+
result = await conn.execute(
24+
resource_tracker_pricing_plans.insert()
25+
.values(
26+
product_name=osparc_product_name,
27+
display_name="ISolve Thermal",
28+
description="",
29+
classification="TIER",
30+
is_active=True,
31+
pricing_plan_key="isolve-thermal",
32+
)
33+
.returning(resource_tracker_pricing_plans.c.pricing_plan_id)
34+
)
35+
row = result.first()
36+
37+
assert row
38+
39+
yield int(row[0])
40+
41+
async with transaction_context(get_asyncpg_engine(client.app)) as conn:
42+
result = await conn.execute(resource_tracker_pricing_plans.delete())

services/web/server/tests/unit/with_dbs/04/licenses/test_license_goods_db.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from collections.abc import AsyncIterator
2-
31
# pylint: disable=redefined-outer-name
42
# pylint: disable=unused-argument
53
# pylint: disable=unused-variable
@@ -17,47 +15,12 @@
1715
from models_library.rest_ordering import OrderBy
1816
from pytest_simcore.helpers.webserver_login import UserInfoDict
1917
from servicelib.aiohttp import status
20-
from simcore_postgres_database.models.resource_tracker_pricing_plans import (
21-
resource_tracker_pricing_plans,
22-
)
23-
from simcore_postgres_database.utils_repos import transaction_context
2418
from simcore_service_webserver.db.models import UserRole
25-
from simcore_service_webserver.db.plugin import get_asyncpg_engine
2619
from simcore_service_webserver.licenses import _license_goods_db
2720
from simcore_service_webserver.licenses.errors import LicenseGoodNotFoundError
2821
from simcore_service_webserver.projects.models import ProjectDict
2922

3023

31-
@pytest.fixture
32-
async def pricing_plan_id(
33-
client: TestClient,
34-
osparc_product_name: str,
35-
) -> AsyncIterator[int]:
36-
assert client.app
37-
38-
async with transaction_context(get_asyncpg_engine(client.app)) as conn:
39-
result = await conn.execute(
40-
resource_tracker_pricing_plans.insert()
41-
.values(
42-
product_name=osparc_product_name,
43-
display_name="ISolve Thermal",
44-
description="",
45-
classification="TIER",
46-
is_active=True,
47-
pricing_plan_key="isolve-thermal",
48-
)
49-
.returning(resource_tracker_pricing_plans.c.pricing_plan_id)
50-
)
51-
row = result.first()
52-
53-
assert row
54-
55-
yield int(row[0])
56-
57-
async with transaction_context(get_asyncpg_engine(client.app)) as conn:
58-
result = await conn.execute(resource_tracker_pricing_plans.delete())
59-
60-
6124
@pytest.mark.parametrize("user_role,expected", [(UserRole.USER, status.HTTP_200_OK)])
6225
async def test_license_goods_db_crud(
6326
client: TestClient,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# pylint: disable=redefined-outer-name
2+
# pylint: disable=unused-argument
3+
# pylint: disable=unused-variable
4+
# pylint: disable=too-many-arguments
5+
# pylint: disable=too-many-statements
6+
from http import HTTPStatus
7+
8+
import pytest
9+
from aiohttp.test_utils import TestClient
10+
from models_library.api_schemas_webserver.license_goods import LicenseGoodGet
11+
from models_library.license_goods import LicenseResourceType
12+
from pytest_simcore.helpers.assert_checks import assert_status
13+
from pytest_simcore.helpers.webserver_login import UserInfoDict
14+
from servicelib.aiohttp import status
15+
from simcore_service_webserver.db.models import UserRole
16+
from simcore_service_webserver.licenses import _license_goods_db
17+
from simcore_service_webserver.projects.models import ProjectDict
18+
19+
20+
@pytest.mark.parametrize("user_role,expected", [(UserRole.USER, status.HTTP_200_OK)])
21+
async def test_license_goods_db_crud(
22+
client: TestClient,
23+
logged_user: UserInfoDict,
24+
user_project: ProjectDict,
25+
osparc_product_name: str,
26+
expected: HTTPStatus,
27+
pricing_plan_id: int,
28+
):
29+
assert client.app
30+
31+
# list
32+
url = client.app.router["list_license_goods"].url_for()
33+
resp = await client.get(f"{url}")
34+
data, _ = await assert_status(resp, status.HTTP_200_OK)
35+
assert data == []
36+
37+
license_good_db = await _license_goods_db.create(
38+
client.app,
39+
product_name=osparc_product_name,
40+
name="Model A",
41+
license_resource_type=LicenseResourceType.VIP_MODEL,
42+
pricing_plan_id=pricing_plan_id,
43+
)
44+
_license_good_id = license_good_db.license_good_id
45+
46+
# list
47+
url = client.app.router["list_license_goods"].url_for()
48+
resp = await client.get(f"{url}")
49+
data, _ = await assert_status(resp, status.HTTP_200_OK)
50+
assert len(data) == 1
51+
assert LicenseGoodGet(**data[0])
52+
53+
# get
54+
url = client.app.router["get_license_good"].url_for(
55+
license_good_id=_license_good_id
56+
)
57+
resp = await client.get(f"{url}")
58+
data, _ = await assert_status(resp, status.HTTP_200_OK)
59+
assert LicenseGoodGet(**data)
60+
61+
# purchase
62+
url = client.app.router["purchase_license_good"].url_for(
63+
license_good_id=_license_good_id
64+
)
65+
resp = await client.post(f"{url}", json={"wallet_id": 1, "num_of_seeds": 5})
66+
# NOTE: Not yet implemented

0 commit comments

Comments
 (0)