Skip to content

Commit

Permalink
tests: add tests for event client manager (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc authored Oct 4, 2024
1 parent ca7269e commit b92999c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ coverage:
status:
project: off
patch: off
ignore:
- "cashu/lightning/lnd_grpc/protos"
- "cashu/nostr"
114 changes: 77 additions & 37 deletions tests/test_mint_db.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,67 @@
import asyncio
from unittest.mock import AsyncMock

import pytest
import pytest_asyncio
from fastapi import WebSocket

from cashu.core.base import MeltQuoteState, MintQuoteState
from cashu.core.json_rpc.base import (
JSONRPCMethods,
JSONRPCNotficationParams,
JSONRPCNotification,
JSONRPCSubscriptionKinds,
)
from cashu.core.models import PostMeltQuoteRequest
from cashu.mint.ledger import Ledger
from cashu.wallet.wallet import Wallet
from cashu.wallet.wallet import Wallet as Wallet1
from tests.conftest import SERVER_ENDPOINT
from tests.helpers import (
assert_err,
is_github_actions,
is_postgres,
pay_if_regtest,
)


@pytest_asyncio.fixture(scope="function")
async def wallet1(ledger: Ledger):
wallet1 = await Wallet1.with_db(
async def wallet(ledger: Ledger):
wallet = await Wallet.with_db(
url=SERVER_ENDPOINT,
db="test_data/wallet1",
name="wallet1",
db="test_data/wallet",
name="wallet",
)
await wallet1.load_mint()
yield wallet1
await wallet.load_mint()
yield wallet


@pytest.mark.asyncio
@pytest.mark.skipif(is_github_actions, reason="GITHUB_ACTIONS")
async def test_mint_proofs_pending(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(64)
async def test_mint_proofs_pending(wallet: Wallet, ledger: Ledger):
invoice = await wallet.request_mint(64)
await pay_if_regtest(invoice.bolt11)
await wallet1.mint(64, id=invoice.id)
proofs = wallet1.proofs.copy()
await wallet.mint(64, id=invoice.id)
proofs = wallet.proofs.copy()

proofs_states_before_split = await wallet1.check_proof_state(proofs)
proofs_states_before_split = await wallet.check_proof_state(proofs)
assert all([s.unspent for s in proofs_states_before_split.states])

await ledger.db_write._verify_spent_proofs_and_set_pending(proofs)

proof_states = await wallet1.check_proof_state(proofs)
proof_states = await wallet.check_proof_state(proofs)
assert all([s.pending for s in proof_states.states])
await assert_err(wallet1.split(wallet1.proofs, 20), "proofs are pending.")
await assert_err(wallet.split(wallet.proofs, 20), "proofs are pending.")

await ledger.db_write._unset_proofs_pending(proofs)

await wallet1.split(proofs, 20)
await wallet.split(proofs, 20)

proofs_states_after_split = await wallet1.check_proof_state(proofs)
proofs_states_after_split = await wallet.check_proof_state(proofs)
assert all([s.spent for s in proofs_states_after_split.states])


@pytest.mark.asyncio
async def test_mint_quote(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(128)
async def test_mint_quote(wallet: Wallet, ledger: Ledger):
invoice = await wallet.request_mint(128)
assert invoice is not None
quote = await ledger.crud.get_mint_quote(quote_id=invoice.id, db=ledger.db)
assert quote is not None
Expand All @@ -67,8 +75,8 @@ async def test_mint_quote(wallet1: Wallet, ledger: Ledger):


@pytest.mark.asyncio
async def test_mint_quote_state_transitions(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(128)
async def test_mint_quote_state_transitions(wallet: Wallet, ledger: Ledger):
invoice = await wallet.request_mint(128)
assert invoice is not None
quote = await ledger.crud.get_mint_quote(quote_id=invoice.id, db=ledger.db)
assert quote is not None
Expand Down Expand Up @@ -120,8 +128,8 @@ async def set_state(quote, state):


@pytest.mark.asyncio
async def test_get_mint_quote_by_request(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(128)
async def test_get_mint_quote_by_request(wallet: Wallet, ledger: Ledger):
invoice = await wallet.request_mint(128)
assert invoice is not None
quote = await ledger.crud.get_mint_quote(request=invoice.bolt11, db=ledger.db)
assert quote is not None
Expand All @@ -134,8 +142,8 @@ async def test_get_mint_quote_by_request(wallet1: Wallet, ledger: Ledger):


@pytest.mark.asyncio
async def test_melt_quote(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(128)
async def test_melt_quote(wallet: Wallet, ledger: Ledger):
invoice = await wallet.request_mint(128)
assert invoice is not None
melt_quote = await ledger.melt_quote(
PostMeltQuoteRequest(request=invoice.bolt11, unit="sat")
Expand All @@ -152,8 +160,8 @@ async def test_melt_quote(wallet1: Wallet, ledger: Ledger):


@pytest.mark.asyncio
async def test_melt_quote_set_pending(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(128)
async def test_melt_quote_set_pending(wallet: Wallet, ledger: Ledger):
invoice = await wallet.request_mint(128)
assert invoice is not None
melt_quote = await ledger.melt_quote(
PostMeltQuoteRequest(request=invoice.bolt11, unit="sat")
Expand All @@ -178,8 +186,8 @@ async def test_melt_quote_set_pending(wallet1: Wallet, ledger: Ledger):


@pytest.mark.asyncio
async def test_melt_quote_state_transitions(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(128)
async def test_melt_quote_state_transitions(wallet: Wallet, ledger: Ledger):
invoice = await wallet.request_mint(128)
assert invoice is not None
melt_quote = await ledger.melt_quote(
PostMeltQuoteRequest(request=invoice.bolt11, unit="sat")
Expand Down Expand Up @@ -209,8 +217,8 @@ async def set_state(quote, state):


@pytest.mark.asyncio
async def test_mint_quote_set_pending(wallet1: Wallet, ledger: Ledger):
invoice = await wallet1.request_mint(128)
async def test_mint_quote_set_pending(wallet: Wallet, ledger: Ledger):
invoice = await wallet.request_mint(128)
assert invoice is not None
quote = await ledger.crud.get_mint_quote(quote_id=invoice.id, db=ledger.db)
assert quote is not None
Expand All @@ -231,7 +239,7 @@ async def test_mint_quote_set_pending(wallet1: Wallet, ledger: Ledger):
assert quote.pending

# try to mint while pending
await assert_err(wallet1.mint(128, id=invoice.id), "Mint quote already pending.")
await assert_err(wallet.mint(128, id=invoice.id), "Mint quote already pending.")

# set unpending
await ledger.db_write._unset_mint_quote_pending(quote.quote, previous_state)
Expand All @@ -245,7 +253,7 @@ async def test_mint_quote_set_pending(wallet1: Wallet, ledger: Ledger):
# quote.state = MintQuoteState.paid
# await ledger.crud.update_mint_quote(quote=quote, db=ledger.db)

await wallet1.mint(quote.amount, id=quote.quote)
await wallet.mint(quote.amount, id=quote.quote)

# check if quote is issued
quote = await ledger.crud.get_mint_quote(quote_id=invoice.id, db=ledger.db)
Expand All @@ -254,7 +262,39 @@ async def test_mint_quote_set_pending(wallet1: Wallet, ledger: Ledger):


@pytest.mark.asyncio
@pytest.mark.skipif(not is_postgres, reason="only works with Postgres")
async def test_postgres_working():
assert is_postgres
assert True
async def test_db_events_add_client(wallet: Wallet, ledger: Ledger):
invoice = await wallet.request_mint(128)
assert invoice is not None
melt_quote = await ledger.melt_quote(
PostMeltQuoteRequest(request=invoice.bolt11, unit="sat")
)
assert melt_quote is not None
assert melt_quote.state == MeltQuoteState.unpaid.value
quote = await ledger.crud.get_melt_quote(quote_id=melt_quote.quote, db=ledger.db)
assert quote is not None
assert quote.quote == melt_quote.quote
assert quote.unpaid

# add event client
websocket_mock = AsyncMock(spec=WebSocket)
client = ledger.events.add_client(websocket_mock, ledger.db, ledger.crud)
asyncio.create_task(client.start())
await asyncio.sleep(0.1)
websocket_mock.accept.assert_called_once()

# add subscription
client.add_subscription(
JSONRPCSubscriptionKinds.BOLT11_MELT_QUOTE, [quote.quote], "subId"
)
quote_pending = await ledger.db_write._set_melt_quote_pending(quote)
await asyncio.sleep(0.1)
notification = JSONRPCNotification(
method=JSONRPCMethods.SUBSCRIBE.value,
params=JSONRPCNotficationParams(
subId="subId", payload=quote_pending.dict()
).dict(),
)
websocket_mock.send_text.assert_called_with(notification.json())

# remove subscription
client.remove_subscription("subId")

0 comments on commit b92999c

Please sign in to comment.