From b92999cb219a8e5e798cb010f3ac73561b11e436 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Fri, 4 Oct 2024 13:11:23 +0200 Subject: [PATCH] tests: add tests for event client manager (#632) --- .codecov.yaml | 3 ++ tests/test_mint_db.py | 114 ++++++++++++++++++++++++++++-------------- 2 files changed, 80 insertions(+), 37 deletions(-) diff --git a/.codecov.yaml b/.codecov.yaml index 35cde5c..e5a109a 100644 --- a/.codecov.yaml +++ b/.codecov.yaml @@ -2,3 +2,6 @@ coverage: status: project: off patch: off +ignore: + - "cashu/lightning/lnd_grpc/protos" + - "cashu/nostr" diff --git a/tests/test_mint_db.py b/tests/test_mint_db.py index 2dc7bb1..6c9864f 100644 --- a/tests/test_mint_db.py +++ b/tests/test_mint_db.py @@ -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 @@ -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 @@ -120,8 +128,8 @@ async def test_mint_quote_state_transitions(wallet1: Wallet, ledger: Ledger): @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 @@ -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") @@ -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") @@ -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") @@ -209,8 +217,8 @@ async def test_melt_quote_state_transitions(wallet1: Wallet, ledger: Ledger): @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 @@ -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) @@ -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) @@ -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")