Question

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.

No correct solution

OTHER TIPS

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top