|
2 | 2 | from unittest.mock import MagicMock, patch |
3 | 3 | import sys |
4 | 4 |
|
5 | | -# Mock 'did' and 'did.query' before importing ndi |
6 | | -mock_did = MagicMock() |
7 | | -sys.modules['did'] = mock_did |
8 | | -sys.modules['did.query'] = mock_did |
9 | | -sys.modules['did.document'] = mock_did |
| 5 | +# We need to ensure dependencies are handled. |
| 6 | +# If 'did' is not installed, we can mock it safely using patch.dict |
| 7 | +# But 'download_new_impl' imports it at module level (via get_cloud_dataset_id... -> query -> did.query) |
| 8 | +# So we need to mock it *before* importing download_new_impl if it's missing. |
10 | 9 |
|
11 | | -from ndi.cloud.sync.download_new import download_new |
12 | | -from ndi.cloud.sync.sync_options import SyncOptions |
| 10 | +needs_did_mock = False |
| 11 | +try: |
| 12 | + import did |
| 13 | +except ImportError: |
| 14 | + needs_did_mock = True |
13 | 15 |
|
14 | 16 | class TestCloudSyncDownloadNew(unittest.TestCase): |
15 | 17 |
|
16 | 18 | def setUp(self): |
17 | 19 | self.mock_dataset = MagicMock() |
18 | 20 | self.mock_dataset.path = '/tmp/fake_dataset' |
19 | 21 |
|
20 | | - @patch('ndi.cloud.sync.download_new.get_cloud_dataset_id_for_local_dataset') |
21 | | - @patch('ndi.cloud.sync.download_new.read_sync_index') |
22 | | - @patch('ndi.cloud.sync.download_new.list_remote_document_ids') |
23 | | - @patch('ndi.cloud.sync.download_new.download_ndi_documents') |
24 | | - @patch('ndi.cloud.sync.download_new.list_local_documents') |
25 | | - @patch('ndi.cloud.sync.download_new.update_sync_index') |
| 22 | + if needs_did_mock: |
| 23 | + self.did_patcher = patch.dict('sys.modules', { |
| 24 | + 'did': MagicMock(), |
| 25 | + 'did.query': MagicMock(), |
| 26 | + 'did.document': MagicMock() |
| 27 | + }) |
| 28 | + self.did_patcher.start() |
| 29 | + |
| 30 | + # Re-import to ensure mocks are used if patched |
| 31 | + # Or import inside test methods |
| 32 | + |
| 33 | + def tearDown(self): |
| 34 | + if needs_did_mock: |
| 35 | + self.did_patcher.stop() |
| 36 | + |
| 37 | + @patch('ndi.cloud.sync.download_new_impl.get_cloud_dataset_id_for_local_dataset') |
| 38 | + @patch('ndi.cloud.sync.download_new_impl.read_sync_index') |
| 39 | + @patch('ndi.cloud.sync.download_new_impl.list_remote_document_ids') |
| 40 | + @patch('ndi.cloud.sync.download_new_impl.download_ndi_documents') |
| 41 | + @patch('ndi.cloud.sync.download_new_impl.list_local_documents') |
| 42 | + @patch('ndi.cloud.sync.download_new_impl.update_sync_index') |
26 | 43 | def test_download_new_success(self, mock_update_index, mock_list_local, mock_download_docs, mock_list_remote, mock_read_index, mock_get_cloud_id): |
| 44 | + # Delayed import to allow setUp to patch sys.modules if needed |
| 45 | + from ndi.cloud.sync.download_new_impl import download_new |
| 46 | + from ndi.cloud.sync.sync_options import SyncOptions |
| 47 | + |
27 | 48 | # Setup mocks |
28 | 49 | mock_get_cloud_id.return_value = ('cloud-id-123', []) |
29 | 50 | mock_read_index.return_value = {'remoteDocumentIdsLastSync': ['doc1']} |
@@ -51,12 +72,16 @@ def test_download_new_success(self, mock_update_index, mock_list_local, mock_dow |
51 | 72 |
|
52 | 73 | mock_update_index.assert_called_once() |
53 | 74 |
|
54 | | - @patch('ndi.cloud.sync.download_new.get_cloud_dataset_id_for_local_dataset') |
55 | | - @patch('ndi.cloud.sync.download_new.read_sync_index') |
56 | | - @patch('ndi.cloud.sync.download_new.list_remote_document_ids') |
57 | | - @patch('ndi.cloud.sync.download_new.download_ndi_documents') |
58 | | - @patch('ndi.cloud.sync.download_new.update_sync_index') |
| 75 | + @patch('ndi.cloud.sync.download_new_impl.get_cloud_dataset_id_for_local_dataset') |
| 76 | + @patch('ndi.cloud.sync.download_new_impl.read_sync_index') |
| 77 | + @patch('ndi.cloud.sync.download_new_impl.list_remote_document_ids') |
| 78 | + @patch('ndi.cloud.sync.download_new_impl.download_ndi_documents') |
| 79 | + @patch('ndi.cloud.sync.download_new_impl.update_sync_index') |
59 | 80 | def test_download_new_no_changes(self, mock_update_index, mock_download_docs, mock_list_remote, mock_read_index, mock_get_cloud_id): |
| 81 | + # Delayed import |
| 82 | + from ndi.cloud.sync.download_new_impl import download_new |
| 83 | + from ndi.cloud.sync.sync_options import SyncOptions |
| 84 | + |
60 | 85 | mock_get_cloud_id.return_value = ('cloud-id-123', []) |
61 | 86 | mock_read_index.return_value = {'remoteDocumentIdsLastSync': ['doc1']} |
62 | 87 | mock_list_remote.return_value = {'ndiId': ['doc1'], 'apiId': ['api1']} |
|
0 commit comments