Skip to content

Commit 3658c68

Browse files
committed
Add some basic tests for api.send_request()
1 parent a469789 commit 3658c68

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

tests/unit/test_api.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,40 @@ def test_upload_image_with_path(self, tmp_path):
397397
"user_id": "test",
398398
"password": "test",
399399
}
400+
401+
402+
class TestSendRequest:
403+
"""Unit tests for the api.send_request function."""
404+
405+
api_config = openfoodfacts.API(user_agent=TEST_USER_AGENT, version="v2").api_config
406+
407+
def test_invalid_method(self):
408+
method = "invalid"
409+
with pytest.raises(NotImplementedError) as excinfo:
410+
openfoodfacts.api.send_request(
411+
"https://example.com/",
412+
api_config=self.api_config,
413+
method=method,
414+
)
415+
assert method in str(excinfo.value)
416+
417+
def test_get_404_returns_none(self):
418+
method = "get"
419+
url = "https://world.openfoodfacts.org/api/v2/product/1223435"
420+
response_data = {
421+
"status": 0,
422+
"status_verbose": "product not found",
423+
}
424+
with requests_mock.mock() as mock:
425+
mock.get(
426+
url,
427+
text=json.dumps(response_data),
428+
status_code=404,
429+
)
430+
res = openfoodfacts.api.send_request(
431+
url,
432+
api_config=self.api_config,
433+
method=method,
434+
return_none_on_404=True,
435+
)
436+
assert res is None

0 commit comments

Comments
 (0)