|
| 1 | +import json |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from app.api import crud, summaries |
| 7 | + |
| 8 | + |
| 9 | +def test_create_summary(test_app, monkeypatch): |
| 10 | + def mock_generate_summary(summary_id, url): |
| 11 | + return None |
| 12 | + |
| 13 | + monkeypatch.setattr(summaries, "generate_summary", mock_generate_summary) |
| 14 | + |
| 15 | + test_request_payload = {"url": "https://foo.bar"} |
| 16 | + test_response_payload = {"id": 1, "url": "https://foo.bar/"} |
| 17 | + |
| 18 | + async def mock_post(payload): |
| 19 | + return 1 |
| 20 | + |
| 21 | + monkeypatch.setattr(crud, "post", mock_post) |
| 22 | + |
| 23 | + response = test_app.post( |
| 24 | + "/summaries/", |
| 25 | + data=json.dumps(test_request_payload), |
| 26 | + ) |
| 27 | + |
| 28 | + assert response.status_code == 201 |
| 29 | + assert response.json() == test_response_payload |
| 30 | + |
| 31 | + |
| 32 | +def test_create_summaries_invalid_json(test_app): |
| 33 | + response = test_app.post("/summaries/", data=json.dumps({})) |
| 34 | + assert response.status_code == 422 |
| 35 | + assert response.json() == { |
| 36 | + "detail": [ |
| 37 | + { |
| 38 | + "type": "missing", |
| 39 | + "loc": ["body", "url"], |
| 40 | + "msg": "Field required", |
| 41 | + "input": {}, |
| 42 | + } |
| 43 | + ] |
| 44 | + } |
| 45 | + |
| 46 | + response = test_app.post("/summaries/", data=json.dumps({"url": "invalid://url"})) |
| 47 | + assert response.status_code == 422 |
| 48 | + assert ( |
| 49 | + response.json()["detail"][0]["msg"] == "URL scheme should be 'http' or 'https'" |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +def test_read_summary(test_app, monkeypatch): |
| 54 | + test_data = { |
| 55 | + "id": 1, |
| 56 | + "url": "https://foo.bar", |
| 57 | + "summary": "summary", |
| 58 | + "created_at": datetime.utcnow().isoformat(), |
| 59 | + } |
| 60 | + |
| 61 | + async def mock_get(id): |
| 62 | + return test_data |
| 63 | + |
| 64 | + monkeypatch.setattr(crud, "get", mock_get) |
| 65 | + |
| 66 | + response = test_app.get("/summaries/1/") |
| 67 | + assert response.status_code == 200 |
| 68 | + assert response.json() == test_data |
| 69 | + |
| 70 | + |
| 71 | +def test_read_summary_incorrect_id(test_app, monkeypatch): |
| 72 | + async def mock_get(id): |
| 73 | + return None |
| 74 | + |
| 75 | + monkeypatch.setattr(crud, "get", mock_get) |
| 76 | + |
| 77 | + response = test_app.get("/summaries/999/") |
| 78 | + assert response.status_code == 404 |
| 79 | + assert response.json()["detail"] == "Summary not found" |
| 80 | + |
| 81 | + |
| 82 | +def test_read_all_summaries(test_app, monkeypatch): |
| 83 | + test_data = [ |
| 84 | + { |
| 85 | + "id": 1, |
| 86 | + "url": "https://foo.bar", |
| 87 | + "summary": "summary", |
| 88 | + "created_at": datetime.utcnow().isoformat(), |
| 89 | + }, |
| 90 | + { |
| 91 | + "id": 2, |
| 92 | + "url": "https://testdrivenn.io", |
| 93 | + "summary": "summary", |
| 94 | + "created_at": datetime.utcnow().isoformat(), |
| 95 | + }, |
| 96 | + ] |
| 97 | + |
| 98 | + async def mock_get_all(): |
| 99 | + return test_data |
| 100 | + |
| 101 | + monkeypatch.setattr(crud, "get_all", mock_get_all) |
| 102 | + |
| 103 | + response = test_app.get("/summaries/") |
| 104 | + assert response.status_code == 200 |
| 105 | + assert response.json() == test_data |
| 106 | + |
| 107 | + |
| 108 | +def test_remove_summary(test_app, monkeypatch): |
| 109 | + async def mock_get(id): |
| 110 | + return { |
| 111 | + "id": 1, |
| 112 | + "url": "https://foo.bar", |
| 113 | + "summary": "summary", |
| 114 | + "created_at": datetime.utcnow().isoformat(), |
| 115 | + } |
| 116 | + |
| 117 | + monkeypatch.setattr(crud, "get", mock_get) |
| 118 | + |
| 119 | + async def mock_delete(id): |
| 120 | + return id |
| 121 | + |
| 122 | + monkeypatch.setattr(crud, "delete", mock_delete) |
| 123 | + |
| 124 | + response = test_app.delete("/summaries/1/") |
| 125 | + assert response.status_code == 200 |
| 126 | + assert response.json() == {"id": 1, "url": "https://foo.bar/"} |
| 127 | + |
| 128 | + |
| 129 | +def test_remove_summary_incorrect_id(test_app, monkeypatch): |
| 130 | + async def mock_get(id): |
| 131 | + return None |
| 132 | + |
| 133 | + monkeypatch.setattr(crud, "get", mock_get) |
| 134 | + |
| 135 | + response = test_app.delete("/summaries/999/") |
| 136 | + assert response.status_code == 404 |
| 137 | + assert response.json()["detail"] == "Summary not found" |
| 138 | + |
| 139 | + |
| 140 | +def test_update_summary(test_app, monkeypatch): |
| 141 | + test_request_payload = {"url": "https://foo.bar", "summary": "updated"} |
| 142 | + test_response_payload = { |
| 143 | + "id": 1, |
| 144 | + "url": "https://foo.bar", |
| 145 | + "summary": "summary", |
| 146 | + "created_at": datetime.utcnow().isoformat(), |
| 147 | + } |
| 148 | + |
| 149 | + async def mock_put(id, payload): |
| 150 | + return test_response_payload |
| 151 | + |
| 152 | + monkeypatch.setattr(crud, "put", mock_put) |
| 153 | + |
| 154 | + response = test_app.put( |
| 155 | + "/summaries/1/", |
| 156 | + data=json.dumps(test_request_payload), |
| 157 | + ) |
| 158 | + assert response.status_code == 200 |
| 159 | + assert response.json() == test_response_payload |
| 160 | + |
| 161 | + |
| 162 | +@pytest.mark.parametrize( |
| 163 | + "summary_id, payload, status_code, detail", |
| 164 | + [ |
| 165 | + [ |
| 166 | + 999, |
| 167 | + {"url": "https://foo.bar", "summary": "updated!"}, |
| 168 | + 404, |
| 169 | + "Summary not found", |
| 170 | + ], |
| 171 | + [ |
| 172 | + 0, |
| 173 | + {"url": "https://foo.bar", "summary": "updated!"}, |
| 174 | + 422, |
| 175 | + [ |
| 176 | + { |
| 177 | + "type": "greater_than", |
| 178 | + "loc": ["path", "id"], |
| 179 | + "msg": "Input should be greater than 0", |
| 180 | + "input": "0", |
| 181 | + "ctx": {"gt": 0}, |
| 182 | + } |
| 183 | + ], |
| 184 | + ], |
| 185 | + [ |
| 186 | + 1, |
| 187 | + {}, |
| 188 | + 422, |
| 189 | + [ |
| 190 | + { |
| 191 | + "type": "missing", |
| 192 | + "loc": ["body", "url"], |
| 193 | + "msg": "Field required", |
| 194 | + "input": {}, |
| 195 | + }, |
| 196 | + { |
| 197 | + "type": "missing", |
| 198 | + "loc": ["body", "summary"], |
| 199 | + "msg": "Field required", |
| 200 | + "input": {}, |
| 201 | + }, |
| 202 | + ], |
| 203 | + ], |
| 204 | + [ |
| 205 | + 1, |
| 206 | + {"url": "https://foo.bar"}, |
| 207 | + 422, |
| 208 | + [ |
| 209 | + { |
| 210 | + "type": "missing", |
| 211 | + "loc": ["body", "summary"], |
| 212 | + "msg": "Field required", |
| 213 | + "input": {"url": "https://foo.bar"}, |
| 214 | + } |
| 215 | + ], |
| 216 | + ], |
| 217 | + ], |
| 218 | +) |
| 219 | +def test_update_summary_invalid( |
| 220 | + test_app, monkeypatch, summary_id, payload, status_code, detail |
| 221 | +): |
| 222 | + async def mock_put(id, payload): |
| 223 | + return None |
| 224 | + |
| 225 | + monkeypatch.setattr(crud, "put", mock_put) |
| 226 | + |
| 227 | + response = test_app.put(f"/summaries/{summary_id}/", data=json.dumps(payload)) |
| 228 | + assert response.status_code == status_code |
| 229 | + assert response.json()["detail"] == detail |
| 230 | + |
| 231 | + |
| 232 | +def test_update_summary_invalid_url(test_app): |
| 233 | + response = test_app.put( |
| 234 | + "/summaries/1/", |
| 235 | + data=json.dumps({"url": "invalid://url", "summary": "updated!"}), |
| 236 | + ) |
| 237 | + assert response.status_code == 422 |
| 238 | + assert ( |
| 239 | + response.json()["detail"][0]["msg"] == "URL scheme should be 'http' or 'https'" |
| 240 | + ) |
0 commit comments