【问题标题】:Testing mongodb using mings mim (mongo in memory)使用 mings mim (mongo in memory) 测试 mongodb
【发布时间】:2014-02-09 00:29:02
【问题描述】:

我想在 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 不会更改连接。我能感觉到我已经很接近了,但同时又不知道让它发生。

提前致谢。

【问题讨论】:

    标签: python pymongo monkeypatching pytest ming


    【解决方案1】:

    要解决这个问题,我们只需要用连接到内存的新数据存储来修补 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 2021-02-01
      • 2021-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多