|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +class TestAioHttpIntegration: |
| 7 | + @pytest.mark.asyncio |
| 8 | + async def test_get_items(self, client): |
| 9 | + """Test retrieving all items""" |
| 10 | + resp = await client.get("/items") |
| 11 | + assert resp.status == 200 |
| 12 | + data = await resp.json() |
| 13 | + assert len(data) == 2 |
| 14 | + assert data[0]["name"] == "Item 1" |
| 15 | + assert data[1]["name"] == "Item 2" |
| 16 | + |
| 17 | + @pytest.mark.asyncio |
| 18 | + async def test_get_items_sync(self, client): |
| 19 | + """Test retrieving all items (synchronous endpoint)""" |
| 20 | + resp = await client.get("/items-sync") |
| 21 | + assert resp.status == 200 |
| 22 | + data = await resp.json() |
| 23 | + assert len(data) == 2 |
| 24 | + assert data[0]["name"] == "Item 1" |
| 25 | + assert data[1]["name"] == "Item 2" |
| 26 | + |
| 27 | + @pytest.mark.asyncio |
| 28 | + async def test_get_items_fail(self, client): |
| 29 | + """Test retrieving items with generated error""" |
| 30 | + resp = await client.get("/items-fail") |
| 31 | + assert resp.status == 500 |
| 32 | + data = await resp.json() |
| 33 | + assert data["detail"] == "TEST ERROR" |
| 34 | + |
| 35 | + @pytest.mark.asyncio |
| 36 | + async def test_get_item(self, client): |
| 37 | + """Test retrieving item by ID""" |
| 38 | + resp = await client.get("/items/1") |
| 39 | + assert resp.status == 200 |
| 40 | + data = await resp.json() |
| 41 | + assert data["id"] == 1 |
| 42 | + assert data["name"] == "Item 1" |
| 43 | + assert data["description"] == "Description 1" |
| 44 | + |
| 45 | + @pytest.mark.asyncio |
| 46 | + async def test_get_item_unprocessable(self, client): |
| 47 | + """Test retrieving item with incorrect parameter type""" |
| 48 | + resp = await client.get("/items/abc") |
| 49 | + assert resp.status == 422 |
| 50 | + data = await resp.json() |
| 51 | + assert "Error casting parameter" in data["detail"] |
| 52 | + |
| 53 | + @pytest.mark.asyncio |
| 54 | + async def test_get_nonexistent_item(self, client): |
| 55 | + """Test retrieving non-existent item""" |
| 56 | + resp = await client.get("/items/999") |
| 57 | + assert resp.status == 404 |
| 58 | + |
| 59 | + @pytest.mark.asyncio |
| 60 | + async def test_create_item(self, client): |
| 61 | + """Test creating an item""" |
| 62 | + new_item = {"name": "New Item", "description": "New Description"} |
| 63 | + headers = {"Content-Type": "application/json"} |
| 64 | + resp = await client.post("/items", data=json.dumps(new_item), headers=headers) |
| 65 | + assert resp.status == 201 |
| 66 | + data = await resp.json() |
| 67 | + assert data["id"] == 3 |
| 68 | + assert data["name"] == "New Item" |
| 69 | + assert data["description"] == "New Description" |
| 70 | + |
| 71 | + @pytest.mark.asyncio |
| 72 | + async def test_create_item_incorrect(self, client): |
| 73 | + """Test creating an item with invalid data""" |
| 74 | + new_item = {"name": None, "description": "New Description"} |
| 75 | + headers = {"Content-Type": "application/json"} |
| 76 | + resp = await client.post("/items", data=json.dumps(new_item), headers=headers) |
| 77 | + assert resp.status == 422 |
| 78 | + data = await resp.json() |
| 79 | + assert "Validation error for parameter" in data["detail"] |
| 80 | + |
| 81 | + @pytest.mark.asyncio |
| 82 | + async def test_create_item_invalid_json(self, client): |
| 83 | + """Test creating an item with invalid JSON""" |
| 84 | + headers = {"Content-Type": "application/json"} |
| 85 | + resp = await client.post("/items", data="incorrect json", headers=headers) |
| 86 | + assert resp.status == 422 |
| 87 | + data = await resp.json() |
| 88 | + assert "Validation error for parameter" in data["detail"] |
| 89 | + |
| 90 | + @pytest.mark.asyncio |
| 91 | + async def test_update_item(self, client): |
| 92 | + """Test updating an item""" |
| 93 | + update_data = {"name": "Updated Item", "description": "Updated Description"} |
| 94 | + headers = {"Content-Type": "application/json"} |
| 95 | + resp = await client.put( |
| 96 | + "/items/2", data=json.dumps(update_data), headers=headers |
| 97 | + ) |
| 98 | + assert resp.status == 200 |
| 99 | + data = await resp.json() |
| 100 | + assert data["id"] == 2 |
| 101 | + assert data["name"] == "Updated Item" |
| 102 | + assert data["description"] == "Updated Description" |
| 103 | + |
| 104 | + @pytest.mark.asyncio |
| 105 | + async def test_delete_item(self, client): |
| 106 | + """Test deleting an item""" |
| 107 | + resp = await client.delete("/items/1") |
| 108 | + assert resp.status == 204 |
| 109 | + |
| 110 | + # Check that the item was actually deleted |
| 111 | + resp = await client.get("/items/1") |
| 112 | + assert resp.status == 404 |
| 113 | + |
| 114 | + @pytest.mark.asyncio |
| 115 | + async def test_openapi_schema_endpoint(self, client): |
| 116 | + """Test OpenAPI schema endpoint""" |
| 117 | + resp = await client.get("/openapi.json") |
| 118 | + assert resp.status == 200 |
| 119 | + schema = await resp.json() |
| 120 | + assert schema["info"]["title"] == "Test API" |
| 121 | + assert "/items" in schema["paths"] |
| 122 | + assert "/items/{item_id}" in schema["paths"] |
| 123 | + |
| 124 | + @pytest.mark.asyncio |
| 125 | + async def test_swagger_ui_endpoint(self, client): |
| 126 | + """Test Swagger UI endpoint""" |
| 127 | + resp = await client.get("/docs") |
| 128 | + assert resp.status == 200 |
| 129 | + text = await resp.text() |
| 130 | + assert "text/html" in resp.headers["Content-Type"] |
| 131 | + assert "swagger-ui" in text |
| 132 | + |
| 133 | + @pytest.mark.asyncio |
| 134 | + async def test_redoc_ui_endpoint(self, client): |
| 135 | + """Test ReDoc UI endpoint""" |
| 136 | + resp = await client.get("/redoc") |
| 137 | + assert resp.status == 200 |
| 138 | + text = await resp.text() |
| 139 | + assert "text/html" in resp.headers["Content-Type"] |
| 140 | + assert "redoc" in text |
0 commit comments