From b745d6a8ff324b5140678733f4ad98d239d19e2b Mon Sep 17 00:00:00 2001 From: James Braza Date: Fri, 18 Oct 2024 12:28:05 -0700 Subject: [PATCH 1/2] Upgraded test_can_modify_settings to include paper_directory, properly use capsys, and check the config isn't present --- tests/test_cli.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index b84b29cd..e856a2cc 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,4 +1,3 @@ -import io import os import sys from pathlib import Path @@ -15,25 +14,37 @@ pytest.skip("agents module is not installed", allow_module_level=True) -def test_can_modify_settings() -> None: +def test_can_modify_settings(capsys, stub_data_dir: Path) -> None: + rel_path_home_to_stub_data = Path("~") / stub_data_dir.relative_to(Path.home()) + + # This test depends on the unit_test config not previously existing + with pytest.raises(FileNotFoundError, match="unit_test"): + Settings.from_name("unit_test") + old_argv = sys.argv - old_stdout = sys.stdout - captured_output = io.StringIO() try: - sys.argv = "paperqa -s debug --llm=my-model-foo save unit_test".split() + sys.argv = ( + "paperqa -s debug --llm=my-model-foo" + f" --agent.index.paper_directory={rel_path_home_to_stub_data!s} save" + " unit_test" + ).split() main() - sys.stdout = captured_output - assert Settings.from_name("unit_test").llm == "my-model-foo" + captured = capsys.readouterr() + assert not captured.err + assert "Settings saved" in captured.out + settings = Settings.from_name("unit_test") + assert settings.llm == "my-model-foo" + assert settings.agent.index.paper_directory == str(rel_path_home_to_stub_data) sys.argv = "paperqa -s unit_test view".split() main() - output = captured_output.getvalue().strip() - assert "my-model-foo" in output + captured = capsys.readouterr() + assert not captured.err + assert "my-model-foo" in captured.out finally: sys.argv = old_argv - sys.stdout = old_stdout os.unlink(pqa_directory("settings") / "unit_test.json") From ce16a8735662cb8b2647787534f3adc7b5a0f94e Mon Sep 17 00:00:00 2001 From: James Braza Date: Fri, 18 Oct 2024 12:32:29 -0700 Subject: [PATCH 2/2] Tested we can use ~ with CLI --- tests/test_cli.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index e856a2cc..f6f4f5b1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,6 +4,7 @@ import pytest +from paperqa import Docs from paperqa.settings import Settings from paperqa.utils import pqa_directory @@ -70,9 +71,14 @@ def test_cli_ask(agent_index_dir: Path, stub_data_dir: Path) -> None: def test_cli_can_build_and_search_index( agent_index_dir: Path, stub_data_dir: Path ) -> None: + rel_path_home_to_stub_data = Path("~") / stub_data_dir.relative_to(Path.home()) settings = Settings.from_name("debug") + settings.agent.index.paper_directory = rel_path_home_to_stub_data settings.agent.index.index_directory = agent_index_dir index_name = "test" build_index(index_name, stub_data_dir, settings) - search_result = search_query("XAI", index_name, settings) - assert search_result + result = search_query("XAI", index_name, settings) + assert len(result) == 1 + assert isinstance(result[0][0], Docs) + assert result[0][0].docnames == {"Wellawatte"} + assert result[0][1] == "paper.pdf"