| | |
| | """ |
| | FRED ML Development Testing |
| | Simple testing script for development environment |
| | """ |
| |
|
| | import os |
| | import sys |
| | import json |
| | import time |
| | from pathlib import Path |
| |
|
| | def test_streamlit_app(): |
| | """Test Streamlit app functionality""" |
| | print("π¨ Testing Streamlit app...") |
| | |
| | try: |
| | |
| | sys.path.append('frontend') |
| | from app import load_config, init_aws_clients |
| | |
| | |
| | config = load_config() |
| | if config: |
| | print("β
Streamlit app configuration loaded") |
| | else: |
| | print("β Failed to load Streamlit app configuration") |
| | return False |
| | |
| | |
| | try: |
| | s3_client, lambda_client = init_aws_clients() |
| | print("β
AWS clients initialized") |
| | except Exception as e: |
| | print(f"β AWS client initialization failed: {str(e)}") |
| | return False |
| | |
| | print("β
Streamlit app test passed") |
| | return True |
| | |
| | except Exception as e: |
| | print(f"β Streamlit app test failed: {str(e)}") |
| | return False |
| |
|
| | def test_lambda_function(): |
| | """Test Lambda function""" |
| | print("β‘ Testing Lambda function...") |
| | |
| | try: |
| | import boto3 |
| | lambda_client = boto3.client('lambda') |
| | |
| | |
| | function_info = lambda_client.get_function(FunctionName='fred-ml-processor') |
| | print(f"β
Lambda function found: {function_info['Configuration']['FunctionArn']}") |
| | |
| | |
| | test_payload = { |
| | 'indicators': ['GDP', 'UNRATE'], |
| | 'start_date': '2023-01-01', |
| | 'end_date': '2023-12-31', |
| | 'test_mode': True |
| | } |
| | |
| | response = lambda_client.invoke( |
| | FunctionName='fred-ml-processor', |
| | InvocationType='RequestResponse', |
| | Payload=json.dumps(test_payload) |
| | ) |
| | |
| | if response['StatusCode'] == 200: |
| | print("β
Lambda function invocation successful") |
| | return True |
| | else: |
| | print(f"β Lambda invocation failed with status {response['StatusCode']}") |
| | return False |
| | |
| | except Exception as e: |
| | print(f"β Lambda function test failed: {str(e)}") |
| | return False |
| |
|
| | def test_s3_access(): |
| | """Test S3 bucket access""" |
| | print("π¦ Testing S3 bucket access...") |
| | |
| | try: |
| | import boto3 |
| | s3 = boto3.client('s3') |
| | |
| | |
| | s3.head_bucket(Bucket='fredmlv1') |
| | print("β
S3 bucket access successful") |
| | |
| | |
| | test_data = "test content" |
| | test_key = f"dev-test/test-{int(time.time())}.txt" |
| | |
| | |
| | s3.put_object( |
| | Bucket='fredmlv1', |
| | Key=test_key, |
| | Body=test_data.encode('utf-8') |
| | ) |
| | print("β
S3 upload successful") |
| | |
| | |
| | response = s3.get_object(Bucket='fredmlv1', Key=test_key) |
| | downloaded_data = response['Body'].read().decode('utf-8') |
| | |
| | if downloaded_data == test_data: |
| | print("β
S3 download successful") |
| | else: |
| | print("β S3 download data mismatch") |
| | return False |
| | |
| | |
| | s3.delete_object(Bucket='fredmlv1', Key=test_key) |
| | print("β
S3 cleanup successful") |
| | |
| | return True |
| | |
| | except Exception as e: |
| | print(f"β S3 access test failed: {str(e)}") |
| | return False |
| |
|
| | def test_fred_api(): |
| | """Test FRED API access""" |
| | print("π Testing FRED API...") |
| | |
| | try: |
| | from fredapi import Fred |
| | fred = Fred(api_key=os.getenv('FRED_API_KEY')) |
| | |
| | |
| | test_series = fred.get_series('GDP', limit=5) |
| | if len(test_series) > 0: |
| | print(f"β
FRED API access successful - retrieved {len(test_series)} data points") |
| | return True |
| | else: |
| | print("β FRED API returned no data") |
| | return False |
| | |
| | except Exception as e: |
| | print(f"β FRED API test failed: {str(e)}") |
| | return False |
| |
|
| | def test_data_processing(): |
| | """Test data processing capabilities""" |
| | print("π Testing data processing...") |
| | |
| | try: |
| | import pandas as pd |
| | import numpy as np |
| | from fredapi import Fred |
| | |
| | fred = Fred(api_key=os.getenv('FRED_API_KEY')) |
| | |
| | |
| | test_data = {} |
| | indicators = ['GDP', 'UNRATE', 'CPIAUCSL'] |
| | |
| | for indicator in indicators: |
| | try: |
| | data = fred.get_series(indicator, limit=100) |
| | test_data[indicator] = data |
| | print(f"β
Retrieved {indicator}: {len(data)} observations") |
| | except Exception as e: |
| | print(f"β Failed to retrieve {indicator}: {str(e)}") |
| | |
| | if not test_data: |
| | print("β No test data retrieved") |
| | return False |
| | |
| | |
| | df = pd.DataFrame(test_data) |
| | df = df.dropna() |
| | |
| | if len(df) > 0: |
| | |
| | summary = df.describe() |
| | correlation = df.corr() |
| | |
| | print(f"β
Data processing successful - {len(df)} data points processed") |
| | print(f" Summary statistics calculated") |
| | print(f" Correlation matrix shape: {correlation.shape}") |
| | return True |
| | else: |
| | print("β No valid data after processing") |
| | return False |
| | |
| | except Exception as e: |
| | print(f"β Data processing test failed: {str(e)}") |
| | return False |
| |
|
| | def test_visualization(): |
| | """Test visualization generation""" |
| | print("π¨ Testing visualization generation...") |
| | |
| | try: |
| | import matplotlib.pyplot as plt |
| | import plotly.express as px |
| | import seaborn as sns |
| | import pandas as pd |
| | import numpy as np |
| | |
| | |
| | np.random.seed(42) |
| | dates = pd.date_range('2023-01-01', '2024-01-01', freq='M') |
| | test_data = pd.DataFrame({ |
| | 'GDP': np.random.normal(100, 5, len(dates)), |
| | 'UNRATE': np.random.normal(5, 1, len(dates)), |
| | 'CPIAUCSL': np.random.normal(200, 10, len(dates)) |
| | }, index=dates) |
| | |
| | |
| | fig, ax = plt.subplots(figsize=(10, 6)) |
| | test_data.plot(ax=ax) |
| | plt.title('Test Visualization') |
| | plt.close() |
| | print("β
Matplotlib visualization created") |
| | |
| | |
| | fig = px.line(test_data, title='Test Plotly Visualization') |
| | fig.update_layout(showlegend=True) |
| | print("β
Plotly visualization created") |
| | |
| | |
| | plt.figure(figsize=(8, 6)) |
| | sns.heatmap(test_data.corr(), annot=True, cmap='coolwarm') |
| | plt.title('Test Correlation Heatmap') |
| | plt.close() |
| | print("β
Seaborn visualization created") |
| | |
| | print("β
All visualization tests passed") |
| | return True |
| | |
| | except Exception as e: |
| | print(f"β Visualization test failed: {str(e)}") |
| | return False |
| |
|
| | def main(): |
| | """Main testing function""" |
| | print("π§ͺ FRED ML Development Testing") |
| | print("=" * 50) |
| | |
| | tests = [ |
| | ("Streamlit App", test_streamlit_app), |
| | ("Lambda Function", test_lambda_function), |
| | ("S3 Bucket Access", test_s3_access), |
| | ("FRED API", test_fred_api), |
| | ("Data Processing", test_data_processing), |
| | ("Visualization", test_visualization) |
| | ] |
| | |
| | passed = 0 |
| | total = len(tests) |
| | |
| | for test_name, test_func in tests: |
| | print(f"\nπ Running {test_name} test...") |
| | if test_func(): |
| | passed += 1 |
| | else: |
| | print(f"β {test_name} test failed") |
| | |
| | print(f"\nπ Test Summary: {passed}/{total} tests passed") |
| | |
| | if passed == total: |
| | print("β
All development tests passed!") |
| | print("\nπ― Your development environment is ready!") |
| | print("You can now:") |
| | print("1. Run the Streamlit app: streamlit run frontend/app.py") |
| | print("2. Test the complete system: python scripts/test_complete_system.py") |
| | return True |
| | else: |
| | print("β Some tests failed. Please check the issues above.") |
| | return False |
| |
|
| | if __name__ == '__main__': |
| | success = main() |
| | sys.exit(0 if success else 1) |