Skip to content

Commit

Permalink
Device tests: use time.time() for mocking updated_at times
Browse files Browse the repository at this point in the history
updated_at from tinytuya is a float, but datetime.now() returns an object.
We used to be able to compare datetime objects to float, but that seems
to have stopped working in latest python.
  • Loading branch information
make-all committed May 1, 2024
1 parent 66ebdfe commit 982ba3d
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions tests/test_device.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from datetime import datetime
from time import time
from unittest import IsolatedAsyncioTestCase
from unittest.mock import ANY, AsyncMock, Mock, call, patch
Expand Down Expand Up @@ -92,7 +91,7 @@ async def test_refreshes_state_if_no_cached_state_exists(self):
self.subject.async_refresh.assert_awaited()

async def test_detection_returns_none_when_device_type_not_detected(self):
self.subject._cached_state = {"2": False, "updated_at": datetime.now()}
self.subject._cached_state = {"2": False, "updated_at": time.time()}

Check failure on line 94 in tests/test_device.py

View workflow job for this annotation

GitHub Actions / tests (3.12)

TestDevice.test_detection_returns_none_when_device_type_not_detected AttributeError: 'builtin_function_or_method' object has no attribute 'time'

Check failure on line 94 in tests/test_device.py

View workflow job for this annotation

GitHub Actions / tests (3.12)

TestDevice.test_detection_returns_none_when_device_type_not_detected AttributeError: 'builtin_function_or_method' object has no attribute 'time'
self.assertEqual(await self.subject.async_inferred_type(), None)

async def test_refreshes_when_there_is_no_pending_reset(self):
Expand Down Expand Up @@ -137,7 +136,7 @@ async def test_refresh_retries_up_to_eleven_times(self):
async def test_refresh_clears_cache_after_allowed_failures(self):
self.subject._cached_state = {"1": True}
self.subject._pending_updates = {
"1": {"value": False, "updated_at": datetime.now(), "sent": True}
"1": {"value": False, "updated_at": time.time(), "sent": True}

Check failure on line 139 in tests/test_device.py

View workflow job for this annotation

GitHub Actions / tests (3.12)

TestDevice.test_refresh_clears_cache_after_allowed_failures AttributeError: 'builtin_function_or_method' object has no attribute 'time'

Check failure on line 139 in tests/test_device.py

View workflow job for this annotation

GitHub Actions / tests (3.12)

TestDevice.test_refresh_clears_cache_after_allowed_failures AttributeError: 'builtin_function_or_method' object has no attribute 'time'
}
self.mock_api().status.side_effect = [
Exception("Error"),
Expand Down Expand Up @@ -234,7 +233,7 @@ async def test_api_protocol_version_is_not_rotated_when_not_auto(self):
def test_reset_cached_state_clears_cached_state_and_pending_updates(self):
self.subject._cached_state = {"1": True, "updated_at": time()}
self.subject._pending_updates = {
"1": {"value": False, "updated_at": datetime.now(), "sent": True}
"1": {"value": False, "updated_at": time.time(), "sent": True}

Check failure on line 236 in tests/test_device.py

View workflow job for this annotation

GitHub Actions / tests (3.12)

TestDevice.test_reset_cached_state_clears_cached_state_and_pending_updates AttributeError: 'builtin_function_or_method' object has no attribute 'time'

Check failure on line 236 in tests/test_device.py

View workflow job for this annotation

GitHub Actions / tests (3.12)

TestDevice.test_reset_cached_state_clears_cached_state_and_pending_updates AttributeError: 'builtin_function_or_method' object has no attribute 'time'
}

self.subject._reset_cached_state()
Expand Down

0 comments on commit 982ba3d

Please sign in to comment.