File size: 2,431 Bytes
099d8d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Test script to verify FRED API key functionality
"""

import os
import sys

from fredapi import Fred

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))

from config.settings import FRED_API_KEY


def test_api_connection():
    """Test the FRED API connection with the provided key."""
    print("Testing FRED API connection...")

    try:
        # Initialize FRED client
        fred = Fred(api_key=FRED_API_KEY)

        # Test with a simple series (GDP)
        print("Fetching GDP data as a test...")
        gdp_data = fred.get_series("GDP", start="2023-01-01", end="2023-12-31")

        if not gdp_data.empty:
            print("βœ“ API connection successful!")
            print(f"βœ“ Retrieved {len(gdp_data)} GDP observations")
            print(f"βœ“ Latest GDP value: ${gdp_data.iloc[-1]:,.2f} billion")
            print(f"βœ“ Date range: {gdp_data.index.min()} to {gdp_data.index.max()}")
            return True
        else:
            print("βœ— No data retrieved")
            return False

    except Exception as e:
        print(f"βœ— API connection failed: {e}")
        return False


def test_series_info():
    """Test getting series information."""
    print("\nTesting series information retrieval...")

    try:
        fred = Fred(api_key=FRED_API_KEY)

        # Test getting info for GDP
        series_info = fred.get_series_info("GDP")

        print("βœ“ Series information retrieved successfully!")
        print(f"  Title: {series_info.title}")
        print(f"  Units: {series_info.units}")
        print(f"  Frequency: {series_info.frequency}")
        print(f"  Last Updated: {series_info.last_updated}")

        return True

    except Exception as e:
        print(f"βœ— Failed to get series info: {e}")
        return False


def main():
    """Run API tests."""
    print("FRED API Key Test")
    print("=" * 30)
    print(f"API Key: {FRED_API_KEY[:8]}...")
    print()

    # Test connection
    connection_ok = test_api_connection()

    # Test series info
    info_ok = test_series_info()

    print("\n" + "=" * 30)
    if connection_ok and info_ok:
        print("βœ“ All tests passed! Your API key is working correctly.")
        print("You can now use the FRED data collector tool.")
    else:
        print("βœ— Some tests failed. Please check your API key.")

    return connection_ok and info_ok


if __name__ == "__main__":
    main()