Skip to content

Commit 4eeccc2

Browse files
committed
🐛 Fix: rename AutoRechargeStmts to AutoRechargeStatements for consistency and update references across the codebase
1 parent 46f6872 commit 4eeccc2

File tree

4 files changed

+86
-76
lines changed

4 files changed

+86
-76
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sqlalchemy.dialects.postgresql import insert as pg_insert
88

99

10-
class AutoRechargeStmts:
10+
class AutoRechargeStatements:
1111
@staticmethod
1212
def is_valid_payment_method(user_id, wallet_id, payment_method_id) -> sa.sql.Select:
1313
return sa.select(payments_methods.c.payment_method_id).where(

packages/postgres-database/tests/test_utils_payments_autorecharge.py

Lines changed: 77 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,75 +4,81 @@
44
# pylint: disable=too-many-arguments
55

66
import datetime
7-
from typing import TypeAlias
7+
from collections.abc import AsyncIterable
88

99
import pytest
10-
import sqlalchemy as sa
11-
from aiopg.sa.connection import SAConnection
12-
from aiopg.sa.result import RowProxy
1310
from faker import Faker
1411
from pytest_simcore.helpers.faker_factories import random_payment_method, utcnow
12+
from pytest_simcore.helpers.postgres_tools import insert_and_get_row_lifespan
1513
from simcore_postgres_database.models.payments_methods import (
1614
InitPromptAckFlowState,
1715
payments_methods,
1816
)
19-
from simcore_postgres_database.utils_payments_autorecharge import AutoRechargeStmts
17+
from simcore_postgres_database.utils_payments_autorecharge import AutoRechargeStatements
18+
from sqlalchemy.engine.row import Row
19+
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine
2020

2121
#
2222
# HELPERS
2323
#
2424

2525

26-
async def _get_auto_recharge(connection, wallet_id) -> RowProxy | None:
26+
async def _get_auto_recharge(connection: AsyncConnection, wallet_id) -> Row | None:
2727
# has recharge trigger?
28-
stmt = AutoRechargeStmts.get_wallet_autorecharge(wallet_id)
28+
stmt = AutoRechargeStatements.get_wallet_autorecharge(wallet_id)
2929
result = await connection.execute(stmt)
30-
return await result.first()
30+
return result.first()
3131

3232

3333
async def _is_valid_payment_method(
34-
connection, user_id, wallet_id, payment_method_id
34+
connection: AsyncConnection, user_id, wallet_id, payment_method_id
3535
) -> bool:
3636

37-
stmt = AutoRechargeStmts.is_valid_payment_method(
37+
stmt = AutoRechargeStatements.is_valid_payment_method(
3838
user_id, wallet_id, payment_method_id
3939
)
4040
primary_payment_method_id = await connection.scalar(stmt)
4141
return primary_payment_method_id == payment_method_id
4242

4343

4444
async def _upsert_autorecharge(
45-
connection,
45+
connection: AsyncConnection,
4646
wallet_id,
4747
enabled,
4848
primary_payment_method_id,
4949
top_up_amount_in_usd,
5050
monthly_limit_in_usd,
51-
) -> RowProxy:
51+
) -> Row:
5252
# using this primary payment-method, create an autorecharge
5353
# NOTE: requires the entire
54-
stmt = AutoRechargeStmts.upsert_wallet_autorecharge(
54+
stmt = AutoRechargeStatements.upsert_wallet_autorecharge(
5555
wallet_id=wallet_id,
5656
enabled=enabled,
5757
primary_payment_method_id=primary_payment_method_id,
5858
top_up_amount_in_usd=top_up_amount_in_usd,
5959
monthly_limit_in_usd=monthly_limit_in_usd,
6060
)
61-
row = await (await connection.execute(stmt)).first()
62-
assert row
63-
return row
61+
result = await connection.execute(stmt)
62+
return result.one()
6463

6564

66-
async def _update_autorecharge(connection, wallet_id, **settings) -> int | None:
67-
stmt = AutoRechargeStmts.update_wallet_autorecharge(wallet_id, **settings)
65+
async def _update_autorecharge(
66+
connection: AsyncConnection, wallet_id, **settings
67+
) -> int | None:
68+
stmt = AutoRechargeStatements.update_wallet_autorecharge(wallet_id, **settings)
6869
return await connection.scalar(stmt)
6970

7071

71-
PaymentMethodRow: TypeAlias = RowProxy
72+
class PaymentMethodRow(dict):
73+
# Convert dict to Row-like object for compatibility
74+
def __getattr__(self, key):
75+
return self[key]
7276

7377

7478
@pytest.fixture
75-
async def payment_method(connection: SAConnection, faker: Faker) -> PaymentMethodRow:
79+
async def payment_method(
80+
asyncpg_engine: AsyncEngine, faker: Faker
81+
) -> AsyncIterable[PaymentMethodRow]:
7682
payment_method_id = faker.uuid4().upper()
7783

7884
raw_payment_method = random_payment_method(
@@ -81,57 +87,61 @@ async def payment_method(connection: SAConnection, faker: Faker) -> PaymentMetho
8187
completed_at=utcnow() + datetime.timedelta(seconds=1),
8288
state=InitPromptAckFlowState.SUCCESS,
8389
)
84-
result = await connection.execute(
85-
payments_methods.insert()
86-
.values(**raw_payment_method)
87-
.returning(sa.literal_column("*"))
88-
)
89-
row = await result.first()
90-
assert row
91-
assert row.payment_method_id == payment_method_id
92-
wallet_id = row.wallet_id
93-
user_id = row.user_id
94-
95-
assert await _is_valid_payment_method(
96-
connection, user_id, wallet_id, payment_method_id
97-
)
98-
return row
90+
91+
# pylint: disable=contextmanager-generator-missing-cleanup
92+
async with insert_and_get_row_lifespan(
93+
asyncpg_engine,
94+
table=payments_methods,
95+
values=raw_payment_method,
96+
pk_col=payments_methods.c.payment_method_id,
97+
pk_value=payment_method_id,
98+
) as row_data:
99+
wallet_id = row_data["wallet_id"]
100+
user_id = row_data["user_id"]
101+
102+
async with asyncpg_engine.connect() as connection:
103+
assert await _is_valid_payment_method(
104+
connection, user_id, wallet_id, payment_method_id
105+
)
106+
107+
yield PaymentMethodRow(row_data)
99108

100109

101110
async def test_payments_automation_workflow(
102-
connection: SAConnection, payment_method: PaymentMethodRow
111+
asyncpg_engine: AsyncEngine, payment_method: PaymentMethodRow
103112
):
104113
payment_method_id = payment_method.payment_method_id
105114
wallet_id = payment_method.wallet_id
106115

107-
# get
108-
auto_recharge = await _get_auto_recharge(connection, wallet_id)
109-
assert auto_recharge is None
110-
111-
# new
112-
await _upsert_autorecharge(
113-
connection,
114-
wallet_id,
115-
enabled=True,
116-
primary_payment_method_id=payment_method_id,
117-
top_up_amount_in_usd=100,
118-
monthly_limit_in_usd=None,
119-
)
120-
121-
auto_recharge = await _get_auto_recharge(connection, wallet_id)
122-
assert auto_recharge is not None
123-
assert auto_recharge.primary_payment_method_id == payment_method_id
124-
assert auto_recharge.enabled is True
125-
126-
# upsert: deactivate countdown
127-
auto_recharge = await _upsert_autorecharge(
128-
connection,
129-
wallet_id,
130-
enabled=True,
131-
primary_payment_method_id=payment_method_id,
132-
top_up_amount_in_usd=100,
133-
monthly_limit_in_usd=10000, # <----
134-
)
135-
assert auto_recharge.monthly_limit_in_usd == 10000
136-
137-
await _update_autorecharge(connection, wallet_id, monthly_limit_in_usd=None)
116+
async with asyncpg_engine.begin() as connection:
117+
# get
118+
auto_recharge = await _get_auto_recharge(connection, wallet_id)
119+
assert auto_recharge is None
120+
121+
# new
122+
await _upsert_autorecharge(
123+
connection,
124+
wallet_id,
125+
enabled=True,
126+
primary_payment_method_id=payment_method_id,
127+
top_up_amount_in_usd=100,
128+
monthly_limit_in_usd=None,
129+
)
130+
131+
auto_recharge = await _get_auto_recharge(connection, wallet_id)
132+
assert auto_recharge is not None
133+
assert auto_recharge.primary_payment_method_id == payment_method_id
134+
assert auto_recharge.enabled is True
135+
136+
# upsert: deactivate countdown
137+
auto_recharge = await _upsert_autorecharge(
138+
connection,
139+
wallet_id,
140+
enabled=True,
141+
primary_payment_method_id=payment_method_id,
142+
top_up_amount_in_usd=100,
143+
monthly_limit_in_usd=10000, # <----
144+
)
145+
assert auto_recharge.monthly_limit_in_usd == 10000
146+
147+
await _update_autorecharge(connection, wallet_id, monthly_limit_in_usd=None)

services/payments/src/simcore_service_payments/db/auto_recharge_repo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from models_library.users import UserID
77
from models_library.wallets import WalletID
88
from pydantic import BaseModel, ConfigDict, PositiveInt
9-
from simcore_postgres_database.utils_payments_autorecharge import AutoRechargeStmts
9+
from simcore_postgres_database.utils_payments_autorecharge import AutoRechargeStatements
1010

1111
from .base import BaseRepository
1212

@@ -33,7 +33,7 @@ async def get_wallet_autorecharge(
3333
"""
3434

3535
async with self.db_engine.begin() as conn:
36-
stmt = AutoRechargeStmts.get_wallet_autorecharge(wallet_id)
36+
stmt = AutoRechargeStatements.get_wallet_autorecharge(wallet_id)
3737
result = await conn.execute(stmt)
3838
row = result.first()
3939
return PaymentsAutorechargeDB.model_validate(row) if row else None
@@ -50,7 +50,7 @@ async def replace_wallet_autorecharge(
5050
5151
"""
5252
async with self.db_engine.begin() as conn:
53-
stmt = AutoRechargeStmts.is_valid_payment_method(
53+
stmt = AutoRechargeStatements.is_valid_payment_method(
5454
user_id=user_id,
5555
wallet_id=new.wallet_id,
5656
payment_method_id=new.primary_payment_method_id,
@@ -61,7 +61,7 @@ async def replace_wallet_autorecharge(
6161
payment_method_id=new.primary_payment_method_id
6262
)
6363

64-
stmt = AutoRechargeStmts.upsert_wallet_autorecharge(
64+
stmt = AutoRechargeStatements.upsert_wallet_autorecharge(
6565
wallet_id=wallet_id,
6666
enabled=new.enabled,
6767
primary_payment_method_id=new.primary_payment_method_id,

services/web/server/src/simcore_service_webserver/payments/_autorecharge_db.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from models_library.users import UserID
88
from models_library.wallets import WalletID
99
from pydantic import BaseModel, ConfigDict, PositiveInt
10-
from simcore_postgres_database.utils_payments_autorecharge import AutoRechargeStmts
10+
from simcore_postgres_database.utils_payments_autorecharge import AutoRechargeStatements
1111
from simcore_postgres_database.utils_repos import (
1212
pass_or_acquire_connection,
1313
transaction_context,
@@ -39,7 +39,7 @@ async def get_wallet_autorecharge(
3939
wallet_id: WalletID,
4040
) -> PaymentsAutorechargeGetDB | None:
4141
async with pass_or_acquire_connection(get_asyncpg_engine(app), connection) as conn:
42-
stmt = AutoRechargeStmts.get_wallet_autorecharge(wallet_id)
42+
stmt = AutoRechargeStatements.get_wallet_autorecharge(wallet_id)
4343
result = await conn.execute(stmt)
4444
row = result.one_or_none()
4545
return PaymentsAutorechargeGetDB.model_validate(row) if row else None
@@ -59,7 +59,7 @@ async def replace_wallet_autorecharge(
5959
6060
"""
6161
async with transaction_context(get_asyncpg_engine(app), connection) as conn:
62-
stmt = AutoRechargeStmts.is_valid_payment_method(
62+
stmt = AutoRechargeStatements.is_valid_payment_method(
6363
user_id=user_id,
6464
wallet_id=new.wallet_id,
6565
payment_method_id=new.primary_payment_method_id,
@@ -70,7 +70,7 @@ async def replace_wallet_autorecharge(
7070
payment_method_id=new.primary_payment_method_id
7171
)
7272

73-
stmt = AutoRechargeStmts.upsert_wallet_autorecharge(
73+
stmt = AutoRechargeStatements.upsert_wallet_autorecharge(
7474
wallet_id=wallet_id,
7575
enabled=new.enabled,
7676
primary_payment_method_id=new.primary_payment_method_id,

0 commit comments

Comments
 (0)