Testing: postgres db backend (#711)

* try postgres run

* fix yaml

* test with postgres

* check with postgres

* inkey_from

* remove trio

* add coverage

* add coverage

* more python testing

* use @pytest_asyncio.fixture

* remove unused imports

* fix api_payment payment lookup

* measure durations
This commit is contained in:
calle 2022-07-07 18:29:26 +02:00 committed by GitHub
parent 262bd32f44
commit 63d4e60542
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 91 additions and 45 deletions

View file

@ -1,5 +1,6 @@
import asyncio
import pytest
import pytest_asyncio
from httpx import AsyncClient
from lnbits.app import create_app
from lnbits.commands import migrate_databases
@ -15,7 +16,7 @@ from lnbits.core.models import User, Wallet, Payment, BalanceCheck
from typing import Tuple
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
def event_loop():
loop = asyncio.get_event_loop()
yield loop
@ -23,7 +24,7 @@ def event_loop():
# use session scope to run once before and once after all tests
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
def app(event_loop):
app = create_app()
# use redefined version of the event loop for scope="session"
@ -37,19 +38,19 @@ def app(event_loop):
# loop.close()
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def client(app):
client = AsyncClient(app=app, base_url=f"http://{HOST}:{PORT}")
yield client
await client.aclose()
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def db():
yield Database("database")
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def from_user_wallet():
user = await create_account()
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_from")
@ -61,7 +62,7 @@ async def from_user_wallet():
yield user, wallet
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def to_user_wallet():
user = await create_account()
wallet = await create_wallet(user_id=user.id, wallet_name="test_wallet_to")
@ -73,7 +74,7 @@ async def to_user_wallet():
yield user, wallet
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def inkey_headers_from(from_user_wallet):
_, wallet = from_user_wallet
yield {
@ -82,7 +83,7 @@ async def inkey_headers_from(from_user_wallet):
}
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def adminkey_headers_from(from_user_wallet):
_, wallet = from_user_wallet
yield {
@ -91,7 +92,7 @@ async def adminkey_headers_from(from_user_wallet):
}
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def inkey_headers_to(to_user_wallet):
_, wallet = to_user_wallet
yield {
@ -100,7 +101,7 @@ async def inkey_headers_to(to_user_wallet):
}
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def adminkey_headers_to(to_user_wallet):
_, wallet = to_user_wallet
yield {
@ -109,7 +110,7 @@ async def adminkey_headers_to(to_user_wallet):
}
@pytest.fixture(scope="session")
@pytest_asyncio.fixture(scope="session")
async def invoice(to_user_wallet):
_, wallet = to_user_wallet
data = await get_random_invoice_data()

View file

@ -1,4 +1,5 @@
import pytest
import pytest_asyncio
from lnbits.core.crud import get_wallet
from ...helpers import get_random_invoice_data
@ -58,11 +59,15 @@ async def test_check_payment_without_key(client, invoice):
# check GET /api/v1/payments/<hash>: payment status
# NOTE: this test is sensitive to which db is used.
# If postgres: it will succeed only with inkey_headers_from
# If sqlite: it will succeed only with adminkey_headers_to
# TODO: fix this
@pytest.mark.asyncio
async def test_check_payment_with_key(client, invoice, inkey_headers_to):
async def test_check_payment_with_key(client, invoice, inkey_headers_from):
# check the payment status
response = await client.get(
f"/api/v1/payments/{invoice['payment_hash']}", headers=inkey_headers_to
f"/api/v1/payments/{invoice['payment_hash']}", headers=inkey_headers_from
)
assert response.status_code < 300
assert response.json()["paid"] == True

View file

@ -1,4 +1,5 @@
import pytest
import pytest_asyncio
from tests.conftest import client

View file

@ -1,4 +1,5 @@
import pytest
import pytest_asyncio
from lnbits.core.crud import get_wallet
# check if the client is working

View file

@ -1,5 +1,6 @@
import json
import pytest
import pytest_asyncio
import secrets
from lnbits.core.crud import create_account, create_wallet
from lnbits.extensions.bleskomat.crud import create_bleskomat, create_bleskomat_lnurl
@ -20,7 +21,7 @@ exchange_rate_providers["dummy"] = {
}
@pytest.fixture
@pytest_asyncio.fixture
async def bleskomat():
user = await create_account()
wallet = await create_wallet(user_id=user.id, wallet_name="bleskomat_test")
@ -34,7 +35,7 @@ async def bleskomat():
return bleskomat
@pytest.fixture
@pytest_asyncio.fixture
async def lnurl(bleskomat):
query = {
"tag": "withdrawRequest",

View file

@ -1,4 +1,5 @@
import pytest
import pytest_asyncio
import secrets
from lnbits.core.crud import get_wallet
from lnbits.settings import HOST, PORT

View file

@ -1,12 +1,9 @@
import time
from mock import AsyncMock
from lnbits import bolt11
from lnbits.wallets.base import (
StatusResponse,
InvoiceResponse,
PaymentResponse,
PaymentStatus,
Wallet,
)
from lnbits.settings import WALLET