Spaces:
Sleeping
Sleeping
File size: 7,048 Bytes
186fe46 | 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 | #!/usr/bin/env python3
"""
Test Firebase connection and collection access with better error handling.
"""
import os
import sys
# Add the parent directory to Python path
current_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, current_dir)
def test_firebase_connection():
"""Test Firebase connection and collection access."""
print("π Testing Firebase Connection")
print("=" * 50)
try:
from cve_factchecker.firebase_loader import FirebaseNewsLoader, FirebaseConfig
# Initialize loader
loader = FirebaseNewsLoader()
print(f"π‘ Project ID: {loader.project_id}")
print(f"π API Key: {loader.api_key[:20]}...")
# Test 1: Try to fetch a small number of articles from main collection
print(f"\nπ Test 1: Fetching from 'articles' collection...")
try:
articles = loader.fetch_articles(limit=3, language=None) # No language filter first
print(f" β
Success: {len(articles)} articles fetched")
if articles:
sample = articles[0]
print(f" π Sample title: {sample.title[:80]}...")
print(f" π Sample content length: {len(sample.content)} chars")
if hasattr(sample, 'language') and sample.language:
print(f" π Sample language: {sample.language}")
else:
print(f" β οΈ No language field found")
except Exception as e:
print(f" β Error: {e}")
import traceback
traceback.print_exc()
# Test 2: Try language filtering
print(f"\nπ Test 2: Testing language filtering...")
try:
english_articles = loader.fetch_articles(limit=3, language="English")
print(f" β
English filter: {len(english_articles)} articles")
# Try different language values
for lang in ["english", "en", "EN"]:
try:
test_articles = loader.fetch_articles(limit=1, language=lang)
print(f" π Language '{lang}': {len(test_articles)} articles")
except Exception as e:
print(f" β Language '{lang}' failed: {e}")
except Exception as e:
print(f" β Language filtering error: {e}")
# Test 3: Try dedicated English collection
print(f"\nπ― Test 3: Testing dedicated English collection...")
try:
english_collection_articles = loader.fetch_english_articles(limit=5)
print(f" β
English collection: {len(english_collection_articles)} articles")
if english_collection_articles:
sample = english_collection_articles[0]
print(f" π English sample: {sample.title[:80]}...")
except Exception as e:
print(f" β English collection error: {e}")
import traceback
traceback.print_exc()
# Test 4: Network connectivity check
print(f"\nπ Test 4: Basic network connectivity...")
try:
import requests
response = requests.get("https://httpbin.org/ip", timeout=10)
if response.status_code == 200:
print(f" β
Internet connection OK")
# Test Firebase endpoint
firebase_test_url = f"https://firestore.googleapis.com/v1/projects/{loader.project_id}"
firebase_response = requests.get(firebase_test_url, timeout=10)
print(f" π‘ Firebase endpoint status: {firebase_response.status_code}")
else:
print(f" β οΈ Network test failed: {response.status_code}")
except Exception as e:
print(f" β Network test error: {e}")
return True
except ImportError as e:
print(f"β Import error: {e}")
return False
except Exception as e:
print(f"β Unexpected error: {e}")
import traceback
traceback.print_exc()
return False
def test_alternative_collection_names():
"""Test if collections exist with different naming patterns."""
print(f"\nπ Testing Alternative Collection Names")
print("=" * 50)
try:
from cve_factchecker.firebase_loader import FirebaseNewsLoader
loader = FirebaseNewsLoader()
# Test different collection patterns by modifying the config
alternative_names = [
"Articles", # Capital A
"English_Articles", # Capital E and underscore
"englishArticles", # camelCase
"EnglishArticles", # PascalCase
"news", # Simple name
"documents" # Generic name
]
for collection_name in alternative_names:
print(f"π Testing collection: '{collection_name}'")
try:
# Temporarily modify the collection name
original_collection = loader.config.ENGLISH_ARTICLES_COLLECTION
loader.config.ENGLISH_ARTICLES_COLLECTION = collection_name
# Try to fetch
articles = loader.fetch_english_articles(limit=1)
print(f" β
Found {len(articles)} articles in '{collection_name}'")
# Restore original
loader.config.ENGLISH_ARTICLES_COLLECTION = original_collection
if articles:
return collection_name # Found working collection
except Exception as e:
# Restore original
loader.config.ENGLISH_ARTICLES_COLLECTION = original_collection
print(f" β Collection '{collection_name}' failed: {str(e)[:50]}...")
return None
except Exception as e:
print(f"β Alternative collection test failed: {e}")
return None
def main():
"""Main test function."""
print("π§ͺ CVE Fact Checker - Firebase Connection Test")
print("=" * 80)
# Basic connection test
success = test_firebase_connection()
if success:
# Test alternative collection names
working_collection = test_alternative_collection_names()
if working_collection:
print(f"\nπ Found working English collection: '{working_collection}'")
else:
print(f"\nπ‘ Recommendation: Use main 'articles' collection with language filtering")
print(f"\nπ Test Summary:")
print(f" Firebase connection: {'β
OK' if success else 'β Failed'}")
return success
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1) |