Skip to content

Commit

Permalink
Mock DDG in unit tests (#1076)
Browse files Browse the repository at this point in the history
  • Loading branch information
vachillo authored Aug 19, 2024
1 parent 3aaeb6e commit 717753c
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ def driver(self, mocker):
{"title": "foo", "href": "bar", "body": "baz"},
{"title": "foo2", "href": "bar2", "body": "baz2"},
]

mocker.patch("duckduckgo_search.DDGS.text", return_value=mock_response)

mock_ddg = mocker.Mock(
text=lambda *args, **kwargs: mock_response,
)
mocker.patch("duckduckgo_search.DDGS", return_value=mock_ddg)
return DuckDuckGoWebSearchDriver()

@pytest.fixture()
def driver_with_error(self, mocker):
mocker.patch("duckduckgo_search.DDGS.text", side_effect=Exception("test_error"))
def error(*args, **kwargs):
raise Exception("test_error")

mock_ddg = mocker.Mock(
text=error,
)
mocker.patch("duckduckgo_search.DDGS", return_value=mock_ddg)

return DuckDuckGoWebSearchDriver()

Expand Down

0 comments on commit 717753c

Please sign in to comment.