Spaces:
Sleeping
Sleeping
Create test_app.py
Browse files- tests/test_app.py +97 -0
tests/test_app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from prophet import Prophet
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# ======================================
|
| 8 |
+
# Test 1: Data Generation
|
| 9 |
+
# ======================================
|
| 10 |
+
def test_sample_data_shape():
|
| 11 |
+
"""Test that sample data has correct structure"""
|
| 12 |
+
dates = pd.date_range(start="2021-01-01", end="2023-12-31", freq="D")
|
| 13 |
+
df = pd.DataFrame({
|
| 14 |
+
"ds": dates,
|
| 15 |
+
"y": np.random.randint(50, 200, len(dates)).astype(float)
|
| 16 |
+
})
|
| 17 |
+
assert "ds" in df.columns, "Missing ds column"
|
| 18 |
+
assert "y" in df.columns, "Missing y column"
|
| 19 |
+
assert len(df) > 0, "Empty dataframe"
|
| 20 |
+
print("✅ Test 1 passed: Data shape correct")
|
| 21 |
+
|
| 22 |
+
# ======================================
|
| 23 |
+
# Test 2: Data Types
|
| 24 |
+
# ======================================
|
| 25 |
+
def test_data_types():
|
| 26 |
+
"""Test correct data types"""
|
| 27 |
+
dates = pd.date_range(start="2022-01-01", periods=100, freq="D")
|
| 28 |
+
df = pd.DataFrame({"ds": dates, "y": np.random.rand(100) * 100})
|
| 29 |
+
assert pd.api.types.is_datetime64_any_dtype(df['ds']), "ds must be datetime"
|
| 30 |
+
assert pd.api.types.is_numeric_dtype(df['y']), "y must be numeric"
|
| 31 |
+
print("✅ Test 2 passed: Data types correct")
|
| 32 |
+
|
| 33 |
+
# ======================================
|
| 34 |
+
# Test 3: No Null Values
|
| 35 |
+
# ======================================
|
| 36 |
+
def test_no_nulls():
|
| 37 |
+
"""Test no null values in data"""
|
| 38 |
+
dates = pd.date_range(start="2022-01-01", periods=100, freq="D")
|
| 39 |
+
df = pd.DataFrame({"ds": dates, "y": np.random.rand(100) * 100})
|
| 40 |
+
assert df['ds'].isnull().sum() == 0, "Nulls found in ds"
|
| 41 |
+
assert df['y'].isnull().sum() == 0, "Nulls found in y"
|
| 42 |
+
print("✅ Test 3 passed: No null values")
|
| 43 |
+
|
| 44 |
+
# ======================================
|
| 45 |
+
# Test 4: Prophet Model Training
|
| 46 |
+
# ======================================
|
| 47 |
+
def test_prophet_trains():
|
| 48 |
+
"""Test Prophet model trains without error"""
|
| 49 |
+
dates = pd.date_range(start="2021-01-01", periods=365, freq="D")
|
| 50 |
+
df = pd.DataFrame({
|
| 51 |
+
"ds": dates,
|
| 52 |
+
"y": np.random.rand(365) * 100 + 50
|
| 53 |
+
})
|
| 54 |
+
model = Prophet(yearly_seasonality=True)
|
| 55 |
+
model.fit(df)
|
| 56 |
+
assert model is not None, "Model failed to train"
|
| 57 |
+
print("✅ Test 4 passed: Prophet model trained")
|
| 58 |
+
|
| 59 |
+
# ======================================
|
| 60 |
+
# Test 5: Prophet Forecast Output
|
| 61 |
+
# ======================================
|
| 62 |
+
def test_prophet_forecast():
|
| 63 |
+
"""Test Prophet generates correct forecast"""
|
| 64 |
+
dates = pd.date_range(start="2021-01-01", periods=365, freq="D")
|
| 65 |
+
df = pd.DataFrame({
|
| 66 |
+
"ds": dates,
|
| 67 |
+
"y": np.random.rand(365) * 100 + 50
|
| 68 |
+
})
|
| 69 |
+
model = Prophet()
|
| 70 |
+
model.fit(df)
|
| 71 |
+
future = model.make_future_dataframe(periods=30)
|
| 72 |
+
forecast = model.predict(future)
|
| 73 |
+
|
| 74 |
+
assert "yhat" in forecast.columns, "Missing yhat in forecast"
|
| 75 |
+
assert "yhat_lower" in forecast.columns, "Missing yhat_lower"
|
| 76 |
+
assert "yhat_upper" in forecast.columns, "Missing yhat_upper"
|
| 77 |
+
assert len(forecast) == len(df) + 30, "Forecast length incorrect"
|
| 78 |
+
print("✅ Test 5 passed: Forecast output correct")
|
| 79 |
+
|
| 80 |
+
# ======================================
|
| 81 |
+
# Test 6: Positive Demand Values
|
| 82 |
+
# ======================================
|
| 83 |
+
def test_positive_demand():
|
| 84 |
+
"""Test that demand values are positive"""
|
| 85 |
+
dates = pd.date_range(start="2021-01-01", periods=365, freq="D")
|
| 86 |
+
y = np.random.rand(365) * 100 + 50
|
| 87 |
+
assert all(y > 0), "Negative demand values found"
|
| 88 |
+
print("✅ Test 6 passed: All demand values positive")
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
test_sample_data_shape()
|
| 92 |
+
test_data_types()
|
| 93 |
+
test_no_nulls()
|
| 94 |
+
test_prophet_trains()
|
| 95 |
+
test_prophet_forecast()
|
| 96 |
+
test_positive_demand()
|
| 97 |
+
print("\n🎉 All tests passed!")
|