mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-22 03:24:18 +01:00
[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>
This commit is contained in:
@@ -38,32 +38,35 @@ from .responses import (
|
||||
router: APIRouter = APIRouter()
|
||||
|
||||
|
||||
def create_wallet(
|
||||
url=settings.mint_url, dir=settings.cashu_dir, name=settings.wallet_name
|
||||
):
|
||||
return Wallet(
|
||||
url=url,
|
||||
db=os.path.join(dir, name),
|
||||
name=name,
|
||||
async def mint_wallet(mint_url: Optional[str] = None):
|
||||
wallet: Wallet = await Wallet.with_db(
|
||||
mint_url or settings.mint_url,
|
||||
db=os.path.join(settings.cashu_dir, settings.wallet_name),
|
||||
name=settings.wallet_name,
|
||||
)
|
||||
|
||||
|
||||
async def load_mint(wallet: Wallet, mint: Optional[str] = None):
|
||||
if mint:
|
||||
wallet = create_wallet(mint)
|
||||
await init_wallet(wallet)
|
||||
await wallet.load_mint()
|
||||
return wallet
|
||||
|
||||
|
||||
wallet = create_wallet()
|
||||
wallet: Wallet = Wallet(
|
||||
settings.mint_url,
|
||||
db=os.path.join(settings.cashu_dir, settings.wallet_name),
|
||||
name=settings.wallet_name,
|
||||
)
|
||||
|
||||
|
||||
@router.on_event("startup")
|
||||
async def start_wallet():
|
||||
global wallet
|
||||
wallet = await Wallet.with_db(
|
||||
settings.mint_url,
|
||||
db=os.path.join(settings.cashu_dir, settings.wallet_name),
|
||||
name=settings.wallet_name,
|
||||
)
|
||||
|
||||
if settings.tor and not TorProxy().check_platform():
|
||||
raise Exception("tor not working.")
|
||||
await init_wallet(wallet)
|
||||
await wallet.load_mint()
|
||||
|
||||
|
||||
@router.post("/pay", name="Pay lightning invoice", response_model=PayResponse)
|
||||
@@ -78,7 +81,7 @@ async def pay(
|
||||
raise Exception("lightning not enabled.")
|
||||
|
||||
global wallet
|
||||
wallet = await load_mint(wallet, mint)
|
||||
wallet = await mint_wallet(mint)
|
||||
|
||||
total_amount, fee_reserve_sat = await wallet.get_pay_amount_with_fees(invoice)
|
||||
assert total_amount > 0, "amount has to be larger than zero."
|
||||
@@ -116,7 +119,7 @@ async def invoice(
|
||||
print(f"Requesting split with {n_splits}*{split} sat tokens.")
|
||||
|
||||
global wallet
|
||||
wallet = await load_mint(wallet, mint)
|
||||
wallet = await mint_wallet(mint)
|
||||
if not settings.lightning:
|
||||
await wallet.mint(amount, split=optional_split)
|
||||
return InvoiceResponse(
|
||||
@@ -150,8 +153,8 @@ async def swap(
|
||||
):
|
||||
if not settings.lightning:
|
||||
raise Exception("lightning not supported")
|
||||
incoming_wallet = await load_mint(wallet, mint=incoming_mint)
|
||||
outgoing_wallet = await load_mint(wallet, mint=outgoing_mint)
|
||||
incoming_wallet = await mint_wallet(incoming_mint)
|
||||
outgoing_wallet = await mint_wallet(outgoing_mint)
|
||||
if incoming_wallet.url == outgoing_wallet.url:
|
||||
raise Exception("mints for swap have to be different")
|
||||
|
||||
@@ -159,7 +162,7 @@ async def swap(
|
||||
invoice = await incoming_wallet.request_mint(amount)
|
||||
|
||||
# pay invoice from outgoing mint
|
||||
await outgoing_wallet.load_proofs()
|
||||
await outgoing_wallet.load_proofs(reload=True)
|
||||
total_amount, fee_reserve_sat = await outgoing_wallet.get_pay_amount_with_fees(
|
||||
invoice.pr
|
||||
)
|
||||
@@ -174,7 +177,7 @@ async def swap(
|
||||
|
||||
# mint token in incoming mint
|
||||
await incoming_wallet.mint(amount, hash=invoice.hash)
|
||||
await incoming_wallet.load_proofs()
|
||||
await incoming_wallet.load_proofs(reload=True)
|
||||
mint_balances = await incoming_wallet.balance_per_minturl()
|
||||
return SwapResponse(
|
||||
outgoing_mint=outgoing_mint,
|
||||
@@ -191,7 +194,7 @@ async def swap(
|
||||
response_model=BalanceResponse,
|
||||
)
|
||||
async def balance():
|
||||
await wallet.load_proofs()
|
||||
await wallet.load_proofs(reload=True)
|
||||
keyset_balances = wallet.balance_per_keyset()
|
||||
mint_balances = await wallet.balance_per_minturl()
|
||||
return BalanceResponse(
|
||||
@@ -229,6 +232,7 @@ async def receive_command(
|
||||
nostr: bool = Query(default=False, description="Receive tokens via nostr"),
|
||||
all: bool = Query(default=False, description="Receive all pending tokens"),
|
||||
):
|
||||
wallet = await mint_wallet()
|
||||
initial_balance = wallet.available_balance
|
||||
if token:
|
||||
tokenObj: TokenV3 = deserialize_token_from_string(token)
|
||||
@@ -269,7 +273,7 @@ async def burn(
|
||||
):
|
||||
global wallet
|
||||
if not delete:
|
||||
wallet = await load_mint(wallet, mint)
|
||||
wallet = await mint_wallet(mint)
|
||||
if not (all or token or force or delete) or (token and all):
|
||||
raise Exception(
|
||||
"enter a token or use --all to burn all pending tokens, --force to check all tokens"
|
||||
|
||||
Reference in New Issue
Block a user