69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
"""
|
|
Integration tests for data_loader.py
|
|
"""
|
|
import pytest
|
|
import pandas as pd
|
|
import numpy as np
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
import os
|
|
|
|
# Add parent directory to path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from data_loader import load_sales_data, validate_data_structure
|
|
|
|
class TestDataLoader:
|
|
"""Test data loading functions"""
|
|
|
|
def test_load_sales_data_basic(self):
|
|
"""Test basic data loading"""
|
|
# Create temporary CSV
|
|
with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as f:
|
|
f.write('InvoiceDate,USD,Customer\n')
|
|
f.write('2023-01-01,100.0,Customer1\n')
|
|
f.write('2023-02-01,200.0,Customer2\n')
|
|
temp_path = f.name
|
|
|
|
try:
|
|
# Temporarily update config
|
|
import config
|
|
original_data_file = config.DATA_FILE
|
|
config.DATA_FILE = Path(temp_path).name
|
|
|
|
df = load_sales_data(Path(temp_path))
|
|
|
|
assert len(df) == 2
|
|
assert 'Year' in df.columns
|
|
assert 'YearMonth' in df.columns
|
|
|
|
# Restore config
|
|
config.DATA_FILE = original_data_file
|
|
finally:
|
|
os.unlink(temp_path)
|
|
|
|
def test_validate_data_structure(self):
|
|
"""Test data structure validation"""
|
|
# Valid DataFrame
|
|
df_valid = pd.DataFrame({
|
|
'InvoiceDate': pd.to_datetime(['2023-01-01', '2023-02-01']),
|
|
'USD': [100.0, 200.0]
|
|
})
|
|
|
|
is_valid, msg = validate_data_structure(df_valid)
|
|
assert is_valid
|
|
assert msg == "OK"
|
|
|
|
# Invalid DataFrame (missing column)
|
|
df_invalid = pd.DataFrame({
|
|
'InvoiceDate': pd.to_datetime(['2023-01-01'])
|
|
})
|
|
|
|
is_valid, msg = validate_data_structure(df_invalid)
|
|
assert not is_valid
|
|
assert 'Missing required column' in msg
|
|
|
|
if __name__ == "__main__":
|
|
pytest.main([__file__, '-v'])
|