简体   繁体   中英

Tornado: AsyncTestCase failing with asyncmongo

Trying to unit test asyncmongo queries. Getting the error below upon invoking wait(). Is this because asyncmongo invokes callbacks with 2 arguments?

File "~/envs/test/local/lib/python2.7/site-packages/tornado/testing.py", line 223, in stop assert _arg is None or not kwargs

class MyTestCase2(AsyncTestCase):
    def test_async_mongo(self):
        self.db = asyncmongo.Client(pool_id='mydb', host='127.0.0.1', port=27017, maxcached=10, maxconnections=50, dbname='bench')
        self.db["ss"].insert({"a": "1"}, callback=self.stop)
        self.wait()

Yes, it's because AsyncTestCase expects functions of one argument and asyncmongo uses two. A general solution for this kind of problem is an adapter function:

self.db["ss"].insert({"a": "1"},
    callback=lambda response, error: self.stop((response, error)))
response, error = self.wait()

Also as an aside, consider using motor instead of asyncmongo - Motor works a little better with modern Tornado idioms like coroutines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM