-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_refactored_code.py
executable file
·109 lines (86 loc) · 3.08 KB
/
test_refactored_code.py
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
#!/usr/bin/env python3
"""
Test script for the refactored Stats Scraper.
This script demonstrates how to use the new architecture by testing the GitHub client and database abstraction layer.
"""
import os
import sys
from datetime import datetime, timedelta
# Add the project root to the Python path
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
from config import config
from database import get_database
from github_client import GitHubClient
from utils import get_logger
# Get logger
logger = get_logger(__name__)
def test_github_client():
"""
Test the GitHub client by fetching repository traffic data.
"""
logger.info("Testing GitHub client...")
try:
# Create the GitHub client
github = GitHubClient(config)
# Get repository traffic data
traffic_data = github.get_repository_traffic()
if traffic_data:
logger.info(f"Successfully fetched {len(traffic_data)} days of traffic data")
logger.info(f"Sample data: {traffic_data[0]}")
else:
logger.warning("No traffic data found")
return traffic_data
except Exception as e:
logger.error(f"Error testing GitHub client: {e}")
return None
def test_database(data):
"""
Test the database abstraction layer by storing and retrieving data.
"""
if not data:
logger.error("No data to test database with")
return False
logger.info(f"Testing database ({config['database']['type']})...")
try:
# Get the database
db = get_database(config)
# Create a test table
table_name = "test_github_visitors"
db.create_table(table_name, """
date DATE,
total_views INTEGER,
unique_visitors INTEGER
""")
# Insert data
affected_rows = db.upsert(table_name, data, primary_key="date")
logger.info(f"Inserted {affected_rows} rows into {table_name}")
# Query data
result = db.query(f"SELECT * FROM {table_name} ORDER BY date DESC LIMIT 5")
logger.info(f"Retrieved {len(result)} rows from {table_name}")
# Clean up
db.query(f"DROP TABLE IF EXISTS {table_name}")
logger.info(f"Dropped table {table_name}")
# Close connection
db.close()
return True
except Exception as e:
logger.error(f"Error testing database: {e}")
return False
def main():
"""
Main function to run the tests.
"""
logger.info("Starting test of refactored Stats Scraper...")
logger.info(f"Using configuration: GitHub: {config['github']['owner']}/{config['github']['repo']}, Database: {config['database']['type']}")
# Test GitHub client
data = test_github_client()
# Test database
if data:
success = test_database(data)
if success:
logger.info("Database test successful")
else:
logger.error("Database test failed")
logger.info("Test completed")
if __name__ == "__main__":
main()