Unit tests to improve core coverage (#9659)
* Code coverage of logging util * Improve async util coverage * Add test coverage for restore_state * get_random_string test
This commit is contained in:
parent
0aa22d9d91
commit
c4810da82f
5 changed files with 247 additions and 20 deletions
|
@ -1,6 +1,6 @@
|
|||
"""Test Home Assistant util methods."""
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import patch, MagicMock
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from homeassistant import util
|
||||
|
@ -266,3 +266,17 @@ class TestUtil(unittest.TestCase):
|
|||
|
||||
self.assertTrue(tester.hello())
|
||||
self.assertTrue(tester.goodbye())
|
||||
|
||||
@patch.object(util, 'random')
|
||||
def test_get_random_string(self, mock_random):
|
||||
"""Test get random string."""
|
||||
results = ['A', 'B', 'C']
|
||||
|
||||
def mock_choice(choices):
|
||||
return results.pop(0)
|
||||
|
||||
generator = MagicMock()
|
||||
generator.choice.side_effect = mock_choice
|
||||
mock_random.SystemRandom.return_value = generator
|
||||
|
||||
assert util.get_random_string(length=3) == 'ABC'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue