簡體   English   中英

使用mings mim(內存中的mongo)測試mongodb

[英]Testing mongodb using mings mim (mongo in memory)

我想在ming中測試我新創建的模型,但在使模擬發生我想念的東西方面不是很成功。

該模型

    from ming import Field, schema
    from ming.declarative import Document

    bind = create_datastore('test')
    session = Session(bind)

    class Post(Document):
        class __mongometa__:
            session = session
            name = 'blog'
        _id = Field(schema.ObjectId)
        title = Field(str)
        text = Field(str)
        comments = Field([str])

考試

    from www.tests.files import intial_post
    from www.models import Post
    from www.views import post_view
    from ming import create_datastore
    import pytest

    @pytest.fixture()
    def no_requests(monkeypatch):
        bind = create_datastore('mim://localhost:27017/test')
        monkeypatch.setattr("www.model.bind", bind)

    def test_blog_view(no_requests):
        Post(intial_post).m.insert()
        post_view() == Post().m.find_one()

測試通過了,但數據不是來自內存,而是來自磁盤中的mongodb,因此Monkeypatch不會更改連接。 我可以感覺到我很親密,但同時又不知道該如何實現。

提前致謝。

要解決此問題,我們只需要使用連接到內存的新數據存儲來修補ming.Session。

from ming import create_datastore
from ming import Session


def no_requests(monkeypatch):
   memory_datastore = create_datastore('mim://localhost:27017', database='test')
   monkeypatch.setattr(Session, 'db', memory_database.db)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM