From ec835a2935482b35c2e072cc4097722023d2d5d6 Mon Sep 17 00:00:00 2001 From: counterweight Date: Mon, 22 Dec 2025 16:06:56 +0100 Subject: [PATCH] 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. --- backend/price_fetcher.py | 4 ++++ backend/routes/audit.py | 6 +++--- backend/tests/test_price_history.py | 18 +++++++++--------- backend/worker.py | 6 +++--- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/backend/price_fetcher.py b/backend/price_fetcher.py index 954e6a3..52f0a05 100644 --- a/backend/price_fetcher.py +++ b/backend/price_fetcher.py @@ -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]: """ diff --git a/backend/routes/audit.py b/backend/routes/audit.py index b9e788e..d3709fc 100644 --- a/backend/routes/audit.py +++ b/backend/routes/audit.py @@ -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, ) diff --git a/backend/tests/test_price_history.py b/backend/tests/test_price_history.py index ec20e84..f0a6bc6 100644 --- a/backend/tests/test_price_history.py +++ b/backend/tests/test_price_history.py @@ -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 diff --git a/backend/worker.py b/backend/worker.py index d1a060f..83f5cb0 100644 --- a/backend/worker.py +++ b/backend/worker.py @@ -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, )