def test_extra_sections_when_lines_dict_with_nested_lines_dicts(self): """Test parser.is_valid in dict of nested dict lists.""" class MySchema(Schema): foo = ListOption( item=DictOption( item=ListOption(item=DictOption()))) config = StringIO(""" [__main__] foo = dict1 dict2 [dict1] bar = dict3 dict4 [dict2] baz = dict5 dict6 [dict3] wham = 1 [dict4] whaz = 2 [dict5] whoosh = 3 [dict6] swoosh = 4 """) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertEqual(parser.values(), {'__main__': {'foo': [ {'bar': [{'wham': '1'}, {'whaz': '2'}]}, {'baz': [{'whoosh': '3'}, {'swoosh': '4'}]}]}}) self.assertTrue(parser.is_valid())
def test_noschema_section(self): config = StringIO( "[__main__]\nfoo=%(bar)s\n[__noschema__]\nbar=hello") parser = SchemaConfigParser(self.schema) parser.readfp(config) parser.parse_all() self.assertTrue(parser.is_valid())
def test_missing_nonfatal_options(self): """Test parser.is_valid when missing non-fatal options.""" class MySchema(Schema): foo = IntOption() bar = IntOption(fatal=True) config = StringIO("[__main__]\nbar=2") parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertTrue(parser.is_valid())
def test_basic_is_not_valid(self): """Test invalid config without error reporting.""" class MySchema(Schema): foo = IntOption() schema = MySchema() config = StringIO("[__main__]\nfoo = 5\nbar = 6") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertFalse(parser.is_valid())
def test_basic_is_valid_with_report(self): """Test basic validation with error reporting.""" class MySchema(Schema): foo = IntOption() config = StringIO("[__main__]\nfoo=5") expected = (True, []) parser = SchemaConfigParser(MySchema()) parser.readfp(config) valid, errors = parser.is_valid(report=True) self.assertEqual((valid, errors), expected)
def test_basic_is_valid(self): """Test basic validation without error reporting.""" class MySchema(Schema): foo = IntOption() schema = MySchema() config = StringIO("[__main__]\nfoo = 5") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertTrue(parser.is_valid())
def test_extra_sections(self): """Test parser.is_valid with extra sections.""" class MySchema(Schema): foo = DictOption(spec={'bar': IntOption()}) config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=1") parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertTrue(parser.is_valid())
def test_multiple_extra_sections(self): """Test parser.is_valid with multiple extra sections.""" class MySchema(Schema): foo = ListOption( item=DictOption(spec={'bar': IntOption()})) config = StringIO('[__main__]\nfoo=d1\n d2\n d3\n' '[d1]\nbar=1\n[d2]\nbar=2\n[d3]\nbar=3') parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertTrue(parser.is_valid())
def test_basic_is_not_valid_with_report(self): """Test invalid config with error reporting.""" class MySchema(Schema): foo = IntOption() config = StringIO("[__main__]\nfoo=5\nbar=6") errors = ["Configuration includes invalid options for " "section '__main__': bar"] expected = (False, errors) parser = SchemaConfigParser(MySchema()) parser.readfp(config) valid, errors = parser.is_valid(report=True) self.assertEqual((valid, errors), expected)
def test_is_not_valid_parser_error(self): """Test parser.is_valid when parser errors.""" class MySchema(Schema): foo = IntOption() def mock_parse_all(self): assert False schema = MySchema() config = StringIO("[__main__]\nfoo = 5") parser = SchemaConfigParser(schema) parser.parse_all = mock_parse_all parser.readfp(config) self.assertFalse(parser.is_valid())
def test_extra_sections_with_missing_section(self): """Test parser.is_valid with dict referencing missing section.""" class MySchema(Schema): foo = DictOption() config = StringIO(textwrap.dedent(""" [__main__] foo = dict1 """)) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertRaises(NoSectionError, parser.values) # config is not valid self.assertFalse(parser.is_valid())
def test_missing_schema_sections(self): class MySchema(Schema): class foo(Section): bar = IntOption() class bar(Section): baz = BoolOption() config = StringIO(textwrap.dedent(""" [foo] bar = 3 """)) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertTrue(parser.is_valid())
def test_glue_missing_section(self): """Test schemaconfigglue with missing section.""" class MySchema(Schema): foo = DictOption() config = StringIO("[__main__]\nfoo = bar") parser = SchemaConfigParser(MySchema()) parser.readfp(config) # hitting the parser directly raises an exception self.assertRaises(NoSectionError, parser.values) self.assertFalse(parser.is_valid()) # which is nicely handled by the glue code, so as not to crash it op, options, args = schemaconfigglue(parser) # there is no value for 'foo' due to the missing section self.assertEqual(options, {'foo': None})
def test_extra_sections_with_nested_dicts_strict(self): """Test parser.is_valid w/ extra sections in a nested dict (strict).""" class MySchema(Schema): foo = DictOption(spec={'bar': DictOption()}, strict=True) config = StringIO(""" [__main__] foo=dict1 [dict1] bar=dict2 [dict2] baz=42 """) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertEqual(parser.values(), {'__main__': {'foo': {'bar': {'baz': '42'}}}}) self.assertTrue(parser.is_valid())
class TestParserIsValid(unittest.TestCase): def setUp(self): class MySchema(Schema): foo = StringOption() self.schema = MySchema() self.parser = SchemaConfigParser(self.schema) self.config = StringIO("[__main__]\nfoo = bar") def test_basic_is_valid(self): """Test basic validation without error reporting.""" class MySchema(Schema): foo = IntOption() schema = MySchema() config = StringIO("[__main__]\nfoo = 5") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertTrue(parser.is_valid()) def test_basic_is_valid_with_report(self): """Test basic validation with error reporting.""" class MySchema(Schema): foo = IntOption() config = StringIO("[__main__]\nfoo=5") expected = (True, []) parser = SchemaConfigParser(MySchema()) parser.readfp(config) valid, errors = parser.is_valid(report=True) self.assertEqual((valid, errors), expected) def test_basic_is_not_valid(self): """Test invalid config without error reporting.""" class MySchema(Schema): foo = IntOption() schema = MySchema() config = StringIO("[__main__]\nfoo = 5\nbar = 6") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertFalse(parser.is_valid()) def test_basic_is_not_valid_with_report(self): """Test invalid config with error reporting.""" class MySchema(Schema): foo = IntOption() config = StringIO("[__main__]\nfoo=5\nbar=6") errors = ["Configuration includes invalid options for " "section '__main__': bar"] expected = (False, errors) parser = SchemaConfigParser(MySchema()) parser.readfp(config) valid, errors = parser.is_valid(report=True) self.assertEqual((valid, errors), expected) def test_is_not_valid_parser_error(self): """Test parser.is_valid when parser errors.""" class MySchema(Schema): foo = IntOption() def mock_parse_all(self): assert False schema = MySchema() config = StringIO("[__main__]\nfoo = 5") parser = SchemaConfigParser(schema) parser.parse_all = mock_parse_all parser.readfp(config) self.assertFalse(parser.is_valid()) def test_parse_invalid_section(self): config = StringIO("[bar]\nbaz=foo") self.parser.readfp(config) self.assertFalse(self.parser.is_valid()) def test_parse_invalid_section_with_report(self): config = StringIO("[bar]\nbaz=foo") self.parser.readfp(config) valid, errors = self.parser.is_valid(report=True) self.assertFalse(valid) self.assertEqual(errors[0], u'Sections in configuration are missing from schema: bar') def test_different_sections(self): config = StringIO("[__main__]\nfoo=1\n[bar]\nbaz=2") self.parser.readfp(config) self.assertFalse(self.parser.is_valid()) def test_missing_fatal_options(self): """Test parser.is_valid when missing fatal options.""" class MySchema(Schema): foo = IntOption() bar = IntOption(fatal=True) config = StringIO("[__main__]\nfoo=1") parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertFalse(parser.is_valid()) def test_missing_nonfatal_options(self): """Test parser.is_valid when missing non-fatal options.""" class MySchema(Schema): foo = IntOption() bar = IntOption(fatal=True) config = StringIO("[__main__]\nbar=2") parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertTrue(parser.is_valid()) def test_extra_sections(self): """Test parser.is_valid with extra sections.""" class MySchema(Schema): foo = DictOption(spec={'bar': IntOption()}) config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=1") parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertTrue(parser.is_valid()) def test_extra_sections_when_dict_with_nested_dicts(self): """Test parser.is_valid with extra sections in a nested dict.""" class MySchema(Schema): foo = DictOption(item=DictOption()) config = StringIO(""" [__main__] foo=dict1 [dict1] bar=dict2 [dict2] baz=42 """) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertEqual(parser.values(), {'__main__': {'foo': {'bar': {'baz': '42'}}}}) self.assertTrue(parser.is_valid()) def test_extra_sections_with_nested_dicts_strict(self): """Test parser.is_valid w/ extra sections in a nested dict (strict).""" class MySchema(Schema): foo = DictOption(spec={'bar': DictOption()}, strict=True) config = StringIO(""" [__main__] foo=dict1 [dict1] bar=dict2 [dict2] baz=42 """) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertEqual(parser.values(), {'__main__': {'foo': {'bar': {'baz': '42'}}}}) self.assertTrue(parser.is_valid()) def test_extra_sections_when_lines_dict_with_nested_dicts(self): """Test parser.is_valid w/ extra section in list of nested dicts.""" class MySchema(Schema): foo = ListOption( item=DictOption(item=DictOption())) config = StringIO(""" [__main__] foo = dict1 dict2 [dict1] bar = dict3 [dict2] baz = dict4 [dict3] wham = 1 [dict4] whaz = 2 """) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertEqual(parser.values(), {'__main__': {'foo': [ {'bar': {'wham': '1'}}, {'baz': {'whaz': '2'}}]}}) self.assertTrue(parser.is_valid()) def test_extra_sections_when_dict_with_nested_lines_dicts(self): """Test parser.is_valid in dict of nested list lists.""" class MySchema(Schema): foo = DictOption( item=ListOption(item=DictOption())) config = StringIO(""" [__main__] foo = dict1 [dict1] bar = dict2 dict3 [dict2] baz = 1 [dict3] wham = 2 """) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertEqual(parser.values(), {'__main__': {'foo': {'bar': [{'baz': '1'}, {'wham': '2'}]}}}) self.assertTrue(parser.is_valid()) def test_extra_sections_when_lines_dict_with_nested_lines_dicts(self): """Test parser.is_valid in dict of nested dict lists.""" class MySchema(Schema): foo = ListOption( item=DictOption( item=ListOption(item=DictOption()))) config = StringIO(""" [__main__] foo = dict1 dict2 [dict1] bar = dict3 dict4 [dict2] baz = dict5 dict6 [dict3] wham = 1 [dict4] whaz = 2 [dict5] whoosh = 3 [dict6] swoosh = 4 """) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertEqual(parser.values(), {'__main__': {'foo': [ {'bar': [{'wham': '1'}, {'whaz': '2'}]}, {'baz': [{'whoosh': '3'}, {'swoosh': '4'}]}]}}) self.assertTrue(parser.is_valid()) def test_extra_sections_with_missing_section(self): """Test parser.is_valid with dict referencing missing section.""" class MySchema(Schema): foo = DictOption() config = StringIO(textwrap.dedent(""" [__main__] foo = dict1 """)) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertRaises(NoSectionError, parser.values) # config is not valid self.assertFalse(parser.is_valid()) def test_multiple_extra_sections(self): """Test parser.is_valid with multiple extra sections.""" class MySchema(Schema): foo = ListOption( item=DictOption(spec={'bar': IntOption()})) config = StringIO('[__main__]\nfoo=d1\n d2\n d3\n' '[d1]\nbar=1\n[d2]\nbar=2\n[d3]\nbar=3') parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertTrue(parser.is_valid()) def test_noschema_section(self): config = StringIO( "[__main__]\nfoo=%(bar)s\n[__noschema__]\nbar=hello") parser = SchemaConfigParser(self.schema) parser.readfp(config) parser.parse_all() self.assertTrue(parser.is_valid()) def test_missing_schema_sections(self): class MySchema(Schema): class foo(Section): bar = IntOption() class bar(Section): baz = BoolOption() config = StringIO(textwrap.dedent(""" [foo] bar = 3 """)) parser = SchemaConfigParser(MySchema()) parser.readfp(config) parser.parse_all() self.assertTrue(parser.is_valid())