Permalink
Browse files

Some more test coverage

  • Loading branch information...
1 parent cb6c178 commit e7f8429747468342377d0c2c85f41c24f4af0b0e @ajdavis ajdavis committed Dec 21, 2011
Showing with 108 additions and 0 deletions.
  1. +65 −0 test/test_command.py
  2. +24 −0 test/test_connection.py
  3. +19 −0 test/test_pooled_db.py
View
@@ -0,0 +1,65 @@
+import tornado.ioloop
+
+import test_shunt
+import asyncmongo
+
+
+class CommandTest(
+ test_shunt.MongoTest,
+ test_shunt.SynchronousMongoTest,
+):
+ mongod_options = [('--port', '27017')]
+
+ def setUp(self):
+ super(CommandTest, self).setUp()
+ self.pymongo_conn.test.foo.insert({'_id': 1})
+
+ def test_find_and_modify(self):
+ db = asyncmongo.Client(pool_id='test_query', host='127.0.0.1', port=int(self.mongod_options[0][1]), dbname='test', mincached=3)
+
+ results = []
+
+ def callback(response, error):
+ tornado.ioloop.IOLoop.instance().stop()
+ self.assert_(error is None)
+ results.append(response['value'])
+
+ before = self.get_open_cursors()
+
+ # First findAndModify creates doc with i: 2 and s: 'a'
+ db.command('findAndModify', 'foo',
+ callback=callback,
+ query={'_id': 2},
+ update={'$set': {'s': 'a'}},
+ upsert=True,
+ new=True,
+ )
+
+ tornado.ioloop.IOLoop.instance().start()
+ self.assertEqual(
+ {'_id': 2, 's': 'a'},
+ results[0]
+ )
+
+ # Second findAndModify updates doc with i: 2, sets s to 'b'
+ db.command('findAndModify', 'foo',
+ callback=callback,
+ query={'_id': 2},
+ update={'$set': {'s': 'b'}},
+ upsert=True,
+ new=True,
+ )
+
+ tornado.ioloop.IOLoop.instance().start()
+ self.assertEqual(
+ {'_id': 2, 's': 'b'},
+ results[1]
+ )
+
+ # check cursors
+ after = self.get_open_cursors()
+ assert before == after, "%d cursors left open (should be 0)" % (after - before)
+
+if __name__ == '__main__':
+ import unittest
+ unittest.main()
View
@@ -4,10 +4,34 @@
import test_shunt
import asyncmongo
+from asyncmongo.errors import DataError
TEST_TIMESTAMP = int(time.time())
class ConnectionTest(test_shunt.MongoTest):
+ def test_getitem(self):
+ db = asyncmongo.Client(pool_id='test_query', host='127.0.0.1', port=27017, dbname='test', mincached=3)
+ self.assert_(
+ repr(db['foo']) == repr(db.foo),
+ "dict-style access of a collection should be same as property access"
+ )
+
+ def test_connection(self):
+ db = asyncmongo.Client(pool_id='test_query', host='127.0.0.1', port=27017, dbname='test', mincached=3)
+ for connection_name in [
+ '.',
+ '..',
+ '.foo',
+ 'foo.',
+ '.foo.',
+ 'foo\x00'
+ '\x00foo'
+ ]:
+ self.assertRaises(
+ DataError,
+ lambda: db.connection(connection_name)
+ )
+
def test_query(self):
logging.info('in test_query')
test_shunt.setup()
View
@@ -1,6 +1,7 @@
import tornado.ioloop
import logging
import time
+from asyncmongo.errors import TooManyConnections
import test_shunt
import asyncmongo
@@ -47,3 +48,21 @@ def pool_callback2(response, error):
tornado.ioloop.IOLoop.instance().start()
test_shunt.assert_called('pool1')
test_shunt.assert_called('pool2')
+
+ def too_many_connections(self):
+ clients = [
+ asyncmongo.Client('id2', maxconnections=2, host='127.0.0.1', port=27017, dbname='test')
+ for i in range(3)
+ ]
+
+ def callback(response, error):
+ pass
+
+ for client in clients[:2]:
+ client.connection('foo').find({}, callback=callback)
+
+ self.assertRaises(
+ TooManyConnections,
+ lambda: clients[2].connection('foo').find({}, callback=callback)
+ )
+

0 comments on commit e7f8429

Please sign in to comment.