문제

I want to test my new created models in ming but haven’t been very successful at making the mock happen what am I missing.

The model

    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])

The Test

    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()

The tests pass but the data does not come from memory it comes from the mongodb in the disk so the monkeypatch is not changing the connection. I can feel that I'm close but at the same time have no idea make it happen.

Thanks in advance.

올바른 솔루션이 없습니다

다른 팁

To resolve this problem we just need to patch the ming.Session with a new datastore that is connected to memory.

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top