mongoengine connect() in settings.py testing problem

后端 未结 4 552
忘掉有多难
忘掉有多难 2021-02-06 04:29

I want to be able to do conditional connect() based on either I started django in testing mode or not.

in my settings.py I use mongoengine connect() method to connect to

相关标签:
4条回答
  • 2021-02-06 05:06

    What I do is use register_connection, and then mock the connections on test.

    In the file that I define the Mongo Documents I have this:

    import mongoengine
    from django.conf import settings
    
    mongoengine.register_connection(
        'default', settings.MONGOENGINE_DB, **settings.MONGOENGINE_CONNECTION)
    

    Then in the tests I use the mock library to change the behave of connections (it would be possible too to mock one of the functions on the connection sub module like get_db) like this:

    connections = patch.dict(
        mongoengine.connection._connections, {'default': None})
    
    dbs = patch.dict(
        mongoengine.connection._dbs, {'default': {
            'your_collection': None,
            'another_collection': None,
            }})
    
    dbs.start()
    connections.start()
    
    insert = patch.object(mongoengine.queryset.QuerySet, 'insert')
    insert_mock = insert.start()
    
    ...
    
    insert_mock.assert_called_once(...)
    
    0 讨论(0)
  • 2021-02-06 05:22

    I'm solving this with a custom test runner. Here is an example I based my solution off of: https://github.com/xintron/django-mongorunner/blob/master/mongorunner/testrunner.py

    This has the advantage of providing a fresh database for each of your unit tests.

    class MyTestRunner(DjangoTestSuiteRunner):
    
        mongodb_name = 'testsuite'
    
        def setup_databases(self, **kwargs):
            from mongoengine.connection import connect, disconnect
            disconnect()
            connect(self.mongodb_name)
            print 'Creating mongo test-database ' + self.mongodb_name
            return super(MyTestRunner, self).setup_databases(**kwargs)
    
        def teardown_databases(self, old_config, **kwargs):
            from mongoengine.connection import get_connection, disconnect
            connection = get_connection()
            connection.drop_database(self.mongodb_name)
            print 'Dropping mongo test-database: ' + self.mongodb_name
            disconnect()
            super(MyTestRunner, self).teardown_databases(old_config, **kwargs)
    
    0 讨论(0)
  • 2021-02-06 05:22

    I'm not sure it's completely foolproof, but I use the fact that in a test, you will have probably started it from the command line with ./manage.py test, so 'test' is one of the command-line args. So this works:

    import sys
    if 'test' not in sys.argv:
        mongodb.connect()
    
    0 讨论(0)
  • 2021-02-06 05:25

    While it is possible to do that, it is easier and common practice to have 2 settings files. One possible configuration could be:

    You have 2 settings files, lsettings.py that doesn't connect and settings.py that does

    from lsettings import *
    mongodb.connect()
    

    So, while locally testing you can:

    python manage.py test --settings=lsettings
    

    And it doesn't connect.

    tl;dr: It is easier to manage configuration differences by having multiple configuration files that import each other conditionally rather than trying to have conditional parameters within the same settings file. YMMV.

    0 讨论(0)
提交回复
热议问题