|
| 1 | +from core.settings import settings |
| 2 | +from core.utils import unique_slug_generator |
| 3 | +from fastapi.testclient import TestClient |
| 4 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 5 | + |
| 6 | +from blog.test_helpers import create_random_category, create_random_post |
| 7 | + |
| 8 | +test_data = { |
| 9 | + "title": "Archangel", |
| 10 | + "description": "macsika" |
| 11 | + } |
| 12 | + |
| 13 | +def test_create_post( |
| 14 | + client: TestClient, |
| 15 | + superuser_token_headers: dict, |
| 16 | + db: AsyncSession |
| 17 | + ) -> None: |
| 18 | + response = client.post( |
| 19 | + f"{settings.API_V1_STR}/blog/posts/", |
| 20 | + headers=superuser_token_headers, |
| 21 | + json=test_data |
| 22 | + ) |
| 23 | + assert response.status_code == 201 or response.status_code == 200 |
| 24 | + content = response.json() |
| 25 | + assert content["title"] == test_data["title"] |
| 26 | + assert content["description"] == test_data["description"] |
| 27 | + assert content["slug"] == unique_slug_generator(test_data["title"]) |
| 28 | + assert "id" in content |
| 29 | + assert "owner_id" in content |
| 30 | + |
| 31 | + |
| 32 | +def test_create_category( |
| 33 | + client: TestClient, |
| 34 | + superuser_token_headers: dict, |
| 35 | + db: AsyncSession |
| 36 | + ) -> None: |
| 37 | + response = client.post( |
| 38 | + f"{settings.API_V1_STR}/blog/tags/", |
| 39 | + headers=superuser_token_headers, |
| 40 | + json=test_data |
| 41 | + ) |
| 42 | + assert response.status_code == 201 or response.status_code == 200 |
| 43 | + content = response.json() |
| 44 | + assert content["title"] == test_data["title"] |
| 45 | + assert content["description"] == test_data["description"] |
| 46 | + assert content["slug"] == unique_slug_generator(test_data["title"]) |
| 47 | + assert "id" in content |
| 48 | + |
| 49 | + |
| 50 | +def test_read_post( |
| 51 | + client: TestClient, |
| 52 | + superuser_token_headers: dict, |
| 53 | + db: AsyncSession) -> None: |
| 54 | + item = create_random_post(db) |
| 55 | + response = client.get( |
| 56 | + f"{settings.API_V1_STR}/blog/posts/{item.slug}", |
| 57 | + headers=superuser_token_headers, |
| 58 | + ) |
| 59 | + assert response.status_code == 200 |
| 60 | + content = response.json() |
| 61 | + assert content["title"] == item.title |
| 62 | + assert content["description"] == item.description |
| 63 | + assert content["slug"] == unique_slug_generator(test_data["title"]) |
| 64 | + assert content["id"] == item.id |
| 65 | + assert content["owner_id"] == item.owner_id |
| 66 | + |
| 67 | + |
| 68 | +def test_read_category( |
| 69 | + client: TestClient, |
| 70 | + superuser_token_headers: dict, |
| 71 | + db: AsyncSession) -> None: |
| 72 | + item = create_random_post(db) |
| 73 | + response = client.get( |
| 74 | + f"{settings.API_V1_STR}/blog/tags/{item.slug}", |
| 75 | + headers=superuser_token_headers, |
| 76 | + ) |
| 77 | + assert response.status_code == 200 |
| 78 | + content = response.json() |
| 79 | + assert content["title"] == item.title |
| 80 | + assert content["description"] == item.description |
| 81 | + assert content["slug"] == unique_slug_generator(test_data["title"]) |
| 82 | + assert content["id"] == item.id |
0 commit comments