コード例 #1
0
ファイル: test_billing.py プロジェクト: angru/abilling
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]
コード例 #2
0
ファイル: test_billing.py プロジェクト: angru/abilling
    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
            },
        ]
コード例 #3
0
ファイル: test_billing.py プロジェクト: angru/abilling
    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
コード例 #4
0
ファイル: conftest.py プロジェクト: angru/abilling
async def db():
    db = Db(app_config.DB_PG_URL)

    await db.init()

    yield db

    await db.stop()
コード例 #5
0
ファイル: test_billing.py プロジェクト: angru/abilling
    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'),
        }
コード例 #6
0
ファイル: test_billing.py プロジェクト: angru/abilling
    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),
        }
コード例 #7
0
ファイル: controllers.py プロジェクト: angru/abilling
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},
        )
コード例 #8
0
ファイル: fastapi.py プロジェクト: angru/abilling
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
コード例 #9
0
ファイル: controllers.py プロジェクト: angru/abilling
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)
コード例 #10
0
ファイル: controllers.py プロジェクト: angru/abilling
async def get_client(client_id: int, db: Db) -> dict:
    async with db.executor(Billing) as billing:
        return await billing.get_client(client_id)
コード例 #11
0
ファイル: test_billing.py プロジェクト: angru/abilling
 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)
コード例 #12
0
ファイル: test_billing.py プロジェクト: angru/abilling
 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')
コード例 #13
0
ファイル: test_billing.py プロジェクト: angru/abilling
 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)
コード例 #14
0
ファイル: test_billing.py プロジェクト: angru/abilling
 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)
コード例 #15
0
ファイル: test_billing.py プロジェクト: angru/abilling
    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
コード例 #16
0
ファイル: controllers.py プロジェクト: angru/abilling
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
コード例 #17
0
ファイル: test_billing.py プロジェクト: angru/abilling
 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'))
コード例 #18
0
ファイル: test_billing.py プロジェクト: angru/abilling
 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'))
コード例 #19
0
ファイル: test_controllers.py プロジェクト: angru/abilling
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, ), )