Skip to content

Commit

Permalink
Testing we can use ~ as paper_directory (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbraza authored Oct 18, 2024
1 parent 1b8f6c8 commit ef0aec2
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import io
import os
import sys
from pathlib import Path

import pytest

from paperqa import Docs
from paperqa.settings import Settings
from paperqa.utils import pqa_directory

Expand All @@ -15,25 +15,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")


Expand All @@ -59,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"

0 comments on commit ef0aec2

Please sign in to comment.