from src.config_parser.ini import INIConfiguration, INIConfigurationSection from src.config_parser.exceptions import INISyntaxError def test_generating(): config = INIConfiguration() config.test_group = INIConfigurationSection('test_group') config.test_group.key1 = 'value1' config.test_group.key2 = 'value2' assert config.to_string() == '''[test_group] key1=value1 key2=value2''' def test_parsing(): # Test deserializer config_string = ''' property1 = 42 property2=123 [configuration_group1] hello=world key1=value1 [group2] world=hel#lo key2 = "val#ue2" [] hello2=world2 invalid_line1 invalid_line2 # comment''' assert dict(INIConfiguration.from_string(config_string, ignore_errors=True)) == {'configuration_group1': {'hello': 'world', 'key1': 'value1'}, 'group2': {'world': 'hel', 'key2': 'val#ue2', 'hello2': 'world2'}} try: INIConfiguration.from_string('key="value') assert False, 'Expected INISyntaxError for unterminated literal' except INISyntaxError: pass try: INIConfiguration.from_string('key=value"') assert False, 'Expected INISyntaxError for unterminated literal' except INISyntaxError: pass def test_default(): default_configuration = INIConfiguration.from_string('''[section1] k1=v1 k2=v2 k3=v3 [section2] l1=w1 l2=w2''') assert dict(INIConfiguration.from_string('''[section1] k1=v1 k2=v2 k4=v4''', default = default_configuration)) == {'section1': {'k1': 'v1', 'k2': 'v2', 'k3': 'v3', 'k4': 'v4'}, 'section2': {'l1': 'w1', 'l2': 'w2'}}