| 1 | """Tests for pinterest.py — ScrapeCreators Pinterest search module.""" |
| 2 | |
| 3 | import unittest |
| 4 | from unittest.mock import patch |
| 5 | |
| 6 | from lib import pinterest |
| 7 | |
| 8 | |
| 9 | class TestSearchPinterestRequest(unittest.TestCase): |
| 10 | """Pin the ScrapeCreators request contract. |
| 11 | |
| 12 | Regression guard: the SC Pinterest endpoint requires a `query` param. |
| 13 | A prior version sent `keyword`, which the API rejects with |
| 14 | 400 bad_request ("You must provide a 'query' param"), making every |
| 15 | Pinterest search silently return zero pins. |
| 16 | """ |
| 17 | |
| 18 | def test_uses_query_param_not_keyword(self): |
| 19 | from lib import http as http_module |
| 20 | with patch.object(http_module, "get") as mock_http_get: |
| 21 | mock_http_get.return_value = {"pins": []} |
| 22 | pinterest.search_pinterest( |
| 23 | "robot vacuum", "2026-05-01", "2026-06-01", |
| 24 | depth="default", token="fake-token", |
| 25 | ) |
| 26 | self.assertEqual(mock_http_get.call_count, 1) |
| 27 | params = mock_http_get.call_args.kwargs["params"] |
| 28 | # The fix this test guards is the param *name* — SC requires `query`, |
| 29 | # not `keyword`. Assert the contract only; the exact value is derived |
| 30 | # from _extract_core_subject() and is that function's concern, not ours. |
| 31 | self.assertIn("query", params) |
| 32 | self.assertNotIn("keyword", params) |
| 33 | self.assertTrue(params["query"]) |
| 34 | |
| 35 | def test_no_token_skips_http_call(self): |
| 36 | from lib import http as http_module |
| 37 | with patch.object(http_module, "get") as mock_http_get: |
| 38 | result = pinterest.search_pinterest( |
| 39 | "robot vacuum", "2026-05-01", "2026-06-01", token=None, |
| 40 | ) |
| 41 | mock_http_get.assert_not_called() |
| 42 | self.assertEqual(result["items"], []) |
| 43 | self.assertIn("error", result) |
| 44 | |
| 45 | def test_parses_pins_response_shape(self): |
| 46 | from lib import http as http_module |
| 47 | payload = { |
| 48 | "pins": [ |
| 49 | { |
| 50 | "id": "123", |
| 51 | "description": "Robot vacuum that handles pet hair", |
| 52 | "link": "https://example.com/p/123", |
| 53 | "save_count": 42, |
| 54 | "pinner": {"username": "petowner"}, |
| 55 | }, |
| 56 | ], |
| 57 | } |
| 58 | with patch.object(http_module, "get") as mock_http_get: |
| 59 | mock_http_get.return_value = payload |
| 60 | result = pinterest.search_pinterest( |
| 61 | "robot vacuum pet hair", "2026-05-01", "2026-06-01", |
| 62 | depth="default", token="fake-token", |
| 63 | ) |
| 64 | self.assertEqual(len(result["items"]), 1) |
| 65 | item = result["items"][0] |
| 66 | self.assertEqual(item["pin_id"], "123") |
| 67 | self.assertEqual(item["engagement"]["saves"], 42) |
| 68 | self.assertEqual(item["url"], "https://example.com/p/123") |
| 69 | |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | unittest.main() |
| 73 |