Reworked tests/components/emulated_hue/test_init.py to not be dependent on the specific internal implementation of util/jsonn.py
This commit is contained in:
parent
44e35b7f52
commit
50f0eac7f3
2 changed files with 69 additions and 67 deletions
|
@ -4,7 +4,7 @@ from typing import Union, List, Dict
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
from os import O_WRONLY, O_CREAT, O_TRUNC
|
import tempfile
|
||||||
|
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
|
|
||||||
|
@ -46,13 +46,17 @@ def save_json(filename: str, data: Union[List, Dict],
|
||||||
|
|
||||||
Returns True on success.
|
Returns True on success.
|
||||||
"""
|
"""
|
||||||
tmp_filename = filename + "__TEMP__"
|
tmp_filename = ""
|
||||||
|
tmp_path = os.path.split(filename)[0]
|
||||||
try:
|
try:
|
||||||
json_data = json.dumps(data, sort_keys=True, indent=4)
|
json_data = json.dumps(data, sort_keys=True, indent=4)
|
||||||
mode = 0o600 if private else 0o644
|
# Modern versions of Python tempfile create this file with mode 0o600
|
||||||
with open(os.open(tmp_filename, O_WRONLY | O_CREAT | O_TRUNC, mode),
|
with tempfile.NamedTemporaryFile(mode="w", encoding='utf-8',
|
||||||
'w', encoding='utf-8') as fdesc:
|
dir=tmp_path, delete=False) as fdesc:
|
||||||
fdesc.write(json_data)
|
fdesc.write(json_data)
|
||||||
|
tmp_filename = fdesc.name
|
||||||
|
if not private:
|
||||||
|
os.chmod(tmp_filename, 0o644)
|
||||||
os.replace(tmp_filename, filename)
|
os.replace(tmp_filename, filename)
|
||||||
except TypeError as error:
|
except TypeError as error:
|
||||||
_LOGGER.exception('Failed to serialize to JSON: %s',
|
_LOGGER.exception('Failed to serialize to JSON: %s',
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
"""Test the Emulated Hue component."""
|
"""Test the Emulated Hue component."""
|
||||||
import json
|
from unittest.mock import patch, Mock, MagicMock
|
||||||
|
|
||||||
from unittest.mock import patch, Mock, mock_open, MagicMock
|
|
||||||
|
|
||||||
from homeassistant.components.emulated_hue import Config
|
from homeassistant.components.emulated_hue import Config
|
||||||
|
|
||||||
|
@ -14,30 +12,30 @@ def test_config_google_home_entity_id_to_number():
|
||||||
'type': 'google_home'
|
'type': 'google_home'
|
||||||
})
|
})
|
||||||
|
|
||||||
mop = mock_open(read_data=json.dumps({'1': 'light.test2'}))
|
with patch('homeassistant.components.emulated_hue.load_json',
|
||||||
handle = mop()
|
return_value={'1': 'light.test2'}) as json_loader:
|
||||||
|
with patch('homeassistant.components.emulated_hue'
|
||||||
|
'.save_json') as json_saver:
|
||||||
|
number = conf.entity_id_to_number('light.test')
|
||||||
|
assert number == '2'
|
||||||
|
|
||||||
with patch('homeassistant.util.json.open', mop, create=True):
|
assert json_saver.mock_calls[0][1][1] == {
|
||||||
with patch('homeassistant.util.json.os.open', return_value=0):
|
'1': 'light.test2', '2': 'light.test'
|
||||||
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 json_saver.call_count == 1
|
||||||
assert number == '2'
|
assert json_loader.call_count == 1
|
||||||
assert handle.write.call_count == 1
|
|
||||||
|
|
||||||
number = conf.entity_id_to_number('light.test2')
|
number = conf.entity_id_to_number('light.test')
|
||||||
assert number == '1'
|
assert number == '2'
|
||||||
assert handle.write.call_count == 1
|
assert json_saver.call_count == 1
|
||||||
|
|
||||||
entity_id = conf.number_to_entity_id('1')
|
number = conf.entity_id_to_number('light.test2')
|
||||||
assert entity_id == 'light.test2'
|
assert number == '1'
|
||||||
|
assert json_saver.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():
|
def test_config_google_home_entity_id_to_number_altered():
|
||||||
|
@ -48,30 +46,30 @@ def test_config_google_home_entity_id_to_number_altered():
|
||||||
'type': 'google_home'
|
'type': 'google_home'
|
||||||
})
|
})
|
||||||
|
|
||||||
mop = mock_open(read_data=json.dumps({'21': 'light.test2'}))
|
with patch('homeassistant.components.emulated_hue.load_json',
|
||||||
handle = mop()
|
return_value={'21': 'light.test2'}) as json_loader:
|
||||||
|
with patch('homeassistant.components.emulated_hue'
|
||||||
|
'.save_json') as json_saver:
|
||||||
|
number = conf.entity_id_to_number('light.test')
|
||||||
|
assert number == '22'
|
||||||
|
assert json_saver.call_count == 1
|
||||||
|
assert json_loader.call_count == 1
|
||||||
|
|
||||||
with patch('homeassistant.util.json.open', mop, create=True):
|
assert json_saver.mock_calls[0][1][1] == {
|
||||||
with patch('homeassistant.util.json.os.open', return_value=0):
|
'21': 'light.test2',
|
||||||
with patch('homeassistant.util.json.os.replace'):
|
'22': 'light.test',
|
||||||
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')
|
number = conf.entity_id_to_number('light.test')
|
||||||
assert number == '22'
|
assert number == '22'
|
||||||
assert handle.write.call_count == 1
|
assert json_saver.call_count == 1
|
||||||
|
|
||||||
number = conf.entity_id_to_number('light.test2')
|
number = conf.entity_id_to_number('light.test2')
|
||||||
assert number == '21'
|
assert number == '21'
|
||||||
assert handle.write.call_count == 1
|
assert json_saver.call_count == 1
|
||||||
|
|
||||||
entity_id = conf.number_to_entity_id('21')
|
entity_id = conf.number_to_entity_id('21')
|
||||||
assert entity_id == 'light.test2'
|
assert entity_id == 'light.test2'
|
||||||
|
|
||||||
|
|
||||||
def test_config_google_home_entity_id_to_number_empty():
|
def test_config_google_home_entity_id_to_number_empty():
|
||||||
|
@ -82,29 +80,29 @@ def test_config_google_home_entity_id_to_number_empty():
|
||||||
'type': 'google_home'
|
'type': 'google_home'
|
||||||
})
|
})
|
||||||
|
|
||||||
mop = mock_open(read_data='')
|
with patch('homeassistant.components.emulated_hue.load_json',
|
||||||
handle = mop()
|
return_value={}) as json_loader:
|
||||||
|
with patch('homeassistant.components.emulated_hue'
|
||||||
|
'.save_json') as json_saver:
|
||||||
|
number = conf.entity_id_to_number('light.test')
|
||||||
|
assert number == '1'
|
||||||
|
assert json_saver.call_count == 1
|
||||||
|
assert json_loader.call_count == 1
|
||||||
|
|
||||||
with patch('homeassistant.util.json.open', mop, create=True):
|
assert json_saver.mock_calls[0][1][1] == {
|
||||||
with patch('homeassistant.util.json.os.open', return_value=0):
|
'1': 'light.test',
|
||||||
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')
|
number = conf.entity_id_to_number('light.test')
|
||||||
assert number == '1'
|
assert number == '1'
|
||||||
assert handle.write.call_count == 1
|
assert json_saver.call_count == 1
|
||||||
|
|
||||||
number = conf.entity_id_to_number('light.test2')
|
number = conf.entity_id_to_number('light.test2')
|
||||||
assert number == '2'
|
assert number == '2'
|
||||||
assert handle.write.call_count == 2
|
assert json_saver.call_count == 2
|
||||||
|
|
||||||
entity_id = conf.number_to_entity_id('2')
|
entity_id = conf.number_to_entity_id('2')
|
||||||
assert entity_id == 'light.test2'
|
assert entity_id == 'light.test2'
|
||||||
|
|
||||||
|
|
||||||
def test_config_alexa_entity_id_to_number():
|
def test_config_alexa_entity_id_to_number():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue