Skip to content

Commit d6a6824

Browse files
committed
fix tests
1 parent 0bc95a4 commit d6a6824

File tree

7 files changed

+49
-28
lines changed

7 files changed

+49
-28
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_products_prices.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ async def get_product_latest_price_info_or_none(
4343
return None
4444

4545

46-
async def get_product_latest_stripe_info(
46+
async def get_product_latest_stripe_info_or_none(
4747
conn: AsyncConnection, product_name: str
48-
) -> tuple[StripePriceID, StripeTaxRateID]:
48+
) -> tuple[StripePriceID, StripeTaxRateID] | None:
4949
# Stripe info of a product for latest price
5050
result = await conn.execute(
5151
sa.select(
@@ -58,10 +58,7 @@ async def get_product_latest_stripe_info(
5858
)
5959

6060
row = result.one_or_none()
61-
if row is None:
62-
msg = f"Required Stripe information missing from product {product_name=}"
63-
raise ValueError(msg)
64-
return (row.stripe_price_id, row.stripe_tax_rate_id)
61+
return (row.stripe_price_id, row.stripe_tax_rate_id) if row else None
6562

6663

6764
async def is_payment_enabled(conn: AsyncConnection, product_name: str) -> bool:

packages/postgres-database/tests/test_models_products_prices.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from simcore_postgres_database.models.products_prices import products_prices
1616
from simcore_postgres_database.utils_products_prices import (
1717
get_product_latest_price_info_or_none,
18-
get_product_latest_stripe_info,
18+
get_product_latest_stripe_info_or_none,
1919
is_payment_enabled,
2020
)
2121
from sqlalchemy.engine.row import Row
@@ -257,12 +257,15 @@ async def test_get_product_latest_stripe_info(
257257
)
258258

259259
# undefined product
260-
with pytest.raises(ValueError, match="undefined"):
261-
await get_product_latest_stripe_info(connection, product_name="undefined")
260+
undefined_product_stripe_info = await get_product_latest_stripe_info_or_none(
261+
connection, product_name="undefined"
262+
)
263+
assert undefined_product_stripe_info is None
262264

263265
# defined product
264-
product_stripe_info = await get_product_latest_stripe_info(
266+
product_stripe_info = await get_product_latest_stripe_info_or_none(
265267
connection, product_name=fake_product.name
266268
)
269+
assert product_stripe_info
267270
assert product_stripe_info[0] == stripe_price_id_value
268271
assert product_stripe_info[1] == stripe_tax_rate_id_value

services/web/server/src/simcore_service_webserver/products/_repository.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
from simcore_postgres_database.utils_products_prices import (
1616
ProductPriceInfo,
1717
get_product_latest_price_info_or_none,
18-
get_product_latest_stripe_info,
18+
get_product_latest_stripe_info_or_none,
1919
)
2020
from simcore_postgres_database.utils_repos import (
2121
pass_or_acquire_connection,
2222
transaction_context,
2323
)
2424
from simcore_service_webserver.constants import FRONTEND_APPS_AVAILABLE
25+
from simcore_service_webserver.products.errors import MissingStripeConfigError
2526
from sqlalchemy.engine import Row
2627
from sqlalchemy.ext.asyncio import AsyncConnection
2728

@@ -160,9 +161,15 @@ async def get_product_stripe_info(
160161
self, product_name: str, connection: AsyncConnection | None = None
161162
) -> ProductStripeInfoGet:
162163
async with pass_or_acquire_connection(self.engine, connection) as conn:
163-
stripe_price_id, stripe_tax_rate_id = await get_product_latest_stripe_info(
164+
latest_stripe_info = await get_product_latest_stripe_info_or_none(
164165
conn, product_name=product_name
165166
)
167+
if latest_stripe_info is None:
168+
exc = MissingStripeConfigError(product_name=product_name)
169+
exc.add_note("Stripe config missing in database")
170+
raise exc
171+
172+
stripe_price_id, stripe_tax_rate_id = latest_stripe_info
166173
return ProductStripeInfoGet(
167174
stripe_price_id=stripe_price_id, stripe_tax_rate_id=stripe_tax_rate_id
168175
)

services/web/server/src/simcore_service_webserver/products/_service.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,18 @@ async def get_product_stripe_info(
111111
app: web.Application, *, product_name: ProductName
112112
) -> ProductStripeInfoGet:
113113
repo = ProductRepository.create_from_app(app)
114+
114115
product_stripe_info = await repo.get_product_stripe_info(product_name)
115116
if (
116-
not product_stripe_info
117-
or "missing!!" in product_stripe_info.stripe_price_id
117+
"missing!!" in product_stripe_info.stripe_price_id
118118
or "missing!!" in product_stripe_info.stripe_tax_rate_id
119119
):
120-
raise MissingStripeConfigError(
121-
product_name=product_name, product_stripe_info=product_stripe_info
120+
exc = MissingStripeConfigError(
121+
product_name=product_name,
122+
product_stripe_info=product_stripe_info,
122123
)
124+
exc.add_note("Probably stripe side is not configured")
125+
raise exc
123126
return product_stripe_info
124127

125128

services/web/server/src/simcore_service_webserver/products/_web_helpers.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from aiohttp import web
55
from models_library.products import ProductName
66
from simcore_postgres_database.utils_products_prices import ProductPriceInfo
7+
from simcore_service_webserver.products.errors import (
8+
FileTemplateNotFoundError,
9+
ProductNotFoundError,
10+
)
711

812
from .._resources import webserver_resources
913
from ..constants import RQ_PRODUCT_KEY
@@ -27,6 +31,14 @@ def get_current_product(request: web.Request) -> Product:
2731
return current_product
2832

2933

34+
def _get_current_product_or_none(request: web.Request) -> Product | None:
35+
try:
36+
product: Product = get_current_product(request)
37+
return product
38+
except ProductNotFoundError:
39+
return None
40+
41+
3042
async def get_current_product_credit_price_info(
3143
request: web.Request,
3244
) -> ProductPriceInfo | None:
@@ -48,19 +60,10 @@ def _themed(dirname: str, template: str) -> Path:
4860
return path
4961

5062

51-
def _get_current_product_or_none(request: web.Request) -> Product | None:
52-
try:
53-
product: Product = get_current_product(request)
54-
return product
55-
except KeyError:
56-
return None
57-
58-
5963
async def _get_common_template_path(filename: str) -> Path:
6064
common_template = _themed("templates/common", filename)
6165
if not common_template.exists():
62-
msg = f"{filename} is not part of the templates/common"
63-
raise ValueError(msg)
66+
raise FileTemplateNotFoundError(filename=filename)
6467
return common_template
6568

6669

services/web/server/src/simcore_service_webserver/products/errors.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ class ProductTemplateNotFoundError(ProductError):
2121

2222

2323
class MissingStripeConfigError(ProductError):
24-
msg_template = "Missing product stripe for product {product_name}"
24+
msg_template = (
25+
"Missing product stripe for product {product_name}.\n"
26+
"NOTE: This is currently setup manually by the operator in pg database via adminer and also in the stripe platform."
27+
)
28+
29+
30+
class FileTemplateNotFoundError(ProductError):
31+
msg_template = "{filename} is not part of the templates/common"

services/web/server/tests/unit/with_dbs/04/products/test_products_repository.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from simcore_service_webserver.products._web_middlewares import (
3636
_get_default_product_name,
3737
)
38+
from simcore_service_webserver.products.errors import MissingStripeConfigError
3839
from sqlalchemy.ext.asyncio import AsyncEngine
3940

4041

@@ -236,7 +237,7 @@ async def test_product_repository_get_product_stripe_info(
236237
assert isinstance(stripe_info, ProductStripeInfoGet)
237238

238239
product_name = "s4l"
239-
with pytest.raises(ValueError, match=product_name):
240+
with pytest.raises(MissingStripeConfigError, match=product_name):
240241
stripe_info = await product_repository.get_product_stripe_info(product_name)
241242

242243

0 commit comments

Comments
 (0)