|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, mock_open, MagicMock |
| 3 | +import json |
| 4 | +import yaml |
| 5 | +from pathlib import Path |
| 6 | +import tempfile |
| 7 | +from pystac import Catalog |
| 8 | + |
| 9 | +from deep_code.tools.publish import Publisher |
| 10 | + |
| 11 | + |
| 12 | +class TestPublisher(unittest.TestCase): |
| 13 | + @patch("fsspec.open") |
| 14 | + @patch("deep_code.tools.publish.GitHubPublisher") |
| 15 | + def setUp(self, mock_github_publisher, mock_fsspec_open): |
| 16 | + # Mock GitHubPublisher to avoid reading .gitaccess |
| 17 | + self.mock_github_publisher_instance = MagicMock() |
| 18 | + mock_github_publisher.return_value = self.mock_github_publisher_instance |
| 19 | + |
| 20 | + # Mock dataset and workflow config files |
| 21 | + self.dataset_config = {"collection_id": "test-collection", "dataset_id": "test-dataset"} |
| 22 | + self.workflow_config = { |
| 23 | + "properties": {"title": "Test Workflow"}, |
| 24 | + "workflow_id": "test-workflow", |
| 25 | + } |
| 26 | + |
| 27 | + # Mock fsspec.open for config files |
| 28 | + self.mock_fsspec_open = mock_fsspec_open |
| 29 | + self.mock_fsspec_open.side_effect = [ |
| 30 | + mock_open(read_data=yaml.dump(self.dataset_config)).return_value, |
| 31 | + mock_open(read_data=yaml.dump(self.workflow_config)).return_value, |
| 32 | + ] |
| 33 | + |
| 34 | + # Initialize Publisher |
| 35 | + self.publisher = Publisher( |
| 36 | + dataset_config_path="test-dataset-config.yaml", |
| 37 | + workflow_config_path="test-workflow-config.yaml", |
| 38 | + ) |
| 39 | + |
| 40 | + def test_normalize_name(self): |
| 41 | + # Test normal input |
| 42 | + self.assertEqual(Publisher._normalize_name("Test Name"), "test-name") |
| 43 | + |
| 44 | + # Test input with multiple spaces |
| 45 | + self.assertEqual(Publisher._normalize_name("Test Name"), "test---name") |
| 46 | + |
| 47 | + # Test empty input |
| 48 | + self.assertIsNone(Publisher._normalize_name("")) |
| 49 | + |
| 50 | + # Test None input |
| 51 | + self.assertIsNone(Publisher._normalize_name(None)) |
| 52 | + |
| 53 | + def test_write_to_file(self): |
| 54 | + # Create a temporary file |
| 55 | + with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
| 56 | + file_path = temp_file.name |
| 57 | + |
| 58 | + # Test data |
| 59 | + data = {"key": "value"} |
| 60 | + |
| 61 | + # Call the method |
| 62 | + Publisher._write_to_file(file_path, data) |
| 63 | + |
| 64 | + # Read the file and verify its content |
| 65 | + with open(file_path, "r") as f: |
| 66 | + content = json.load(f) |
| 67 | + self.assertEqual(content, data) |
| 68 | + |
| 69 | + # Clean up |
| 70 | + Path(file_path).unlink() |
| 71 | + |
| 72 | + def test_update_base_catalog(self): |
| 73 | + # Create a mock Catalog |
| 74 | + catalog = Catalog(id="test-catalog", description="Test Catalog") |
| 75 | + |
| 76 | + # Mock file path and item ID |
| 77 | + catalog_path = "test-catalog.json" |
| 78 | + item_id = "test-item" |
| 79 | + self_href = "https://example.com/catalog.json" |
| 80 | + |
| 81 | + self.publisher.workflow_title = "Test Workflow" |
| 82 | + |
| 83 | + # Mock the Catalog.from_file method |
| 84 | + with patch("pystac.Catalog.from_file", return_value=catalog): |
| 85 | + updated_catalog = self.publisher._update_base_catalog(catalog_path, |
| 86 | + item_id, self_href) |
| 87 | + |
| 88 | + # Assertions |
| 89 | + self.assertEqual(updated_catalog.get_self_href(), self_href) |
| 90 | + self.assertIsInstance(updated_catalog, Catalog) |
| 91 | + |
| 92 | + def test_read_config_files(self): |
| 93 | + # Mock dataset and workflow config files |
| 94 | + dataset_config = {"collection_id": "test-collection", "dataset_id": "test-dataset"} |
| 95 | + workflow_config = { |
| 96 | + "properties": {"title": "Test Workflow"}, |
| 97 | + "workflow_id": "test-workflow", |
| 98 | + } |
| 99 | + |
| 100 | + # Mock fsspec.open for config files |
| 101 | + self.mock_fsspec_open.side_effect = [ |
| 102 | + mock_open(read_data=yaml.dump(dataset_config)).return_value, |
| 103 | + mock_open(read_data=yaml.dump(workflow_config)).return_value, |
| 104 | + ] |
| 105 | + |
| 106 | + # Assertions |
| 107 | + self.assertEqual(self.publisher.dataset_config, dataset_config) |
| 108 | + self.assertEqual(self.publisher.workflow_config, workflow_config) |
0 commit comments