test_search.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. """Integration tests for archivebox search command."""
  3. import os
  4. import subprocess
  5. import sqlite3
  6. import json
  7. import pytest
  8. from .fixtures import process, disable_extractors_dict
  9. def test_search_returns_snapshots(tmp_path, process, disable_extractors_dict):
  10. """Test that search returns snapshots."""
  11. os.chdir(tmp_path)
  12. # Add some snapshots
  13. subprocess.run(
  14. ['archivebox', 'add', '--index-only', 'https://example.com'],
  15. capture_output=True,
  16. env=disable_extractors_dict,
  17. )
  18. result = subprocess.run(
  19. ['archivebox', 'search'],
  20. capture_output=True,
  21. text=True,
  22. )
  23. # Should return some output (path or URL info)
  24. assert result.stdout.strip() != '' or result.returncode == 0
  25. def test_search_filter_by_substring(tmp_path, process, disable_extractors_dict):
  26. """Test that substring filter works."""
  27. os.chdir(tmp_path)
  28. subprocess.run(
  29. ['archivebox', 'add', '--index-only', 'https://example.com'],
  30. capture_output=True,
  31. env=disable_extractors_dict,
  32. )
  33. # Search with filter - may not find if URL isn't stored as expected
  34. result = subprocess.run(
  35. ['archivebox', 'search', '--filter-type=substring', 'example'],
  36. capture_output=True,
  37. text=True,
  38. )
  39. # Should run without error
  40. assert result.returncode == 0 or 'No Snapshots' in result.stderr
  41. def test_search_sort_option(tmp_path, process, disable_extractors_dict):
  42. """Test that --sort option works."""
  43. os.chdir(tmp_path)
  44. subprocess.run(
  45. ['archivebox', 'add', '--index-only', 'https://example.com'],
  46. capture_output=True,
  47. env=disable_extractors_dict,
  48. )
  49. result = subprocess.run(
  50. ['archivebox', 'search', '--sort=url'],
  51. capture_output=True,
  52. text=True,
  53. )
  54. # Should run without error
  55. assert result.returncode == 0
  56. def test_search_with_headers_requires_format(tmp_path, process):
  57. """Test that --with-headers requires --json, --html, or --csv."""
  58. os.chdir(tmp_path)
  59. result = subprocess.run(
  60. ['archivebox', 'search', '--with-headers'],
  61. capture_output=True,
  62. text=True,
  63. )
  64. # Should fail with error message
  65. assert result.returncode != 0
  66. assert 'requires' in result.stderr.lower() or 'json' in result.stderr.lower()
  67. def test_search_status_option(tmp_path, process, disable_extractors_dict):
  68. """Test that --status option filters by status."""
  69. os.chdir(tmp_path)
  70. subprocess.run(
  71. ['archivebox', 'add', '--index-only', 'https://example.com'],
  72. capture_output=True,
  73. env=disable_extractors_dict,
  74. )
  75. result = subprocess.run(
  76. ['archivebox', 'search', '--status=indexed'],
  77. capture_output=True,
  78. text=True,
  79. )
  80. # Should run without error
  81. assert result.returncode == 0
  82. def test_search_no_snapshots_message(tmp_path, process):
  83. """Test that searching empty archive shows appropriate output."""
  84. os.chdir(tmp_path)
  85. result = subprocess.run(
  86. ['archivebox', 'search'],
  87. capture_output=True,
  88. text=True,
  89. )
  90. # Should complete (empty results are OK)
  91. assert result.returncode == 0
  92. class TestSearchCLI:
  93. """Test the CLI interface for search command."""
  94. def test_cli_help(self, tmp_path, process):
  95. """Test that --help works for search command."""
  96. os.chdir(tmp_path)
  97. result = subprocess.run(
  98. ['archivebox', 'search', '--help'],
  99. capture_output=True,
  100. text=True,
  101. )
  102. assert result.returncode == 0
  103. assert '--filter-type' in result.stdout or '-f' in result.stdout
  104. assert '--status' in result.stdout
  105. assert '--sort' in result.stdout
  106. if __name__ == '__main__':
  107. pytest.main([__file__, '-v'])