|
1 | 1 | import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
2 | 3 | from datetime import datetime |
3 | 4 |
|
4 | | -from tasks.missing_bounding_boxes.rebuild_missing_bounding_boxes import get_parameters |
| 5 | +from tasks.missing_bounding_boxes.rebuild_missing_bounding_boxes import ( |
| 6 | + get_parameters, |
| 7 | + rebuild_missing_bounding_boxes, |
| 8 | +) |
5 | 9 |
|
6 | 10 |
|
7 | 11 | class TestTasksExecutor(unittest.TestCase): |
8 | 12 | def test_get_parameters(self): |
9 | | - """ |
10 | | - Test the get_parameters function to ensure it correctly extracts parameters from the payload. |
11 | | - """ |
12 | | - payload = { |
13 | | - "dry_run": True, |
14 | | - } |
15 | | - |
| 13 | + payload = {"dry_run": True} |
16 | 14 | dry_run, after_date = get_parameters(payload) |
17 | 15 | self.assertTrue(dry_run) |
18 | 16 | self.assertIsNone(after_date) |
19 | 17 |
|
20 | 18 | def test_get_parameters_with_valid_after_date(self): |
21 | | - """ |
22 | | - Test get_parameters returns a valid ISO date string for after_date. |
23 | | - """ |
24 | | - payload = { |
25 | | - "dry_run": False, |
26 | | - "after_date": "2024-06-01", |
27 | | - } |
28 | | - |
| 19 | + payload = {"dry_run": False, "after_date": "2024-06-01"} |
29 | 20 | dry_run, after_date = get_parameters(payload) |
30 | 21 | self.assertFalse(dry_run) |
31 | | - # Check that after_date is a valid ISO date string |
| 22 | + self.assertEqual(after_date, "2024-06-01") |
| 23 | + # Check ISO format |
32 | 24 | try: |
33 | 25 | datetime.fromisoformat(after_date) |
34 | 26 | except ValueError: |
35 | 27 | self.fail(f"after_date '{after_date}' is not a valid ISO date string") |
| 28 | + |
| 29 | + def test_get_parameters_with_string_bool(self): |
| 30 | + payload = {"dry_run": "false", "after_date": None} |
| 31 | + dry_run, after_date = get_parameters(payload) |
| 32 | + self.assertFalse(dry_run) |
| 33 | + self.assertIsNone(after_date) |
| 34 | + |
| 35 | + def test_get_parameters_missing_keys(self): |
| 36 | + payload = {} |
| 37 | + dry_run, after_date = get_parameters(payload) |
| 38 | + self.assertTrue(dry_run) |
| 39 | + self.assertIsNone(after_date) |
| 40 | + |
| 41 | + @patch( |
| 42 | + "tasks.missing_bounding_boxes.rebuild_missing_bounding_boxes.get_feeds_with_missing_bounding_boxes_query" |
| 43 | + ) |
| 44 | + def test_rebuild_missing_bounding_boxes_dry_run(self, mock_query): |
| 45 | + # Mock the query and its .all() method |
| 46 | + mock_query.return_value.filter.return_value = mock_query.return_value |
| 47 | + mock_query.return_value.all.return_value = [ |
| 48 | + ("feed1", "dataset1"), |
| 49 | + ("feed2", "dataset2"), |
| 50 | + ] |
| 51 | + result = rebuild_missing_bounding_boxes( |
| 52 | + dry_run=True, after_date=None, db_session=MagicMock() |
| 53 | + ) |
| 54 | + self.assertIn("Dry run", result["message"]) |
| 55 | + self.assertEqual(result["total_processed"], 2) |
| 56 | + |
| 57 | + @patch( |
| 58 | + "tasks.missing_bounding_boxes.rebuild_missing_bounding_boxes.publish_messages" |
| 59 | + ) |
| 60 | + @patch( |
| 61 | + "tasks.missing_bounding_boxes.rebuild_missing_bounding_boxes.get_feeds_with_missing_bounding_boxes_query" |
| 62 | + ) |
| 63 | + def test_rebuild_missing_bounding_boxes_publish(self, mock_query, mock_publish): |
| 64 | + mock_query.return_value.filter.return_value = mock_query.return_value |
| 65 | + mock_query.return_value.all.return_value = [("feed1", "dataset1")] |
| 66 | + mock_publish.return_value = None |
| 67 | + result = rebuild_missing_bounding_boxes( |
| 68 | + dry_run=False, after_date=None, db_session=MagicMock() |
| 69 | + ) |
| 70 | + self.assertIn("Successfully published", result["message"]) |
| 71 | + self.assertEqual(result["total_processed"], 1) |
| 72 | + |
| 73 | + @patch( |
| 74 | + "tasks.missing_bounding_boxes.rebuild_missing_bounding_boxes.get_feeds_with_missing_bounding_boxes_query" |
| 75 | + ) |
| 76 | + def test_rebuild_missing_bounding_boxes_invalid_after_date(self, mock_query): |
| 77 | + mock_query.return_value.filter.return_value = mock_query.return_value |
| 78 | + mock_query.return_value.all.return_value = [] |
| 79 | + # Should log a warning and not raise |
| 80 | + result = rebuild_missing_bounding_boxes( |
| 81 | + dry_run=True, after_date="not-a-date", db_session=MagicMock() |
| 82 | + ) |
| 83 | + self.assertIn("Dry run", result["message"]) |
| 84 | + self.assertEqual(result["total_processed"], 0) |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + unittest.main() |
0 commit comments