Skip to content

Commit c6efc0c

Browse files
committed
Add /address/{address}/utxo/{currency} tests
1 parent ecd22d2 commit c6efc0c

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

app/models/output.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from sqlalchemy.orm import Mapped
44
from sqlalchemy import Numeric
55
from sqlalchemy import String
6+
from decimal import Decimal
67
from .base import Base
78
from typing import Any
89

@@ -16,7 +17,7 @@ class Output(Base):
1617
blockhash: Mapped[str] = mapped_column(String(64), index=True)
1718
address: Mapped[str] = mapped_column(String(70), index=True)
1819
txid: Mapped[str] = mapped_column(String(64), index=True)
19-
amount: Mapped[Numeric] = mapped_column(Numeric(28, 8))
20+
amount: Mapped[Decimal] = mapped_column(Numeric(28, 8))
2021
timelock: Mapped[int]
2122
type: Mapped[str] = mapped_column(String(64), index=True)
2223
script: Mapped[str]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from sqlalchemy.ext.asyncio import AsyncSession
2+
from async_asgi_testclient import TestClient
3+
from tests.client_requests import addresses
4+
from app.models.output import Output
5+
from app.utils import to_satoshi
6+
from tests import helpers
7+
import secrets
8+
9+
10+
async def test_default(client: TestClient, address_utxo: Output, session: AsyncSession):
11+
extra_amount = 100
12+
13+
utxo1 = await helpers.create_output(
14+
session,
15+
address_utxo.currency,
16+
address=address_utxo.address,
17+
spent=False,
18+
shortcut=secrets.token_hex(16),
19+
amount=123,
20+
)
21+
22+
required_amount = address_utxo.amount + utxo1.amount + extra_amount
23+
24+
response = await addresses.get_address_utxo(
25+
client, address_utxo.address, float(required_amount), "MBC"
26+
)
27+
print(response.json())
28+
assert response.status_code == 200
29+
30+
assert response.json()["pagination"] == {"total": 2, "pages": 1, "page": 1}
31+
32+
for txo in response.json()["list"]:
33+
assert txo["timelock"] in (address_utxo.timelock, utxo1.timelock)
34+
assert txo["amount"] in (
35+
to_satoshi(float(address_utxo.amount)),
36+
to_satoshi(float(utxo1.amount)),
37+
)
38+
assert txo["currency"] in (address_utxo.currency, utxo1.currency)
39+
assert txo["index"] in (address_utxo.index, utxo1.index)
40+
assert txo["type"] in (address_utxo.type, utxo1.type)
41+
assert txo["txid"] in (address_utxo.txid, utxo1.txid)
42+
assert txo["spent"] is False
43+
44+
45+
async def test_none(client, address):
46+
response = await addresses.get_address_utxo(client, address.address, 1, "MBC")
47+
print(response.json())
48+
assert response.status_code == 200
49+
50+
assert response.json()["pagination"] == {"total": 0, "pages": 0, "page": 1}
51+
assert response.json()["list"] == []

tests/client_requests/addresses.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ async def get_unspent_address_outputs(
1111
)
1212

1313

14+
async def get_address_utxo(
15+
client: TestClient, address: str, amount: float, currency: str, page: int = 1
16+
):
17+
"""Do not confuse get_unspent_address_outputs with this function (this may sound similar, but its not the same)"""
18+
return await client.get(
19+
f"/address/{address}/utxo/{currency}",
20+
query_string={"amount": amount, "page": page},
21+
)
22+
23+
1424
async def get_address_transactions(
1525
client: TestClient, address: str, page: int = 1
1626
) -> Response:
@@ -22,6 +32,4 @@ async def get_address_transactions(
2232
async def get_address_balances(
2333
client: TestClient, address: str, page: int = 1
2434
) -> Response:
25-
return await client.get(
26-
f"/address/{address}/balances", query_string={"page": page}
27-
)
35+
return await client.get(f"/address/{address}/balances", query_string={"page": page})

0 commit comments

Comments
 (0)