Exemplo n.º 1
0
 def test_database(self):
     """Connection.database"""
     con = connection.Connection(uri)
     try:
         self.assertEqual(con.database.name, 'test', "incorrect database returned")
     finally:
         con.close()
Exemplo n.º 2
0
 def test_add_default(self):
     """connection.add('test', 'mongodb://localhost/test', True)"""
     con = connection.Connection(uri)
     try:
         name = 'test'
         connection.add(name, con, True)
         self.assertEquals(
             connection.connections[None], con, "registered connection is incorrect")
     finally:
         con.close()
         connection.connections = {}
Exemplo n.º 3
0
 def test_get(self):
     """connection.get"""
     con = connection.Connection(uri)
     try:
         name = 'test'
         connection.connections[name] = con
         self.assertEqual(
             connection.get(name), con, "returned connection is incorrect")
     finally:
         con.close()
         connection.connections = {}
Exemplo n.º 4
0
 def test_add(self):
     """connection.add('test')"""
     name = 'test'
     uri = 'mongodb://localhost/{}'.format(name)
     con = connection.Connection(uri)
     try:
         connection.add(name)
         self.assertEquals(
             connection.connections[name].uri, con.uri, "registered connection is incorrect")
     finally:
         con.close()
         connection.connections = {}
Exemplo n.º 5
0
    def test_autoreconnect(self):
        """Connection.autoreconnect"""

        def func(*args, **kwargs):
            if self.tries > 0:
                self.tries -= 1
                raise AutoReconnect("tries == {}".format(self.tries))
            return True

        self.tries = 1
        con = connection.Connection(uri)
        try:
            reconnect_func = con.autoreconnect(func)
            self.assertTrue(reconnect_func(), "autoreconnect func returned incorrect value")
        finally:
            con.close()

        self.tries = 5
        con = connection.Connection(uri, retries=1)
        try:
            reconnect_func = con.autoreconnect(func)
            self.assertRaises(AutoReconnect, reconnect_func)
        finally:
            con.close()
Exemplo n.º 6
0
    def test_collection(self):
        """Connection.__getitem__"""
        prefix = 'units_'
        name = 'test'
        con = connection.Connection(uri, prefix=prefix)
        try:
            collection = con[name]
            self.assertIsInstance(
                collection, connection.CollectionProxy, "collection proxy not returned")
            self.assertEqual(collection.name, prefix + name, "incorrect collection returned")

            collection = con[None]
            self.assertIsNone(
                collection, "connection did not return None for empty collection name")
        finally:
            con.close()
Exemplo n.º 7
0
 def setUp(self):
     self.prefix = 'units_'
     self.name = 'test'
     self.con = connection.Connection(uri, prefix=self.prefix)
Exemplo n.º 8
0
 def test_client(self):
     """Connection.client"""
     con = connection.Connection(uri)
     self.assertIsNone(con._client, "client is not None prior to use")
     self.assertIsInstance(con.client, MongoClient, "client is not a MongoClient")