mirror of
https://github.com/lnbits/lnbits.git
synced 2025-09-26 20:06:17 +02:00
[TEST] use clean db in postgres tests (#1928)
This commit is contained in:
@@ -2,7 +2,7 @@ import asyncio
|
|||||||
|
|
||||||
import uvloop
|
import uvloop
|
||||||
|
|
||||||
uvloop.install() # noqa
|
uvloop.install()
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import pytest_asyncio
|
import pytest_asyncio
|
||||||
@@ -16,7 +16,12 @@ from lnbits.core.services import update_wallet_balance
|
|||||||
from lnbits.core.views.api import api_payments_create_invoice
|
from lnbits.core.views.api import api_payments_create_invoice
|
||||||
from lnbits.db import Database
|
from lnbits.db import Database
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
from tests.helpers import get_hold_invoice, get_random_invoice_data, get_real_invoice
|
from tests.helpers import (
|
||||||
|
clean_database,
|
||||||
|
get_hold_invoice,
|
||||||
|
get_random_invoice_data,
|
||||||
|
get_real_invoice,
|
||||||
|
)
|
||||||
|
|
||||||
# override settings for tests
|
# override settings for tests
|
||||||
settings.lnbits_admin_extensions = []
|
settings.lnbits_admin_extensions = []
|
||||||
@@ -35,6 +40,7 @@ def event_loop():
|
|||||||
# use session scope to run once before and once after all tests
|
# use session scope to run once before and once after all tests
|
||||||
@pytest_asyncio.fixture(scope="session")
|
@pytest_asyncio.fixture(scope="session")
|
||||||
async def app():
|
async def app():
|
||||||
|
clean_database(settings)
|
||||||
app = create_app()
|
app = create_app()
|
||||||
await app.router.startup()
|
await app.router.startup()
|
||||||
yield app
|
yield app
|
||||||
|
@@ -7,8 +7,12 @@ import time
|
|||||||
from subprocess import PIPE, Popen, TimeoutExpired
|
from subprocess import PIPE, Popen, TimeoutExpired
|
||||||
from typing import Tuple
|
from typing import Tuple
|
||||||
|
|
||||||
|
import psycopg2
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
from sqlalchemy.engine.url import make_url
|
||||||
|
|
||||||
|
from lnbits import core
|
||||||
|
from lnbits.db import DB_TYPE, POSTGRES
|
||||||
from lnbits.wallets import get_wallet_class, set_wallet_class
|
from lnbits.wallets import get_wallet_class, set_wallet_class
|
||||||
|
|
||||||
|
|
||||||
@@ -131,3 +135,28 @@ def pay_onchain(address: str, sats: int) -> str:
|
|||||||
cmd = docker_bitcoin_cli.copy()
|
cmd = docker_bitcoin_cli.copy()
|
||||||
cmd.extend(["sendtoaddress", address, str(btc)])
|
cmd.extend(["sendtoaddress", address, str(btc)])
|
||||||
return run_cmd(cmd)
|
return run_cmd(cmd)
|
||||||
|
|
||||||
|
|
||||||
|
def clean_database(settings):
|
||||||
|
if DB_TYPE == POSTGRES:
|
||||||
|
db_url = make_url(settings.lnbits_database_url)
|
||||||
|
|
||||||
|
conn = psycopg2.connect(settings.lnbits_database_url)
|
||||||
|
conn.autocommit = True
|
||||||
|
with conn.cursor() as cur:
|
||||||
|
try:
|
||||||
|
cur.execute("DROP DATABASE lnbits_test")
|
||||||
|
except psycopg2.errors.InvalidCatalogName:
|
||||||
|
pass
|
||||||
|
cur.execute("CREATE DATABASE lnbits_test")
|
||||||
|
|
||||||
|
db_url.database = "lnbits_test"
|
||||||
|
settings.lnbits_database_url = str(db_url)
|
||||||
|
|
||||||
|
core.db.__init__("database")
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
else:
|
||||||
|
# FIXME: do this once mock data is removed from test data folder
|
||||||
|
# os.remove(settings.lnbits_data_folder + "/database.sqlite3")
|
||||||
|
pass
|
||||||
|
Reference in New Issue
Block a user