Esempio n. 1
0
    def test_custom_repository(self):
        class MyMemoryAggregate(Aggregate):
            pass

        reg = self._registry
        config = Configurator(registry=reg)
        config.add_memory_repository('test', aggregate_class=MyMemoryAggregate)
        repo_mgr = config.get_registered_utility(IRepositoryManager)
        repo = repo_mgr.get('test')
        config.add_resource(IFoo,
                            FooMember,
                            FooEntity,
                            collection_root_name="foos",
                            repository='test')
        self.assert_raises(RuntimeError, repo.get_collection, IFoo)
        self.assert_raises(RuntimeError, repo.get_aggregate, IFoo)
        repo.initialize()
        coll = repo.get_collection(IFoo)
        agg = coll.get_aggregate()
        self.assert_true(isinstance(agg, MyMemoryAggregate))
        entity = FooEntity(id=1)
        agg.add(entity)
        self.assert_true(agg.count() == 1)
        self.assert_equal(list(agg.iterator())[0].id, entity.id)
        self.assert_equal(agg.get_by_id(1).id, entity.id)
        self.assert_equal(agg.get_by_slug('1').slug, entity.slug)
        agg.remove(entity)
        self.assert_true(agg.count() == 0)
Esempio n. 2
0
 def test_configure_with_full_zcml(self):
     reg = self._registry
     # Check adapters.
     ent = FooEntity()
     member = object.__new__(FooMember)
     coll = object.__new__(FooCollection)
     # Make sure no adapters are in the registry.
     self.assert_is_none(
         reg.queryAdapter(coll, IMemberResource, name='member-class'))
     self.assert_is_none(
         reg.queryAdapter(member,
                          ICollectionResource,
                          name='collection-class'))
     self.assert_is_none(
         reg.queryAdapter(member, IEntity, name='entity-class'))
     self.assert_is_none(reg.queryAdapter(ent, IMemberResource))
     self.assert_is_none(
         reg.queryAdapter(coll, IRepresenter,
                          name=CsvMime.mime_type_string))
     # Load the configuration.
     config = Configurator(registry=reg, package=package)
     config.load_zcml('everest.tests.simple_app:configure.zcml')
     self.__check(reg, member, ent, coll)
     rpr = as_representer(coll, CsvMime)
     self.assert_true(isinstance(rpr, Representer))
Esempio n. 3
0
    def test_custom_repository(self, simple_config):
        class MyMemoryAggregate(Aggregate):
            pass

        reg = simple_config.registry
        config = Configurator(registry=reg)
        config.add_memory_repository('test', aggregate_class=MyMemoryAggregate)
        repo_mgr = config.get_registered_utility(IRepositoryManager)
        repo = repo_mgr.get('test')
        config.add_resource(IFoo,
                            FooMember,
                            FooEntity,
                            collection_root_name="foos",
                            repository='test')
        with pytest.raises(RuntimeError):
            repo.get_collection(IFoo)
        with pytest.raises(RuntimeError):
            repo.get_aggregate(IFoo)
        repo.initialize()
        coll = repo.get_collection(IFoo)
        agg = coll.get_aggregate()
        assert isinstance(agg, MyMemoryAggregate)
        entity = FooEntity(id=1)
        agg.add(entity)
        assert agg.count() == 1
        assert list(agg.iterator())[0].id == entity.id
        assert agg.get_by_id(1).id == entity.id
        assert agg.get_by_slug('1').slug == entity.slug
        agg.remove(entity)
        assert agg.count() == 0
Esempio n. 4
0
def simple_config(request):
    set_up_testing()
    reg = get_current_registry()
    config = Configurator(registry=reg, package=package)
    config.setup_registry()
    request.addfinalizer(tear_down_testing)
    return config
Esempio n. 5
0
def app_factory(global_settings, **local_settings):  # pylint: disable=W0613
    config = Configurator()
    config.setup_registry(settings=local_settings, root_factory=RootFactory())
    #    reg = get_current_registry()
    #    repo_mgr = reg.getUtility(IRepositoryManager)
    #    repo_mgr.initialize_all()
    return config.make_wsgi_app()
