Example #1
0
def test_the_lifecycle_of_a_ui(web_fixture):
    """This test illustrates the steps a UserInterface goes through from being specified, to
       being used. It tests a couple of lower-level implementation issues (see comments)."""
    def current_view_is_plugged_in(page):
        return page.slot_contents['main_slot'].__class__ is Div

    @stubclass(UserInterface)
    class UserInterfaceStub(UserInterface):
        assembled = False

        def assemble(self, **ui_arguments):
            self.controller_at_assemble_time = self.controller
            root = self.define_view('/some/path', title='A view')
            root.set_slot('slotA', Div.factory())
            self.assembled = True

    fixture = web_fixture

    # Phase1: specifying a user_interface and assembleing it to a site (with kwargs)
    parent_ui = None
    #        parent_ui = EmptyStub(base_path='/')
    slot_map = {'slotA': 'main_slot'}
    ui_factory = UserInterfaceFactory(parent_ui, RegexPath('/', '/', {}),
                                      slot_map, UserInterfaceStub, 'test_ui')

    # Phase2: creating a user_interface
    request = Request.blank('/some/path')
    fixture.context.request = request
    user_interface = ui_factory.create('/some/path', for_bookmark=False)

    # - Assembly happened correctly
    assert user_interface.parent_ui is parent_ui
    assert user_interface.slot_map is slot_map
    assert user_interface.name is 'test_ui'
    assert user_interface.relative_base_path == '/'
    assert user_interface.controller_at_assemble_time is not None
    assert user_interface.controller is not None
    assert user_interface.assembled

    # - Create for_bookmark empties the relative_path
    user_interface = ui_factory.create('/some/path', for_bookmark=True)
    assert user_interface.relative_path == ''

    # - The relative_path is reset if not done for_bookmark
    #   This is done in case a for_bookmark call just
    #   happened to be done for the same UserInterface in the same request
    #   before the "real" caal is done
    user_interface = ui_factory.create('/some/path', for_bookmark=False)
    assert user_interface.relative_path == '/some/path'

    # Phase5: create the page and plug the view into it
    page = Widget.factory().create(user_interface.current_view)
    page.add_child(Slot(user_interface.current_view, 'main_slot'))

    page.plug_in(user_interface.current_view)
    assert current_view_is_plugged_in(page)
    assert isinstance(user_interface.sub_resources, FactoryDict)
Example #2
0
    def factory_from_path_regex(self, fixture):
        """An FactoryFromUrlRegex parses args from the given URL and passes these as kwargs to create_method 
           along with the args and kwargs passed to its .create()."""

        def create_method(path, path_arg=None, extra_kwarg=None):
            instance = EmptyStub()
            instance.path_arg = path_arg
            instance.extra_kwarg = extra_kwarg
            return instance

        argument_fields = {'path_arg': Field()}
        factory = FactoryFromUrlRegex(RegexPath('some(?P<path_arg>.+)path', 'some${path_arg}path', argument_fields), create_method, 
                                      dict(extra_kwarg='42'))
        instance = factory.create('somecoolpath')
        vassert( instance.path_arg == 'cool' )
        vassert( instance.extra_kwarg == '42' )