Skip to content

Commit d2b0169

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

File tree

4 files changed

+64
-5
lines changed

4 files changed

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

tests/helpers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from decimal import Decimal
12
import secrets
23

34
from sqlalchemy.ext.asyncio import AsyncSession
@@ -110,7 +111,7 @@ async def create_output(
110111
blockhash=blockhash or secrets.token_hex(32),
111112
address=address or secrets.token_hex(32),
112113
txid=txid or secrets.token_hex(32),
113-
amount=amount, # type: ignore
114+
amount=Decimal(amount),
114115
timelock=timelock,
115116
type=type,
116117
meta={},

0 commit comments

Comments
 (0)