From 9d15fd98edbee7ed67be6ab3b184522594b229d4 Mon Sep 17 00:00:00 2001 From: Ouziel Slama Date: Tue, 28 Jan 2025 10:33:32 +0000 Subject: [PATCH] mock for api v1 --- .../counterpartycore/pytest/conftest.py | 3 ++ .../counterpartycore/pytest/mocks/apiv1.py | 34 +++++++++++++++++++ .../pytest/units/api/apiv1_test.py | 15 ++++++++ 3 files changed, 52 insertions(+) create mode 100644 counterparty-core/counterpartycore/pytest/mocks/apiv1.py diff --git a/counterparty-core/counterpartycore/pytest/conftest.py b/counterparty-core/counterpartycore/pytest/conftest.py index f688249eb..b27c6cc65 100644 --- a/counterparty-core/counterpartycore/pytest/conftest.py +++ b/counterparty-core/counterpartycore/pytest/conftest.py @@ -7,6 +7,7 @@ from counterpartycore.lib.utils.helpers import to_short_json from counterpartycore.pytest.mocks.counterpartydbs import check_records +from .mocks.apiv1 import apiv1_app, apiv1_client from .mocks.bitcoind import bitcoind_mock, blockchain_mock, monkeymodule from .mocks.counterpartydbs import ( build_dbs, @@ -43,6 +44,8 @@ def test_helpers(): __all__ = [ + "apiv1_app", + "apiv1_client", "bitcoind_mock", "blockchain_mock", "build_dbs", diff --git a/counterparty-core/counterpartycore/pytest/mocks/apiv1.py b/counterparty-core/counterpartycore/pytest/mocks/apiv1.py new file mode 100644 index 000000000..470f2cc7e --- /dev/null +++ b/counterparty-core/counterpartycore/pytest/mocks/apiv1.py @@ -0,0 +1,34 @@ +import pytest +from counterpartycore.lib.api import apiv1 + + +@pytest.fixture() +def apiv1_app(ledger_db, state_db): + app = apiv1.create_app() + app.config.update( + { + "TESTING": True, + } + ) + yield app + + +def rpc_call(client, method, params): + import json + + headers = {"content-type": "application/json"} + payload = { + "method": method, + "params": params, + "jsonrpc": "2.0", + "id": 0, + } + return client.post("/", data=json.dumps(payload), headers=headers, auth=("rpc", "rpc")) + + +@pytest.fixture() +def apiv1_client(apiv1_app): + def call(method, params): + return rpc_call(apiv1_app.test_client(), method, params) + + return call diff --git a/counterparty-core/counterpartycore/pytest/units/api/apiv1_test.py b/counterparty-core/counterpartycore/pytest/units/api/apiv1_test.py index e118fcfe7..97d278759 100644 --- a/counterparty-core/counterpartycore/pytest/units/api/apiv1_test.py +++ b/counterparty-core/counterpartycore/pytest/units/api/apiv1_test.py @@ -5,6 +5,21 @@ from counterpartycore.lib.api import apiv1 +def test_create_burn(apiv1_client, defaults): + result = apiv1_client( + "create_burn", + { + "source": defaults["addresses"][1], + "quantity": defaults["burn_quantity"], + "encoding": "multisig", + }, + ).json + assert ( + result["result"] + == "0200000001c5c9b5c88b6c5e28fe4a4fd8b6792f2980c5cacc0cca959eff213818be01e0a80000000000ffffffff02800bb203000000001976a914a11b66a67b3ff69671c8f82254099faf374b800e88acbcbce837000000001976a9148d6ae8a3b381663118b4e1eff4cfc7d0954dd6ec88ac00000000" + ) + + def test_get_rows(ledger_db, state_db, monkeypatch): # print(json.dumps(apiv1.get_rows("balances", None, "AND", None, None, None, None, None, 1, 0, True), indent=4))