Esempio n. 6
0
 def set_up(self):
     super(BaseTestCaseWithConfiguration, self).set_up()
     # Create and configure a new testing registry.
     reg = Registry('testing')
     self.config = Configurator(registry=reg,
                                package=self.package_name)
     if not self.ini_section_name is None:
         settings = self.ini.get_settings(self.ini_section_name)
     else:
         try:
             settings = self.ini.get_settings('DEFAULT')
         except configparser.NoSectionError:
             settings = None
     self.config.setup_registry(settings=settings)
     if not self.package_name is None:
         if not settings is None:
             cfg_zcml = settings.get('configure_zcml',
                                     self.config_file_name)
         else:
             cfg_zcml = self.config_file_name
         self.config.begin()
         try:
             self.config.load_zcml(cfg_zcml)
         finally:
             self.config.end()
Esempio n. 7
0
 def after(self):
     # Register resources eagerly so the various adapters and utilities are
     # available for other directives.
     discriminator = ('resource', self.interface)
     reg = get_current_registry()
     config = Configurator(reg, package=self.context.package)
     config.add_resource(self.interface,
                         self.member,
                         self.entity,
                         collection=self.collection,
                         collection_root_name=self.collection_root_name,
                         collection_title=self.collection_title,
                         repository=self.repository,
                         expose=self.expose,
                         _info=self.context.info)
     for key, value in self.representers.iteritems():
         cnt_type, rc_kind = key
         opts, mp_opts = value
         if rc_kind == RESOURCE_KINDS.member:
             rc = get_member_class(self.interface)
         elif rc_kind == RESOURCE_KINDS.collection:
             rc = get_collection_class(self.interface)
         else:  # None
             rc = self.interface
         discriminator = ('resource_representer', rc, cnt_type, rc_kind)
         self.action(
             discriminator=discriminator,  # pylint: disable=E1101
             callable=config.add_resource_representer,
             args=(rc, cnt_type),
             kw=dict(options=opts,
                     attribute_options=mp_opts,
                     _info=self.context.info),
         )
Esempio n. 8
0
 def set_up(self):
     testing_set_up()
     reg = self._registry = get_current_registry()
     self._config = Configurator(registry=reg, package=package)
     self._config.setup_registry()
     repo_mgr = self._config.get_registered_utility(IRepositoryManager)
     repo_mgr.initialize_all()
Esempio n. 9
0
 def set_up(self):
     super(FunctionalTestCase, self).set_up()
     # Create the WSGI application and set up a configurator.
     wsgiapp = self._load_wsgiapp()
     self.config = Configurator(registry=wsgiapp.registry,
                                package=self.package_name)
     self.config.begin()
     self.app = TestApp(wsgiapp,
                        extra_environ=self._create_extra_environment())
Esempio n. 10
0
 def set_up(self):
     super(BaseTestCaseWithConfiguration, self).set_up()
     # Create and configure a new testing registry.
     reg = Registry('testing')
     self.config = Configurator(registry=reg, package=self.package_name)
     if not self.ini_section_name is None:
         settings = self.ini.get_settings(self.ini_section_name)
         self.config.setup_registry(settings=settings)
     else:
         self.config.setup_registry()
Esempio n. 11
0
 def __create(cls, ini):
     reg = Registry('testing')
     conf = Configurator(registry=reg, package=ini.package_name)
     conf.setup_registry(settings=ini.settings)
     if not ini.config_file_name is None and not ini.package_name is None:
         conf.begin()
         try:
             conf.load_zcml(ini.config_file_name)
         finally:
             conf.end()
     return conf
Esempio n. 12
0
 def after(self):
     discriminator = \
         ('representer',
          self.content_type or self.representer_class.content_type)
     self.action(discriminator=discriminator)  # pylint: disable=E1101
     # Representers are created eagerly so the resource declarations can use
     # them.
     reg = get_current_registry()
     config = Configurator(reg, package=self.context.package)
     config.add_representer(content_type=self.content_type,
                            representer_class=self.representer_class,
                            options=self.options)
