44# pylint: disable=too-many-arguments
55
66
7+ from collections .abc import AsyncIterator
8+
79import pytest
810import sqlalchemy as sa
9- from aiopg .sa .connection import SAConnection
10- from aiopg .sa .result import RowProxy
1111from faker import Faker
1212from pytest_simcore .helpers .faker_factories import random_product
1313from simcore_postgres_database .errors import CheckViolation , ForeignKeyViolation
1818 get_product_latest_stripe_info ,
1919 is_payment_enabled ,
2020)
21+ from sqlalchemy .engine .row import Row
22+ from sqlalchemy .ext .asyncio import AsyncConnection , AsyncEngine
2123
2224
2325@pytest .fixture
24- async def fake_product (connection : SAConnection ) -> RowProxy :
26+ async def connection (asyncpg_engine : AsyncEngine ) -> AsyncIterator [AsyncConnection ]:
27+ async with asyncpg_engine .begin () as conn :
28+ yield conn
29+
30+
31+ @pytest .fixture
32+ async def fake_product (connection : AsyncConnection ) -> Row :
2533 result = await connection .execute (
2634 products .insert ()
2735 .values (random_product (group_id = None ))
2836 .returning (sa .literal_column ("*" ))
2937 )
30- product = await result .first ()
31- assert product is not None
32- return product
38+ return result .one ()
3339
3440
3541async def test_creating_product_prices (
36- connection : SAConnection , fake_product : RowProxy , faker : Faker
42+ connection : AsyncConnection , fake_product : Row , faker : Faker
3743):
44+
3845 # a price per product
3946 result = await connection .execute (
4047 products_prices .insert ()
@@ -47,12 +54,12 @@ async def test_creating_product_prices(
4754 )
4855 .returning (sa .literal_column ("*" ))
4956 )
50- product_prices = await result .first ()
57+ product_prices = result .one ()
5158 assert product_prices
5259
5360
5461async def test_non_negative_price_not_allowed (
55- connection : SAConnection , fake_product : RowProxy , faker : Faker
62+ connection : AsyncConnection , fake_product : Row , faker : Faker
5663):
5764 # negative price not allowed
5865 with pytest .raises (CheckViolation ) as exc_info :
@@ -81,7 +88,7 @@ async def test_non_negative_price_not_allowed(
8188
8289
8390async def test_delete_price_constraints (
84- connection : SAConnection , fake_product : RowProxy , faker : Faker
91+ connection : AsyncConnection , fake_product : Row , faker : Faker
8592):
8693 # products_prices
8794 await connection .execute (
@@ -106,7 +113,7 @@ async def test_delete_price_constraints(
106113
107114
108115async def test_get_product_latest_price_or_none (
109- connection : SAConnection , fake_product : RowProxy , faker : Faker
116+ connection : AsyncConnection , fake_product : Row , faker : Faker
110117):
111118 # undefined product
112119 assert (
@@ -130,7 +137,7 @@ async def test_get_product_latest_price_or_none(
130137
131138
132139async def test_price_history_of_a_product (
133- connection : SAConnection , fake_product : RowProxy , faker : Faker
140+ connection : AsyncConnection , fake_product : Row , faker : Faker
134141):
135142 # initial price
136143 await connection .execute (
@@ -163,7 +170,7 @@ async def test_price_history_of_a_product(
163170
164171
165172async def test_get_product_latest_stripe_info (
166- connection : SAConnection , fake_product : RowProxy , faker : Faker
173+ connection : AsyncConnection , fake_product : Row , faker : Faker
167174):
168175 stripe_price_id_value = faker .word ()
169176 stripe_tax_rate_id_value = faker .word ()
@@ -187,5 +194,5 @@ async def test_get_product_latest_stripe_info(
187194 assert product_stripe_info [1 ] == stripe_tax_rate_id_value
188195
189196 # undefined product
190- with pytest .raises (ValueError ) as exc_info :
197+ with pytest .raises (ValueError , match = "undefined" ) :
191198 await get_product_latest_stripe_info (connection , product_name = "undefined" )
0 commit comments