def test_not_raise_when_database_was_initiated(self): """[DatabaseTestCase] - Not raises ValueError when connect to inititated database""" database1 = Database.connect("localhost:27027", dbname="test") database2 = Database.connect("localhost:27027", dbname="test") database1.should.be.equal(database2)
def test_raises_erro_when_use_collection_with_not_connected_database(self): """[CollectionTestCase] - Raises DatabaseError when use collection with a not connected database""" class CollectionTest(Collection): __collection__ = 'collection_test' Database.disconnect() CollectionTest().save.when.called_with(callback=None) \ .throw(DatabaseError, 'you must be connect') Database.connect(["localhost:27027", "localhost:27028"], dbname='test')
def test_raises_error_when_mode_is_secondary_and_secondary_is_down(self): """[ReplicaSetTestCase] - Raise error when mode is secondary and secondary is down""" os.system('make mongo-kill > /dev/null 2>&1') os.system('make mongo-start-node1') os.system('make mongo-start-arbiter') time.sleep(2) Database.connect(["localhost:27027", "localhost:27028"], dbname='test') Database().send_message.when.called_with('', read_preference=ReadPreference.SECONDARY)\ .throw(DatabaseError) os.system('make mongo-start-node2') time.sleep(10)
def test_raises_error_when_could_not_find_node(self): """[DatabaseTestCase] - Raises DatabaseError when could not find valid nodes""" database = Database.connect(["localhost:27030"], dbname="test") database.send_message.when.called_with("", callback=None).throw( DatabaseError, "could not find an available node" )
def test_send_test_message(self): """[DatabaseTestCase] - Send a test message to database""" Database.connect(["localhost:27027", "localhost:27028"], dbname="test") object_id = ObjectId() message_test = message.query(0, "mongotor_test.$cmd", 0, 1, {"driverOIDTest": object_id}) Database().send_message(message_test, callback=self.stop) response, error = self.wait() result = response["data"][0] result["oid"].should.be(object_id) result["ok"].should.be(1.0) result["str"].should.be(str(object_id))
def test_configure_nodes(self): """[ReplicaSetTestCase] - Configure nodes""" Database.connect(["localhost:27027", "localhost:27028"], dbname='test') master_node = ReadPreference.select_primary_node(Database()._nodes) secondary_node = ReadPreference.select_node(Database()._nodes, mode=ReadPreference.SECONDARY) master_node.host.should.be('localhost') master_node.port.should.be(27027) secondary_node.host.should.be('localhost') secondary_node.port.should.be(27028) nodes = Database()._nodes nodes.should.have.length_of(2)
def test_insert_and_find_with_elemmatch(self): documents = [{ '_id': ObjectId(), 'name': 'should be name 1', 'comment': [{'author': 'joe'}, {'author': 'ana'}] }, { '_id': ObjectId(), 'name': 'should be name 2', 'comment': [{'author': 'ana'}] }] db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') db.articles.insert(documents, callback=self.stop) self.wait() db.articles.find({'comment.author': 'joe'}, ('comment.$.author', ), limit=-1, callback=self.stop) result, _ = self.wait() keys = result.keys() keys.sort() keys.should.be.equal(['_id', 'comment']) str(result['_id']).should.be.equal(str(documents[0]['_id'])) result['comment'].should.have.length_of(1) result['comment'][0]['author'].should.be.equal('joe') _.should.be.none
def test_run_command(self): """[DatabaseTestCase] - Run a database command""" database = Database.connect(["localhost:27027"], dbname="test") database.command("ismaster", callback=self.stop) response, error = self.wait() response["ok"].should.be.ok
def test_send_test_message(self): """[DatabaseTestCase] - Send a test message to database""" Database.connect(["localhost:27017"], dbname='test') object_id = ObjectId() message_test = message.query(0, 'mongotor_test.$cmd', 0, 1, {'driverOIDTest': object_id}) Database().send_message(message_test, self.stop) response, error = self.wait() result = response['data'][0] result['oid'].should.be(object_id) result['ok'].should.be(1.0) result['str'].should.be(str(object_id))
def test_insert_a_single_document(self): """[ClientTestCase] - insert a single document with client""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') document = {'_id': ObjectId(), 'name': 'shouldbename'} db.collection_test.insert(document, callback=self.stop) response, error = self.wait() response['ok'].should.be.equal(1.0) error.should.be.none
def test_find_on_secondary(self): """[SecondaryPreferredTestCase] - test find document from secondary""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test', read_preference=ReadPreference.SECONDARY_PREFERRED) doc = {'_id': ObjectId()} db.test.insert(doc, callback=self.stop) self.wait() time.sleep(2) db.test.find_one(doc, callback=self.stop) doc_found, error = self.wait() doc_found.should.be.eql(doc)
def test_aggregate_collection(self): """[ClientTestCase] - aggregate command""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') documents = [{ "title": "this is my title", "author": "bob", "posted": datetime.now(), "pageViews": 5, "tags": ["good", "fun"], }, { "title": "this is my title", "author": "joe", "posted": datetime.now(), "pageViews": 5, "tags": ["good"], }] db.articles.insert(documents, callback=self.stop) response, error = self.wait() try: pipeline = { "$project": { "author": 1, "tags": 1, } }, { "$unwind": "$tags" }, { "$group": { "_id": {"tags": "$tags"}, "authors": {"$addToSet": "$author"} } } db.articles.aggregate(pipeline, callback=self.stop) response = self.wait() response['result'][0]['_id'].should.be.equal({u'tags': u'fun'}) response['result'][0]['authors'].should.be.equal([u'bob']) response['result'][1]['_id'].should.be.equal({u'tags': u'good'}) response['result'][1]['authors'].should.be.equal([u'joe', u'bob']) finally: db.articles.remove({}, callback=self.stop) self.wait()
def test_count_all_documents(self): """[ClientTestCase] - counting among all documents""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') documents = [{'_id': ObjectId(), 'param': 'shouldbeparam1'}, {'_id': ObjectId(), 'param': 'shouldbeparam1'}, {'_id': ObjectId(), 'param': 'shouldbeparam2'}] db.collection_test.insert(documents, callback=self.stop) response, error = self.wait() db.collection_test.count(callback=self.stop) total = self.wait() total.should.be.equal(3)
def test_remove_document_by_spec(self): """[ClientTestCase] - remove a document by spec""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') documents = [{'_id': ObjectId(), 'name': 'shouldbename'}, {'_id': ObjectId(), 'name': 'shouldbename2'}] db.collection_test.insert(documents, callback=self.stop) response, error = self.wait() db.collection_test.remove({'name': 'shouldbename'}, callback=self.stop) response, error = self.wait() response['ok'].should.be.equal(1.0) error.should.be.none
def test_distinct_documents_in_find(self): """[ClientTestCase] - distinct documents in query""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') documents = [{'_id': ObjectId(), 'param': 'shouldbeparam1', 'uuid': 100}, {'_id': ObjectId(), 'param': 'shouldbeparam1', 'uuid': 100}, {'_id': ObjectId(), 'param': 'shouldbeparam2', 'uuid': 200}] db.collection_test.insert(documents, callback=self.stop) response, error = self.wait() db.collection_test.find({"param": 'shouldbeparam1'}).distinct('uuid', callback=self.stop) distincts = self.wait() distincts.should.have.length_of(1) distincts[0].should.be.equal(100)
def test_find_one_non_existing_document(self): """[ClientTestCase] - find one document which not exists""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') documents = [{'_id': '1', 'param': 'shouldbeparam'}, {'_id': '2', 'param': 'shouldbeparam2'}] db.collection_test.insert(documents, callback=self.stop) response, error = self.wait() db.collection_test.find_one('non_existing_id', callback=self.stop) response, error = self.wait() response.should.be.none error.should.be.none
def test_check_connections_when_use_cursors(self): """[ConnectionPoolTestCase] - check connections when use cursors""" db = Database.connect('localhost:27027', dbname='test', maxconnections=10, maxusage=29) try: for i in range(2): db.cards.insert({'_id': ObjectId(), 'range': i}, callback=self.stop) self.wait() db._nodes[0].pool._connections.should.be.equal(0) db.cards.find({}, callback=self.stop) self.wait() db._nodes[0].pool._connections.should.be.equal(0) finally: Database.disconnect()
def test_update_document(self): """[ClientTestCase] - update a document""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') documents = [{'_id': ObjectId(), 'name': 'shouldbename'}, {'_id': ObjectId(), 'name': 'shouldbename2'}] db.collection_test.insert(documents, callback=self.stop) response, error = self.wait() db.collection_test.update(documents[0], {'$set': {'name': 'should be a new name'}}, callback=self.stop) response, error = self.wait() response['ok'].should.be.equal(1.0) error.should.be.none
def test_raises_error_when_cant_send_message(self): """[DatabaseTestCase] - Raises error when cant send message""" database = Database.connect(["localhost:27017"], dbname='test') database._pool = fudge.Fake() fake_connection = fudge.Fake('connection') fake_connection.expects('send_message').raises( ValueError('shoud not be send message')) fake_connection.expects('close') def fake_connection_method(callback): callback(fake_connection) database._pool.connection = fudge.Fake(callable=True) \ .calls(fake_connection_method) database.send_message.when.called_with("", callback=None) \ .throw(ValueError, 'shoud not be send message')
def test_find_one_document_by_id(self): """[ClientTestCase] - find one document by id""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') documents = [{'_id': ObjectId(), 'param': 'shouldbeparam'}, {'_id': ObjectId(), 'param': 'shouldbeparam1'}, {'_id': ObjectId(), 'param': 'shouldbeparam2'}] db.collection_test.insert(documents, callback=self.stop) response, error = self.wait() db.collection_test.find_one(documents[2]['_id'], callback=self.stop) response, error = self.wait() response['_id'].should.be(documents[2]['_id']) error.should.be.none
def test_find_document(self): """[ClientTestCase] - find a document""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') documents = [{'_id': ObjectId(), 'someflag': 1}, {'_id': ObjectId(), 'someflag': 1}, {'_id': ObjectId(), 'someflag': 2}] db.collection_test.insert(documents, callback=self.stop) response, error = self.wait() db.collection_test.find({'someflag': 1}, callback=self.stop) response, error = self.wait() response[0]['_id'].should.be(documents[0]['_id']) response[1]['_id'].should.be(documents[1]['_id']) error.should.be.none
def test_group(self): """[ClientTestCase] - group command""" db = Database.connect(["localhost:27027", "localhost:27028"], dbname='test') group = { 'key': None, 'condition': {'author': 'joe'}, 'initial': {'csum': 0}, 'reduce': 'function(obj,prev){prev.csum+=obj.pageViews;}' } documents = [{ "title": "this is my title", "author": "bob", "posted": datetime.now(), "pageViews": 5, "tags": ["good", "fun"], }, { "title": "this is my title", "author": "joe", "posted": datetime.now(), "pageViews": 6, "tags": ["good"], }, { "title": "this is my title", "author": "joe", "posted": datetime.now(), "pageViews": 10, "tags": ["good"], }] db.articles.insert(documents, callback=self.stop) response, error = self.wait() try: db.articles.group(callback=self.stop, **group) result = self.wait() result['retval'][0]['csum'].should.be.equal(16) finally: db.articles.remove({}, callback=self.stop) self.wait()
def test_raises_error_when_cant_send_message(self, fake_node): """[DatabaseTestCase] - Raises error when cant send message""" fake_connection = fudge.Fake("connection") fake_connection.expects("send_message").raises(ValueError("shoud not be send message")) fake_connection.expects("close") fake_pool = fudge.Fake("pool") fake_node_instace = fake_node.is_callable().returns_fake().has_attr(pool=fake_pool) def config(callback): fake_node_instace.initialized = True fake_node_instace.available = True fake_node_instace.is_primary = True callback() fake_node_instace.expects("config").calls(config) fake_pool.expects("connection").calls(lambda callback: callback(fake_connection)) database = Database.connect(["localhost:27027"], dbname="test") database.send_message.when.called_with("", callback=None).throw(ValueError, "shoud not be send message")
def setUp(self): super(SignalTestCase, self).setUp() SignalTestCase.signal_triggered = False Database.connect(["localhost:27027", "localhost:27028"], dbname='mongotor_test')
def test_create_singleton_database_connection(self): """[DatabaseTestCase] - Create a singleton database connection""" database = Database.connect(["localhost:27017"], dbname='test') database._pool.should.be.a('mongotor.pool.ConnectionPool') database.should.be.equal(Database())
def test_raises_error_when_database_was_initiated(self): """[DatabaseTestCase] - Raises ValueError when connect to inititated database""" Database.connect(["localhost:27017"], dbname='test') Database.connect.when.called_with(["localhost:27017"], dbname='test').throw(ValueError, 'Database already intiated')
import tornado.httpserver import tornado.auth import tornado.web import tornado.ioloop import tornado.options from mongotor.database import Database from config import STATIC_PATH, settings from handlers import MainHandler, NotSupportedHandler from users.handlers import LogoutHandler, LoginHandler, ChatHandler,\ NewMessage, StartChatHandler, MessagesCatcher from room_chat.handlers import RoomMessage, PopularRoomsHandler,\ AllRoomsHandler, RoomMessagesCatcher, RoomHandler, RoomHistoryHandler tornado.options.parse_command_line() Database.connect(['localhost:27017'], 'anon_chat') application = tornado.web.Application([ (r'/', MainHandler), (r'/login', LoginHandler), (r'/not_supported', NotSupportedHandler), (r'/chat', ChatHandler), (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': STATIC_PATH}), (r'/msg', NewMessage), (r'/room_msg/(.*)', RoomMessage), (r'/logout', LogoutHandler), (r'/ws/track', MessagesCatcher), (r'/popular_rooms', PopularRoomsHandler), (r'/all_rooms', AllRoomsHandler), (r'/ws/room_track/(.*)', RoomMessagesCatcher), (r'/room/(.*)', RoomHandler),
def setUp(self): super(ManagerTestCase, self).setUp() Database.connect(["localhost:27017"], dbname='mongotor_test')
def test_disconnect_database(self): """[DatabaseTestCase] - Disconnect the database""" Database.connect(["localhost:27017"], dbname='test') Database.disconnect() Database._instance.should.be.none
def setUp(self): super(CollectionTestCase, self).setUp() Database.connect(["localhost:27017"], dbname='test')
def setUp(self): super(CursorTestCase, self).setUp() Database.connect(["localhost:27027", "localhost:27028"], dbname='mongotor_test')
def test_create_singleton_database_connection(self): """[DatabaseTestCase] - Create a singleton database connection""" database = Database.connect("localhost:27027", dbname="test") database.should.be.equal(Database())
def setUp(self): super(ManagerTestCase, self).setUp() Database.connect(["localhost:27017"], dbname='mongotor_test')
def test_create_singleton_database_connection_using_connect_method(self): """[DatabaseTestCase] - Create a singleton database connection using connect method""" database = Database.connect("localhost:27027", dbname='test') database.should.be.equal(Database())
def test_create_singleton_database_connection_using_connect_method(self): """[DatabaseTestCase] - Create a singleton database connection using connect method""" database = Database.connect("localhost:27027", dbname='test') database.should.be.equal(Database())
def test_disconnect_database(self): """[DatabaseTestCase] - Disconnect the database""" Database.connect(["localhost:27027"], dbname="test") Database.disconnect() Database._instance.should.be.none