refactor: extract 'bitfinex' and 'BTC/EUR' magic strings to constants
Add SOURCE_BITFINEX and PAIR_BTC_EUR constants in price_fetcher.py and use them consistently in routes/audit.py, worker.py, and tests.
This commit is contained in:
parent
dd7bec6091
commit
ec835a2935
4 changed files with 19 additions and 15 deletions
|
|
@ -7,6 +7,10 @@ import httpx
|
||||||
BITFINEX_TICKER_URL = "https://api-pub.bitfinex.com/v2/ticker/tBTCEUR"
|
BITFINEX_TICKER_URL = "https://api-pub.bitfinex.com/v2/ticker/tBTCEUR"
|
||||||
LAST_PRICE_INDEX = 6
|
LAST_PRICE_INDEX = 6
|
||||||
|
|
||||||
|
# Constants for price history records
|
||||||
|
SOURCE_BITFINEX = "bitfinex"
|
||||||
|
PAIR_BTC_EUR = "BTC/EUR"
|
||||||
|
|
||||||
|
|
||||||
async def fetch_btc_eur_price() -> tuple[float, datetime]:
|
async def fetch_btc_eur_price() -> tuple[float, datetime]:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ from pagination import (
|
||||||
calculate_total_pages,
|
calculate_total_pages,
|
||||||
create_paginated_response,
|
create_paginated_response,
|
||||||
)
|
)
|
||||||
from price_fetcher import fetch_btc_eur_price
|
from price_fetcher import PAIR_BTC_EUR, SOURCE_BITFINEX, fetch_btc_eur_price
|
||||||
from schemas import (
|
from schemas import (
|
||||||
CounterRecordResponse,
|
CounterRecordResponse,
|
||||||
PaginatedCounterRecords,
|
PaginatedCounterRecords,
|
||||||
|
|
@ -194,8 +194,8 @@ async def fetch_price_now(
|
||||||
price, timestamp = await fetch_btc_eur_price()
|
price, timestamp = await fetch_btc_eur_price()
|
||||||
|
|
||||||
record = PriceHistory(
|
record = PriceHistory(
|
||||||
source="bitfinex",
|
source=SOURCE_BITFINEX,
|
||||||
pair="BTC/EUR",
|
pair=PAIR_BTC_EUR,
|
||||||
price=price,
|
price=price,
|
||||||
timestamp=timestamp,
|
timestamp=timestamp,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from models import PriceHistory
|
from models import PriceHistory
|
||||||
from price_fetcher import fetch_btc_eur_price
|
from price_fetcher import PAIR_BTC_EUR, SOURCE_BITFINEX, fetch_btc_eur_price
|
||||||
from worker import process_bitcoin_price_job
|
from worker import process_bitcoin_price_job
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -134,8 +134,8 @@ class TestGetPriceHistory:
|
||||||
now = datetime.now(UTC)
|
now = datetime.now(UTC)
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
record = PriceHistory(
|
record = PriceHistory(
|
||||||
source="bitfinex",
|
source=SOURCE_BITFINEX,
|
||||||
pair="BTC/EUR",
|
pair=PAIR_BTC_EUR,
|
||||||
price=90000.0 + i * 100,
|
price=90000.0 + i * 100,
|
||||||
timestamp=now.replace(second=i),
|
timestamp=now.replace(second=i),
|
||||||
)
|
)
|
||||||
|
|
@ -159,8 +159,8 @@ class TestGetPriceHistory:
|
||||||
now = datetime.now(UTC)
|
now = datetime.now(UTC)
|
||||||
for i in range(25):
|
for i in range(25):
|
||||||
record = PriceHistory(
|
record = PriceHistory(
|
||||||
source="bitfinex",
|
source=SOURCE_BITFINEX,
|
||||||
pair="BTC/EUR",
|
pair=PAIR_BTC_EUR,
|
||||||
price=90000.0 + i,
|
price=90000.0 + i,
|
||||||
timestamp=now.replace(microsecond=i),
|
timestamp=now.replace(microsecond=i),
|
||||||
)
|
)
|
||||||
|
|
@ -209,8 +209,8 @@ class TestManualFetch:
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
data = response.json()
|
data = response.json()
|
||||||
assert data["source"] == "bitfinex"
|
assert data["source"] == SOURCE_BITFINEX
|
||||||
assert data["pair"] == "BTC/EUR"
|
assert data["pair"] == PAIR_BTC_EUR
|
||||||
assert data["price"] == 95123.45
|
assert data["price"] == 95123.45
|
||||||
assert "id" in data
|
assert "id" in data
|
||||||
assert "timestamp" in data
|
assert "timestamp" in data
|
||||||
|
|
@ -280,8 +280,8 @@ class TestProcessBitcoinPriceJob:
|
||||||
call_args = mock_conn.execute.call_args
|
call_args = mock_conn.execute.call_args
|
||||||
|
|
||||||
# Check the SQL parameters
|
# Check the SQL parameters
|
||||||
assert call_args[0][1] == "bitfinex" # source
|
assert call_args[0][1] == SOURCE_BITFINEX # source
|
||||||
assert call_args[0][2] == "BTC/EUR" # pair
|
assert call_args[0][2] == PAIR_BTC_EUR # pair
|
||||||
assert call_args[0][3] == 95000.0 # price
|
assert call_args[0][3] == 95000.0 # price
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ from pgqueuer.queries import Queries
|
||||||
|
|
||||||
from database import ASYNCPG_DATABASE_URL
|
from database import ASYNCPG_DATABASE_URL
|
||||||
from jobs import JOB_FETCH_BITCOIN_PRICE, JOB_RANDOM_NUMBER
|
from jobs import JOB_FETCH_BITCOIN_PRICE, JOB_RANDOM_NUMBER
|
||||||
from price_fetcher import fetch_btc_eur_price
|
from price_fetcher import PAIR_BTC_EUR, SOURCE_BITFINEX, fetch_btc_eur_price
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.INFO,
|
level=logging.INFO,
|
||||||
|
|
@ -116,8 +116,8 @@ async def process_bitcoin_price_job(db_pool: asyncpg.Pool) -> None:
|
||||||
VALUES ($1, $2, $3, $4, NOW())
|
VALUES ($1, $2, $3, $4, NOW())
|
||||||
ON CONFLICT (source, pair, timestamp) DO NOTHING
|
ON CONFLICT (source, pair, timestamp) DO NOTHING
|
||||||
""",
|
""",
|
||||||
"bitfinex",
|
SOURCE_BITFINEX,
|
||||||
"BTC/EUR",
|
PAIR_BTC_EUR,
|
||||||
price,
|
price,
|
||||||
timestamp,
|
timestamp,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue