|
| 1 | +# |
| 2 | +# MobilityData 2025 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | +import sys |
| 17 | +import unittest |
| 18 | +from unittest.mock import MagicMock, patch |
| 19 | + |
| 20 | + |
| 21 | +class TestCreateWebRevalidationTask(unittest.TestCase): |
| 22 | + def setUp(self): |
| 23 | + # google-cloud-tasks is not installed in the test environment. |
| 24 | + # Provide a MagicMock so `from google.cloud import tasks_v2` succeeds |
| 25 | + # for tests that proceed past the early-return guards. |
| 26 | + self._mock_tasks_v2 = MagicMock() |
| 27 | + self._sys_modules_patcher = patch.dict(sys.modules, {"google.cloud.tasks_v2": self._mock_tasks_v2}) |
| 28 | + self._sys_modules_patcher.start() |
| 29 | + |
| 30 | + def tearDown(self): |
| 31 | + self._sys_modules_patcher.stop() |
| 32 | + |
| 33 | + def test_empty_feed_ids(self): |
| 34 | + """Should return early without creating any tasks.""" |
| 35 | + from shared.common.gcp_utils import create_web_revalidation_task |
| 36 | + |
| 37 | + # Should not raise |
| 38 | + create_web_revalidation_task([]) |
| 39 | + |
| 40 | + @patch.dict( |
| 41 | + "os.environ", |
| 42 | + { |
| 43 | + "PROJECT_ID": "test-project", |
| 44 | + "WEB_REVALIDATION_QUEUE": "", |
| 45 | + "GCP_REGION": "us-central1", |
| 46 | + "ENVIRONMENT": "dev", |
| 47 | + }, |
| 48 | + ) |
| 49 | + def test_missing_queue_env_var(self): |
| 50 | + """Should log a warning and return without creating tasks.""" |
| 51 | + from shared.common.gcp_utils import create_web_revalidation_task |
| 52 | + |
| 53 | + # Should not raise |
| 54 | + create_web_revalidation_task(["mdb-123"]) |
| 55 | + |
| 56 | + @patch("shared.common.gcp_utils.create_http_task_with_name") |
| 57 | + @patch.dict( |
| 58 | + "os.environ", |
| 59 | + { |
| 60 | + "PROJECT_ID": "test-project", |
| 61 | + "WEB_REVALIDATION_QUEUE": "web-revalidation-queue", |
| 62 | + "GCP_REGION": "us-central1", |
| 63 | + "ENVIRONMENT": "dev", |
| 64 | + "SERVICE_ACCOUNT_EMAIL": "test@test.iam.gserviceaccount.com", |
| 65 | + }, |
| 66 | + ) |
| 67 | + def test_creates_tasks_for_each_feed(self, mock_create_task): |
| 68 | + """Should create one Cloud Task per feed stable ID.""" |
| 69 | + from shared.common.gcp_utils import create_web_revalidation_task |
| 70 | + |
| 71 | + create_web_revalidation_task(["mdb-100", "mdb-200"]) |
| 72 | + |
| 73 | + self.assertEqual(mock_create_task.call_count, 2) |
| 74 | + |
| 75 | + # Verify the task bodies contain the correct feed IDs |
| 76 | + first_call_body = mock_create_task.call_args_list[0] |
| 77 | + second_call_body = mock_create_task.call_args_list[1] |
| 78 | + |
| 79 | + self.assertIn(b"mdb-100", first_call_body.kwargs.get("body", b"")) |
| 80 | + self.assertIn(b"mdb-200", second_call_body.kwargs.get("body", b"")) |
| 81 | + |
| 82 | + @patch("shared.common.gcp_utils.create_http_task_with_name") |
| 83 | + @patch.dict( |
| 84 | + "os.environ", |
| 85 | + { |
| 86 | + "PROJECT_ID": "test-project", |
| 87 | + "WEB_REVALIDATION_QUEUE": "web-revalidation-queue", |
| 88 | + "GCP_REGION": "us-central1", |
| 89 | + "ENVIRONMENT": "dev", |
| 90 | + "SERVICE_ACCOUNT_EMAIL": "test@test.iam.gserviceaccount.com", |
| 91 | + }, |
| 92 | + ) |
| 93 | + def test_dedup_task_name_contains_feed_id(self, mock_create_task): |
| 94 | + """Task name should include the feed stable ID for deduplication.""" |
| 95 | + from shared.common.gcp_utils import create_web_revalidation_task |
| 96 | + |
| 97 | + create_web_revalidation_task(["mdb-42"]) |
| 98 | + |
| 99 | + self.assertEqual(mock_create_task.call_count, 1) |
| 100 | + task_name = mock_create_task.call_args.kwargs.get("task_name", "") |
| 101 | + self.assertTrue(task_name.startswith("revalidate-mdb-42-")) |
| 102 | + |
| 103 | + @patch("shared.common.gcp_utils.create_http_task_with_name") |
| 104 | + @patch.dict( |
| 105 | + "os.environ", |
| 106 | + { |
| 107 | + "PROJECT_ID": "test-project", |
| 108 | + "WEB_REVALIDATION_QUEUE": "web-revalidation-queue", |
| 109 | + "GCP_REGION": "us-central1", |
| 110 | + "ENVIRONMENT": "dev", |
| 111 | + "SERVICE_ACCOUNT_EMAIL": "test@test.iam.gserviceaccount.com", |
| 112 | + }, |
| 113 | + ) |
| 114 | + def test_already_exists_is_handled_gracefully(self, mock_create_task): |
| 115 | + """ALREADY_EXISTS errors should be caught and logged, not raised.""" |
| 116 | + mock_create_task.side_effect = Exception("409 ALREADY_EXISTS: task already exists") |
| 117 | + from shared.common.gcp_utils import create_web_revalidation_task |
| 118 | + |
| 119 | + # Should not raise |
| 120 | + create_web_revalidation_task(["mdb-123"]) |
| 121 | + |
| 122 | + @patch("shared.common.gcp_utils.create_http_task_with_name") |
| 123 | + @patch.dict( |
| 124 | + "os.environ", |
| 125 | + { |
| 126 | + "PROJECT_ID": "test-project", |
| 127 | + "WEB_REVALIDATION_QUEUE": "web-revalidation-queue", |
| 128 | + "GCP_REGION": "us-central1", |
| 129 | + "ENVIRONMENT": "dev", |
| 130 | + "SERVICE_ACCOUNT_EMAIL": "test@test.iam.gserviceaccount.com", |
| 131 | + }, |
| 132 | + ) |
| 133 | + def test_targets_tasks_executor_url(self, mock_create_task): |
| 134 | + """Tasks should target the tasks_executor Cloud Function URL.""" |
| 135 | + from shared.common.gcp_utils import create_web_revalidation_task |
| 136 | + |
| 137 | + create_web_revalidation_task(["mdb-1"]) |
| 138 | + |
| 139 | + url = mock_create_task.call_args.kwargs.get("url", "") |
| 140 | + self.assertIn("tasks_executor-dev", url) |
| 141 | + self.assertIn("us-central1", url) |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + unittest.main() |
0 commit comments