Files
python_parser/test_api.py
2025-09-01 13:58:42 +03:00

84 lines
3.2 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Тестовый скрипт для проверки API
"""
import requests
import json
def test_api_endpoints():
"""Тестирование API эндпоинтов"""
base_url = "http://localhost:8000"
print("🧪 ТЕСТИРОВАНИЕ API")
print("=" * 50)
# Тест 1: Проверка доступности API
print("\n1⃣ Проверка доступности API...")
try:
response = requests.get(f"{base_url}/")
if response.status_code == 200:
print(f"✅ API доступен: {response.json()}")
else:
print(f"❌ API недоступен: {response.status_code}")
return False
except Exception as e:
print(f"❌ Ошибка подключения к API: {e}")
return False
# Тест 2: Список парсеров
print("\n2⃣ Получение списка парсеров...")
try:
response = requests.get(f"{base_url}/parsers")
if response.status_code == 200:
parsers = response.json()
print(f"✅ Парсеры: {parsers}")
else:
print(f"❌ Ошибка получения парсеров: {response.status_code}")
except Exception as e:
print(f"❌ Ошибка: {e}")
# Тест 3: Информация о геттерах
print("\n3⃣ Информация о геттерах парсеров...")
parsers_to_test = ["svodka_pm", "svodka_ca", "monitoring_fuel"]
for parser in parsers_to_test:
try:
response = requests.get(f"{base_url}/parsers/{parser}/getters")
if response.status_code == 200:
getters = response.json()
print(f"{parser}: {len(getters.get('getters', {}))} геттеров")
else:
print(f"{parser}: ошибка {response.status_code}")
except Exception as e:
print(f"{parser}: ошибка {e}")
# Тест 4: Загрузка тестового файла
print("\n4⃣ Тест загрузки файла...")
try:
# Создаем простой Excel файл для теста
test_data = b"test content"
files = {"file": ("test.xlsx", test_data, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")}
response = requests.post(f"{base_url}/svodka_ca/upload", files=files)
print(f"📤 Результат загрузки: {response.status_code}")
if response.status_code == 200:
result = response.json()
print(f"✅ Файл загружен: {result}")
else:
print(f"❌ Ошибка загрузки: {response.status_code}")
try:
error_detail = response.json()
print(f"📋 Детали ошибки: {error_detail}")
except:
print(f"📋 Текст ошибки: {response.text}")
except Exception as e:
print(f"❌ Ошибка теста загрузки: {e}")
print("\n🎯 Тестирование завершено!")
return True
if __name__ == "__main__":
test_api_endpoints()