test_cli_search.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python3
  2. """
  3. Tests for archivebox search command.
  4. Verify search queries snapshots from DB.
  5. """
  6. import os
  7. import subprocess
  8. import sqlite3
  9. from .fixtures import *
  10. def test_search_finds_snapshots(tmp_path, process, disable_extractors_dict):
  11. """Test that search command finds matching snapshots."""
  12. os.chdir(tmp_path)
  13. # Add snapshots
  14. subprocess.run(
  15. ['archivebox', 'add', '--index-only', '--depth=0', 'https://example.com'],
  16. capture_output=True,
  17. env=disable_extractors_dict,
  18. )
  19. # Search for it
  20. result = subprocess.run(
  21. ['archivebox', 'search', 'example'],
  22. capture_output=True,
  23. text=True,
  24. timeout=30,
  25. )
  26. assert result.returncode == 0
  27. assert 'example' in result.stdout
  28. def test_search_returns_no_results_for_missing_term(tmp_path, process, disable_extractors_dict):
  29. """Test search returns empty for non-existent term."""
  30. os.chdir(tmp_path)
  31. subprocess.run(
  32. ['archivebox', 'add', '--index-only', '--depth=0', 'https://example.com'],
  33. capture_output=True,
  34. env=disable_extractors_dict,
  35. )
  36. result = subprocess.run(
  37. ['archivebox', 'search', 'nonexistentterm12345'],
  38. capture_output=True,
  39. text=True,
  40. timeout=30,
  41. )
  42. # Should complete with no results
  43. assert result.returncode in [0, 1]
  44. def test_search_on_empty_archive(tmp_path, process):
  45. """Test search works on empty archive."""
  46. os.chdir(tmp_path)
  47. result = subprocess.run(
  48. ['archivebox', 'search', 'anything'],
  49. capture_output=True,
  50. text=True,
  51. timeout=30,
  52. )
  53. # Should complete without error
  54. assert result.returncode in [0, 1]