| 1 | import json |
| 2 | import subprocess |
| 3 | import unittest |
| 4 | from unittest.mock import patch |
| 5 | |
| 6 | from lib import techmeme |
| 7 | |
| 8 | |
| 9 | class TechmemeResponseShapeTest(unittest.TestCase): |
| 10 | def run_cli(self, stdout): |
| 11 | result = subprocess.CompletedProcess(["techmeme"], 0, stdout, "") |
| 12 | with ( |
| 13 | patch.object(techmeme, "_is_available", return_value=True), |
| 14 | patch.object(techmeme.subproc, "run_with_timeout", return_value=result), |
| 15 | ): |
| 16 | return techmeme._run_cli(["techmeme"], timeout=1) |
| 17 | |
| 18 | def test_incompatible_json_is_a_provider_error(self): |
| 19 | for payload in ({"error": "temporary failure"}, {"results": None}, None): |
| 20 | with self.subTest(payload=payload): |
| 21 | result = self.run_cli(json.dumps(payload)) |
| 22 | self.assertEqual([], result["results"]) |
| 23 | self.assertIn("response shape", result["error"]) |
| 24 | |
| 25 | def test_true_empty_responses_are_accepted(self): |
| 26 | for stdout in ("[]", '{"results":[]}', 'No results for "query"', ""): |
| 27 | with self.subTest(stdout=stdout): |
| 28 | self.assertEqual({"results": []}, self.run_cli(stdout)) |
| 29 |