Просмотр исходного кода

Fix issues with tests

- YAML converts 3.10 to 3.1 if not quoted (unquoted it is a float)
- fix async_receive tests - the local tests that "passed" before last
  checkin were running the wrong test (test_device_config.py instead
  of test_device.py)
Jason Rumney 3 лет назад
Родитель
Сommit
0be696f2e5
3 измененных файлов с 25 добавлено и 14 удалено
  1. 1 1
      .github/workflows/linting.yml
  2. 1 1
      .github/workflows/tests.yml
  3. 23 12
      tests/test_device.py

+ 1 - 1
.github/workflows/linting.yml

@@ -11,7 +11,7 @@ jobs:
       - name: Setup Python
         uses: actions/setup-python@v2
         with:
-          python-version: 3.10
+          python-version: '3.10'
 
       - name: Install dependencies
         run: |

+ 1 - 1
.github/workflows/tests.yml

@@ -7,7 +7,7 @@ jobs:
     runs-on: ubuntu-latest
     strategy:
       matrix:
-        python-version: [3.10]
+        python-version: ['3.10']
 
     steps:
       - uses: actions/checkout@v2

+ 23 - 12
tests/test_device.py

@@ -541,23 +541,27 @@ class TestDevice(IsolatedAsyncioTestCase):
 
     async def test_async_receive(self):
         # Set up preconditions
-        status = AsyncMock(
+        def job(func, *args):
+            return func(*args)
+
+        status = Mock(
             name="status",
             return_value={"dps": {"1": "INIT", "2": 2}},
         )
         self.mock_api().status = status
-        heartbeat = AsyncMock(name="heartbeat")
+        heartbeat = Mock(name="heartbeat")
         self.mock_api().heartbeat = heartbeat
-        receive = AsyncMock(name="receive", return_value={"1": "UPDATE"})
+        receive = Mock(name="receive", return_value={"1": "UPDATED"})
         self.mock_api().receive = receive
-        self.subject._running = True
-        self.subject._cached_state = {"updated_at": 0}
         async_add_executor_job = AsyncMock(
             name="async_add_executor_job",
-            side_effect=lambda func, *args: func(*args),
+            side_effect=job,
         )
         self.hass().async_add_executor_job = async_add_executor_job
-
+        self.subject._running = True
+        self.subject._cached_state = {"updated_at": 0}
+        self.subject._api_protocol_version_index = 0
+        self.subject._api_protocol_working = True
         # Call the function under test
         loop = self.subject.async_receive()
         result = await loop.__anext__()
@@ -565,26 +569,33 @@ class TestDevice(IsolatedAsyncioTestCase):
         # Check that the loop was started
         self.mock_api().set_socketPersistent.assert_called_once_with(True)
         # Check that a full poll was done
-        async_add_executor_job.assert_called_once_with(status)
+        async_add_executor_job.assert_called_once()
+        status.assert_called_once()
         self.assertDictEqual(result, {"1": "INIT", "2": 2})
         # Prepare for next round
         self.mock_api().set_socketPersistent.reset_mock()
+        status.reset_mock()
         self.subject._cached_state["updated_at"] = time()
 
         # Call the function under test
         result = await loop.__anext__()
+
         # Check that the loop was not restarted
         self.mock_api().set_socketPersistent.assert_not_called()
         # Check that a heartbeat poll was done
-        async_add_executor_job.assert_called_with(heartbeat)
-        async_add_executor_job.assert_called_with(receive)
+        status.assert_not_called()
+        heartbeat.assert_called_once()
+        receive.assert_called_once()
         self.assertDictEqual(result, {"1": "UPDATED"})
         # Prepare for next iteration
         async_add_executor_job.reset_mock()
         self.subject._running = False
 
         # Call the function under test
-        result = await loop.__anext__()
+        try:
+            result = await loop.__anext__()
+            self.assertTrue(False)
         # Check that the loop terminated
+        except StopAsyncIteration:
+            pass
         self.mock_api().set_socketPersistent.assert_called_once_with(False)
-        self.assertIsNone(result)