| 1 | """Run saved watchlist queries from current and legacy entries.""" |
| 2 | |
| 3 | from subprocess import CompletedProcess |
| 4 | from unittest.mock import patch |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | import store |
| 9 | import watchlist |
| 10 | |
| 11 | |
| 12 | @pytest.mark.parametrize("saved", ['"mistaken string"', '[0]', '["a\\u0000b"]']) |
| 13 | def test_invalid_saved_queries_fail_without_starting_research(tmp_path, monkeypatch, saved): |
| 14 | with store.scoped_db(tmp_path / "research.db"): |
| 15 | topic = store.add_topic("Watched topic") |
| 16 | topic["search_queries"] = saved |
| 17 | calls = [] |
| 18 | |
| 19 | def subprocess_run(*args, **kwargs): |
| 20 | calls.append(args) |
| 21 | return watchlist.subprocess.CompletedProcess([], 1, "", "offline") |
| 22 | |
| 23 | monkeypatch.setattr(watchlist.subprocess, "run", subprocess_run) |
| 24 | result = watchlist._run_topic(topic) |
| 25 | |
| 26 | assert result["status"] == "failed" |
| 27 | assert not calls |
| 28 | assert "saved search queries" in result["error"].lower() |
| 29 | assert store.list_topics()[0]["last_status"] == "failed" |
| 30 | |
| 31 | |
| 32 | @pytest.mark.parametrize(("queries", "expected"), [ |
| 33 | ("SQLite,", "SQLite"), |
| 34 | (", SQLite ,Postgres", "SQLite"), |
| 35 | (" , ", "Watched topic"), |
| 36 | ]) |
| 37 | def test_legacy_comma_separated_queries_run_first_nonblank_term(tmp_path, queries, expected): |
| 38 | with store.scoped_db(tmp_path / "research.db"): |
| 39 | store.add_topic("Watched topic", search_queries=queries.split(",")) |
| 40 | topic = store.get_topic("Watched topic") |
| 41 | with patch("watchlist.subprocess.run", return_value=CompletedProcess([], 1, "", "offline")) as run: |
| 42 | watchlist._run_topic(topic) |
| 43 | |
| 44 | run.assert_called_once() |
| 45 | assert run.call_args.args[0][-2:] == ["--", expected] |
| 46 |