File size: 6,820 Bytes
6ce20d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3
"""
Core functionality tests for FRED ML
Tests basic functionality without AWS dependencies
"""

import pytest
import pandas as pd
import numpy as np
from unittest.mock import Mock, patch
import sys
from pathlib import Path

# Add src to path
project_root = Path(__file__).parent.parent.parent
sys.path.append(str(project_root / 'src'))

class TestCoreFunctionality:
    """Test core functionality without AWS dependencies"""
    
    def test_fred_api_client_import(self):
        """Test that FRED API client can be imported"""
        try:
            from frontend.fred_api_client import FREDAPIClient
            assert FREDAPIClient is not None
        except ImportError as e:
            pytest.skip(f"FRED API client not available: {e}")
    
    def test_demo_data_import(self):
        """Test that demo data can be imported"""
        try:
            from frontend.demo_data import get_demo_data
            assert get_demo_data is not None
        except ImportError as e:
            pytest.skip(f"Demo data not available: {e}")
    
    def test_config_import(self):
        """Test that config can be imported"""
        try:
            from config.settings import FRED_API_KEY, AWS_REGION
            assert FRED_API_KEY is not None
            assert AWS_REGION is not None
        except ImportError as e:
            pytest.skip(f"Config not available: {e}")
    
    def test_streamlit_app_import(self):
        """Test that Streamlit app can be imported"""
        try:
            # Just test that the file exists and can be read
            app_path = project_root / 'frontend' / 'app.py'
            assert app_path.exists()
            
            # Test basic imports from the app
            import streamlit as st
            assert st is not None
        except ImportError as e:
            pytest.skip(f"Streamlit not available: {e}")
    
    def test_pandas_functionality(self):
        """Test basic pandas functionality"""
        # Create test data
        dates = pd.date_range('2024-01-01', '2024-01-05', freq='D')
        df = pd.DataFrame({
            'GDP': [100.0, 101.0, 102.0, 103.0, 104.0],
            'UNRATE': [3.5, 3.6, 3.7, 3.8, 3.9]
        }, index=dates)
        
        # Test basic operations
        assert not df.empty
        assert len(df) == 5
        assert 'GDP' in df.columns
        assert 'UNRATE' in df.columns
        
        # Test statistics
        assert df['GDP'].mean() == 102.0
        assert df['GDP'].min() == 100.0
        assert df['GDP'].max() == 104.0
    
    def test_numpy_functionality(self):
        """Test basic numpy functionality"""
        # Test array operations
        arr = np.array([1, 2, 3, 4, 5])
        assert arr.mean() == 3.0
        assert arr.std() > 0
        
        # Test random number generation
        random_arr = np.random.randn(100)
        assert len(random_arr) == 100
        assert random_arr.mean() != 0  # Should be close to 0 but not exactly
    
    def test_plotly_import(self):
        """Test plotly import"""
        try:
            import plotly.express as px
            import plotly.graph_objects as go
            assert px is not None
            assert go is not None
        except ImportError as e:
            pytest.skip(f"Plotly not available: {e}")
    
    def test_boto3_import(self):
        """Test boto3 import"""
        try:
            import boto3
            assert boto3 is not None
        except ImportError as e:
            pytest.skip(f"Boto3 not available: {e}")
    
    def test_requests_import(self):
        """Test requests import"""
        try:
            import requests
            assert requests is not None
        except ImportError as e:
            pytest.skip(f"Requests not available: {e}")
    
    def test_data_processing(self):
        """Test basic data processing functionality"""
        # Create test data
        data = {
            'dates': pd.date_range('2024-01-01', '2024-01-10', freq='D'),
            'values': [100 + i for i in range(10)]
        }
        
        # Create DataFrame
        df = pd.DataFrame({
            'date': data['dates'],
            'value': data['values']
        })
        
        # Test data processing
        df['value_lag1'] = df['value'].shift(1)
        df['value_change'] = df['value'].diff()
        
        assert len(df) == 10
        assert 'value_lag1' in df.columns
        assert 'value_change' in df.columns
        
        # Test that we can handle missing values
        df_clean = df.dropna()
        assert len(df_clean) < len(df)  # Should have fewer rows due to NaN values
    
    def test_string_parsing(self):
        """Test string parsing functionality (for FRED API values)"""
        # Test parsing FRED API values with commas
        test_values = [
            "2,239.7",
            "1,000.0",
            "100.5",
            "1,234,567.89"
        ]
        
        expected_values = [
            2239.7,
            1000.0,
            100.5,
            1234567.89
        ]
        
        for test_val, expected_val in zip(test_values, expected_values):
            # Remove commas and convert to float
            cleaned_val = test_val.replace(',', '')
            parsed_val = float(cleaned_val)
            assert parsed_val == expected_val
    
    def test_error_handling(self):
        """Test error handling functionality"""
        # Test handling of invalid data
        invalid_values = [
            "N/A",
            ".",
            "",
            "invalid"
        ]
        
        for invalid_val in invalid_values:
            try:
                # Try to convert to float
                float_val = float(invalid_val)
                # If we get here, it's unexpected
                assert False, f"Should have failed for {invalid_val}"
            except (ValueError, TypeError):
                # Expected behavior
                pass
    
    def test_configuration_loading(self):
        """Test configuration loading"""
        try:
            from config.settings import (
                FRED_API_KEY, 
                AWS_REGION, 
                DEBUG, 
                LOG_LEVEL,
                get_aws_config,
                is_fred_api_configured,
                is_aws_configured
            )
            
            # Test configuration functions
            aws_config = get_aws_config()
            assert isinstance(aws_config, dict)
            
            fred_configured = is_fred_api_configured()
            assert isinstance(fred_configured, bool)
            
            aws_configured = is_aws_configured()
            assert isinstance(aws_configured, bool)
            
        except ImportError as e:
            pytest.skip(f"Configuration not available: {e}")