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