fix: wrap test_delete_existing_community.py code in test function
- Fixes pytest collection error during import - Prevents code execution at module level - Maintains functionality when run as script - All 361 tests now collect successfully
This commit is contained in:
@@ -4,75 +4,86 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import pytest
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
# GraphQL endpoint
|
# GraphQL endpoint
|
||||||
url = "http://localhost:8000/graphql"
|
url = "http://localhost:8000/graphql"
|
||||||
|
|
||||||
# Сначала авторизуемся
|
def test_delete_existing_community():
|
||||||
login_mutation = """
|
"""Тест удаления существующего сообщества через API"""
|
||||||
mutation Login($email: String!, $password: String!) {
|
|
||||||
login(email: $email, password: $password) {
|
# Сначала авторизуемся
|
||||||
token
|
login_mutation = """
|
||||||
author {
|
mutation Login($email: String!, $password: String!) {
|
||||||
id
|
login(email: $email, password: $password) {
|
||||||
name
|
token
|
||||||
email
|
author {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
email
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
"""
|
||||||
"""
|
|
||||||
|
|
||||||
login_variables = {"email": "test_admin@discours.io", "password": "password123"}
|
login_variables = {"email": "test_admin@discours.io", "password": "password123"}
|
||||||
|
|
||||||
print("🔐 Авторизуемся...")
|
print("🔐 Авторизуемся...")
|
||||||
response = requests.post(url, json={"query": login_mutation, "variables": login_variables})
|
response = requests.post(url, json={"query": login_mutation, "variables": login_variables})
|
||||||
|
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
print(f"❌ Ошибка авторизации: {response.status_code}")
|
print(f"❌ Ошибка авторизации: {response.status_code}")
|
||||||
print(response.text)
|
print(response.text)
|
||||||
exit(1)
|
pytest.fail(f"Ошибка авторизации: {response.status_code}")
|
||||||
|
|
||||||
login_data = response.json()
|
login_data = response.json()
|
||||||
print(f"✅ Авторизация успешна: {json.dumps(login_data, indent=2)}")
|
print(f"✅ Авторизация успешна: {json.dumps(login_data, indent=2)}")
|
||||||
|
|
||||||
if "errors" in login_data:
|
if "errors" in login_data:
|
||||||
print(f"❌ Ошибки в авторизации: {login_data['errors']}")
|
print(f"❌ Ошибки в авторизации: {login_data['errors']}")
|
||||||
exit(1)
|
pytest.fail(f"Ошибки в авторизации: {login_data['errors']}")
|
||||||
|
|
||||||
token = login_data["data"]["login"]["token"]
|
token = login_data["data"]["login"]["token"]
|
||||||
author_id = login_data["data"]["login"]["author"]["id"]
|
author_id = login_data["data"]["login"]["author"]["id"]
|
||||||
print(f"🔑 Токен получен: {token[:50]}...")
|
print(f"🔑 Токен получен: {token[:50]}...")
|
||||||
print(f"👤 Author ID: {author_id}")
|
print(f"👤 Author ID: {author_id}")
|
||||||
|
|
||||||
# Теперь попробуем удалить существующее сообщество
|
# Теперь попробуем удалить существующее сообщество
|
||||||
delete_mutation = """
|
delete_mutation = """
|
||||||
mutation DeleteCommunity($slug: String!) {
|
mutation DeleteCommunity($slug: String!) {
|
||||||
delete_community(slug: $slug) {
|
delete_community(slug: $slug) {
|
||||||
success
|
success
|
||||||
error
|
error
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
"""
|
||||||
"""
|
|
||||||
|
|
||||||
delete_variables = {"slug": "test-admin-community-test-26b67fa4"}
|
delete_variables = {"slug": "test-admin-community-test-26b67fa4"}
|
||||||
|
|
||||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||||
|
|
||||||
print(f"\n🗑️ Пытаемся удалить сообщество {delete_variables['slug']}...")
|
print(f"\n🗑️ Пытаемся удалить сообщество {delete_variables['slug']}...")
|
||||||
response = requests.post(url, json={"query": delete_mutation, "variables": delete_variables}, headers=headers)
|
response = requests.post(url, json={"query": delete_mutation, "variables": delete_variables}, headers=headers)
|
||||||
|
|
||||||
print(f"📊 Статус ответа: {response.status_code}")
|
print(f"📊 Статус ответа: {response.status_code}")
|
||||||
print(f"📄 Ответ: {response.text}")
|
print(f"📄 Ответ: {response.text}")
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
print(f"📋 JSON ответ: {json.dumps(data, indent=2)}")
|
print(f"📋 JSON ответ: {json.dumps(data, indent=2)}")
|
||||||
|
|
||||||
if "errors" in data:
|
if "errors" in data:
|
||||||
print(f"❌ GraphQL ошибки: {data['errors']}")
|
print(f"❌ GraphQL ошибки: {data['errors']}")
|
||||||
|
pytest.fail(f"GraphQL ошибки: {data['errors']}")
|
||||||
|
else:
|
||||||
|
print(f"✅ Результат: {data['data']['delete_community']}")
|
||||||
|
# Проверяем, что удаление прошло успешно
|
||||||
|
assert data['data']['delete_community']['success'] is True
|
||||||
else:
|
else:
|
||||||
print(f"✅ Результат: {data['data']['delete_community']}")
|
print(f"❌ HTTP ошибка: {response.status_code}")
|
||||||
else:
|
pytest.fail(f"HTTP ошибка: {response.status_code}")
|
||||||
print(f"❌ HTTP ошибка: {response.status_code}")
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Для запуска как скрипт
|
||||||
|
pytest.main([__file__, "-v"])
|
||||||
|
|||||||
Reference in New Issue
Block a user