Spaces:
Paused
Paused
File size: 6,460 Bytes
f3bcb28 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | """Tests for Budget API."""
import pytest
from backend.api.admin.budgets import (
BudgetResponse,
BudgetListResponse,
CreateBudgetRequest,
UpdateBudgetRequest,
)
from backend.db.models import BudgetScope, BudgetPeriod
class TestBudgetResponse:
"""Test BudgetResponse model."""
def test_budget_response_fields(self):
"""Should have correct fields for budget."""
response = BudgetResponse(
uuid="test-uuid",
app_id="my-app",
app_name="My Application",
limit_usd=100.0,
spent_usd=25.0,
remaining_usd=75.0,
usage_percent=25.0,
period="monthly",
is_exceeded=False,
)
assert response.uuid == "test-uuid"
assert response.app_id == "my-app"
assert response.app_name == "My Application"
assert response.limit_usd == 100.0
assert response.spent_usd == 25.0
assert response.remaining_usd == 75.0
assert response.usage_percent == 25.0
assert response.period == "monthly"
assert response.is_exceeded is False
def test_budget_response_exceeded(self):
"""Should correctly identify exceeded budget."""
response = BudgetResponse(
uuid="test-uuid",
app_id="my-app",
app_name=None,
limit_usd=100.0,
spent_usd=150.0,
remaining_usd=0.0,
usage_percent=150.0,
period="monthly",
is_exceeded=True,
)
assert response.is_exceeded is True
assert response.remaining_usd == 0.0
assert response.usage_percent == 150.0
class TestCreateBudgetRequest:
"""Test CreateBudgetRequest validation."""
def test_create_budget_request_valid(self):
"""Should accept valid create request."""
request = CreateBudgetRequest(
app_id="my-app",
limit_usd=100.0,
)
assert request.app_id == "my-app"
assert request.limit_usd == 100.0
assert request.period == "monthly" # default
def test_create_budget_request_custom_period(self):
"""Should accept custom period."""
request = CreateBudgetRequest(
app_id="my-app",
limit_usd=500.0,
period="yearly",
)
assert request.period == "yearly"
def test_create_budget_request_requires_positive_limit(self):
"""Should reject negative limit."""
with pytest.raises(ValueError):
CreateBudgetRequest(
app_id="my-app",
limit_usd=-10.0,
)
class TestUpdateBudgetRequest:
"""Test UpdateBudgetRequest validation."""
def test_update_budget_request_valid(self):
"""Should accept valid update request."""
request = UpdateBudgetRequest(limit_usd=200.0)
assert request.limit_usd == 200.0
def test_update_budget_request_optional_limit(self):
"""Should allow empty update."""
request = UpdateBudgetRequest()
assert request.limit_usd is None
def test_update_budget_request_requires_non_negative_limit(self):
"""Should reject negative limit."""
with pytest.raises(ValueError):
UpdateBudgetRequest(limit_usd=-5.0)
class TestBudgetListResponse:
"""Test BudgetListResponse model."""
def test_budget_list_response_empty(self):
"""Should handle empty list."""
response = BudgetListResponse(items=[], total=0)
assert len(response.items) == 0
assert response.total == 0
def test_budget_list_response_with_items(self):
"""Should handle list with items."""
items = [
BudgetResponse(
uuid="uuid-1",
app_id="app-1",
app_name="App One",
limit_usd=100.0,
spent_usd=50.0,
remaining_usd=50.0,
usage_percent=50.0,
period="monthly",
is_exceeded=False,
),
BudgetResponse(
uuid="uuid-2",
app_id="app-2",
app_name="App Two",
limit_usd=200.0,
spent_usd=250.0,
remaining_usd=0.0,
usage_percent=125.0,
period="monthly",
is_exceeded=True,
),
]
response = BudgetListResponse(items=items, total=2)
assert len(response.items) == 2
assert response.total == 2
assert response.items[0].app_id == "app-1"
assert response.items[1].is_exceeded is True
class TestBudgetCalculations:
"""Test budget calculation logic."""
def test_usage_percent_calculation(self):
"""Test usage percentage calculation."""
# 50% used
spent = 50.0
limit = 100.0
usage = (spent / limit * 100) if limit > 0 else 0
assert usage == 50.0
# 0% used
spent = 0.0
usage = (spent / limit * 100) if limit > 0 else 0
assert usage == 0.0
# 100% used
spent = 100.0
usage = (spent / limit * 100) if limit > 0 else 0
assert usage == 100.0
# Over 100%
spent = 150.0
usage = (spent / limit * 100) if limit > 0 else 0
assert usage == 150.0
def test_remaining_calculation(self):
"""Test remaining amount calculation."""
limit = 100.0
spent = 25.0
remaining = max(0, limit - spent)
assert remaining == 75.0
# Over budget should return 0
spent = 150.0
remaining = max(0, limit - spent)
assert remaining == 0.0
def test_is_exceeded_logic(self):
"""Test exceeded detection."""
limit = 100.0
# Not exceeded
spent = 99.99
assert (spent >= limit) is False
# Exactly at limit = exceeded
spent = 100.0
assert (spent >= limit) is True
# Over limit
spent = 100.01
assert (spent >= limit) is True
class TestBudgetScope:
"""Test budget scope and period."""
def test_application_scope_value(self):
"""APPLICATION scope should be valid."""
assert BudgetScope.APPLICATION.value == "application"
def test_budget_period_monthly(self):
"""MONTHLY period should be the default."""
assert BudgetPeriod.MONTHLY.value == "monthly"
|