44# pylint: disable=too-many-arguments
55
66import datetime
7- from typing import TypeAlias
7+ from collections . abc import AsyncIterable
88
99import pytest
10- import sqlalchemy as sa
11- from aiopg .sa .connection import SAConnection
12- from aiopg .sa .result import RowProxy
1310from faker import Faker
1411from pytest_simcore .helpers .faker_factories import random_payment_method , utcnow
12+ from pytest_simcore .helpers .postgres_tools import insert_and_get_row_lifespan
1513from 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
3333async 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
4444async 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
101110async 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 )
0 commit comments