reddit_scraper/test_api.py

84 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Quick test script for Reddit Scraper API."""
import requests
import json
def test_health():
"""Test health endpoint."""
response = requests.get("http://localhost:8000/health")
print("\n=== Health Check ===")
print(json.dumps(response.json(), indent=2))
assert response.status_code == 200, "Health check failed"
def test_subreddit():
"""Test subreddit scraping."""
url = "http://localhost:8000/scrape/subreddit/python"
params = {"limit": 1, "time_range": "week", "depth": 1}
response = requests.get(url, params=params)
print("\n=== Subreddit Scraping ===")
data = response.json()
if "Error" not in data:
print(f"Subreddit: {data['subreddit']}")
print(f"Posts found: {data['posts_count']}")
if data.get('data'):
post = data['data'][0]
print(f"Top post: {post['title'][:50]}...")
print(f"Score: {post['score']}")
else:
print(f"Error: {data['Error']}")
def test_custom():
"""Test custom scraping."""
url = "http://localhost:8000/scrape/custom"
payload = {
"type": "subreddit",
"target": "AskReddit",
"limit": 1,
"time_range": "day",
"depth": 1
}
response = requests.post(url, json=payload)
print("\n=== Custom Scraping ===")
data = response.json()
if "Error" not in data:
print(f"Subreddit: {data['subreddit']}")
print(f"Posts found: {data['posts_count']}")
else:
print(f"Error: {data['Error']}")
def test_include_comments_false():
"""Test that include_comments=false skips comment extraction."""
url = "http://localhost:8000/scrape/subreddit/python"
params = {"limit": 1, "time_range": "week", "include_comments": False}
response = requests.get(url, params=params)
print("\n=== Include Comments=False Test ===")
data = response.json()
if "Error" not in data:
print(f"Request with include_comments=false successful")
print(f"Posts found: {data['posts_count']}")
if data.get('data'):
post = data['data'][0]
has_comments = 'comments' in post and len(post['comments']) > 0
print(f"Has comments field: {'comments' in post}")
print(f"Comments empty: {not has_comments}")
else:
print(f"Error: {data['Error']}")
if __name__ == "__main__":
try:
test_health()
test_subreddit()
test_custom()
print("\n✅ All tests passed!")
except Exception as e:
print(f"\n❌ Test failed: {e}")