Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing we can use ~ as paper_directory #610

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading