lnbits/tests/mocks.py
calle 8535d70d36
Poetry dev (#837)
* black and isort for tests

* black and isort for build

* use poetry in Makefile, update pyproject.toml dependencies and add configs for black, pytest, mypy, isort there

* switching github workflow to poetry

* set mininum version to python 3.7

* fix tests

* add types-protobuf to dev packages

* fix cln regtest

* update docs

* try fix 1

* mypy fix2

* fix isort formatting workflow

* add prettier to dev docs

* multiple valid python version for pyproject

* update poetry.lock

* remove development installation, not needed anymore

* fix migration workflows

* format into one test

* fix yaml

* fix pipeline

* fix pipeline

* fix again

* fix

* rename checks

* remove venv tests

* venv test once

Co-authored-by: dni <dni.khr@gmail.com>
2022-08-03 14:10:32 +02:00

77 lines
1.9 KiB
Python

from mock import AsyncMock
from lnbits import bolt11
from lnbits.settings import WALLET
from lnbits.wallets.base import PaymentResponse, PaymentStatus, StatusResponse
from lnbits.wallets.fake import FakeWallet
from .helpers import get_random_string
# generates an invoice with FakeWallet
async def generate_mock_invoice(**x):
invoice = await FakeWallet.create_invoice(
FakeWallet(), amount=10, memo=f"mock invoice {get_random_string()}"
)
return invoice
WALLET.status = AsyncMock(
return_value=StatusResponse(
"", # no error
1000000, # msats
)
)
# Note: if this line is uncommented, invoices will always be generated by FakeWallet
# WALLET.create_invoice = generate_mock_invoice
# NOTE: This mock fails since it yields the same invoice multiple
# times which makes the db throw an error due to uniqueness contraints
# on the checking ID
# # primitive event loop for generate_mock_invoice()
# def drive(c):
# while True:
# try:
# c.send(None)
# except StopIteration as e:
# return e.value
# # finally we await it
# invoice = drive(generate_mock_invoice())
# WALLET.create_invoice = AsyncMock(
# return_value=InvoiceResponse(
# True, # ok
# invoice.checking_id, # checking_id (i.e. payment_hash)
# invoice.payment_request, # payment_request
# "", # no error
# )
# )
def pay_invoice_side_effect(
payment_request: str, fee_limit_msat: int
) -> PaymentResponse:
invoice = bolt11.decode(payment_request)
return PaymentResponse(
True, # ok
invoice.payment_hash, # checking_id (i.e. payment_hash)
0, # fee_msat
"", # no error
)
WALLET.pay_invoice = AsyncMock(side_effect=pay_invoice_side_effect)
WALLET.get_invoice_status = AsyncMock(
return_value=PaymentStatus(
True, # paid
)
)
WALLET.get_payment_status = AsyncMock(
return_value=PaymentStatus(
True, # paid
)
)