core/tests/test_shouts.py

86 lines
2.1 KiB
Python
Raw Normal View History

2025-02-11 09:00:35 +00:00
from datetime import datetime
2025-02-09 19:26:50 +00:00
import pytest
2025-02-11 09:00:35 +00:00
2025-05-29 20:40:27 +00:00
from auth.orm import Author
2025-02-09 19:26:50 +00:00
from orm.shout import Shout
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
@pytest.fixture
def test_shout(db_session):
"""Create test shout with required fields."""
now = int(datetime.now().timestamp())
author = Author(name="Test Author", slug="test-author", user="test-user-id")
db_session.add(author)
db_session.flush()
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
now = int(datetime.now().timestamp())
shout = Shout(
title="Test Shout",
slug="test-shout",
created_by=author.id,
body="Test body",
layout="article",
lang="ru",
community=1,
created_at=now,
2025-02-11 09:00:35 +00:00
updated_at=now,
2025-02-09 19:26:50 +00:00
)
db_session.add(shout)
db_session.commit()
return shout
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
@pytest.mark.asyncio
async def test_get_shout(test_client, db_session):
"""Test retrieving a shout."""
# Создаем автора
author = Author(name="Test Author", slug="test-author", user="test-user-id")
db_session.add(author)
db_session.flush()
now = int(datetime.now().timestamp())
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
# Создаем публикацию со всеми обязательными полями
shout = Shout(
title="Test Shout",
body="This is a test shout",
slug="test-shout",
created_by=author.id,
layout="article",
lang="ru",
community=1,
created_at=now,
2025-02-11 09:00:35 +00:00
updated_at=now,
2025-02-09 19:26:50 +00:00
)
db_session.add(shout)
db_session.commit()
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
response = test_client.post(
"/",
json={
"query": """
query GetShout($slug: String!) {
get_shout(slug: $slug) {
id
title
body
created_at
updated_at
created_by {
id
name
slug
}
}
}
""",
2025-02-11 09:00:35 +00:00
"variables": {"slug": "test-shout"},
},
2025-02-09 19:26:50 +00:00
)
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
data = response.json()
assert response.status_code == 200
assert "errors" not in data
2025-02-11 09:00:35 +00:00
assert data["data"]["get_shout"]["title"] == "Test Shout"