Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mongomock if app.config['TESTING'] #98

Closed
ocean1 opened this issue Jun 9, 2014 · 18 comments
Closed

add mongomock if app.config['TESTING'] #98

ocean1 opened this issue Jun 9, 2014 · 18 comments
Assignees
Labels
type: enhancement Enhancement update for old feature
Milestone

Comments

@ocean1
Copy link

ocean1 commented Jun 9, 2014

i would love to be able to have a mongomock connection if app.config['TESTING'] is true
this would allow easier testing of the flask application (otherwise an in-memory mongo like the one implemented in ming would be great)

@aviau
Copy link

aviau commented Jul 7, 2015

+1

2 similar comments
@sri85
Copy link

sri85 commented Oct 2, 2015

+1

@brunobelarmino
Copy link
Member

+1

@losintikfos
Copy link
Member

I will be looking into adding this to the milestone.

Thanks

@losintikfos losintikfos added this to the Version 0.7.6 milestone Jan 10, 2016
@losintikfos losintikfos self-assigned this Jan 10, 2016
@losintikfos losintikfos added the type: enhancement Enhancement update for old feature label Jan 10, 2016
@losintikfos
Copy link
Member

Target

  • Create a temporary mongod instance for testing purposes only. The presence of setting app.config['TESTING'] will instruct Flask-MongoEngine bootstrap to provide temporary mongod instance else a valid instance connection is established and provided instead.

@brunobelarmino
Copy link
Member

@losintikfos why not use mongomock like mongo engine?
I think the best solution is change the "_create_connection" method of flask-mongoengine to not call pymongo "uri_parser.parse_uri" method when it contains the protocol: "mongomock://" like the mongoengine team has done (MongoEngine/mongoengine@23f07fd).

The problem is the current version of mongo engine (0.10.5) doens't support it yet. But the next version (0.10.6) will.

What do you think about?

@losintikfos
Copy link
Member

@brunobelarmino so similarly to MongoEngine - check for the connection string protocol construct instead of the app.config['TESTING'] setting?

@brunobelarmino
Copy link
Member

@losintikfos IMHO it is the better solution. 😄

let me know yours.

@losintikfos
Copy link
Member

@brunobelarmino I did not patronise nor pooped your suggestion. I was enquiring for confirmation to ensure you meant avoid using setting app.config['TESTING'], but instead check for the connection string protocol construct.

Your solution might be best to you but not to others.

@brunobelarmino
Copy link
Member

Sorry whether my message seems selfish. But it isnt the intention.

I think flask-mongoengine shouldnt decide whats the best way to test apps. For example: create a mongod instance, use a existing mongod instance or use mongomock.

I think the best solution is turn flask-mongoengine flexible and working with all this approaches.

But my point is today flask-mongoengine doesnt work with mongomock. I Think when the app.config['TESTING'] is true we could by pass de pymongo uri validation. Additionally would be nice have a default approach to use when neither of the others is defined.

@losintikfos
Copy link
Member

@brunobelarmino agree +1

@lafrech
Copy link
Member

lafrech commented Jun 29, 2016

Fixed in master. @brunobelarmino, @ocean1, time to give it a try before the release ships.

@lafrech lafrech closed this as completed Jun 29, 2016
@the-jake-z
Copy link

Is there any word for adding back support for MongoMock in? I saw it was removed in 2bb06df, but for a lot of people this could be a deal breaker for using flask and mongo who want to write good unittests.

@LaundroMat
Copy link

LaundroMat commented Feb 8, 2019

Hi @jzarob - I've been able to get things to work with pytest, flask-mongoengine and mongomock by adding this to my conftest.py

@pytest.fixture
def app():
    os.environ["MONGO_DB"] = "test"
    os.environ["MONGO_URI"] = 'mongomock://localhost'

    app = create_app()
    return app

This overloads the environment settings for when flask_mongo_engine.MongoEngine(app) is called in create_app(). (Of course, I'm assuming that your create_app() gets its settings from environment variables, like mine does).

@claradaia
Copy link

Hi @LaundroMat, could you provide more details? Perhaps your create_app code?

@LaundroMat
Copy link

Well, the only relevant code in create_app() is

    app = Flask(__name__)
    app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
    csrf = CSRFProtect(app)
    app.config['MONGODB_SETTINGS'] = {
        'db': os.getenv('MONGO_DB'),
        'host': os.environ['MONGO_URI']
    }
    app.config['MONGODB_CONNECT'] = False  # open connection only on first database access
    db = MongoEngine(app)

You also need to include app as a parameter to every test function that touches the database. E.g.

def test_create_model_instance(app):    # <- by including app, you're using the conftest app and thereby overriding the normal db connection
    # your test code.

@claradaia
Copy link

Thank you, @LaundroMat! Turns out my problem was I had not imported the main 'app' nor included the fixture as a parameter like you mentioned, so the database was never configured.

@thmsklngr
Copy link

Hi,
I'm just a newbie using pytest for testing my code. Is there any full code example out there to see how a combination Flask/MongoEngine/mongomock could work? Snippets are not so meaningful to me, unfortunately.
Many thanks in advance for any answer, regards, Thomas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Enhancement update for old feature
Projects
None yet
Development

No branches or pull requests

10 participants