core/tests/test_drafts.py

95 lines
2.4 KiB
Python
Raw Normal View History

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-11 09:00:35 +00:00
from orm.shout import Shout
2025-02-09 19:26:50 +00:00
@pytest.fixture
def test_author(db_session):
"""Create a test author."""
2025-02-11 09:00:35 +00:00
author = Author(name="Test Author", slug="test-author", user="test-user-id")
2025-02-09 19:26:50 +00:00
db_session.add(author)
db_session.commit()
return author
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."""
author = Author(name="Test Author", slug="test-author", user="test-user-id")
db_session.add(author)
db_session.flush()
shout = Shout(
title="Test Shout",
slug="test-shout",
created_by=author.id, # Обязательное поле
body="Test body",
layout="article",
2025-02-11 09:00:35 +00:00
lang="ru",
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_create_shout(test_client, db_session, test_author):
"""Test creating a new shout."""
response = test_client.post(
"/",
json={
"query": """
2025-02-11 09:00:35 +00:00
mutation CreateDraft($draft_input: DraftInput!) {
create_draft(draft_input: $draft_input) {
2025-02-09 19:26:50 +00:00
error
draft {
id
title
body
}
}
}
""",
"variables": {
"input": {
"title": "Test Shout",
"body": "This is a test shout",
}
2025-02-11 09:00:35 +00:00
},
},
2025-02-09 19:26:50 +00:00
)
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
assert response.status_code == 200
data = response.json()
assert "errors" not in data
assert data["data"]["create_draft"]["draft"]["title"] == "Test Shout"
2025-02-11 09:00:35 +00:00
2025-02-09 19:26:50 +00:00
@pytest.mark.asyncio
async def test_load_drafts(test_client, db_session):
"""Test retrieving a shout."""
response = test_client.post(
"/",
json={
"query": """
query {
load_drafts {
error
drafts {
id
title
body
}
}
}
""",
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
assert response.status_code == 200
data = response.json()
assert "errors" not in data
2025-02-11 09:00:35 +00:00
assert data["data"]["load_drafts"]["drafts"] == []