async def get_transactions(wallet_id: int, db: Db): async with db.connection() as connection: transactions = await connection.fetch( sa.select([models.transaction]).where(wallet_id == wallet_id), ) return [dict(t) for t in transactions]
async def test_make_transaction(self, client: dict, db: Db): wallet_id = client['wallet']['id'] transactions = await get_transactions(wallet_id, db) assert transactions == [] async with db.connection() as connection: await Billing(connection).save_transaction( wallet_id, Decimal('1'), OperationType.ACCRUAL, description={'wallet_from': 2}, ) transactions = await get_transactions(wallet_id, db) assert transactions == [ { 'id': EqMock(int), 'date': EqMock(datetime), 'type': OperationType.ACCRUAL, 'wallet_id': wallet_id, 'amount': Decimal('1'), 'description': '{"wallet_from": 2}', # BUG: https://github.com/CanopyTax/asyncpgsa/issues/44 }, ]
async def client(self, db: Db): async with db.executor(Billing) as executor: client = await executor.create_client('Bill') wallet = await executor.create_wallet(client['id']) client['wallet'] = wallet return client
async def db(): db = Db(app_config.DB_PG_URL) await db.init() yield db await db.stop()
async def test_create_wallet(self, db: Db): async with db.executor(Billing) as executor: client = await executor.create_client('Bill') wallet = await executor.create_wallet(client['id']) assert wallet == { 'id': EqMock(int), 'balance': Decimal('0'), }
async def test_create_client(self, db: Db): async with db.transaction() as t: client = await Billing(t).create_client('Bill') assert client == { 'id': EqMock(int), 'name': 'Bill', 'date': EqMock(datetime), }
async def make_transfer(wallet_from, wallet_to, amount: Decimal, db: Db): async with db.executor(Billing, transaction=True) as billing: await billing.change_balance(wallet_id=wallet_from, amount=-amount) await billing.save_transaction( wallet_id=wallet_from, amount=-amount, operation_type=OperationType.WRITE_OFF, description={'to': wallet_to}, ) await billing.change_balance(wallet_id=wallet_to, amount=amount) await billing.save_transaction( wallet_id=wallet_to, amount=amount, operation_type=OperationType.ACCRUAL, description={'from': wallet_from}, )
def create_app() -> FastAPI: app = FastAPI() db = Db(config.DB_PG_URL) app.extra['db'] = db @app.on_event("startup") async def startup(): await db.init() @app.on_event("shutdown") async def shutdown(): await db.stop() apply_error_handlers(app) app.include_router(views.billing_router) return app
async def charge_wallet(wallet_id: int, amount: Decimal, db: Db): async with db.executor(Billing, transaction=True) as billing: await billing.change_balance(wallet_id, amount) await billing.save_transaction(wallet_id, amount, OperationType.ACCRUAL)
async def get_client(client_id: int, db: Db) -> dict: async with db.executor(Billing) as billing: return await billing.get_client(client_id)
async def test_make_transaction_wallet_not_found(self, db: Db): async with db.connection() as connection: with pytest.raises(errors.NotFound): await Billing(connection).save_transaction(1, Decimal('1'), OperationType.ACCRUAL)
async def test_create_wallet_with_wrong_currency(self, db: Db): async with db.connection() as connection: with pytest.raises(ValueError): await Billing(connection).create_wallet(1, 'RUB')
async def test_create_wallet_client_not_found(self, db: Db): async with db.connection() as connection: with pytest.raises(errors.NotFound): await Billing(connection).create_wallet(client_id=1)
async def test_get_client_not_found(self, db: Db): async with db.connection() as connection: with pytest.raises(errors.NotFound): await Billing(connection).get_client(1)
async def test_get_client(self, client, db: Db): async with db.connection() as connection: found_client = await Billing(connection).get_client(client['id']) assert found_client == client assert found_client is not client
async def create_client(name: str, db: Db) -> dict: async with db.executor(Billing, transaction=True) as executor: client = await executor.create_client(name=name) client['wallet'] = await executor.create_wallet(client_id=client['id']) return client
async def test_change_balance_not_enough_money(self, client: dict, db: Db): async with db.connection() as connection: with pytest.raises(errors.NotEnoughMoney): await Billing(connection).change_balance(client['wallet']['id'], -Decimal('1'))
async def test_change_balance_wallet_not_found(self, db: Db): async with db.connection() as connection: with pytest.raises(errors.NotFound): await Billing(connection).change_balance(1, Decimal('1'))
async def get_balance(wallet_id, db: Db): async with db.connection() as connection: return await connection.fetchval( sa.select([models.wallet.c.balance ]).where(models.wallet.c.id == wallet_id, ), )