test_run_acquisition_cli.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from __future__ import annotations
  2. from uuid import uuid4
  3. import scripts.run_acquisition as cli
  4. from acquisition.runner import RunBatchResult
  5. class DummyTransaction:
  6. def __init__(self, conn):
  7. self.conn = conn
  8. def __enter__(self):
  9. return self.conn
  10. def __exit__(self, exc_type, exc, tb):
  11. return False
  12. def test_run_acquisition_cli_is_thin_delegate(monkeypatch, capsys):
  13. captured = {}
  14. batch_id = uuid4()
  15. settings = object()
  16. db_config = object()
  17. conn = object()
  18. monkeypatch.setattr(cli.Settings, "from_env", lambda env_file: settings)
  19. monkeypatch.setattr(cli.CreationDbConfig, "from_env", lambda env_file: db_config)
  20. monkeypatch.setattr(cli, "transaction", lambda config: DummyTransaction(conn))
  21. monkeypatch.setattr(cli, "PostgresAcquisitionRepository", lambda c: ("repo", c))
  22. def fake_run_batch(repo, **kwargs):
  23. captured["repo"] = repo
  24. captured.update(kwargs)
  25. return RunBatchResult(
  26. run_id=uuid4(),
  27. total_jobs=1,
  28. done=1,
  29. partial=0,
  30. failed=0,
  31. skipped=0,
  32. )
  33. monkeypatch.setattr(cli, "run_batch", fake_run_batch)
  34. rc = cli.main(
  35. [
  36. "--batch-id",
  37. str(batch_id),
  38. "--platform",
  39. "weixin",
  40. "--search-limit",
  41. "2",
  42. "--display-limit",
  43. "1",
  44. "--no-classify",
  45. "--run-key",
  46. "rk",
  47. "--env-file",
  48. ".env.test",
  49. ]
  50. )
  51. out = capsys.readouterr().out
  52. assert rc == 0
  53. assert '"done": 1' in out
  54. assert captured["repo"] == ("repo", conn)
  55. assert captured["batch_id"] == batch_id
  56. assert captured["settings"] is settings
  57. assert captured["platforms"] == ("weixin",)
  58. assert captured["search_limit"] == 2
  59. assert captured["display_limit"] == 1
  60. assert captured["classify"] is False
  61. assert captured["run_key"] == "rk"