hadith-ingestion/hadith-ingestion/test_hadithapi.py

88 lines
3.0 KiB
Python

#!/usr/bin/env python3
"""
Quick test script for hadithapi_client.py
"""
import sys
from venv import logger
sys.path.insert(0, '/app')
from src.api_clients.hadithapi_client import HadithAPIClient
from config.settings import settings
def test_api_connection():
"""Test basic API connectivity"""
print("=== Testing HadithAPI Client ===\n")
client = HadithAPIClient()
# Test 1: Get books
print("Test 1: Fetching available books...")
try:
books = client.get_books()
print(f"✓ Success! Found {len(books)} books")
for book in books[:3]: # Show first 3
print(f" - {book.get('bookName')} ({book.get('bookSlug')})")
print(f" Hadiths: {book.get('hadiths_count')}, Chapters: {book.get('chapters_count')}")
logger.info(f"Fetched {len(books)} books successfully")
except Exception as e:
print(f"✗ Failed: {e}")
return False
# Test 2: Get chapters for Sahih Bukhari
print("\nTest 2: Fetching chapters for Sahih Bukhari...")
try:
chapters = client.get_chapters('sahih-bukhari')
print(f"✓ Success! Found {len(chapters)} chapters")
if chapters:
print(f" First chapter: {chapters[0].get('chapterEnglish')}")
except Exception as e:
print(f"✗ Failed: {e}")
return False
# Test 3: Fetch first page of hadiths
print("\nTest 3: Fetching first page of hadiths...")
book_id = None
try:
book = client.get_book_by_slug('sahih-bukhari')
if not book:
print("✗ Failed: Book 'sahih-bukhari' not found")
return False
book_id = book.get('id')
page_data = client.get_hadiths_page('sahih-bukhari', page=1, limit=5)
hadiths = page_data.get('hadiths', [])
print(f"✓ Success! Fetched {len(hadiths)} hadiths")
if hadiths:
first = hadiths[0]
print(f" First hadith number: {first.get('hadithNumber')}")
print(f" Arabic text (first 100 chars): {first.get('hadithArabic', '')[:100]}...")
except Exception as e:
print(f"✗ Failed: {e}")
return False
if book_id is None:
print("✗ Failed: Book ID unavailable for iterator test")
return False
# # Test 4: Test iterator (fetch 3 hadiths)
print("\nTest 4: Testing hadith iterator (3 hadiths)...")
try:
count = 0
for hadith in client.iter_all_hadiths_in_book(book_id='sahih-bukhari', book_slug='sahih-bukhari', batch_size=10):
count += 1
print(f" Hadith #{hadith.get('hadithNumber')} is {hadith.get('englishNarrator')} and is {hadith.get('status')} ")
if count >= 3:
break
print(f"✓ Success! Iterator working correctly")
except Exception as e:
print(f"✗ Failed: {e}")
return False
client.close()
print("\n=== All Tests Passed! ===")
return True
if __name__ == "__main__":
success = test_api_connection()
sys.exit(0 if success else 1)