def test_schemafactory_get_unknown_build(self, mock_logging):
        schemas = DjangoSchemaFactory()
        version_string = django.get_version()
        self.assertFalse(version_string in schemas._schemas)

        # build schema
        schema = schemas.get(version_string, strict=False)()
        # get expected schema
        options = dict([(name.lower(), value) for (name, value) in
            inspect.getmembers(global_settings) if name.isupper()])
        project_options = dict([(name.lower(), value) for (name, value) in
            inspect.getmembers(get_project_settings()) if name.isupper()])
        # handle special case of ROOT_URLCONF which depends on the
        # project name
        root_urlconf = project_options['root_urlconf'].replace(
            '{{ project_name }}.', '')
        project_options['root_urlconf'] = root_urlconf

        options.update(project_options)
        expected = schemas.build(django.get_version(), options)()

        # compare schemas
        self.assert_schemas_equal(schema, expected)

        self.assertEqual(mock_logging.warn.call_args_list[0][0][0],
            "No schema registered for version '{0}'".format(version_string))
        self.assertEqual(mock_logging.warn.call_args_list[1][0][0],
            "Dynamically creating schema for version '{0}'".format(
                version_string))
    def test_schemafactory_build_complex_options(self):
        schemas = DjangoSchemaFactory()

        options = {
            'dict1': {'foo': 1, 'bar': '2'},
            'dict2': {'foo': {'bar': 2, 'baz': 3}},
            'list1': ['1', '2', '3'],
            'list2': [1, 2, 3],
        }
        expected = {
            'dict1': DictOption(name='dict1', default=options['dict1'],
                                item=StringOption()),
            'dict2': DictOption(name='dict2', default=options['dict2'],
                                item=DictOption()),
            'list1': ListOption(name='list1', default=options['list1'],
                                item=StringOption()),
            'list2': ListOption(name='list2', default=options['list2'],
                                item=IntOption()),
        }

        schema = schemas.build('foo', options)()

        sections = sorted(
            [section.name for section in schema.sections()])
        section = schema.section('django')
        self.assertEqual(sections, ['django'])

        for name in options:
            option = expected[name]
            option.section = section
            self.assertEqual(section.option(name), option)
            self.assertEqual(section.option(name).item, option.item)
Пример #3
0
    def test_schemafactory_build_django(self):
        schemas = DjangoSchemaFactory()

        schema = schemas.build()
        # get known django schema
        # fail for unknown versions of django
        django_schema = schemas.get(django.get_version(), strict=True)

        self.assert_schemas_equal(schema(), django_schema())
    def test_schemafactory_get_django(self):
        schemas = DjangoSchemaFactory()

        # build a django schema
        # get only django settings
        options = dict([(name, value) for (name, value) in
            inspect.getmembers(settings) if name.isupper()])
        schema = schemas.build(django.get_version(), options)
        # get known django schema
        # fail for unknown versions of django
        django_schema = schemas.get(django.get_version(), strict=True)

        self.assert_schemas_equal(schema(), django_schema())
    def test_schemafactory_build(self):
        schemas = DjangoSchemaFactory()

        data = [
            (IntOption, 1),
            (BoolOption, True),
            (TupleOption, (1, 2)),
            (ListOption, [1, 2]),
            (DictOption, {'a': 1}),
            (StringOption, 'foo'),
        ]

        for i, (opt_type, default) in enumerate(data):
            schema_cls = schemas.build('bogus.%d' % i, {'foo': default}, Schema)
            # do common checks
            self.assert_schema_structure(schema_cls, 'bogus.%d' % i,
                {'foo': opt_type(name='foo', default=default)})
Пример #6
0
    def test_register_without_version(self):
        class MySchema(Schema):
            pass

        schemas = DjangoSchemaFactory()
        self.assertRaises(ValueError, schemas.register, MySchema)