mirror of
https://github.com/Routstr/routstr-core.git
synced 2026-07-30 15:26:14 +00:00
test: update tests to patch settings and use fixed pricing flags
This commit is contained in:
@@ -33,8 +33,8 @@ if use_local_services:
|
||||
"RECEIVE_LN_ADDRESS": "test@routstr.com",
|
||||
"REFUND_PROCESSING_INTERVAL": "3600",
|
||||
"NSEC": "nsec1testkey1234567890abcdef",
|
||||
"COST_PER_REQUEST": "10",
|
||||
"MODEL_BASED_PRICING": "true",
|
||||
"FIXED_COST_PER_REQUEST": "10",
|
||||
"FIXED_PRICING": "false",
|
||||
"MINIMUM_PAYOUT": "1000",
|
||||
"PAYOUT_INTERVAL": "86400",
|
||||
"NAME": "TestRoutstrNode",
|
||||
@@ -55,8 +55,8 @@ else:
|
||||
"RECEIVE_LN_ADDRESS": "test@routstr.com",
|
||||
"REFUND_PROCESSING_INTERVAL": "3600",
|
||||
"NSEC": "nsec1testkey1234567890abcdef",
|
||||
"COST_PER_REQUEST": "10",
|
||||
"MODEL_BASED_PRICING": "true",
|
||||
"FIXED_COST_PER_REQUEST": "10",
|
||||
"FIXED_PRICING": "false",
|
||||
"MINIMUM_PAYOUT": "1000",
|
||||
"PAYOUT_INTERVAL": "86400",
|
||||
}
|
||||
@@ -507,10 +507,11 @@ async def integration_app(
|
||||
else:
|
||||
# Use testmint with wallet patches for all integration tests
|
||||
mint_url = os.environ.get("CASHU_MINTS", "http://localhost:3338")
|
||||
from routstr.core.settings import settings as _settings
|
||||
|
||||
with (
|
||||
patch("routstr.core.db.engine", integration_engine),
|
||||
patch("routstr.wallet.TRUSTED_MINTS", [mint_url]),
|
||||
patch("routstr.wallet.PRIMARY_MINT_URL", mint_url),
|
||||
patch.object(_settings, "cashu_mints", [mint_url]),
|
||||
patch("routstr.auth.credit_balance", testmint_wallet.credit_balance),
|
||||
patch("routstr.wallet.credit_balance", testmint_wallet.credit_balance),
|
||||
patch("routstr.balance.credit_balance", testmint_wallet.credit_balance),
|
||||
|
||||
@@ -628,14 +628,11 @@ class TestEdgeCaseCombinations:
|
||||
a single request (which costs 1000 msats). It then makes 5 concurrent requests
|
||||
to verify that all requests fail with 402 Payment Required errors.
|
||||
|
||||
Note: The test disables MODEL_BASED_PRICING to avoid model lookup errors
|
||||
Note: The test enables fixed pricing to avoid model lookup errors
|
||||
since the test environment doesn't have models configured.
|
||||
"""
|
||||
# Disable MODEL_BASED_PRICING for this test to avoid model lookup issues
|
||||
monkeypatch.setattr(
|
||||
"routstr.payment.cost_caculation.MODEL_BASED_PRICING", False
|
||||
)
|
||||
monkeypatch.setattr("routstr.payment.helpers.MODEL_BASED_PRICING", False)
|
||||
# Disable model-based pricing for this test to avoid model lookup issues
|
||||
monkeypatch.setattr("routstr.core.settings.settings.fixed_pricing", True)
|
||||
|
||||
# Create a new API key with very low balance
|
||||
# Generate a unique API key
|
||||
@@ -645,7 +642,7 @@ class TestEdgeCaseCombinations:
|
||||
# Create the API key with only 500 msats (less than one request cost)
|
||||
new_key = ApiKey(
|
||||
hashed_key=api_key_hash,
|
||||
balance=500, # Less than COST_PER_REQUEST (1000 msats)
|
||||
balance=500, # Less than fixed cost per request (1000 msats)
|
||||
reserved_balance=0,
|
||||
total_spent=0,
|
||||
total_requests=0,
|
||||
|
||||
@@ -5,6 +5,7 @@ from unittest.mock import Mock, patch
|
||||
os.environ["UPSTREAM_BASE_URL"] = "http://test"
|
||||
os.environ["UPSTREAM_API_KEY"] = "test"
|
||||
|
||||
from routstr.core.settings import settings # noqa: E402
|
||||
from routstr.payment.helpers import get_max_cost_for_model # noqa: E402
|
||||
|
||||
|
||||
@@ -15,23 +16,23 @@ def test_get_max_cost_for_model_known() -> None:
|
||||
mock_model.sats_pricing.max_cost = 500
|
||||
|
||||
with patch("routstr.payment.helpers.MODELS", [mock_model]):
|
||||
with patch("routstr.payment.helpers.MODEL_BASED_PRICING", True):
|
||||
with patch.object(settings, "fixed_pricing", False):
|
||||
cost = get_max_cost_for_model("gpt-4", tolerance_percentage=0)
|
||||
assert cost == 500000 # 500 sats * 1000 = msats
|
||||
|
||||
|
||||
def test_get_max_cost_for_model_unknown() -> None:
|
||||
with patch("routstr.payment.helpers.MODELS", []):
|
||||
with patch("routstr.payment.helpers.COST_PER_REQUEST", 100):
|
||||
with patch.object(settings, "fixed_cost_per_request", 100):
|
||||
cost = get_max_cost_for_model("unknown-model", tolerance_percentage=0)
|
||||
assert cost == 100
|
||||
assert cost == 100000
|
||||
|
||||
|
||||
def test_get_max_cost_for_model_disabled() -> None:
|
||||
with patch("routstr.payment.helpers.MODEL_BASED_PRICING", False):
|
||||
with patch("routstr.payment.helpers.COST_PER_REQUEST", 200):
|
||||
with patch.object(settings, "fixed_pricing", True):
|
||||
with patch.object(settings, "fixed_cost_per_request", 200):
|
||||
cost = get_max_cost_for_model("any-model", tolerance_percentage=0)
|
||||
assert cost == 200
|
||||
assert cost == 200000
|
||||
|
||||
|
||||
def test_get_max_cost_for_model_tolerance() -> None:
|
||||
@@ -41,6 +42,6 @@ def test_get_max_cost_for_model_tolerance() -> None:
|
||||
mock_model.sats_pricing.max_cost = 500
|
||||
|
||||
with patch("routstr.payment.helpers.MODELS", [mock_model]):
|
||||
with patch("routstr.payment.helpers.MODEL_BASED_PRICING", True):
|
||||
with patch.object(settings, "fixed_pricing", False):
|
||||
cost = get_max_cost_for_model("gpt-4", tolerance_percentage=10)
|
||||
assert cost == 450000 # 500 sats * 1000 * 0.9 = 450000
|
||||
|
||||
@@ -39,7 +39,9 @@ async def test_recieve_token_valid() -> None:
|
||||
mock_wallet = Mock()
|
||||
mock_wallet.split = AsyncMock()
|
||||
|
||||
with patch("routstr.wallet.TRUSTED_MINTS", ["http://mint:3338"]):
|
||||
from routstr.core.settings import settings
|
||||
|
||||
with patch.object(settings, "cashu_mints", ["http://mint:3338"]):
|
||||
with patch("routstr.wallet.deserialize_token_from_string") as mock_deserialize:
|
||||
mock_token = Mock()
|
||||
mock_token.keysets = ["keyset1"]
|
||||
@@ -82,7 +84,9 @@ async def test_credit_balance() -> None:
|
||||
mock_key.balance = 5000000
|
||||
mock_session = AsyncMock()
|
||||
|
||||
with patch("routstr.wallet.PRIMARY_MINT_URL", "http://mint:3338"):
|
||||
from routstr.core.settings import settings
|
||||
|
||||
with patch.object(settings, "cashu_mints", ["http://mint:3338"]):
|
||||
with patch(
|
||||
"routstr.wallet.recieve_token",
|
||||
return_value=(1000, "sat", "http://mint:3338"),
|
||||
|
||||
Reference in New Issue
Block a user