I'm writing a test module for a tornado based web application. The application uses motor as mongodb connector and I wish that my tests run on a temporary database. I am using a mocking technic on the delegate_class of the connector client as follows:
import json
import mock
import motor
import tornado.ioloop
import tornado.testing
import mongomock
import myapp
patch_motor_client = mock.patch('motor.motor_tornado.MotorClient.__delegate_class__', new=mongomock.MongoClient)
patch_motor_database = mock.patch('motor.motor_tornado.MotorDatabase.__delegate_class__', new=mock.MagicMock)
patch_motor_client.start()
patch_motor_database.start()
class TestHandlerBase(tornado.testing.AsyncHTTPTestCase):
"""
Base test handler
"""
def setUp(self):
# Create your Application for testing
self.application = myapp.app.Application()
super(TestHandlerBase, self).setUp()
def get_app(self):
return self.application
def get_new_ioloop(self):
return tornado.ioloop.IOLoop.instance()
class TestMyHandler(TestHandlerBase):
def test_post_ok(self):
"""
POST a resource is OK
"""
post_args = {
'data': 'some data here..'
}
response = self.fetch('myapi/v1/scripts', method='POST', body=json.dumps(post_args))
# assert status is 201
self.assertEqual(response.code, 201)
When I launch my tests, I'm getting this error:
File "/data/.virtualenvs/myapp/lib/python3.5/site-packages/motor/core.py", line 162, in __getitem__
return db_class(self, name)
File "/data/.virtualenvs/myapp/lib/python3.5/site-packages/motor/core.py", line 217, in __init__
client.delegate, name, **kwargs)
File "/data/.virtualenvs/myapp/lib/python3.5/site-packages/pymongo/database.py", line 102, in __init__
read_concern or client.read_concern)
File "/data/.virtualenvs/myapp/lib/python3.5/site-packages/pymongo/common.py", line 614, in __init__
raise TypeError("codec_options must be an instance of "
TypeError: codec_options must be an instance of bson.codec_options.CodecOptions
For the moment I'm not able to make it work, and I'm wondering if what I want to do is even possible with the current versions of motor (1.2.1), mongomock (3.8.0) and tornado (4.5.3), or I miss something?
Thanks for all your suggestions.