Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for deleting dataset #51

Merged
merged 2 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions python_otbr_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ async def create_active_dataset(self, dataset: ActiveDataSet) -> None:
if response.status not in (HTTPStatus.CREATED, HTTPStatus.OK):
raise OTBRError(f"unexpected http status {response.status}")

async def delete_active_dataset(self) -> None:
"""Delete active operational dataset."""
response = await self._session.delete(
f"{self._url}/node/dataset/active",
timeout=aiohttp.ClientTimeout(total=self._timeout),
)

if response.status == HTTPStatus.CONFLICT:
raise ThreadNetworkActiveError
if response.status != HTTPStatus.OK:
raise OTBRError(f"unexpected http status {response.status}")

async def create_pending_dataset(self, dataset: PendingDataSet) -> None:
"""Create pending operational dataset.

Expand All @@ -148,6 +160,18 @@ async def create_pending_dataset(self, dataset: PendingDataSet) -> None:
if response.status not in (HTTPStatus.CREATED, HTTPStatus.OK):
raise OTBRError(f"unexpected http status {response.status}")

async def delete_pending_dataset(self) -> None:
"""Delete pending operational dataset."""
response = await self._session.delete(
f"{self._url}/node/dataset/pending",
timeout=aiohttp.ClientTimeout(total=self._timeout),
)

if response.status == HTTPStatus.CONFLICT:
raise ThreadNetworkActiveError
if response.status != HTTPStatus.OK:
raise OTBRError(f"unexpected http status {response.status}")

async def set_active_dataset_tlvs(self, dataset: bytes) -> None:
"""Set current active operational dataset.

Expand Down
72 changes: 72 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,19 @@ async def test_create_active_dataset(aioclient_mock: AiohttpClientMocker):
}


async def test_delete_active_dataset(aioclient_mock: AiohttpClientMocker):
"""Test delete_active_dataset."""
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())

aioclient_mock.delete(f"{BASE_URL}/node/dataset/active", status=HTTPStatus.OK)

await otbr.delete_active_dataset()
assert aioclient_mock.call_count == 1
assert aioclient_mock.mock_calls[-1][0] == "DELETE"
assert aioclient_mock.mock_calls[-1][1].path == "/node/dataset/active"
assert aioclient_mock.mock_calls[-1][2] is None


async def test_create_pending_dataset(aioclient_mock: AiohttpClientMocker):
"""Test create_pending_dataset."""
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())
Expand Down Expand Up @@ -231,6 +244,19 @@ async def test_create_pending_dataset(aioclient_mock: AiohttpClientMocker):
}


async def test_delete_pending_dataset(aioclient_mock: AiohttpClientMocker):
"""Test delete_pending_dataset."""
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())

aioclient_mock.delete(f"{BASE_URL}/node/dataset/pending", status=HTTPStatus.OK)

await otbr.delete_pending_dataset()
assert aioclient_mock.call_count == 1
assert aioclient_mock.mock_calls[-1][0] == "DELETE"
assert aioclient_mock.mock_calls[-1][1].path == "/node/dataset/pending"
assert aioclient_mock.mock_calls[-1][2] is None


async def test_set_channel(aioclient_mock: AiohttpClientMocker) -> None:
"""Test set_channel."""
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())
Expand Down Expand Up @@ -430,6 +456,26 @@ async def test_create_active_dataset_202(aioclient_mock: AiohttpClientMocker):
await otbr.create_active_dataset(python_otbr_api.ActiveDataSet())


async def test_delete_active_dataset_thread_active(aioclient_mock: AiohttpClientMocker):
"""Test delete_active_dataset with error."""
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())

aioclient_mock.delete(f"{BASE_URL}/node/dataset/active", status=HTTPStatus.CONFLICT)

with pytest.raises(python_otbr_api.ThreadNetworkActiveError):
await otbr.delete_active_dataset()


async def test_delete_active_dataset_202(aioclient_mock: AiohttpClientMocker):
"""Test delete_active_dataset with error."""
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())

aioclient_mock.delete(f"{BASE_URL}/node/dataset/active", status=HTTPStatus.ACCEPTED)

with pytest.raises(python_otbr_api.OTBRError):
await otbr.delete_active_dataset()


async def test_create_pending_dataset_thread_active(
aioclient_mock: AiohttpClientMocker,
):
Expand All @@ -452,6 +498,32 @@ async def test_create_pending_dataset_202(aioclient_mock: AiohttpClientMocker):
await otbr.create_pending_dataset(python_otbr_api.PendingDataSet())


async def test_delete_pending_dataset_thread_active(
aioclient_mock: AiohttpClientMocker,
):
"""Test delete_pending_dataset with error."""
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())

aioclient_mock.delete(
f"{BASE_URL}/node/dataset/pending", status=HTTPStatus.CONFLICT
)

with pytest.raises(python_otbr_api.ThreadNetworkActiveError):
await otbr.delete_pending_dataset()


async def test_delete_pending_dataset_202(aioclient_mock: AiohttpClientMocker):
"""Test delete_pending_dataset with error."""
otbr = python_otbr_api.OTBR(BASE_URL, aioclient_mock.create_session())

aioclient_mock.delete(
f"{BASE_URL}/node/dataset/pending", status=HTTPStatus.ACCEPTED
)

with pytest.raises(python_otbr_api.OTBRError):
await otbr.delete_pending_dataset()


async def test_set_active_dataset_tlvs_thread_active(
aioclient_mock: AiohttpClientMocker,
):
Expand Down