def test_get_context_data(self, per_page, error_out, name, kwargs):
        paginated = [Mock(), Mock(), Mock()]

        instance = sqlalchemy.MultipleObjectMixin()
        instance.object_list = query = mock_query()
        instance.apply_pagination = pagination = Mock(return_value=paginated)
        instance.get_context_object_name = Mock(return_value=name or None)
        instance.get_per_page = Mock(return_value=per_page)
        instance.get_error_out = Mock(return_value=error_out)

        context = kwargs.copy()

        if per_page > 0:
            context.setdefault('pagination', paginated[0])
            context.setdefault('object_list', paginated[1])
            context.setdefault('is_paginated', paginated[2])
            if name:
                context.setdefault(name, paginated[1])
        else:
            context.setdefault('pagination', None)
            context.setdefault('object_list', query.all.return_value)
            context.setdefault('is_paginated', False)
            if name:
                context.setdefault(name, query.all.return_value)

        with patch.object(core.ContextMixin, 'get_context_data') as m:
            assert instance.get_context_data(**kwargs) == m.return_value

            m.assert_called_once_with(**context)

            if per_page > 0:
                pagination.assert_called_once_with(query, per_page, error_out)

        instance.get_context_object_name.assert_called_once_with()
    def test_get_query(self, model, query, order_by):
        order_by = [Mock() for x in range(order_by)]

        instance = sqlalchemy.MultipleObjectMixin()
        instance.get_order_by = Mock(return_value=order_by)

        if query:
            instance.query = Mock()

        if model:
            instance.model = Mock()

        if not model and not query:
            with pytest.raises(NotImplementedError) as excinfo:
                instance.get_query()

            error = ('MultipleObjectMixin requires either a definition of '
                     "'query', 'model', or an implementation of 'get_query()'")

            assert excinfo.value.args[0] == error
        else:
            result = instance.get_query()

            if query:
                expected = instance.query
            else:
                expected = instance.model.query

            if order_by:
                expected.order_by.assert_called_once_with(*order_by)
                expected = expected.order_by.return_value

            assert result == expected
    def test_apply_pagination(self, page, per_page, total, error_out):
        pages = int(ceil(total / float(per_page)))  # number of pages
        count = max(min(total - per_page * (page - 1), per_page), 0)

        instance = sqlalchemy.MultipleObjectMixin()
        instance.get_page = Mock(return_value=page)
        instance.get_pagination = Mock()

        pagination = instance.get_pagination.return_value
        pagination.pages = pages

        items = [object() for v in range(count)]

        object_list = mock_query()
        object_list_limit = object_list.limit
        object_list_offset = object_list_limit.return_value.offset
        object_list_all = object_list_offset.return_value.all
        object_list_order_by = object_list.order_by
        object_list_count = object_list_order_by.return_value.count

        object_list_all.return_value = items
        object_list_count.return_value = total

        offset = (page - 1) * per_page

        if count == 0 and page != 1 and error_out:
            with pytest.raises(HTTPException) as excinfo:
                instance.apply_pagination(object_list, per_page, error_out)

            assert excinfo.value.code == 404
        else:
            result = instance.apply_pagination(
                object_list, per_page, error_out)

            assert result == (pagination, pagination.items, pages > 1)

            instance.get_pagination.assert_called_once_with(object_list, page,
                                                            per_page, total,
                                                            items)

        object_list_limit.assert_called_once_with(per_page)
        object_list_offset.assert_called_once_with(offset)
        object_list_all.assert_called_once_with()

        instance.get_page.assert_called_once_with(error_out)

        if object_list_count.called:
            object_list_order_by.assert_called_once_with(None)
    def test_get_pagination(self):
        instance = sqlalchemy.MultipleObjectMixin()
        instance.pagination_class = Mock()
        query = Mock()
        page = Mock()
        per_page = Mock()
        total = Mock()
        items = Mock()

        result = instance.get_pagination(
            query, page, per_page, total, items)

        assert result == instance.pagination_class.return_value

        instance.pagination_class.assert_called_once_with(query, page,
                                                          per_page, total,
                                                          items)
    def test_get_model(self, query, model):
        instance = sqlalchemy.MultipleObjectMixin()

        if query:
            instance.query = mock_query()

        if model:
            instance.model = Mock()

        cls = instance.get_model()

        if query:
            assert cls == instance.query._entities[0].entity_zero.class_
        elif model:
            assert cls == instance.model
        else:
            assert cls is None
    def test_get_context_object_name(self, name, class_name):
        instance = sqlalchemy.MultipleObjectMixin()
        instance.get_model = Mock(return_value=None)

        if name:
            instance.context_object_name = Mock()

        if class_name:
            instance.get_model.return_value = type(class_name, (object,), {})

        result = instance.get_context_object_name()

        if name:
            assert result == instance.context_object_name
        elif class_name:
            assert result == '{0}_list'.format(underscore(class_name))
        else:
            assert result is None
    def test_get_page(self, page_arg, view_arg_value, arg_value, error_out):
        instance = sqlalchemy.MultipleObjectMixin()
        instance.page_arg = page_arg

        with patch.object(sqlalchemy, 'request') as m1:
            if view_arg_value is not None:
                m1.view_args = {page_arg: view_arg_value}
            else:
                m1.view_args = {}

            if arg_value is not None:
                m1.args = {page_arg: arg_value}
            else:
                m1.args = {}

            if view_arg_value is not None:
                expected = view_arg_value
            elif arg_value is not None:
                expected = arg_value
            else:
                expected = 1

            try:
                expected = int(expected)
            except ValueError:
                pass

            print(repr(expected))

            if error_out and (not isinstance(expected, integer_types) or
                              expected < 1):
                with pytest.raises(HTTPException) as excinfo:
                    instance.get_page(error_out)

                assert excinfo.value.code == 404
            else:
                if not isinstance(expected, integer_types) or expected < 1:
                    expected = 1

                assert instance.get_page(error_out) == expected
    def test_get_per_page(self):
        instance = sqlalchemy.MultipleObjectMixin()
        instance.per_page = Mock()

        assert instance.get_per_page() == instance.per_page
    def test_get_error_out(self):
        instance = sqlalchemy.MultipleObjectMixin()
        instance.error_out = Mock()

        assert instance.get_error_out() == instance.error_out
    def test_get_order_by(self):
        instance = sqlalchemy.MultipleObjectMixin()
        instance.order_by = Mock()

        assert instance.get_order_by() == instance.order_by