|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | +import respx |
| 4 | + |
| 5 | + |
| 6 | +def test_fetch_success(resource_client): |
| 7 | + expected_response = httpx.Response( |
| 8 | + httpx.codes.OK, |
| 9 | + json={"data": {"id": "RES-123", "name": "Test Resource", "status": "active"}}, |
| 10 | + ) |
| 11 | + |
| 12 | + with respx.mock: |
| 13 | + mock_route = respx.get("https://api.example.com/api/v1/test-resource/RES-123").mock( |
| 14 | + return_value=expected_response |
| 15 | + ) |
| 16 | + |
| 17 | + resource = resource_client.fetch() |
| 18 | + |
| 19 | + assert resource.to_dict() == {"id": "RES-123", "name": "Test Resource", "status": "active"} |
| 20 | + assert mock_route.called |
| 21 | + assert mock_route.call_count == 1 |
| 22 | + assert resource_client.resource_ is not None |
| 23 | + |
| 24 | + |
| 25 | +def test_get_attribute(resource_client): |
| 26 | + expected_response = httpx.Response( |
| 27 | + httpx.codes.OK, |
| 28 | + json={"data": {"id": "RES-123", "contact": {"name": "Albert"}, "status": "active"}}, |
| 29 | + ) |
| 30 | + |
| 31 | + with respx.mock: |
| 32 | + mock_route = respx.get("https://api.example.com/api/v1/test-resource/RES-123").mock( |
| 33 | + return_value=expected_response |
| 34 | + ) |
| 35 | + |
| 36 | + assert resource_client.id == "RES-123" |
| 37 | + assert resource_client.contact.name == "Albert" |
| 38 | + assert mock_route.call_count == 1 |
| 39 | + |
| 40 | + |
| 41 | +def test_set_attribute(resource_client): |
| 42 | + expected_response = httpx.Response( |
| 43 | + httpx.codes.OK, |
| 44 | + json={"data": {"id": "RES-123", "contact": {"name": "Albert"}, "status": "active"}}, |
| 45 | + ) |
| 46 | + |
| 47 | + with respx.mock: |
| 48 | + respx.get("https://api.example.com/api/v1/test-resource/RES-123").mock( |
| 49 | + return_value=expected_response |
| 50 | + ) |
| 51 | + |
| 52 | + resource_client.status = "disabled" |
| 53 | + resource_client.contact.name = "Alice" |
| 54 | + |
| 55 | + assert resource_client.status == "disabled" |
| 56 | + assert resource_client.contact.name == "Alice" |
| 57 | + |
| 58 | + |
| 59 | +def test_fetch_not_found(resource_client): |
| 60 | + error_response = httpx.Response(httpx.codes.NOT_FOUND, json={"error": "Resource not found"}) |
| 61 | + |
| 62 | + with respx.mock: |
| 63 | + respx.get("https://api.example.com/api/v1/test-resource/RES-123").mock( |
| 64 | + return_value=error_response |
| 65 | + ) |
| 66 | + |
| 67 | + with pytest.raises(httpx.HTTPStatusError): |
| 68 | + resource_client.fetch() |
| 69 | + |
| 70 | + |
| 71 | +def test_fetch_server_error(resource_client): |
| 72 | + error_response = httpx.Response( |
| 73 | + httpx.codes.INTERNAL_SERVER_ERROR, json={"error": "Internal server error"} |
| 74 | + ) |
| 75 | + |
| 76 | + with respx.mock: |
| 77 | + respx.get("https://api.example.com/api/v1/test-resource/RES-123").mock( |
| 78 | + return_value=error_response |
| 79 | + ) |
| 80 | + |
| 81 | + with pytest.raises(httpx.HTTPStatusError): |
| 82 | + resource_client.fetch() |
| 83 | + |
| 84 | + |
| 85 | +def test_fetch_with_special_characters_in_id(resource_client): |
| 86 | + expected_response = httpx.Response( |
| 87 | + httpx.codes.OK, json={"data": {"id": "RES-123", "name": "Special Resource"}} |
| 88 | + ) |
| 89 | + |
| 90 | + with respx.mock: |
| 91 | + mock_route = respx.get("https://api.example.com/api/v1/test-resource/RES-123").mock( |
| 92 | + return_value=expected_response |
| 93 | + ) |
| 94 | + |
| 95 | + resource = resource_client.fetch() |
| 96 | + |
| 97 | + assert resource.to_dict() == {"id": "RES-123", "name": "Special Resource"} |
| 98 | + assert mock_route.called |
| 99 | + |
| 100 | + |
| 101 | +def test_fetch_verifies_correct_url_construction(resource_client): |
| 102 | + expected_response = httpx.Response(httpx.codes.OK, json={"data": {"id": "RES-123"}}) |
| 103 | + |
| 104 | + with respx.mock: |
| 105 | + mock_route = respx.get("https://api.example.com/api/v1/test-resource/RES-123").mock( |
| 106 | + return_value=expected_response |
| 107 | + ) |
| 108 | + |
| 109 | + resource_client.fetch() |
| 110 | + |
| 111 | + request = mock_route.calls[0].request |
| 112 | + |
| 113 | + assert request.method == "GET" |
| 114 | + assert str(request.url) == "https://api.example.com/api/v1/test-resource/RES-123" |
0 commit comments