Fix patent search client test to mock httpx transport
LocationBasedServices/smartguide/pipeline/pr-dev This commit looks good
LocationBasedServices/smartguide/pipeline/pr-dev This commit looks good
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import json
|
||||
|
||||
import httpx
|
||||
import pytest
|
||||
from unittest.mock import patch, MagicMock
|
||||
from inse.model import ESIndex
|
||||
from inse.patents.elastic_search_client import (
|
||||
DEFAULT_SEARCH_SIZE,
|
||||
@@ -52,13 +54,21 @@ def test_uspto_query_by_assignee_uses_assignees_orgs_field():
|
||||
async def test_search_epo_posts_to_correct_index():
|
||||
captured: list[dict] = []
|
||||
|
||||
def fake_search(index: str, body: dict, cluster=None):
|
||||
captured.append({"index": index, "body": body})
|
||||
return {"hits": {"hits": []}}
|
||||
def handler(request: httpx.Request) -> httpx.Response:
|
||||
path_parts = request.url.path.strip("/").split("/")
|
||||
captured.append(
|
||||
{
|
||||
"index": path_parts[-2],
|
||||
"body": json.loads(request.content) if request.content else {},
|
||||
}
|
||||
)
|
||||
return httpx.Response(200, json={"hits": {"hits": []}})
|
||||
|
||||
with patch("inse.integrations.elasticsearch.search", side_effect=fake_search):
|
||||
client = PatentSearchClient(api_key="test-key")
|
||||
await client.search_epo(client.epo_query_by_inventor("Weber", "Christian"))
|
||||
client = PatentSearchClient(
|
||||
api_key="test-key",
|
||||
transport=httpx.MockTransport(handler),
|
||||
)
|
||||
await client.search_epo(client.epo_query_by_inventor("Weber", "Christian"))
|
||||
|
||||
assert len(captured) == 1
|
||||
assert captured[0]["index"] == ESIndex.EPO_INDEX
|
||||
Reference in New Issue
Block a user