def test_parse_bool(self): """Test BoolOption parse a boolean value.""" class MySchema(Schema): foo = self.cls() config = StringIO("[__main__]\nfoo = Yes") expected_values = {'__main__': {'foo': True}} schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(parser.values(), expected_values) config = StringIO("[__main__]\nfoo = tRuE") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(parser.values(), expected_values) config = StringIO("[__main__]\nfoo =") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertRaises(ValueError, parser.values) config = StringIO("[__main__]\nfoo = bla") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertRaises(ValueError, parser.values)
def test_multi_file_dict_list_config(self): """Test parsing a list of dicts option spanning multiple files.""" class MySchema(Schema): foo = ListOption( item=DictOption(spec={ 'bar': IntOption(), 'baz': IntOption(), }, strict=True)) config1 = StringIO('[__main__]\nfoo=mydict\n[mydict]\nbar=1\nbaz=1') expected_values = {'__main__': {'foo': [{'bar': 1, 'baz': 1}]}} parser = SchemaConfigParser(MySchema()) parser.readfp(config1) self.assertEqual(parser.values(), expected_values) # override used dictionaries config2 = StringIO('[__main__]\nfoo=otherdict\n[otherdict]\nbar=2') expected_values = {'__main__': {'foo': [{'bar': 2, 'baz': 0}]}} parser.readfp(config2) self.assertEqual(parser.values(), expected_values) # override existing dictionaries config3 = StringIO('[otherdict]\nbaz=3') expected_values = {'__main__': {'foo': [{'bar': 2, 'baz': 3}]}} parser.readfp(config3) self.assertEqual(parser.values(), expected_values) # reuse existing dict config4 = StringIO('[__main__]\nfoo=mydict\n otherdict') expected_values = {'__main__': {'foo': [{'bar': 1, 'baz': 1}, {'bar': 2, 'baz': 3}]}} parser.readfp(config4) self.assertEqual(parser.values(), expected_values)
class TestListOfTuples(unittest.TestCase): def setUp(self): class MySchema(Schema): foo = ListOption(item=TupleOption(length=3)) schema = MySchema() self.parser = SchemaConfigParser(schema) def test_parse_list_of_tuples(self): config = StringIO('[__main__]\nfoo = a, b, c\n d, e, f') expected_values = { '__main__': { 'foo': [('a', 'b', 'c'), ('d', 'e', 'f')] } } self.parser.readfp(config) self.assertEqual(self.parser.values(), expected_values) def test_parse_wrong_tuple_size(self): config = StringIO('[__main__]\nfoo = a, b, c\n d, e') self.parser.readfp(config) self.assertRaises(ValueError, self.parser.values) def test_parse_empty_tuple(self): config = StringIO('[__main__]\nfoo=()') expected_values = {'__main__': {'foo': [()]}} self.parser.readfp(config) self.assertEqual(self.parser.values(), expected_values)
class TestListOfTuples(unittest.TestCase): def setUp(self): class MySchema(Schema): foo = ListOption(item=TupleOption(length=3)) schema = MySchema() self.parser = SchemaConfigParser(schema) def test_parse_list_of_tuples(self): config = StringIO('[__main__]\nfoo = a, b, c\n d, e, f') expected_values = { '__main__': {'foo': [('a', 'b', 'c'), ('d', 'e', 'f')]}} self.parser.readfp(config) self.assertEqual(self.parser.values(), expected_values) def test_parse_wrong_tuple_size(self): config = StringIO('[__main__]\nfoo = a, b, c\n d, e') self.parser.readfp(config) self.assertRaises(ValueError, self.parser.values) def test_parse_empty_tuple(self): config = StringIO('[__main__]\nfoo=()') expected_values = {'__main__': {'foo': [()]}} self.parser.readfp(config) self.assertEqual(self.parser.values(), expected_values)
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_parse_dict_no_json(self): """Test DictOption parse a dict when json is disabled.""" class MySchema(Schema): foo = self.cls(spec={ 'bar': StringOption(), 'baz': IntOption(), 'bla': BoolOption(), }, parse_json=False) config = StringIO("""[__main__] foo = mydict [mydict] bar=baz baz=42 bla=Yes """) expected_values = { '__main__': { 'foo': { 'bar': 'baz', 'baz': 42, 'bla': True } } } schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_parse_dict_with_dicts(self): innerspec = {'bar': StringOption(), 'baz': IntOption(), 'bla': BoolOption(), } spec = {'name': StringOption(), 'size': IntOption(), 'options': DictOption(spec=innerspec)} class MySchema(Schema): foo = DictOption(spec=spec) config = StringIO("""[__main__] foo = outerdict [outerdict] options = innerdict [innerdict] bar = something baz = 42 """) expected_values = { '__main__': {'foo': {'name': '', 'size': 0, 'options': {'bar': 'something', 'baz': 42, 'bla': False}}}} schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_parse_lines_of_dict(self): """Test ListOption parse a list of dicts.""" class MySchema(Schema): foo = ListOption(item=DictOption( spec={ 'bar': StringOption(), 'baz': IntOption(), 'bla': BoolOption(), })) config = StringIO("""[__main__] foo = mylist0 mylist1 [mylist0] bar=baz baz=42 bla=Yes [mylist1] bar=zort baz=123 bla=0 """) expected_values = { '__main__': {'foo': [{'bar': 'baz', 'baz': 42, 'bla': True}, {'bar': 'zort', 'baz': 123, 'bla': False}, ]}} schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_parse_dict_json(self): """Test DictOption parse a json dict.""" class MySchema(Schema): foo = self.cls(spec={ 'bar': StringOption(), 'baz': IntOption(), 'bla': BoolOption(), }) config = StringIO( textwrap.dedent(""" [__main__] foo = { "bar": "baz", "baz": "42", "bla": "Yes"} """)) expected_values = { '__main__': { 'foo': { 'bar': 'baz', 'baz': 42, 'bla': True } } } schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_parse_no_strict_extra_args(self): class MySchema(Schema): foo = self.cls() config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2") expected_values = {'__main__': {'foo': {'bar': '2'}}} parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_basic_interpolate(self): class MySchema(Schema): foo = StringOption() bar = BoolOption() config = StringIO('[__main__]\nbar=%(foo)s\nfoo=True') parser = SchemaConfigParser(MySchema()) parser.readfp(config, 'my.cfg') self.assertEquals({'__main__': {'foo': 'True', 'bar': True}}, parser.values())
def test_ambiguous_option(self): """Test schemaconfigglue when an ambiguous option is specified.""" class MySchema(Schema): class foo(Section): baz = IntOption() class bar(Section): baz = IntOption() config = StringIO("[foo]\nbaz=1") parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values('foo'), {'baz': 1}) self.assertEqual(parser.values('bar'), {'baz': 0}) op, options, args = schemaconfigglue(parser, argv=['--bar_baz', '2']) self.assertEqual(parser.values('foo'), {'baz': 1}) self.assertEqual(parser.values('bar'), {'baz': 2})
def test_time_zone_is_null(self): config = StringIO(textwrap.dedent(""" [django] time_zone = None """)) parser = SchemaConfigParser(BaseDjangoSchema()) parser.readfp(config) value = parser.values()['django']['time_zone'] self.assertEqual(value, None)
def test_parse_no_strict_missing_args(self): """Test DictOption parse a missing key in non-strict mode.""" class MySchema(Schema): foo = self.cls(spec={'bar': IntOption()}) config = StringIO("[__main__]\nfoo=mydict\n[mydict]") expected_values = {'__main__': {'foo': {'bar': 0}}} parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_parse_invalid_key_in_parsed(self): """Test DictOption parse with an invalid key in the config.""" class MySchema(Schema): foo = self.cls(spec={'bar': IntOption()}) config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbaz=2") expected_values = {'__main__': {'foo': {'bar': 0, 'baz': '2'}}} parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_parse_no_json(self): class MySchema(Schema): foo = self.cls(item=IntOption(), parse_json=False) config = BytesIO(b"[__main__]\nfoo = 42\n 43\n 44") expected_values = {'__main__': {'foo': [42, 43, 44]}} schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_parse_no_length(self): """Test TupleOption parse without length.""" class MySchema(Schema): foo = self.cls() config = StringIO('[__main__]\nfoo=1,2,3,4') expected_values = {'__main__': {'foo': ('1', '2', '3', '4')}} parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_parse_json(self): class MySchema(Schema): foo = self.cls(item=IntOption()) config = StringIO("[__main__]\nfoo = [42, 43, 44]") expected_values = {'__main__': {'foo': [42, 43, 44]}} schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_fatal_option_with_config(self): class MySchema(Schema): foo = IntOption(fatal=True) config = StringIO("[__main__]\nfoo=1") parser = SchemaConfigParser(MySchema()) parser.readfp(config) op, options, args = schemaconfigglue(parser) self.assertEqual(parser.values(), {'__main__': {'foo': 1}})
def test_ambiguous_option(self): """Test schemaconfigglue when an ambiguous option is specified.""" class MySchema(Schema): class foo(Section): baz = IntOption() class bar(Section): baz = IntOption() config = StringIO("[foo]\nbaz=1") parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values('foo'), {'baz': 1}) self.assertEqual(parser.values('bar'), {'baz': 0}) op, options, args = schemaconfigglue( parser, argv=['--bar_baz', '2']) self.assertEqual(parser.values('foo'), {'baz': 1}) self.assertEqual(parser.values('bar'), {'baz': 2})
def test_django13_custom_logging_config(self): config = StringIO(textwrap.dedent(""" [django] logging_config = foo """)) schema = schemas.get('1.3') parser = SchemaConfigParser(schema()) parser.readfp(config) value = parser.values()['django']['logging_config'] self.assertEqual(value, 'foo')
def test_parse_bool_empty_lines(self): """Test ListOption parse an empty list of booleans.""" class MySchema(Schema): foo = self.cls(item=BoolOption()) schema = MySchema() config = StringIO("[__main__]\nfoo =") parser = SchemaConfigParser(schema) parser.readfp(config) expected_values = {'__main__': {'foo': []}} self.assertEqual(expected_values, parser.values())
def test_parse_bool_lines(self): """Test ListOption parse a list of booleans.""" class MySchema(Schema): foo = self.cls(item=BoolOption()) schema = MySchema() config = StringIO("[__main__]\nfoo = tRuE\n No\n 0\n 1") expected_values = {'__main__': {'foo': [True, False, False, True]}} parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(expected_values, parser.values())
def test_parse_strict_missing_vars(self): """Test DictOption parse in strict mode with missing values.""" class MySchema(Schema): spec = {'bar': IntOption(), 'baz': IntOption()} foo = self.cls(spec=spec, strict=True) config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2") expected_values = {'__main__': {'foo': {'bar': 2, 'baz': 0}}} parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_glue_json_dict(self): class MySchema(Schema): foo = DictOption() parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue(parser, argv=['--foo', '{"bar": "baz"}']) self.assertEqual(options, {'foo': '{"bar": "baz"}'}) self.assertEqual(parser.values(), {'__main__': {'foo': {'bar': 'baz'}}})
def test_parse_int_lines(self): """Test ListOption parse a list of integers.""" class MySchema(Schema): foo = self.cls(item=IntOption()) config = StringIO("[__main__]\nfoo = 42\n 43\n 44") expected_values = {'__main__': {'foo': [42, 43, 44]}} schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_remove_duplicates(self): """Test ListOption with remove_duplicates.""" class MySchema(Schema): foo = self.cls(item=StringOption(), remove_duplicates=True) schema = MySchema() config = StringIO("[__main__]\nfoo = bla\n blah\n bla") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEquals({'__main__': {'foo': ['bla', 'blah']}}, parser.values())
def test_remove_dict_duplicates(self): """Test ListOption remove_duplicates with DictOption.""" class MyOtherSchema(Schema): foo = self.cls(item=DictOption(), remove_duplicates=True) schema = MyOtherSchema() config = StringIO("[__main__]\nfoo = bla\n bla\n[bla]\nbar = baz") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEquals({'__main__': {'foo': [{'bar': 'baz'}]}}, parser.values())
def test_parse_strict(self): """Test DictOption parse in strict mode.""" class MySchema(Schema): spec = {'bar': IntOption()} foo = self.cls(spec=spec, strict=True) config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2") expected_values = {'__main__': {'foo': {'bar': 2}}} parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_interpolate_environment_in_config(self, mock_os): mock_os.environ = {'PYTHONPATH': 'foo', 'PATH': 'bar'} class MySchema(Schema): pythonpath = StringOption() path = StringOption() config = StringIO("[__main__]\npythonpath=${PYTHONPATH}\npath=$PATH") parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values('__main__'), {'pythonpath': 'foo', 'path': 'bar'})
def test_remove_duplicates(self): """Test ListOption with remove_duplicates.""" class MySchema(Schema): foo = self.cls(item=StringOption(), remove_duplicates=True) schema = MySchema() config = StringIO("[__main__]\nfoo = bla\n blah\n bla") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEquals({'__main__': { 'foo': ['bla', 'blah'] }}, parser.values())
def test_get_django_settings_encoding(self): class MySchema(Schema): foo = StringOption() expected = {'FOO': u'€'.encode(SETTINGS_ENCODING)} config = StringIO(u'[__main__]\nfoo=€'.encode(CONFIG_FILE_ENCODING)) parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values('__main__'), {'foo': u'€'}) result = get_django_settings(parser) self.assertEqual(result, expected)
def test_remove_dict_duplicates(self): """Test ListOption remove_duplicates with DictOption.""" class MyOtherSchema(Schema): foo = self.cls(item=DictOption(), remove_duplicates=True) schema = MyOtherSchema() config = StringIO("[__main__]\nfoo = bla\n bla\n[bla]\nbar = baz") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEquals({'__main__': { 'foo': [{ 'bar': 'baz' }] }}, parser.values())
def test_multi_file_dict_config(self): """Test parsing a dict option spanning multiple files.""" class MySchema(Schema): foo = DictOption(spec={ 'bar': IntOption(), 'baz': IntOption(), }, strict=True) config1 = StringIO('[__main__]\nfoo=mydict\n[mydict]\nbar=1\nbaz=1') config2 = StringIO('[mydict]\nbaz=2') expected_values = {'__main__': {'foo': {'bar': 1, 'baz': 2}}} parser = SchemaConfigParser(MySchema()) parser.readfp(config1) parser.readfp(config2) self.assertEqual(parser.values(), expected_values)
def test_default_values(self): """Test parser reads default option values.""" class MySchema(Schema): foo = BoolOption(default=True) class bar(Section): baz = IntOption() bla = StringOption(default='hello') schema = MySchema() config = StringIO("[bar]\nbaz=123") expected_values = {'__main__': {'foo': True}, 'bar': {'baz': 123, 'bla': 'hello'}} parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEquals(expected_values, parser.values()) config = StringIO("[bar]\nbla=123") expected = { '__main__': {'foo': True}, 'bar': {'baz': 0, 'bla': '123'}} parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEquals(expected, parser.values())
def test_glue_json_dict(self): class MySchema(Schema): foo = DictOption() parser = SchemaConfigParser(MySchema()) op, options, args = schemaconfigglue(parser, argv=['--foo', '{"bar": "baz"}']) self.assertEqual(options, {'foo': '{"bar": "baz"}'}) self.assertEqual(parser.values(), {'__main__': { 'foo': { 'bar': 'baz' } }})
def test_fatal_options(self): """Test parsing non-provided options marked as fatal.""" class MySchema(Schema): foo = IntOption(fatal=True) bar = IntOption() schema = MySchema() config = StringIO("[__main__]\nfoo=123") expected = {'__main__': {'foo': 123, 'bar': 0}} parser = SchemaConfigParser(schema) parser.readfp(config) self.assertEquals(expected, parser.values()) config = StringIO("[__main__]\nbar=123") parser = SchemaConfigParser(schema) parser.readfp(config) self.assertRaises(NoOptionError, parser.values)
def test_values_many_sections_different_options(self): """Test parser.values for many sections with different options.""" class MySchema(Schema): class foo(Section): bar = IntOption() class bar(Section): baz = IntOption() config = StringIO("[foo]\nbar=3\n[bar]\nbaz=4") expected_values = {'foo': {'bar': 3}, 'bar': {'baz': 4}} schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) values = parser.values() self.assertEqual(values, expected_values)
def test_parse_no_strict_with_item(self): """Test DictOption parse in non-strict mode with an item spec.""" class MySchema(Schema): foo = self.cls(item=self.cls(item=IntOption())) config = StringIO(""" [__main__] foo = mydict [mydict] bar = baz [baz] wham=42 """) expected_values = {'__main__': {'foo': {'bar': {'wham': 42}}}} parser = SchemaConfigParser(MySchema()) parser.readfp(config) self.assertEqual(parser.values(), expected_values)
def test_parse_json_raw_with_interpolation_marks(self): """Test DictOption parse json using raw=True when data has interpolation marks.""" class MySchema(Schema): class logging(Section): formatters = self.cls(raw=True, item=self.cls()) config = BytesIO(textwrap.dedent(""" [logging] formatters = {"sample": {"format": "%(name)s"}} """).encode('utf-8')) expected = {'sample': {'format': '%(name)s'}} schema = MySchema() parser = SchemaConfigParser(schema) parser.readfp(config) parsed = parser.values('logging')['formatters'] self.assertEqual(parsed, expected)