| 1 | """Tests for the Update feature.""" |
| 2 | |
| 3 | import pytest |
| 4 | from unittest.mock import AsyncMock, MagicMock |
| 5 | |
| 6 | from blebox_uniapi.update import Update |
| 7 | from blebox_uniapi import error |
| 8 | |
| 9 | |
| 10 | @pytest.fixture |
| 11 | def mock_product(): |
| 12 | product = MagicMock() |
| 13 | product.firmware_version = "0.176" |
| 14 | product.available_firmware_version = None |
| 15 | return product |
| 16 | |
| 17 | |
| 18 | @pytest.fixture |
| 19 | def update(mock_product): |
| 20 | return Update(mock_product, "update", {}) |
| 21 | |
| 22 | |
| 23 | class TestInstalledVersion: |
| 24 | def test_returns_firmware_version(self, update, mock_product): |
| 25 | assert update.installed_version == "0.176" |
| 26 | |
| 27 | def test_returns_none_when_not_set(self, update, mock_product): |
| 28 | mock_product.firmware_version = None |
| 29 | assert update.installed_version is None |
| 30 | |
| 31 | |
| 32 | class TestLatestVersion: |
| 33 | def test_returns_none_when_not_set(self, update, mock_product): |
| 34 | assert update.latest_version is None |
| 35 | |
| 36 | def test_returns_available_firmware_version(self, update, mock_product): |
| 37 | mock_product.available_firmware_version = "1.2.3" |
| 38 | assert update.latest_version == "1.2.3" |
| 39 | |
| 40 | |
| 41 | class TestAsyncUpdate: |
| 42 | async def test_calls_ota_check_on_product(self, update, mock_product): |
| 43 | mock_product.async_ota_check = AsyncMock() |
| 44 | await update.async_update() |
| 45 | mock_product.async_ota_check.assert_called_once() |
| 46 | |
| 47 | async def test_propagates_connection_error_from_ota_check( |
| 48 | self, update, mock_product |
| 49 | ): |
| 50 | mock_product.async_ota_check = AsyncMock( |
| 51 | side_effect=error.ConnectionError("connection refused") |
| 52 | ) |
| 53 | with pytest.raises(error.ConnectionError): |
| 54 | await update.async_update() |
| 55 | |
| 56 | |
| 57 | class TestAsyncInstall: |
| 58 | async def test_calls_ota_update_on_product(self, update, mock_product): |
| 59 | mock_product.async_ota_update = AsyncMock() |
| 60 | await update.async_install() |
| 61 | mock_product.async_ota_update.assert_called_once() |
| 62 | |
| 63 | async def test_propagates_connection_error_from_ota_update( |
| 64 | self, update, mock_product |
| 65 | ): |
| 66 | mock_product.async_ota_update = AsyncMock( |
| 67 | side_effect=error.ConnectionError("connection refused") |
| 68 | ) |
| 69 | with pytest.raises(error.ConnectionError): |
| 70 | await update.async_install() |