<s> """<STR_LIT>""" <EOL> import unittest <EOL> import unittest . mock as mock <EOL> import os <EOL> import pytest <EOL> from voluptuous import MultipleInvalid <EOL> from homeassistant . core import DOMAIN , HomeAssistantError <EOL> import homeassistant . config as config_util <EOL> from homeassistant . const import ( <EOL> CONF_LATITUDE , CONF_LONGITUDE , CONF_TEMPERATURE_UNIT , CONF_NAME , <EOL> CONF_TIME_ZONE ) <EOL> from tests . common import get_test_config_dir <EOL> CONFIG_DIR = get_test_config_dir ( ) <EOL> YAML_PATH = os . path . join ( CONFIG_DIR , config_util . YAML_CONFIG_FILE ) <EOL> def create_file ( path ) : <EOL> """<STR_LIT>""" <EOL> with open ( path , '<STR_LIT:w>' ) : <EOL> pass <EOL> class TestConfig ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def tearDown ( self ) : <EOL> """<STR_LIT>""" <EOL> if os . path . isfile ( YAML_PATH ) : <EOL> os . remove ( YAML_PATH ) <EOL> def test_create_default_config ( self ) : <EOL> """<STR_LIT>""" <EOL> config_util . create_default_config ( CONFIG_DIR , False ) <EOL> self . assertTrue ( os . path . isfile ( YAML_PATH ) ) <EOL> def test_find_config_file_yaml ( self ) : <EOL> """<STR_LIT>""" <EOL> create_file ( YAML_PATH ) <EOL> self . assertEqual ( YAML_PATH , config_util . find_config_file ( CONFIG_DIR ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_ensure_config_exists_creates_config ( self , mock_print ) : <EOL> """<STR_LIT>""" <EOL> config_util . ensure_config_exists ( CONFIG_DIR , False ) <EOL> self . assertTrue ( os . path . isfile ( YAML_PATH ) ) <EOL> self . assertTrue ( mock_print . called ) <EOL> def test_ensure_config_exists_uses_existing_config ( self ) : <EOL> """<STR_LIT>""" <EOL> create_file ( YAML_PATH ) <EOL> config_util . ensure_config_exists ( CONFIG_DIR , False ) <EOL> with open ( YAML_PATH ) as f : <EOL> content = f . read ( ) <EOL> self . assertEqual ( '<STR_LIT>' , content ) <EOL> def test_load_yaml_config_converts_empty_files_to_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> create_file ( YAML_PATH ) <EOL> self . assertIsInstance ( <EOL> config_util . load_yaml_config_file ( YAML_PATH ) , dict ) <EOL> def test_load_yaml_config_raises_error_if_not_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> with open ( YAML_PATH , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT:5>' ) <EOL> with self . assertRaises ( HomeAssistantError ) : <EOL> config_util . load_yaml_config_file ( YAML_PATH ) <EOL> def test_load_yaml_config_raises_error_if_malformed_yaml ( self ) : <EOL> """<STR_LIT>""" <EOL> with open ( YAML_PATH , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT::>' ) <EOL> with self . assertRaises ( HomeAssistantError ) : <EOL> config_util . load_yaml_config_file ( YAML_PATH ) <EOL> def test_load_yaml_config_raises_error_if_unsafe_yaml ( self ) : <EOL> """<STR_LIT>""" <EOL> with open ( YAML_PATH , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> with self . assertRaises ( HomeAssistantError ) : <EOL> config_util . load_yaml_config_file ( YAML_PATH ) <EOL> def test_load_yaml_config_preserves_key_order ( self ) : <EOL> """<STR_LIT>""" <EOL> with open ( YAML_PATH , '<STR_LIT:w>' ) as f : <EOL> f . write ( '<STR_LIT>' ) <EOL> f . write ( '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> [ ( '<STR_LIT:hello>' , <NUM_LIT:0> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ] , <EOL> list ( config_util . load_yaml_config_file ( YAML_PATH ) . items ( ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_create_default_config_detect_location ( self , mock_print ) : <EOL> """<STR_LIT>""" <EOL> config_util . ensure_config_exists ( CONFIG_DIR ) <EOL> config = config_util . load_yaml_config_file ( YAML_PATH ) <EOL> self . assertIn ( DOMAIN , config ) <EOL> ha_conf = config [ DOMAIN ] <EOL> expected_values = { <EOL> CONF_LATITUDE : <NUM_LIT> , <EOL> CONF_LONGITUDE : <NUM_LIT:1.0> , <EOL> CONF_TEMPERATURE_UNIT : '<STR_LIT:F>' , <EOL> CONF_NAME : '<STR_LIT>' , <EOL> CONF_TIME_ZONE : '<STR_LIT>' <EOL> } <EOL> self . assertEqual ( expected_values , ha_conf ) <EOL> self . assertTrue ( mock_print . called ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_create_default_config_returns_none_if_write_error ( self , <EOL> mock_print ) : <EOL> """<STR_LIT>""" <EOL> self . assertIsNone ( <EOL> config_util . create_default_config ( <EOL> os . path . join ( CONFIG_DIR , '<STR_LIT>' ) , False ) ) <EOL> self . assertTrue ( mock_print . called ) <EOL> def test_core_config_schema ( self ) : <EOL> for value in ( <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : - <NUM_LIT> } , <EOL> { '<STR_LIT>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : { '<STR_LIT>' : { } } } , <EOL> { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:100> } } , <EOL> ) : <EOL> with pytest . raises ( MultipleInvalid ) : <EOL> config_util . CORE_CONFIG_SCHEMA ( value ) <EOL> config_util . CORE_CONFIG_SCHEMA ( { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:c>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : True , <EOL> } , <EOL> } , <EOL> } ) </s>
