|
| 1 | +import pytest |
| 2 | +from fastapi import status |
| 3 | +from fastapi.testclient import TestClient |
| 4 | + |
| 5 | +from main import app |
| 6 | + |
| 7 | +client = TestClient(app) |
| 8 | + |
| 9 | +initial_post_title = "Hello" |
| 10 | +initial_post_description = "World" |
| 11 | +changed_post_description = "From the other side" |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.dependency() |
| 15 | +def test_create_post(request): |
| 16 | + response = client.post( |
| 17 | + "/posts/create", |
| 18 | + json={"title": initial_post_title, "description": initial_post_description}, |
| 19 | + ) |
| 20 | + assert response.status_code == status.HTTP_201_CREATED |
| 21 | + assert response.json()["title"] == "Hello" |
| 22 | + assert response.json()["description"] == "World" |
| 23 | + request.config.cache.set("post_id", response.json()["id"]) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.dependency(depends=["test_create_post"]) |
| 27 | +def test_get_all_posts(): |
| 28 | + response = client.get("/posts/list/all") |
| 29 | + assert response.status_code == status.HTTP_200_OK |
| 30 | + assert response.json() is not None |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.dependency(depends=["test_create_post"]) |
| 34 | +def test_get_one_post(request): |
| 35 | + post_id = request.config.cache.get("post_id", None) |
| 36 | + response = client.get(f"/posts/get/{post_id}") |
| 37 | + assert response.status_code == status.HTTP_200_OK |
| 38 | + assert response.json()["id"] == post_id |
| 39 | + assert response.json()["title"] == initial_post_title |
| 40 | + assert ( |
| 41 | + response.json()["description"] == initial_post_description |
| 42 | + or changed_post_description |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +@pytest.mark.dependency(depends=["test_create_post", "test_get_one_post"]) |
| 47 | +def test_patch_post(request): |
| 48 | + post_id = request.config.cache.get("post_id", None) |
| 49 | + response = client.patch( |
| 50 | + "/posts/update", |
| 51 | + json={ |
| 52 | + "id": post_id, |
| 53 | + "title": initial_post_title, |
| 54 | + "description": changed_post_description, |
| 55 | + }, |
| 56 | + ) |
| 57 | + assert response.status_code == status.HTTP_200_OK |
| 58 | + assert response.json()["id"] == post_id |
| 59 | + assert response.json()["title"] == initial_post_title |
| 60 | + assert response.json()["description"] == changed_post_description |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.dependency( |
| 64 | + depends=[ |
| 65 | + "test_create_post", |
| 66 | + "test_get_one_post", |
| 67 | + "test_patch_post", |
| 68 | + "test_get_all_posts", |
| 69 | + ] |
| 70 | +) |
| 71 | +def test_delete_post(request): |
| 72 | + post_id = request.config.cache.get("post_id", None) |
| 73 | + response = client.delete(f"/posts/delete/{post_id}") |
| 74 | + assert response.status_code == status.HTTP_200_OK |
| 75 | + assert response.json()["detail"] == "Post Deleted" |
0 commit comments