Skip to content

Commit 07d794e

Browse files
committed
improving tests
1 parent 83c7e67 commit 07d794e

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

services/web/server/tests/unit/isolated/products/conftest.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def product_name() -> ProductName:
2424

2525

2626
@pytest.fixture
27-
def fake_product_from_db(faker: Faker, product_name: ProductName) -> dict[str, Any]:
27+
def product_db_server_defaults() -> dict[str, Any]:
2828
server_defaults = {}
2929
for c in products_table.columns:
3030
if c.server_default is not None:
@@ -34,8 +34,15 @@ def fake_product_from_db(faker: Faker, product_name: ProductName) -> dict[str, A
3434
m = re.match(r"^'(.+)'::jsonb$", c.server_default.arg.text)
3535
if m:
3636
server_defaults[c.name] = json.loads(m.group(1))
37+
return server_defaults
38+
39+
40+
@pytest.fixture
41+
def fake_product_from_db(
42+
faker: Faker, product_name: ProductName, product_db_server_defaults: dict[str, Any]
43+
) -> dict[str, Any]:
3744
return random_product(
3845
name=product_name,
3946
fake=faker,
40-
**server_defaults,
47+
**product_db_server_defaults,
4148
)

services/web/server/tests/unit/isolated/products/test_products_middlewares.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import pytest
99
from aiohttp import web
1010
from aiohttp.test_utils import make_mocked_request
11+
from faker import Faker
12+
from pytest_simcore.helpers.faker_factories import random_product
1113
from servicelib.aiohttp import status
1214
from servicelib.rest_constants import X_PRODUCT_NAME_HEADER
13-
from simcore_postgres_database.models.products import LOGIN_SETTINGS_DEFAULT
14-
from simcore_postgres_database.webserver_models import products
1515
from simcore_service_webserver.products import products_web
1616
from simcore_service_webserver.products._web_events import _set_app_state
1717
from simcore_service_webserver.products._web_middlewares import (
@@ -22,51 +22,51 @@
2222
from yarl import URL
2323

2424

25-
@pytest.fixture()
26-
def mock_postgres_product_table() -> list[dict[str, Any]]:
27-
# NOTE: try here your product's host_regex before adding them in the database!
28-
column_defaults: dict[str, Any] = {
29-
c.name: f"{c.server_default.arg}" for c in products.columns if c.server_default
30-
}
31-
32-
column_defaults["login_settings"] = LOGIN_SETTINGS_DEFAULT
25+
@pytest.fixture
26+
def mock_postgres_product_data(
27+
faker: Faker, product_db_server_defaults: dict[str, Any]
28+
) -> list[dict[str, Any]]:
3329

3430
_SUBDOMAIN_PREFIX = r"[\w-]+\."
3531

3632
return [
37-
dict(
33+
random_product(
3834
name="osparc",
3935
host_regex=rf"^({_SUBDOMAIN_PREFIX})*osparc[\.-]",
40-
**column_defaults,
36+
fake=faker,
37+
**product_db_server_defaults,
4138
),
42-
dict(
39+
random_product(
4340
name="s4l",
4441
host_regex=rf"^({_SUBDOMAIN_PREFIX})*(s4l|sim4life)[\.-]",
45-
**column_defaults,
42+
fake=faker,
43+
**product_db_server_defaults,
4644
),
47-
dict(
45+
random_product(
4846
name="tis",
4947
host_regex=rf"^({_SUBDOMAIN_PREFIX})*(tis|^ti-solutions)[\.-]",
48+
fake=faker,
5049
vendor={
5150
"name": "ACME",
5251
"address": "sesame street",
5352
"copyright": "© ACME correcaminos",
5453
"url": "https://acme.com",
5554
"forum_url": "https://forum.acme.com",
5655
},
57-
**column_defaults,
56+
**product_db_server_defaults,
5857
),
5958
]
6059

6160

6261
@pytest.fixture
63-
def mock_app(mock_postgres_product_table: list[dict[str, Any]]) -> web.Application:
62+
def mock_app(mock_postgres_product_data: list[dict[str, Any]]) -> web.Application:
6463
app = web.Application()
6564

6665
app_products: dict[str, Product] = {
6766
product_from_db["name"]: Product.model_validate(product_from_db)
68-
for product_from_db in mock_postgres_product_table
67+
for product_from_db in mock_postgres_product_data
6968
}
69+
7070
default_product_name = next(iter(app_products.keys()))
7171
_set_app_state(app, app_products, default_product_name)
7272

0 commit comments

Comments
 (0)