使用 mings mim(内存中的 mongo)测试 mongodb



我想在明中测试我新创建的模型,但在模拟发生方面并不是很成功,我错过了什么。

模型

    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。与连接到内存的新数据存储进行会话。

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)

最新更新