Ejemplo n.º 1
0
 def create_child_components():
     page.root_node.add_component(
         ComponentContainerBase(cid='child_node_0'))
     for i in range(0, 10):
         getattr(page, 'child_node_%s' % i) \
             .add_component(ComponentContainerBase(cid='child_node_%s' % (i + 1)))
         for x in range(0, 3):
             getattr(page,
                     'child_node_%s' % (i + 1)) \
                 .add_component(ComponentContainerBase(cid='child_node_%s_%s' % (i + 1, x)))
def test_show_search(page, bool_toggle):
    page.root_node = ComponentContainerBase(node_list=[
        components.PaginatedListLayout(
            cid='placeholder',
            search_placeholder='a search placeholder',
            show_search=bool_toggle),
        components.PaginatedListLayout(cid='no_placeholder',
                                       show_search=bool_toggle),
    ])

    page.handle_transaction()

    if bool_toggle:
        assert '<input class="form-control epfl-search-input"' in page.placeholder.render(
        )
        assert '<input class="form-control epfl-search-input"' in page.no_placeholder.render(
        )
        assert 'placeholder="a search placeholder"' in page.placeholder.render(
        )
        assert 'placeholder="Search..."' in page.no_placeholder.render()
    else:
        assert '<input class="form-control epfl-search-input"' not in page.placeholder.render(
        )
        assert '<input class="form-control epfl-search-input"' not in page.no_placeholder.render(
        )
        assert 'placeholder="a search placeholder"' not in page.placeholder.render(
        )
        assert 'placeholder="Search..."' not in page.no_placeholder.render()
Ejemplo n.º 3
0
def test_breadcrumb(page):
    page.root_node = ComponentContainerBase(node_list=[
        components.Link(cid='first_link', text='foobar', breadcrumb=True)
    ])
    page.handle_transaction()

    root = page.root_node
    compo = page.first_link

    assert compo.is_first(
    ), 'Link is first in container but is_first() is False.'
    assert_breadcrumb(compo)

    root.add_component(
        components.Link(cid='second_link', text='foobar', breadcrumb=True))
    assert compo.is_first(
    ), 'Link is first in container but is_first() is False.'
    assert_breadcrumb(compo)

    root.add_component(components.Link(cid='third_link',
                                       slot='foobar',
                                       text='foobar',
                                       breadcrumb=True),
                       position=0)

    assert compo.is_first(
    ), 'Link is first in container but is_first() is False.'
    assert_breadcrumb(compo)

    root.add_component(components.Link(text='foobar', breadcrumb=True),
                       position=0)
    assert not compo.is_first(
    ), 'Link is not first in container but is_first() is True.'
    assert_breadcrumb(compo)
Ejemplo n.º 4
0
    class MyPage(Page):

        root_node = ComponentContainerBase(
            cid='root_node',
            node_list=[
                ComponentContainerBase(
                    cid='container_1',
                    node_list=[ComponentBase(cid='compo_1')]),
                ComponentContainerBase(
                    cid='container_2',
                    node_list=[ComponentBase(cid='compo_2')]),
                ComponentContainerBase(
                    cid='container_3',
                    node_list=[ComponentBase(cid='compo_3')]),
                ComponentContainerBase(
                    cid='container_4',
                    node_list=[ComponentBase(cid='compo_4')]),
            ])
Ejemplo n.º 5
0
def test_component_deletion(pyramid_req):
    """Check if anything goes wrong when components are deleted.
    """

    page = Page(None, pyramid_req)
    transaction = page.transaction
    transaction['components_assigned'] = True
    transaction.set_component(
        'root_node', {
            'cid': 'root_node',
            'slot': None,
            'config': {},
            'class': (ComponentContainerBase, {}, ('root_node', None))
        })

    # Instantiate the pre generated component from the transaction.
    page.handle_transaction()

    # Add a container for our children doomed to be deleted.
    page.root_node.add_component(ComponentContainerBase(cid='child_node_0'))

    # Generate some doomed children.
    for i in range(0, 10):
        getattr(page, 'child_node_%s' % i) \
            .add_component(ComponentContainerBase(cid='child_node_%s' % (i + 1)))
        getattr(page, 'child_node_%s' % (i + 1))
        for x in range(0, 3):
            getattr(page,
                    'child_node_%s' % (i + 1)) \
                .add_component(ComponentContainerBase(cid='child_node_%s_%s' % (i + 1, x)))
            getattr(page, 'child_node_%s_%s' % (i + 1, x))

    # Chop the children off at their respective root.
    page.child_node_0.delete_component()

    # Make sure they're really dead.
    assert transaction.has_component('child_node_0') is False
    for i in range(0, 10):
        assert transaction.has_component('child_node_%s' % (i + 1)) is False
        for x in range(0, 3):
            assert transaction.has_component('child_node_%s_%s' %
                                             (i + 1, x)) is False
Ejemplo n.º 6
0
def test_component_rendering_ajax(pyramid_req):
    """Check if the rendering process generates all required AJAX scripts.
    """

    # Create a Transaction with an assigned root_node.
    page = Page(None, pyramid_req)
    page.request.is_xhr = True
    page.page_request.params = {"q": []}
    transaction = page.transaction
    transaction['components_assigned'] = True
    transaction.set_component(
        'root_node', {
            'cid': 'root_node',
            'slot': None,
            'config': {},
            'class': (ComponentContainerBase, {}, ('root_node', None))
        })

    page.handle_transaction()

    base_components = 10
    leaf_components = 200

    # Generate a nice round 210 child components.
    page.root_node.add_component(ComponentContainerBase(cid='child_node_0'))
    for i in range(0, base_components):
        getattr(page, 'child_node_%s' % i) \
            .add_component(ComponentContainerBase(cid='child_node_%s' % (i + 1)))
        for x in range(0, leaf_components):
            getattr(page,
                    'child_node_%s' % (i + 1)) \
                .add_component(ComponentContainerBase(cid='child_node_%s_%s' % (i + 1, x)))

    # Redraw and handle_ajax_events, so that all necessary output will be generated.
    page.root_node.redraw()

    page.handle_ajax_events()

    assert True not in [c.is_rendered for c in page.get_active_components()]

    # start_time = time.time()
    out = page.render()
Ejemplo n.º 7
0
def test_container_assign(pyramid_req):
    """Check if components are assigned to the proper containers.
    """

    Page.root_node = ComponentContainerBase(
        cid='root_node',
        node_list=[
            ComponentContainerBase(cid='container_1',
                                   node_list=[ComponentBase(cid='compo_1')]),
            ComponentContainerBase(cid='container_2',
                                   node_list=[ComponentBase(cid='compo_2')]),
            ComponentContainerBase(cid='container_3',
                                   node_list=[ComponentBase(cid='compo_3')]),
            ComponentContainerBase(cid='container_4',
                                   node_list=[ComponentBase(cid='compo_4')]),
        ])

    page = Page(None, pyramid_req)

    page.handle_transaction()

    # The two trailing chars have to be the same.
    for compo in page.root_node.components:
        assert compo.cid[-2:] == compo.compo_info['compo_struct'][0][-2:]
