|
| 1 | +# .github/scripts/test_homework3.py |
| 2 | +from fastapi.testclient import TestClient |
| 3 | + |
| 4 | +try: |
| 5 | + from main import app # Assuming the FastAPI application is in main.py |
| 6 | +except ImportError: |
| 7 | + from app.main import app # If unable to import from the main module, try importing from app.main |
| 8 | + |
| 9 | +client = TestClient(app) |
| 10 | + |
| 11 | +# Test the correctness of GET /items/{item_id} |
| 12 | +def test_get_item_valid(): |
| 13 | + response = client.get("/items/123?q=test_query&sort_order=asc") |
| 14 | + assert response.status_code == 200 |
| 15 | + assert response.json() == { |
| 16 | + "item_id": 123, |
| 17 | + "description": "This is a sample item that matches the query test_query", |
| 18 | + "sort_order": "asc" |
| 19 | + } |
| 20 | + |
| 21 | +# Test GET /items/{item_id} with invalid path parameter |
| 22 | +def test_get_item_invalid_id(): |
| 23 | + response = client.get("/items/1001") |
| 24 | + assert response.status_code == 422 # Should return 422 Unprocessable Entity |
| 25 | + assert "detail" in response.json() |
| 26 | + |
| 27 | +# Test GET /items/{item_id} with invalid query parameter q |
| 28 | +def test_get_item_invalid_query(): |
| 29 | + response = client.get("/items/123?q=ab&sort_order=asc") |
| 30 | + assert response.status_code == 422 # Should return 422 Unprocessable Entity |
| 31 | + assert "detail" in response.json() |
| 32 | + |
| 33 | +# Test GET /items/{item_id} without query parameters |
| 34 | +def test_get_item_no_query(): |
| 35 | + response = client.get("/items/123") |
| 36 | + assert response.status_code == 200 |
| 37 | + assert response.json() == { |
| 38 | + "item_id": 123, |
| 39 | + "description": "This is a sample item.", |
| 40 | + "sort_order": "asc" |
| 41 | + } |
| 42 | + |
| 43 | +# Test PUT /items/{item_id} to update item data |
| 44 | +def test_put_item_valid(): |
| 45 | + data = { |
| 46 | + "name": "Updated Item", |
| 47 | + "description": "Updated item description", |
| 48 | + "price": 20.0, |
| 49 | + "tax": 2.5 |
| 50 | + } |
| 51 | + response = client.put("/items/123", json=data) |
| 52 | + assert response.status_code == 200 |
| 53 | + assert response.json() == { |
| 54 | + "item_id": 123, |
| 55 | + "name": "Updated Item", |
| 56 | + "description": "Updated item description", |
| 57 | + "price": 20.0, |
| 58 | + "tax": 2.5 |
| 59 | + } |
| 60 | + |
| 61 | +# Test PUT /items/{item_id} with valid query parameter q |
| 62 | +def test_put_item_with_query(): |
| 63 | + data = { |
| 64 | + "name": "Updated Item", |
| 65 | + "description": "Updated item description", |
| 66 | + "price": 20.0, |
| 67 | + "tax": 2.5 |
| 68 | + } |
| 69 | + response = client.put("/items/123?q=test_query", json=data) |
| 70 | + assert response.status_code == 200 |
| 71 | + assert response.json() == { |
| 72 | + "item_id": 123, |
| 73 | + "name": "Updated Item", |
| 74 | + "description": "Updated item description", |
| 75 | + "price": 20.0, |
| 76 | + "tax": 2.5, |
| 77 | + "q": "test_query" |
| 78 | + } |
| 79 | + |
| 80 | +# Test PUT /items/{item_id} with invalid path parameter |
| 81 | +def test_put_item_invalid_id(): |
| 82 | + data = { |
| 83 | + "name": "Updated Item", |
| 84 | + "description": "Updated item description", |
| 85 | + "price": 20.0, |
| 86 | + "tax": 2.5 |
| 87 | + } |
| 88 | + response = client.put("/items/1001", json=data) |
| 89 | + assert response.status_code == 422 |
| 90 | + assert "detail" in response.json() |
| 91 | + |
| 92 | +# Test PUT /items/{item_id} with invalid query parameter q |
| 93 | +def test_put_item_invalid_query(): |
| 94 | + data = { |
| 95 | + "name": "Updated Item", |
| 96 | + "description": "Updated item description", |
| 97 | + "price": 20.0, |
| 98 | + "tax": 2.5 |
| 99 | + } |
| 100 | + response = client.put("/items/123?q=ab", json=data) |
| 101 | + assert response.status_code == 422 |
| 102 | + assert "detail" in response.json() |
0 commit comments