Skip to content

Commit 01105a3

Browse files
Port ndi.cloud.sync.downloadNew and sync infrastructure
1 parent 9b191f7 commit 01105a3

3 files changed

Lines changed: 44 additions & 19 deletions

File tree

src/ndi/cloud/sync/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .download_new import download_new
1+
from .download_new_impl import download_new
22
from .sync_options import SyncOptions
33
from .enum.sync_mode import SyncMode

tests/test_cloud_sync_download_new.py

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,49 @@
22
from unittest.mock import MagicMock, patch
33
import sys
44

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.
109

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
1315

1416
class TestCloudSyncDownloadNew(unittest.TestCase):
1517

1618
def setUp(self):
1719
self.mock_dataset = MagicMock()
1820
self.mock_dataset.path = '/tmp/fake_dataset'
1921

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')
2643
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+
2748
# Setup mocks
2849
mock_get_cloud_id.return_value = ('cloud-id-123', [])
2950
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
5172

5273
mock_update_index.assert_called_once()
5374

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')
5980
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+
6085
mock_get_cloud_id.return_value = ('cloud-id-123', [])
6186
mock_read_index.return_value = {'remoteDocumentIdsLastSync': ['doc1']}
6287
mock_list_remote.return_value = {'ndiId': ['doc1'], 'apiId': ['api1']}

0 commit comments

Comments
 (0)