Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@



# --- 1. UNIT TEST: The Math Engine ---
def test_calculate_optimal_dispatch_basic_arbitrage():
# Setup a perfectly efficient battery for easy math
battery = BatterySpecs(capacity_mwh=2.0, max_mw=1.0, efficiency=1.0)
market_data = [
MarketInterval(timestamp=datetime.now(timezone.utc), price_aud_mwh=10.0), # Cheap
Expand All @@ -35,18 +33,13 @@ def test_calculate_optimal_dispatch_basic_arbitrage():
# ----------------


# The Math:
# Charge 1.0 MW for 1 hour (2 blocks) = 1.0 MWh total energy
# Cost: 1.0 MWh * $10 = $10
# Discharge: 1.0 MWh * $100 = $100
# Expected Profit: $100 - $10 = $90

assert result["total_profit_aud"] == 90.0
assert result["schedule"][0]["charge_mw"] == 1.0 # Should charge in the first block




# --- 2. INTEGRATION TEST: The API Endpoint ---
def test_optimize_endpoint_success():
payload = {
"battery": {
Expand All @@ -60,13 +53,10 @@ def test_optimize_endpoint_success():
]
}

# Send a fake POST request to your API
response = client.post("/api/v1/optimize", json=payload)

# Assert the server responded with 200 OK
assert response.status_code == 200

# Assert the JSON structure is exactly what we expect
data = response.json()
assert data["status"] == "success"
assert "total_profit_aud" in data["data"]
Expand All @@ -75,11 +65,10 @@ def test_optimize_endpoint_success():



# --- 3. EDGE CASE TEST: Pydantic Validation ---
def test_optimize_endpoint_invalid_battery():
payload = {
"battery": {
"capacity_mwh": -5.0, # INVALID: Capacity cannot be negative!
"capacity_mwh": -5.0,
"max_mw": 2.5,
"efficiency": 0.9
},
Expand All @@ -90,6 +79,4 @@ def test_optimize_endpoint_invalid_battery():

response = client.post("/api/v1/optimize", json=payload)

# Pydantic should intercept this bad data and return a 422 Unprocessable Entity error
# before it ever reaches the PuLP math engine.
assert response.status_code == 422
assert response.status_code == 422
Loading