|
| 1 | +"""Unit tests for ndi.cloud.api.datasets — no network required.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +def _make_client(org_id: str = "org-123") -> MagicMock: |
| 11 | + """Return a mock CloudClient with a config carrying *org_id*.""" |
| 12 | + client = MagicMock() |
| 13 | + client.config.org_id = org_id |
| 14 | + client.config.api_url = "https://api.ndi-cloud.com/v1" |
| 15 | + client.get.return_value = { |
| 16 | + "datasets": [{"id": "ds-1", "name": "Test"}], |
| 17 | + "totalNumber": 1, |
| 18 | + } |
| 19 | + return client |
| 20 | + |
| 21 | + |
| 22 | +class TestListDatasets: |
| 23 | + """listDatasets should work with or without an explicit org_id.""" |
| 24 | + |
| 25 | + def test_without_org_id_uses_client_config(self): |
| 26 | + """Calling listDatasets() with no org_id should resolve it from client config.""" |
| 27 | + from ndi.cloud.api.datasets import listDatasets |
| 28 | + |
| 29 | + client = _make_client(org_id="org-abc") |
| 30 | + result = listDatasets(client=client) |
| 31 | + |
| 32 | + client.get.assert_called_once() |
| 33 | + call_kwargs = client.get.call_args |
| 34 | + assert call_kwargs.kwargs["organizationId"] == "org-abc" |
| 35 | + assert result["datasets"][0]["name"] == "Test" |
| 36 | + |
| 37 | + def test_with_explicit_org_id(self): |
| 38 | + """Passing org_id explicitly should use that value.""" |
| 39 | + from ndi.cloud.api.datasets import listDatasets |
| 40 | + |
| 41 | + client = _make_client(org_id="org-abc") |
| 42 | + listDatasets("org-explicit", client=client) |
| 43 | + |
| 44 | + call_kwargs = client.get.call_args |
| 45 | + assert call_kwargs.kwargs["organizationId"] == "org-explicit" |
| 46 | + |
| 47 | + def test_no_org_id_and_no_config_raises(self): |
| 48 | + """If org_id is omitted and client config has none, raise ValueError.""" |
| 49 | + from ndi.cloud.api.datasets import listDatasets |
| 50 | + |
| 51 | + client = _make_client(org_id="") |
| 52 | + with pytest.raises(ValueError, match="org_id is required"): |
| 53 | + listDatasets(client=client) |
| 54 | + |
| 55 | + |
| 56 | +class TestListAllDatasets: |
| 57 | + """listAllDatasets should work without an explicit org_id.""" |
| 58 | + |
| 59 | + def test_without_org_id(self): |
| 60 | + from ndi.cloud.api.datasets import listAllDatasets |
| 61 | + |
| 62 | + client = _make_client(org_id="org-abc") |
| 63 | + result = listAllDatasets(client=client) |
| 64 | + |
| 65 | + assert len(result.data) == 1 |
| 66 | + |
| 67 | + |
| 68 | +class TestCreateDataset: |
| 69 | + """createDataset should work without an explicit org_id.""" |
| 70 | + |
| 71 | + def test_without_org_id(self): |
| 72 | + from ndi.cloud.api.datasets import createDataset |
| 73 | + |
| 74 | + client = _make_client(org_id="org-abc") |
| 75 | + client.post.return_value = {"id": "ds-new", "name": "NewDS"} |
| 76 | + createDataset(name="NewDS", client=client) |
| 77 | + |
| 78 | + call_kwargs = client.post.call_args |
| 79 | + assert call_kwargs.kwargs["organizationId"] == "org-abc" |
| 80 | + |
| 81 | + def test_without_org_id_and_no_config_raises(self): |
| 82 | + from ndi.cloud.api.datasets import createDataset |
| 83 | + |
| 84 | + client = _make_client(org_id="") |
| 85 | + with pytest.raises(ValueError, match="org_id is required"): |
| 86 | + createDataset(name="NewDS", client=client) |
0 commit comments