Ejemplo n.º 8
0
def container_type(request, page, component_container_type_class):
    """Generates test scenarios for ComponentContainerBase components.
    """
    # These tests are defective and cannot be fixed.
    if request.param in ['static_with_child', 'dynamic_with_child'] and \
            component_container_type_class in [components.TableLayout]:
        component_container_type_class = ComponentContainerBase

    # The child_cls to be used if one is required. If possible the components own default_child_cls is used for better
    # compatibility.
    child_cls = getattr(component_container_type_class, 'default_child_cls',
                        None)

    if child_cls is None:
        child_cls = ComponentBase
    elif isinstance(child_cls, CompoStateAttribute):
        ## XXX: why is recursive tree giving a CompoStateAttribute object as default_child_cls?
        child_cls = ComponentBase

    default_args = getattr(component_container_type_class, 'data_interface',
                           {})
    if isinstance(default_args, CompoStateAttribute):
        default_args = default_args.initial_value

    # For dynamic tests the Component will be added dynamically to a ComponentContainerBase root_node.
    root_node = ComponentContainerBase

    # For static tests the Component will be added as root_node itself.
    if request.param == 'static':
        root_node = component_container_type_class()

    # For static with child tests the Component will be added as root_node itself and receive a child_cls component in
    # its node_list.
    elif request.param == 'static_with_child':
        root_node = component_container_type_class(
            node_list=[child_cls(cid='child_compo', **default_args)])

    # For static as child tests the Component will be added as child component in the node_list of a
    # ComponentContainerBase component.
    elif request.param == 'static_as_child':
        root_node = ComponentContainerBase(
            node_list=[component_container_type_class(cid='tested_component')])

    # EPF house keeping.
    page.root_node = root_node
    page.handle_transaction()

    # For dynamic tests the Component will be added dynamically to the root_node.
    if request.param == 'dynamic':
        page.root_node.add_component(
            component_container_type_class(cid='tested_component'))

    # For dynamic with child tests the Component will be added dynamically to the root_node while containing a child_cls
    # component in its node_list.
    elif request.param == 'dynamic_with_child':
        page.root_node.add_component(
            component_container_type_class(
                cid='tested_component', node_list=[child_cls(**default_args)]))

    # Return the appropriate set of test objects. If injected as root_node the cid is forced to be root_node.
    try:
        return page, page.tested_component, page.transaction.get_component(
            'tested_component')
    except AttributeError:
        return page, page.root_node, page.transaction.get_component(
            'root_node')
Ejemplo n.º 9
0
def test_handle_ajax_events_component_events(pyramid_req):
    """ Ajax events are given as json to the page_request object. Event Type ('t') for component events is 'ce'.
    The event name 'e' is used to call the corrosponding handle function. Given event params 'p' are used. The
    component is identified by its id via cid. """

    # define a component with handler method
    class CounterComponent(ComponentBase):
        counter = 0

        def handle_increase_counter(self, counter=None):
            if counter is None:
                self.counter += 1
            else:
                self.counter = counter

    page = PageWithEventHandler(None, pyramid_req)
    page.request.is_xhr = True
    page.page_request.params = {"q": []}
    page.root_node = ComponentContainerBase(
        node_list=[CounterComponent(cid='counter_compo')])
    page.handle_transaction()

    # initial call - no events registered, so the counter is set to 0
    assert page.counter_compo.counter == 0
    page.handle_ajax_events()
    assert page.counter_compo.counter == 0

    # now add an valid event: this increases the counter
    page.page_request.params = {
        "q": [{
            u'cid': 'counter_compo',
            u'p': {},
            u'e': u'increase_counter',
            u'id': 1,
            u't': u'ce'
        }]
    }
    page.handle_ajax_events()
    assert page.counter_compo.counter == 1
    page.handle_ajax_events()
    assert page.counter_compo.counter == 2

    # next try to call an event, which has no handler,
    # note: spot the different exception, in opposite to the page test above!
    page.page_request.params = {
        "q": [{
            u'cid': 'counter_compo',
            u'p': {},
            u'e': u'foobar',
            u'id': 1,
            u't': u'ce'
        }]
    }
    with pytest.raises(MissingEventHandlerException) as excinfo:
        page.handle_ajax_events()

    # events can have params, too. check if they are used.
    page.page_request.params = {
        "q": [{
            u'cid': 'counter_compo',
            u'p': {
                u'counter': 666
            },
            u'e': u'increase_counter',
            u'id': 1,
            u't': u'ce'
        }]
    }
    page.handle_ajax_events()
    assert page.counter_compo.counter == 666

    # make sure all event handlers did not modified the counter of the page, as the test above
    assert page.counter == 0

    # now try to handle an event to an unexisting compo
    page.page_request.params = {
        "q": [{
            u'cid': 'not_existing',
            u'p': {},
            u'e': u'increase_counter',
            u'id': 1,
            u't': u'ce'
        }]
    }
    with pytest.raises(MissingEventTargetException) as excinfo:
        page.handle_ajax_events()
    assert "Target element with CID 'not_existing' for event" in str(
        excinfo.value)
Ejemplo n.º 10
0
def test_grouping(page):
    event_name = None
    if bool_toggle:
        event_name = 'test_event'

    page.root_node = ComponentContainerBase(node_list=[
        components.GroupedLinkListLayout(
            event_name=event_name,
            links=[{
                'id': i,
                'text': 'text %s' % i,
                'url': 'url_%s' % i,
                'menu_group': '...group %s...' % (i / 10)
            } for i in range(0, 100)]),
        components.GroupedLinkListLayout(event_name=event_name,
                                         links=[{
                                             'id': i,
                                             'text': 'text %s' % i,
                                             'url': 'url_%s' % i,
                                             'menu_group': None
                                         } for i in range(100, 200)]),
        components.GroupedLinkListLayout(
            cid='fucktard',
            event_name=event_name,
            use_headings=True,
            links=[{
                'id': i,
                'text': 'text %s' % i,
                'url': 'url_%s' % i,
                'menu_group': ('...group %s...' % (i / 10), (3, 7))
            } for i in range(200, 300)]),
    ])
    page.handle_transaction()

    compo1 = page.root_node.components[0]
    compo2 = page.root_node.components[1]
    compo3 = page.root_node.components[2]

    compo1.after_event_handling()
    compo2.after_event_handling()
    compo3.after_event_handling()

    if bool_toggle:
        for c in list(compo1.components) + list(compo2.components) + list(
                compo3.components):
            assert c.event_name == 'test_event'
    else:
        for c in list(compo1.components) + list(compo2.components) + list(
                compo3.components):
            assert not hasattr(c, 'event_name') or not c.event_name

    assert compo1.links == [{
        'id': c.id,
        'text': c.text,
        'url': c.url,
        'menu_group': c.menu_group
    } for c in compo1.components]
    assert compo2.links == [{
        'id': c.id,
        'text': c.text,
        'url': c.url,
        'menu_group': c.menu_group
    } for c in compo2.components]
    assert compo3.links == [{
        'id': c.id,
        'text': c.text,
        'url': c.url,
        'menu_group': c.menu_group
    } for c in compo3.components]

    for i in range(0, 10):
        assert '...group %s...' % i in compo1.render()

    for i in range(10, 20):
        assert '<a class="list-group-item col-sm-12">' not in compo2.render()

    assert '<div class="list-group-item col-sm-12 list-group-item-info">' in compo3.render(
    )
    for i in range(20, 30):
        assert '...<mark>grou</mark>p %s...' % i in compo3.render()
Ejemplo n.º 11
0
def test_min_max_value_validator(page, bool_toggle):
    ## adding a min or a max value should also add a validator for these
    min_value = None
    max_value = None
    if bool_toggle:
        min_value = 1
        max_value = 1

    page.root_node = ComponentContainerBase(node_list=[
        components.NumberInput(
            cid='min', name='inputname-min', min_value=min_value),
        components.NumberInput(
            cid='max', name='inputname-max', max_value=max_value),
        components.NumberInput(cid='minmax',
                               name='inputname-min-max',
                               min_value=min_value,
                               max_value=max_value)
    ])

    page.handle_transaction()

    if bool_toggle:
        ## there should be two validators added, where one is the one with the given min_value
        min_input = page['min']
        assert len(min_input.validators) == 2
        assert len([
            validator for validator in min_input.validators
            if validator['min_value'] == min_value
        ]) == 1

        max_input = page['max']
        assert len(max_input.validators) == 2
        assert len([
            validator for validator in max_input.validators
            if validator['max_value'] == max_value
        ]) == 1

        minmax_input = page['minmax']
        assert len(minmax_input.validators) == 2
        assert len([
            validator for validator in minmax_input.validators
            if validator['max_value'] == max_value
            and validator['min_value'] == min_value
        ]) == 1

    else:
        ## only the mandatory is added, and no one with min_value set.
        min_input = page['min']
        assert len(min_input.validators) == 1
        assert len([
            validator for validator in min_input.validators
            if validator['min_value']
        ]) == 0

        max_input = page['max']
        assert len(max_input.validators) == 1
        assert len([
            validator for validator in max_input.validators
            if validator['max_value']
        ]) == 0

        minmax_input = page['minmax']
        assert len(minmax_input.validators) == 1