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"
|
||||
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]:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ from pagination import (
|
|||
calculate_total_pages,
|
||||
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 (
|
||||
CounterRecordResponse,
|
||||
PaginatedCounterRecords,
|
||||
|
|
@ -194,8 +194,8 @@ async def fetch_price_now(
|
|||
price, timestamp = await fetch_btc_eur_price()
|
||||
|
||||
record = PriceHistory(
|
||||
source="bitfinex",
|
||||
pair="BTC/EUR",
|
||||
source=SOURCE_BITFINEX,
|
||||
pair=PAIR_BTC_EUR,
|
||||
price=price,
|
||||
timestamp=timestamp,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
|||
import pytest
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
|
@ -134,8 +134,8 @@ class TestGetPriceHistory:
|
|||
now = datetime.now(UTC)
|
||||
for i in range(5):
|
||||
record = PriceHistory(
|
||||
source="bitfinex",
|
||||
pair="BTC/EUR",
|
||||
source=SOURCE_BITFINEX,
|
||||
pair=PAIR_BTC_EUR,
|
||||
price=90000.0 + i * 100,
|
||||
timestamp=now.replace(second=i),
|
||||
)
|
||||
|
|
@ -159,8 +159,8 @@ class TestGetPriceHistory:
|
|||
now = datetime.now(UTC)
|
||||
for i in range(25):
|
||||
record = PriceHistory(
|
||||
source="bitfinex",
|
||||
pair="BTC/EUR",
|
||||
source=SOURCE_BITFINEX,
|
||||
pair=PAIR_BTC_EUR,
|
||||
price=90000.0 + i,
|
||||
timestamp=now.replace(microsecond=i),
|
||||
)
|
||||
|
|
@ -209,8 +209,8 @@ class TestManualFetch:
|
|||
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert data["source"] == "bitfinex"
|
||||
assert data["pair"] == "BTC/EUR"
|
||||
assert data["source"] == SOURCE_BITFINEX
|
||||
assert data["pair"] == PAIR_BTC_EUR
|
||||
assert data["price"] == 95123.45
|
||||
assert "id" in data
|
||||
assert "timestamp" in data
|
||||
|
|
@ -280,8 +280,8 @@ class TestProcessBitcoinPriceJob:
|
|||
call_args = mock_conn.execute.call_args
|
||||
|
||||
# Check the SQL parameters
|
||||
assert call_args[0][1] == "bitfinex" # source
|
||||
assert call_args[0][2] == "BTC/EUR" # pair
|
||||
assert call_args[0][1] == SOURCE_BITFINEX # source
|
||||
assert call_args[0][2] == PAIR_BTC_EUR # pair
|
||||
assert call_args[0][3] == 95000.0 # price
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ from pgqueuer.queries import Queries
|
|||
|
||||
from database import ASYNCPG_DATABASE_URL
|
||||
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(
|
||||
level=logging.INFO,
|
||||
|
|
@ -116,8 +116,8 @@ async def process_bitcoin_price_job(db_pool: asyncpg.Pool) -> None:
|
|||
VALUES ($1, $2, $3, $4, NOW())
|
||||
ON CONFLICT (source, pair, timestamp) DO NOTHING
|
||||
""",
|
||||
"bitfinex",
|
||||
"BTC/EUR",
|
||||
SOURCE_BITFINEX,
|
||||
PAIR_BTC_EUR,
|
||||
price,
|
||||
timestamp,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue