def test_get_object_neither(self):
        instance = sqlalchemy.SingleObjectMixin()
        instance.query = mock_query()

        with patch.object(sqlalchemy, 'request') as m:
            m.view_args = {}

            with pytest.raises(RuntimeError) as excinfo:
                instance.get_object()

        error = ('SingleObjectMixin must be called with either object pk or '
                 'slug')

        assert excinfo.value.args[0] == error
    def test_get_model(self, query, model):
        instance = sqlalchemy.SingleObjectMixin()
        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.SingleObjectMixin()
        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 == underscore(class_name)
        else:
            assert result is None
    def test_get_context_data(self, obj, context_object_name, kwargs):
        instance = sqlalchemy.SingleObjectMixin()

        context = kwargs.copy()

        if obj:
            instance.object = Mock()
            context.setdefault('object', instance.object)

            if context_object_name:
                context.setdefault(context_object_name, instance.object)

        with patch.object(core.ContextMixin, 'get_context_data') as m1:
            with patch.object(instance, 'get_context_object_name') as m2:
                m2.return_value = context_object_name or None

                result = instance.get_context_data(**kwargs)

                m1.assert_called_once_with(**context)

                assert result == m1.return_value
    def test_get_query(self, model, query):
        instance = sqlalchemy.SingleObjectMixin()
        if model:
            instance.model = Mock()

        if query:
            instance.query = Mock()

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

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

            assert excinfo.value.args[0] == error

        elif query:
            assert instance.get_query() == instance.query
        else:
            assert instance.get_query() == instance.model.query
    def test_get_object_slug(self, slug_view_arg, slug_field, slug, success):
        instance = sqlalchemy.SingleObjectMixin()
        instance.query = query = mock_query(success)
        instance.slug_view_arg = slug_view_arg

        instance.get_slug_field = Mock(return_value=slug_field)

        return_value = query.filter_by.return_value.one.return_value

        with patch.object(sqlalchemy, 'request') as m:
            m.view_args = {slug_view_arg: slug}

            if not success:
                with pytest.raises(HTTPException) as excinfo:
                    instance.get_object()

                assert excinfo.value.code == 404
            else:
                assert instance.get_object() == return_value

        query.filter_by.assert_called_once_with(**{slug_field: slug})
    def test_get_object_pk(self, pk_view_arg, fields, pk, success):
        instance = sqlalchemy.SingleObjectMixin()
        instance.pk_view_arg = pk_view_arg
        instance.query = query = mock_query(success)
        instance.get_model = Mock()

        return_value = query.filter_by.return_value.one.return_value

        primary_key = [Column(key=name) for name in fields]

        with patch.object(sqlalchemy, 'inspect') as m1:
            with patch.object(sqlalchemy, 'request') as m2:
                m1.return_value.primary_key = primary_key
                m2.view_args = {pk_view_arg: pk}

                if len(fields) == 1:
                    if not success:
                        with pytest.raises(HTTPException) as excinfo:
                            instance.get_object()

                        assert excinfo.value.code == 404
                    else:
                        assert instance.get_object() == return_value

                    query.filter_by.assert_called_once_with(**{fields[0]: pk})
                else:
                    with pytest.raises(RuntimeError) as excinfo:
                        instance.get_object()

                    if len(fields) > 1:
                        error = ('SingleObjectMixin requires non composite '
                                 'primary key')
                    else:
                        error = ('SingleObjectMixin requires primary key')

                    assert excinfo.value.args[0] == error

            m1.assert_called_once_with(instance.get_model.return_value)
            instance.get_model.assert_called_once_with()
    def test_get_object(self, pk, slug, fields, query_pk_and_slug):
        instance = sqlalchemy.SingleObjectMixin()
        instance.slug_field = fields[0]
        instance.query_pk_and_slug = query_pk_and_slug

        query = mock_query()

        instance.query = query

        rv = query.filter_by.return_value.one.return_value

        with patch.object(sqlalchemy, 'inspect') as m1:
            with patch.object(sqlalchemy, 'request') as m2:
                m1.return_value.primary_key = [Column(name=fields[1])]
                m2.view_args = {instance.pk_view_arg: pk,
                                instance.slug_view_arg: slug}

                assert instance.get_object() == rv

        if query_pk_and_slug:
            query.filter_by.assert_called_once_with(**{fields[0]: slug,
                                                       fields[1]: pk})
        else:
            query.filter_by.assert_called_once_with(**{fields[1]: pk})
    def test_get_slug_field(self, slug):
        instance = sqlalchemy.SingleObjectMixin()
        instance.slug_field = slug

        assert instance.get_slug_field() == slug