Esempio n. 13
0
File: run.py Progetto: papagr/TheLMA
def create_config(settings, package='thelma', registry=None):
    """
    Returns a configurator for TheLMA.
    """
    config = Configurator(package=package, registry=registry)
    if registry is None:
        config.setup_registry(settings=settings, root_factory=RootFactory())
        config.load_zcml('configure.zcml')
    # tractor registration
    tractor_config_file = settings['tractor_config_file']
    tractor_api = make_api_from_config(tractor_config_file)
    config.registry.registerUtility(tractor_api, ITractor)  # pylint: disable=E1103
    return config
Esempio n. 14
0
def _repository(_context, name, make_default, agg_cls, repo_cls, repo_type,
                config_method, cnf):
    # Repository directives are applied eagerly. Note that custom repositories
    # must be declared *before* they can be referenced in resource directives.
    discriminator = (repo_type, name)
    _context.action(discriminator=discriminator)
    reg = get_current_registry()
    config = Configurator(reg, package=_context.package)
    method = getattr(config, config_method)
    method(name,
           aggregate_class=agg_cls,
           repository_class=repo_cls,
           configuration=cnf,
           make_default=make_default)
Esempio n. 15
0
def _resource_view(_context, for_, default_content_type,
                   default_response_content_type, config_callable_name, kw):
    reg = get_current_registry()
    config = Configurator(reg, package=_context.package)
    config_callable = getattr(config, config_callable_name)
    option_tuples = tuple(sorted([(k, str(v)) for (k, v) in kw.items()]))
    kw['default_content_type'] = default_content_type
    kw['default_response_content_type'] = default_response_content_type
    for rc in for_:
        discriminator = ('resource_view', rc, config_callable_name) \
                        + option_tuples
        _context.action(
            discriminator=discriminator,  # pylint: disable=E1101
            callable=config_callable,
            args=(rc, ),
            kw=kw)
Esempio n. 16
0
def messaging(_context, repository, reset_on_start=False):
    """
    Directive for setting up the user message resource in the appropriate
    repository.
    
    :param str repository: The repository to create the user messages resource
      in.
    """
    discriminator = ('messaging', repository)
    reg = get_current_registry()
    config = Configurator(reg, package=_context.package)
    _context.action(
        discriminator=discriminator,  # pylint: disable=E1101
        callable=config.setup_system_repository,
        args=(repository, ),
        kw=dict(reset_on_start=reset_on_start))
Esempio n. 17
0
def app_factory(global_settings, **local_settings):  # pylint: disable=W0613
    """
    Default factory for creating a WSGI application using the everest
    configurator and root factory.

    :param dict global_settings: Global settings extracted from an ini file.
        Not used in this default app factory.
    :param dict local_settings: App settings extracted from an ini file.
    """
    config = Configurator()
    config.setup_registry(settings=local_settings, root_factory=RootFactory())
    if 'configure_zcml' in local_settings:
        config.load_zcml(local_settings['configure_zcml'])
    app = config.make_wsgi_app()
    # In the absence of an application name in the settings, we have to
    # extract the main app's name from the ini file, which unfortunately
    # means parsing it again.
    app_name = app_name_from_ini_file(global_settings['__file__'])
    ep_group = "%s.plugins" % app_name
    plugin_mgr = config.get_registered_utility(IPluginManager)
    plugin_mgr.load_all(ep_group)
    return app
Esempio n. 18
0
def app_factory(global_settings, **local_settings):  # pylint: disable=W0613
    config = Configurator()
    config.setup_registry(settings=local_settings, root_factory=RootFactory())
    return config.make_wsgi_app()
Esempio n. 19
0
 def __init__(self, app, package_name, **kw):
     TestApp.__init__(self, app, **kw)
     self.__config = Configurator(registry=app.registry,
                                  package=package_name)
Esempio n. 20
0
 def set_up(self):
     testing_set_up()
     reg = self._registry = get_current_registry()
     self._config = Configurator(registry=reg, package=package)
     self._config.setup_registry()