|
| 1 | +from datetime import datetime, timezone |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +import pytest |
| 5 | +from fastapi.testclient import TestClient |
| 6 | + |
| 7 | +from api.db.database import get_db |
| 8 | +from api.v1.models.blog import Blog |
| 9 | +from main import app |
| 10 | + |
| 11 | +client = TestClient(app) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def db_session_mock(): |
| 16 | + return MagicMock() |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture(autouse=True) |
| 20 | +def override_get_db(db_session_mock): |
| 21 | + app.dependency_overrides[get_db] = lambda: db_session_mock |
| 22 | + yield |
| 23 | + app.dependency_overrides[get_db] = None |
| 24 | + |
| 25 | + |
| 26 | +def test_update_blog_success(db_session_mock): |
| 27 | + """Ensure that the endpoint successfully updates the data of an existing blog post.""" |
| 28 | + existing_blog = Blog( |
| 29 | + id=1, |
| 30 | + title="Existing Blog Post", |
| 31 | + excerpt="A summary of the blog post...", |
| 32 | + content="The content of the blog post...", |
| 33 | + image_url="image-url-link", |
| 34 | + created_at=datetime.now(timezone.utc), |
| 35 | + updated_at=datetime.now(timezone.utc), |
| 36 | + ) |
| 37 | + updated_data = { |
| 38 | + "title": "Updated Blog Post", |
| 39 | + "excerpt": "An updated summary...", |
| 40 | + "content": "Updated content...", |
| 41 | + "image_url": "updated-image-url-link", |
| 42 | + } |
| 43 | + |
| 44 | + db_session_mock.query.return_value.filter.return_value.first.side_effect = [ |
| 45 | + existing_blog, # Return the existing blog for the first query |
| 46 | + None, # Return None for the second query to simulate no conflict |
| 47 | + ] |
| 48 | + db_session_mock.commit.side_effect = lambda: None |
| 49 | + db_session_mock.refresh.side_effect = lambda blog: setattr( |
| 50 | + blog, "updated_at", datetime.now(timezone.utc) |
| 51 | + ) |
| 52 | + |
| 53 | + response = client.patch(f"/api/v1/blogs/{existing_blog.id}", json=updated_data) |
| 54 | + |
| 55 | + assert response.status_code == 200 |
| 56 | + response_data = response.json() |
| 57 | + assert response_data["title"] == updated_data["title"] |
| 58 | + assert response_data["excerpt"] == updated_data["excerpt"] |
| 59 | + assert response_data["content"] == updated_data["content"] |
| 60 | + assert response_data["image_url"] == updated_data["image_url"] |
| 61 | + assert "updated_at" in response_data |
| 62 | + |
| 63 | + |
| 64 | +def test_update_blog_conflict(db_session_mock): |
| 65 | + """Simulate a request to update a blog post with a title that already exists.""" |
| 66 | + existing_blog = Blog( |
| 67 | + id=1, |
| 68 | + title="Existing Blog Post", |
| 69 | + excerpt="A summary of the blog post...", |
| 70 | + content="The content of the blog post...", |
| 71 | + image_url="image-url-link", |
| 72 | + created_at=datetime.now(timezone.utc), |
| 73 | + updated_at=datetime.now(timezone.utc), |
| 74 | + ) |
| 75 | + conflicting_blog = Blog( |
| 76 | + id=2, |
| 77 | + title="Updated Blog Post", |
| 78 | + excerpt="Another summary...", |
| 79 | + content="Another content...", |
| 80 | + image_url="another-image-url-link", |
| 81 | + created_at=datetime.now(timezone.utc), |
| 82 | + updated_at=datetime.now(timezone.utc), |
| 83 | + ) |
| 84 | + updated_data = { |
| 85 | + "title": "Updated Blog Post", |
| 86 | + "excerpt": "An updated summary...", |
| 87 | + "content": "Updated content...", |
| 88 | + "image_url": "updated-image-url-link", |
| 89 | + } |
| 90 | + |
| 91 | + db_session_mock.query.return_value.filter.return_value.first.side_effect = [ |
| 92 | + existing_blog, # Return the existing blog for the first query |
| 93 | + conflicting_blog, # Return the conflicting blog for the second query |
| 94 | + ] |
| 95 | + |
| 96 | + response = client.patch(f"/api/v1/blogs/{existing_blog.id}", json=updated_data) |
| 97 | + |
| 98 | + assert response.status_code == 409 |
| 99 | + assert response.json()["detail"] == "A blog post with this title already exists." |
| 100 | + |
| 101 | + |
| 102 | +def test_update_blog_not_found(db_session_mock): |
| 103 | + """Simulate a request to update a blog post that does not exist.""" |
| 104 | + updated_data = { |
| 105 | + "title": "Updated Blog Post", |
| 106 | + "excerpt": "An updated summary...", |
| 107 | + "content": "Updated content...", |
| 108 | + "image_url": "updated-image-url-link", |
| 109 | + } |
| 110 | + |
| 111 | + db_session_mock.query.return_value.filter.return_value.first.return_value = None |
| 112 | + |
| 113 | + response = client.patch("/api/v1/blogs/999", json=updated_data) |
| 114 | + |
| 115 | + assert response.status_code == 404 |
| 116 | + assert response.json()["detail"] == "Blog post not found." |
| 117 | + |
| 118 | + |
| 119 | +def test_update_blog_internal_server_error(db_session_mock): |
| 120 | + """Simulate an internal server error to raise an exception.""" |
| 121 | + updated_data = { |
| 122 | + "title": "Updated Blog Post", |
| 123 | + "excerpt": "An updated summary...", |
| 124 | + "content": "Updated content...", |
| 125 | + "image_url": "updated-image-url-link", |
| 126 | + } |
| 127 | + |
| 128 | + db_session_mock.query.side_effect = Exception("Unexpected error") |
| 129 | + |
| 130 | + response = client.patch("/api/v1/blogs/1", json=updated_data) |
| 131 | + |
| 132 | + assert response.status_code == 500 |
| 133 | + assert response.json()["detail"] == "Internal server error." |
| 134 | + |
| 135 | + |
| 136 | +def test_update_blog_invalid_data(): |
| 137 | + """Send requests with invalid data and check for validation errors.""" |
| 138 | + invalid_data = { |
| 139 | + "excerpt": "An updated summary...", |
| 140 | + "content": "Updated content...", |
| 141 | + "image_url": "updated-image-url-link", |
| 142 | + } |
| 143 | + |
| 144 | + response = client.patch("/api/v1/blogs/1", json=invalid_data) |
| 145 | + |
| 146 | + assert response.status_code == 422 |
| 147 | + |
| 148 | + |
| 149 | +def test_update_blog_boundary_testing(db_session_mock): |
| 150 | + """Test the maximum length constraints for the title and excerpt fields.""" |
| 151 | + existing_blog = Blog( |
| 152 | + id=1, |
| 153 | + title="Existing Blog Post", |
| 154 | + excerpt="A summary of the blog post...", |
| 155 | + content="The content of the blog post...", |
| 156 | + image_url="image-url-link", |
| 157 | + created_at=datetime.now(timezone.utc), |
| 158 | + updated_at=datetime.now(timezone.utc), |
| 159 | + ) |
| 160 | + boundary_blog_data = { |
| 161 | + "title": "T" * 255, # Maximum allowed length for title |
| 162 | + "excerpt": "E" * 300, # Maximum allowed length for excerpt |
| 163 | + "content": "Content of the blog post...", |
| 164 | + "image_url": "image-url-link", |
| 165 | + } |
| 166 | + |
| 167 | + db_session_mock.query.return_value.filter.return_value.first.side_effect = [ |
| 168 | + existing_blog, # Return the existing blog for the first query |
| 169 | + None, # Return None for the second query to simulate no conflict |
| 170 | + ] |
| 171 | + db_session_mock.commit.side_effect = lambda: None |
| 172 | + db_session_mock.refresh.side_effect = lambda blog: setattr( |
| 173 | + blog, "updated_at", datetime.now(timezone.utc) |
| 174 | + ) |
| 175 | + |
| 176 | + response = client.patch( |
| 177 | + f"/api/v1/blogs/{existing_blog.id}", json=boundary_blog_data |
| 178 | + ) |
| 179 | + |
| 180 | + assert response.status_code == 200 |
| 181 | + response_data = response.json() |
| 182 | + assert response_data["title"] == boundary_blog_data["title"] |
| 183 | + assert response_data["excerpt"] == boundary_blog_data["excerpt"] |
| 184 | + assert response_data["content"] == boundary_blog_data["content"] |
| 185 | + assert response_data["image_url"] == boundary_blog_data["image_url"] |
| 186 | + assert "updated_at" in response_data |
0 commit comments