def test_invalid_path(self): conf = Configuration( name='invalid_path', path='/dev/null', parse=False ) self.assertRaises(IOError, lambda: conf._parse_section(path='')) self.assertRaises(IOError, lambda: conf._parse_section(path=None)) self.assertRaises(IOError, lambda: conf._parse_section(path='.')) self.assertRaises(IOError, lambda: conf._parse_section(path='/non-existent/path'))
def test_parse(self): conf = Configuration( name='invalid_path', path='/dev/null', parse=False ) expected = {} actual = conf._parse_section(path='./data/empty.ini') self.assertDictEqual(expected, actual) expected = {'section': {'key': 'value'}} actual = conf._parse_section(path='./data/single_section.ini') self.assertDictEqual(expected, actual) expected = {'section': {'key': 'value', 'key_two': 'other_value'}} actual = conf._parse_section(path='./data/single_section.ini', defaults={'key_two': 'other_value'}) self.assertDictEqual(expected, actual) expected = {'section': {'key': 'value', 'key_two': 'other_value'}} actual = conf._parse_section(path='./data/single_section.ini', defaults={'key_two': 'other_value'}, only_section='section') self.assertDictEqual(expected, actual) expected = {} actual = conf._parse_section(path='./data/single_section.ini', defaults={'key_two': 'other_value'}, remove_section='section') self.assertDictEqual(expected, actual) expected = { 'derp': {'cats': '1', 'expected': 'the spanish inquisition', 'no': 'sleep', 'til': 'brooklyn'}, 'multiple_sections': {'cats': '1', 'expected': 'the spanish inquisition', 'no': 'one'}, 'section': {'cats': '1', 'expected': 'the spanish inquisition', 'key': 'value', 'no': 'one'} } actual = conf._parse_section(path='./data/multiple_sections.ini', defaults={ 'no': 'one', 'expected': 'the spanish inquisition', 'cats': '1', }) self.assertDictEqual(expected, actual) expected = { 'multiple_sections': {'cats': '1', 'expected': 'the spanish inquisition', 'no': 'one'}, } actual = conf._parse_section(path='./data/multiple_sections.ini', only_section='multiple_sections', defaults={ 'no': 'one', 'expected': 'the spanish inquisition', 'cats': '1', }) self.assertDictEqual(expected, actual) expected = { 'multiple_sections': {'cats': '1', 'expected': 'the spanish inquisition', 'no': 'one'}, 'section': {'cats': '1', 'expected': 'the spanish inquisition', 'key': 'value', 'no': 'one'} } actual = conf._parse_section(path='./data/multiple_sections.ini', remove_section='derp', defaults={ 'no': 'one', 'expected': 'the spanish inquisition', 'cats': '1', }) self.assertDictEqual(expected, actual)
def test_parse(self): conf = Configuration( name='invalid_path', path='/dev/null', parse=False ) expected = {} actual = conf._parse_section(path='./data/empty.ini') self.assertEqual(expected, actual) expected = {'section': {'key': 'value'}} actual = conf._parse_section(path='./data/single_section.ini') self.assertEqual(expected, actual) expected = {'section': {'key': 'value', 'key_two': 'other_value'}} actual = conf._parse_section(path='./data/single_section.ini', defaults={'key_two': 'other_value'}) self.assertEqual(expected, actual) expected = {'section': {'key': 'value', 'key_two': 'other_value'}} actual = conf._parse_section(path='./data/single_section.ini', defaults={'key_two': 'other_value'}, only_section='section') self.assertEqual(expected, actual) expected = {} actual = conf._parse_section(path='./data/single_section.ini', defaults={'key_two': 'other_value'}, remove_section='section') self.assertEqual(expected, actual) expected = { 'derp': {'cats': '1', 'expected': 'the spanish inquisition', 'no': 'sleep', 'til': 'brooklyn'}, 'multiple_sections': {'cats': '1', 'expected': 'the spanish inquisition', 'no': 'one'}, 'section': {'cats': '1', 'expected': 'the spanish inquisition', 'key': 'value', 'no': 'one'} } actual = conf._parse_section(path='./data/multiple_sections.ini', defaults={ 'no': 'one', 'expected': 'the spanish inquisition', 'cats': '1', }) self.assertEqual(expected, actual) expected = { 'multiple_sections': {'cats': '1', 'expected': 'the spanish inquisition', 'no': 'one'}, } actual = conf._parse_section(path='./data/multiple_sections.ini', only_section='multiple_sections', defaults={ 'no': 'one', 'expected': 'the spanish inquisition', 'cats': '1', }) self.assertEqual(expected, actual) expected = { 'multiple_sections': {'cats': '1', 'expected': 'the spanish inquisition', 'no': 'one'}, 'section': {'cats': '1', 'expected': 'the spanish inquisition', 'key': 'value', 'no': 'one'} } actual = conf._parse_section(path='./data/multiple_sections.ini', remove_section='derp', defaults={ 'no': 'one', 'expected': 'the spanish inquisition', 'cats': '1', }) self.assertEqual(expected, actual)
def test_custom_config_parser(self): conf = Configuration( name='multiple_sections', path='./data/multiple_sections.ini', config_parser=TestConfigParser, parse=False ) self.assertRaises(NotImplementedError, lambda: conf._parse_section(path='./data/multiple_sections.ini'))