Files
nutshell/tests/conftest.py
dni ⚡ 0a5beb75a2 [FEAT] Improve tests (#296)
* test cli arent async tests

* unused SERVER_ENDPOINT var

* async test werent marked async

* make test didnt use correct ports

* enable more verbose test logging

* refactor conftest variable

* not needed anymore are set in conftest

* using test_data now for conftest

* formatting

* comment out invalid hex

* remove test dir before creating it to be sure

* keep data from altest testrun and ad test_data to ignore

* ignore error for CI

* add duplicate env var

* fix confest

* Update pyproject.toml

* fix up tests

* short p2pk locktimes for faster tests

---------

Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
2023-08-06 18:35:34 +02:00

85 lines
2.2 KiB
Python

import multiprocessing
import os
import shutil
import time
from pathlib import Path
import pytest
import pytest_asyncio
import uvicorn
from uvicorn import Config, Server
from cashu.core.db import Database
from cashu.core.migrations import migrate_databases
from cashu.core.settings import settings
from cashu.lightning.fake import FakeWallet
from cashu.mint import migrations as migrations_mint
from cashu.mint.ledger import Ledger
SERVER_PORT = 3337
SERVER_ENDPOINT = f"http://localhost:{SERVER_PORT}"
settings.cashu_dir = "./test_data/"
settings.mint_host = "localhost"
settings.mint_port = SERVER_PORT
settings.mint_host = "0.0.0.0"
settings.mint_listen_port = SERVER_PORT
settings.mint_url = SERVER_ENDPOINT
settings.lightning = False
settings.tor = False
settings.mint_lightning_backend = "FakeWallet"
settings.mint_database = "./test_data/test_mint"
settings.mint_derivation_path = "0/0/0/0"
settings.mint_private_key = "TEST_PRIVATE_KEY"
shutil.rmtree(settings.cashu_dir, ignore_errors=True)
Path(settings.cashu_dir).mkdir(parents=True, exist_ok=True)
class UvicornServer(multiprocessing.Process):
def __init__(self, config: Config):
super().__init__()
self.server = Server(config=config)
self.config = config
def stop(self):
self.terminate()
def run(self, *args, **kwargs):
self.server.run()
@pytest_asyncio.fixture(scope="function")
async def ledger():
async def start_mint_init(ledger: Ledger):
await migrate_databases(ledger.db, migrations_mint)
await ledger.load_used_proofs()
await ledger.init_keysets()
db_file = "test_data/mint/test.sqlite3"
if os.path.exists(db_file):
os.remove(db_file)
ledger = Ledger(
db=Database("test", "test_data/mint"),
seed=settings.mint_private_key,
derivation_path=settings.mint_derivation_path,
lightning=FakeWallet(),
)
await start_mint_init(ledger)
yield ledger
@pytest.fixture(autouse=True, scope="session")
def mint():
config = uvicorn.Config(
"cashu.mint.app:app",
port=settings.mint_listen_port,
host=settings.mint_listen_host,
)
server = UvicornServer(config=config)
server.start()
time.sleep(1)
yield server
server.stop()