File size: 897 Bytes
2aa3e66 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | from ddgs_api import cli
def test_cli_main_emits_json(monkeypatch, capsys):
monkeypatch.setenv("DDGS_OUTPUT", "json")
def fake_search(query, **kwargs):
assert query == "openai"
assert kwargs["region"] == "us-en"
return [{"title": "Result", "href": "https://example.com", "markdown": None}]
monkeypatch.setattr("ddgs_api.cli.ddgs_search", fake_search)
exit_code = cli.main(["openai"])
captured = capsys.readouterr()
assert exit_code == 0
assert '"title": "Result"' in captured.out
def test_cli_main_returns_error_code_on_search_failure(monkeypatch, capsys):
def fake_search(query, **kwargs):
raise RuntimeError("boom")
monkeypatch.setattr("ddgs_api.cli.ddgs_search", fake_search)
exit_code = cli.main(["openai"])
captured = capsys.readouterr()
assert exit_code == 1
assert "Error: boom" in captured.err
|