首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有mongomock的py.test中的self._cursor.next()异常

带有mongomock的py.test中的self._cursor.next()异常
EN

Stack Overflow用户
提问于 2016-05-18 10:54:23
回答 1查看 245关注 0票数 1

当使用mongodb后端为我的Django rest framework API运行py.test时,我遇到了一个问题,而视图在实际操作中工作。

如果我启动服务器,API就会按预期工作。只有在运行测试时,才会因为self._cursor是一个列表而不是迭代器而失败。

我想知道这是否与pymongo的mongomock和Django rest框架分页有关。

以下是设置:

settings_test.py

代码语言:javascript
复制
# override the mongodb with mocks for pytest
mongoengine.register_connection(
    'default',
    name='testing',
    host='mongomock://localhost',
    # let datetime in pymongo/mongoengine with timezone information
    tz_aware=True)

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
    }
}

正在测试中的视图

代码语言:javascript
复制
from rest_framework_mongoengine.generics import ListAPIView
class ListPropertyApi(ListAPIView):
    queryset = PropertyObject.objects.all()
    serializer_class = PropertySerializer

最热的

代码语言:javascript
复制
class TestListPropertyApi:
    def test_lonlat_query(self, rf):
        request = rf.get(
            u'/property/list/')
        view = views.ListPropertyApi.as_view()
        response = view(request)

错误消息

代码语言:javascript
复制
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py:58: in wrapped_view
    return view_func(*args, **kwargs)
/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py:68: in view
    return self.dispatch(request, *args, **kwargs)
/usr/local/lib/python2.7/dist-packages/rest_framework/views.py:466: in dispatch
    response = self.handle_exception(exc)
/usr/local/lib/python2.7/dist-packages/rest_framework/views.py:463: in dispatch
    response = handler(request, *args, **kwargs)
share/cache.py:96: in get
    return decorated_get(self, request, *args, **kwargs)
lib/rest_framework_mongoengine/generics.py:77: in get
    return self.list(request, *args, **kwargs)
/usr/local/lib/python2.7/dist-packages/rest_framework/mixins.py:42: in list
    page = self.paginate_queryset(queryset)
/usr/local/lib/python2.7/dist-packages/rest_framework/generics.py:172: in paginate_queryset
    return self.paginator.paginate_queryset(queryset, self.request, view=self)
/usr/local/lib/python2.7/dist-packages/rest_framework/pagination.py:448: in paginate_queryset
    results = list(queryset[offset:offset + self.page_size + 1])
/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/queryset.py:80: in _iter_results
    self._populate_cache()
/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/queryset.py:92: in _populate_cache
    self._result_cache.append(self.next())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = .. queryset mid-iteration ..

    def next(self):
        """Wrap the result in a :class:`~mongoengine.Document` object.
            """
        if self._limit == 0 or self._none:
            raise StopIteration

>       raw_doc = self._cursor.next()
E       AttributeError: 'list' object has no attribute 'next'

/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py:1407: AttributeError

环境

代码语言:javascript
复制
mongoengine==0.10.6
mongomock==3.3.0
pymongo==3.2.2
djangorestframework==3.3.3
pytest-django==2.9.1
Django==1.9.4
EN

回答 1

Stack Overflow用户

发布于 2016-06-08 00:14:06

我也遇到过类似的问题,通过遵循这个问题https://github.com/vmalloc/mongomock/issues/130就可以很容易地修复它

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37289456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档