Skip to content
This repository has been archived by the owner on Nov 5, 2019. It is now read-only.

Commit

Permalink
Some more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdavis committed Dec 23, 2011
1 parent cb6c178 commit e7f8429
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
65 changes: 65 additions & 0 deletions test/test_command.py
Original file line number Diff line number Diff line change
@@ -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()
24 changes: 24 additions & 0 deletions test/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 19 additions & 0 deletions test/test_pooled_db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import tornado.ioloop
import logging
import time
from asyncmongo.errors import TooManyConnections

import test_shunt
import asyncmongo
Expand Down Expand Up @@ -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.