hass-core/tests/components/emulated_hue/test_init.py
Nicko van Someren b0b3620b2b Added working support for private storage (#16903)
* Fixed file corruption bugs in private storage code.

* Restoring fixed test case.

* Implemented test suite for utils/json.py

* Added new unit test cases for util/json.py

* Dixed formatting nags

* Fixed more nags from the Hound

* Added doc strings to some very short functions

* Fixing lint's complains about my choice of parts of speach. Sigh.

* Moved atomic save operations down into util/json.py so that all benefit.

Added extra clean-up code to ensure that temporary files are removed in
case of errors.
Updated emulated_hue unit tests to avoid errors.

* Apparently 'e' is not allows as a variable name for an exception...
2018-10-02 10:16:43 +02:00

126 lines
4.4 KiB
Python

"""Test the Emulated Hue component."""
import json
from unittest.mock import patch, Mock, mock_open, MagicMock
from homeassistant.components.emulated_hue import Config
def test_config_google_home_entity_id_to_number():
"""Test config adheres to the type."""
mock_hass = Mock()
mock_hass.config.path = MagicMock("path", return_value="test_path")
conf = Config(mock_hass, {
'type': 'google_home'
})
mop = mock_open(read_data=json.dumps({'1': 'light.test2'}))
handle = mop()
with patch('homeassistant.util.json.open', mop, create=True):
with patch('homeassistant.util.json.os.open', return_value=0):
with patch('homeassistant.util.json.os.replace'):
number = conf.entity_id_to_number('light.test')
assert number == '2'
assert handle.write.call_count == 1
assert json.loads(handle.write.mock_calls[0][1][0]) == {
'1': 'light.test2',
'2': 'light.test',
}
number = conf.entity_id_to_number('light.test')
assert number == '2'
assert handle.write.call_count == 1
number = conf.entity_id_to_number('light.test2')
assert number == '1'
assert handle.write.call_count == 1
entity_id = conf.number_to_entity_id('1')
assert entity_id == 'light.test2'
def test_config_google_home_entity_id_to_number_altered():
"""Test config adheres to the type."""
mock_hass = Mock()
mock_hass.config.path = MagicMock("path", return_value="test_path")
conf = Config(mock_hass, {
'type': 'google_home'
})
mop = mock_open(read_data=json.dumps({'21': 'light.test2'}))
handle = mop()
with patch('homeassistant.util.json.open', mop, create=True):
with patch('homeassistant.util.json.os.open', return_value=0):
with patch('homeassistant.util.json.os.replace'):
number = conf.entity_id_to_number('light.test')
assert number == '22'
assert handle.write.call_count == 1
assert json.loads(handle.write.mock_calls[0][1][0]) == {
'21': 'light.test2',
'22': 'light.test',
}
number = conf.entity_id_to_number('light.test')
assert number == '22'
assert handle.write.call_count == 1
number = conf.entity_id_to_number('light.test2')
assert number == '21'
assert handle.write.call_count == 1
entity_id = conf.number_to_entity_id('21')
assert entity_id == 'light.test2'
def test_config_google_home_entity_id_to_number_empty():
"""Test config adheres to the type."""
mock_hass = Mock()
mock_hass.config.path = MagicMock("path", return_value="test_path")
conf = Config(mock_hass, {
'type': 'google_home'
})
mop = mock_open(read_data='')
handle = mop()
with patch('homeassistant.util.json.open', mop, create=True):
with patch('homeassistant.util.json.os.open', return_value=0):
with patch('homeassistant.util.json.os.replace'):
number = conf.entity_id_to_number('light.test')
assert number == '1'
assert handle.write.call_count == 1
assert json.loads(handle.write.mock_calls[0][1][0]) == {
'1': 'light.test',
}
number = conf.entity_id_to_number('light.test')
assert number == '1'
assert handle.write.call_count == 1
number = conf.entity_id_to_number('light.test2')
assert number == '2'
assert handle.write.call_count == 2
entity_id = conf.number_to_entity_id('2')
assert entity_id == 'light.test2'
def test_config_alexa_entity_id_to_number():
"""Test config adheres to the type."""
conf = Config(None, {
'type': 'alexa'
})
number = conf.entity_id_to_number('light.test')
assert number == 'light.test'
number = conf.entity_id_to_number('light.test')
assert number == 'light.test'
number = conf.entity_id_to_number('light.test2')
assert number == 'light.test2'
entity_id = conf.number_to_entity_id('light.test')
assert entity_id == 'light.test'