62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
import unittest
|
|
import pymongo
|
|
|
|
import mongoengine.connection
|
|
|
|
from mongoengine import *
|
|
from mongoengine.connection import get_db, get_connection
|
|
|
|
|
|
class ConnectionTest(unittest.TestCase):
|
|
|
|
def tearDown(self):
|
|
mongoengine.connection._connection_settings = {}
|
|
mongoengine.connection._connections = {}
|
|
mongoengine.connection._dbs = {}
|
|
|
|
def test_connect(self):
|
|
"""Ensure that the connect() method works properly.
|
|
"""
|
|
connect('mongoenginetest')
|
|
|
|
conn = get_connection()
|
|
self.assertTrue(isinstance(conn, pymongo.connection.Connection))
|
|
|
|
db = get_db()
|
|
self.assertTrue(isinstance(db, pymongo.database.Database))
|
|
self.assertEqual(db.name, 'mongoenginetest')
|
|
|
|
connect('mongoenginetest2', alias='testdb')
|
|
conn = get_connection('testdb')
|
|
self.assertTrue(isinstance(conn, pymongo.connection.Connection))
|
|
|
|
def test_connect_uri(self):
|
|
"""Ensure that the connect() method works properly with uri's
|
|
"""
|
|
|
|
connect("testdb_uri", host='mongodb://username:password@localhost/mongoenginetest')
|
|
|
|
conn = get_connection()
|
|
self.assertTrue(isinstance(conn, pymongo.connection.Connection))
|
|
|
|
db = get_db()
|
|
self.assertTrue(isinstance(db, pymongo.database.Database))
|
|
self.assertEqual(db.name, 'mongoenginetest')
|
|
|
|
def test_register_connection(self):
|
|
"""Ensure that connections with different aliases may be registered.
|
|
"""
|
|
register_connection('testdb', 'mongoenginetest2')
|
|
|
|
self.assertRaises(ConnectionError, get_connection)
|
|
conn = get_connection('testdb')
|
|
self.assertTrue(isinstance(conn, pymongo.connection.Connection))
|
|
|
|
db = get_db('testdb')
|
|
self.assertTrue(isinstance(db, pymongo.database.Database))
|
|
self.assertEqual(db.name, 'mongoenginetest2')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|