Validate solar panel inputs and add tests

This commit is contained in:
blackboxprogramming
2025-08-30 17:56:30 -05:00
parent 844f304db0
commit c618e52219
2 changed files with 28 additions and 2 deletions

View File

@@ -18,9 +18,11 @@ def solar_panel_output(power_watt: float, hours: float, efficiency: float = 0.15
Parameters Parameters
---------- ----------
power_watt : float power_watt : float
The nominal power rating of the panel in watts (W). The nominal power rating of the panel in watts (W). Must be
nonnegative.
hours : float hours : float
Number of hours the panel operates under rated conditions. Number of hours the panel operates under rated conditions. Must be
nonnegative.
efficiency : float, optional efficiency : float, optional
Conversion efficiency (0 ≤ efficiency ≤ 1). Defaults to 0.15 (15 %). Conversion efficiency (0 ≤ efficiency ≤ 1). Defaults to 0.15 (15 %).
@@ -36,6 +38,8 @@ def solar_panel_output(power_watt: float, hours: float, efficiency: float = 0.15
factors. This simplified function treats the rated power and efficiency factors. This simplified function treats the rated power and efficiency
as constants over the specified duration. as constants over the specified duration.
""" """
if power_watt < 0 or hours < 0:
raise ValueError("Power and hours must be non-negative")
if efficiency < 0 or efficiency > 1: if efficiency < 0 or efficiency > 1:
raise ValueError("Efficiency must be between 0 and 1") raise ValueError("Efficiency must be between 0 and 1")
# Convert hours to seconds to compute energy in joules # Convert hours to seconds to compute energy in joules

View File

@@ -0,0 +1,22 @@
import pathlib
import sys
import pytest
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
from native_ai_quantum_energy.energy_simulator import solar_panel_output
def test_solar_panel_output_valid():
energy = solar_panel_output(100, 2, 0.5)
assert energy == pytest.approx(100 * 0.5 * 2 * 3600)
def test_solar_panel_output_negative_power():
with pytest.raises(ValueError):
solar_panel_output(-10, 1)
def test_solar_panel_output_negative_hours():
with pytest.raises(ValueError):
solar_panel_output(10, -1)