2016-05-18 10 views
1

ビューが実際の動作で動作している間、mongodbバックエンドを使用して私のDjangoレストフレームワークAPIのpy.testを実行するときに問題が発生しました。py.test mongomockでの例外

サーバーを起動すると、APIが正常に動作します。テストを実行しているときのみ、イテレータではなくリストであるself._cursorが失敗します。

pymongoのmongomockとDjango rest frameworkのページ付けと関係があるのだろうかと思います。ここで

は、セットアップは、次のとおりです。

settings_test.py

# 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', 
    } 
} 

ビュー

from rest_framework_mongoengine.generics import ListAPIView 
class ListPropertyApi(ListAPIView): 
    queryset = PropertyObject.objects.all() 
    serializer_class = PropertySerializer 

pytest

class TestListPropertyApi: 
    def test_lonlat_query(self, rf): 
     request = rf.get(
      u'/property/list/') 
     view = views.ListPropertyApi.as_view() 
     response = view(request) 
テストの下のエラーメッセージ
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/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 

環境

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 

答えて

1

私は同様の問題を持っていた、それが唯一の解決策は、クローンとmongomockを変更しているように見えます。この問題にhttps://github.com/vmalloc/mongomock/issues/130

+0

に従うことによって解決することは容易になりました。なぜこの修正プログラムがリリースに入ることがないのだろうか?私は可能な限り私自身の枝を維持しないことを望んでいます。 (私は3.4にアップグレードしましたが、まだ修正されていません) – Andy

関連する問題