|
| 1 | +from io import BytesIO, StringIO |
| 2 | +from unittest.mock import Mock |
| 3 | + |
| 4 | +import pytest |
| 5 | +import requests |
| 6 | + |
| 7 | +import serpapi |
| 8 | + |
| 9 | + |
| 10 | +def json_response(data): |
| 11 | + response = requests.Response() |
| 12 | + response.status_code = 200 |
| 13 | + response._content = data |
| 14 | + return response |
| 15 | + |
| 16 | + |
| 17 | +def test_upload_image_path_sends_multipart_request(tmp_path): |
| 18 | + image_path = tmp_path / "test.png" |
| 19 | + image_path.write_bytes(b"fake-png-data") |
| 20 | + client = serpapi.Client(api_key="test-api-key") |
| 21 | + |
| 22 | + def request(**kwargs): |
| 23 | + assert kwargs["method"] == "POST" |
| 24 | + assert kwargs["url"] == "https://serpapi.com/image" |
| 25 | + assert kwargs["params"] == {} |
| 26 | + assert kwargs["data"] == {"api_key": "test-api-key"} |
| 27 | + assert kwargs["files"]["image"].name == str(image_path) |
| 28 | + assert kwargs["files"]["image"].read() == b"fake-png-data" |
| 29 | + return json_response( |
| 30 | + b'{"message": "Image uploaded successfully.", "image_id": "image-123"}' |
| 31 | + ) |
| 32 | + |
| 33 | + client.session.request = Mock(side_effect=request) |
| 34 | + |
| 35 | + result = client.upload_image(image_path) |
| 36 | + |
| 37 | + assert result["image_id"] == "image-123" |
| 38 | + |
| 39 | + |
| 40 | +def test_upload_image_accepts_open_binary_file_and_request_options(): |
| 41 | + image = BytesIO(b"fake-image-data") |
| 42 | + client = serpapi.Client(api_key="client-api-key", timeout=10) |
| 43 | + client.session.request = Mock( |
| 44 | + return_value=json_response(b'{"image_id": "image-456"}') |
| 45 | + ) |
| 46 | + |
| 47 | + result = client.upload_image( |
| 48 | + image, |
| 49 | + api_key="request-api-key", |
| 50 | + timeout=5, |
| 51 | + zero_trace="true", |
| 52 | + ) |
| 53 | + |
| 54 | + assert result == {"image_id": "image-456"} |
| 55 | + assert not image.closed |
| 56 | + _, request_kwargs = client.session.request.call_args |
| 57 | + assert request_kwargs["params"] == {} |
| 58 | + assert request_kwargs["data"] == { |
| 59 | + "api_key": "request-api-key", |
| 60 | + "zero_trace": "true", |
| 61 | + } |
| 62 | + assert request_kwargs["files"] == {"image": image} |
| 63 | + assert request_kwargs["timeout"] == 5 |
| 64 | + |
| 65 | + |
| 66 | +def test_upload_image_rejects_text_mode_file(tmp_path): |
| 67 | + image_path = tmp_path / "test.png" |
| 68 | + image_path.write_text("not binary image data") |
| 69 | + client = serpapi.Client(api_key="test-api-key") |
| 70 | + client.session.request = Mock() |
| 71 | + |
| 72 | + with image_path.open("r") as image: |
| 73 | + with pytest.raises(TypeError, match="opened in binary mode"): |
| 74 | + client.upload_image(image) |
| 75 | + |
| 76 | + client.session.request.assert_not_called() |
| 77 | + |
| 78 | + |
| 79 | +def test_upload_image_rejects_string_io(): |
| 80 | + client = serpapi.Client(api_key="test-api-key") |
| 81 | + client.session.request = Mock() |
| 82 | + |
| 83 | + with pytest.raises(TypeError, match="opened in binary mode"): |
| 84 | + client.upload_image(StringIO("not binary image data")) |
| 85 | + |
| 86 | + client.session.request.assert_not_called() |
| 87 | + |
| 88 | + |
| 89 | +def test_request_injects_api_key_when_form_data_does_not_include_it(): |
| 90 | + client = serpapi.Client(api_key="test-api-key") |
| 91 | + client.session.request = Mock(return_value=json_response(b"{}")) |
| 92 | + |
| 93 | + client.request("POST", "/example", params={}, data={"field": "value"}) |
| 94 | + |
| 95 | + _, request_kwargs = client.session.request.call_args |
| 96 | + assert request_kwargs["params"] == {"api_key": "test-api-key"} |
| 97 | + assert request_kwargs["data"] == {"field": "value"} |
| 98 | + |
| 99 | + |
| 100 | +def test_module_exposes_upload_image_entrypoint(): |
| 101 | + assert callable(serpapi.upload_image) |
0 commit comments