From 74bf4122e06d11c9f3cd145fcbd5b08e33be97b9 Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Thu, 7 May 2009 12:27:08 -0400 Subject: [PATCH] make Connection paramater defaults configurable with class attributes: thanks to kless for the original implementation of this --- pymongo/connection.py | 31 ++++++++++++++++++++++++++----- test/test_connection.py | 15 +++++++++++++-- test/test_pooling.py | 10 ++++++++-- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/pymongo/connection.py b/pymongo/connection.py index 203969af1..51c5573bb 100644 --- a/pymongo/connection.py +++ b/pymongo/connection.py @@ -37,8 +37,13 @@ _CONNECT_TIMEOUT = 20.0 class Connection(object): """A connection to Mongo. """ - def __init__(self, host="localhost", port=27017, - pool_size=1, auto_start_request=True, _connect=True): + HOST = "localhost" + PORT = 27017 + POOL_SIZE = 1 + AUTO_START_REQUEST = True + + def __init__(self, host=None, port=None, pool_size=None, + auto_start_request=None, _connect=True): """Open a new connection to a Mongo instance at host:port. The resultant connection object has connection-pooling built in. It also @@ -64,6 +69,15 @@ class Connection(object): - `auto_start_request` (optional): automatically start a request on every operation - see documentation for `start_request`. """ + if host is None: + host = self.HOST + if port is None: + port = self.PORT + if pool_size is None: + pool_size = self.POOL_SIZE + if auto_start_request is None: + auto_start_request = self.AUTO_START_REQUEST + if not isinstance(host, types.StringType): raise TypeError("host must be an instance of str") if not isinstance(port, types.IntType): @@ -117,8 +131,8 @@ class Connection(object): self.__find_master() - def paired(cls, left, right=("localhost", 27017), - pool_size=1, auto_start_request=True): + def paired(cls, left, right=None, + pool_size=None, auto_start_request=None): """Open a new paired connection to Mongo. Raises TypeError if either `left` or `right` is not a tuple of the form @@ -130,6 +144,13 @@ class Connection(object): - `pool_size` (optional): same as argument to `__init__` - `auto_start_request` (optional): same as argument to `__init__` """ + if right is None: + right = (self.HOST, self.PORT) + if pool_size is None: + pool_size = self.POOL_SIZE + if auto_start_request is None: + auto_start_request = self.AUTO_START_REQUEST + connection = cls(left[0], left[1], pool_size, auto_start_request, _connect=False) connection.__pair_with(*right) @@ -155,7 +176,7 @@ class Connection(object): else: strings = result["remote"].split(":", 1) if len(strings) == 1: - port = 27017 + port = self.PORT else: port = int(strings[1]) return (strings[0], port) diff --git a/test/test_connection.py b/test/test_connection.py index 6fd1c01c6..4bc8068c7 100644 --- a/test/test_connection.py +++ b/test/test_connection.py @@ -36,13 +36,24 @@ class TestConnection(unittest.TestCase): def test_types(self): self.assertRaises(TypeError, Connection, 1) self.assertRaises(TypeError, Connection, 1.14) - self.assertRaises(TypeError, Connection, None) self.assertRaises(TypeError, Connection, []) self.assertRaises(TypeError, Connection, "localhost", "27017") self.assertRaises(TypeError, Connection, "localhost", 1.14) - self.assertRaises(TypeError, Connection, "localhost", None) self.assertRaises(TypeError, Connection, "localhost", []) + def test_constants(self): + Connection.HOST = self.host + Connection.PORT = self.port + self.assert_(Connection()) + + Connection.HOST = "somedomainthatdoesntexist.org" + Connection.PORT = 123456789 + self.assertRaises(ConnectionFailure, Connection) + + Connection.HOST = self.host + Connection.PORT = self.port + self.assert_(Connection()) + def test_connect(self): self.assertRaises(ConnectionFailure, Connection, "somedomainthatdoesntexist.org") self.assertRaises(ConnectionFailure, Connection, self.host, 123456789) diff --git a/test/test_pooling.py b/test/test_pooling.py index 47f7a2ff5..8a8b0c5aa 100644 --- a/test/test_pooling.py +++ b/test/test_pooling.py @@ -40,8 +40,6 @@ class TestPooling(unittest.TestCase): self.no_auto_pooled_db = no_auto_pooled_connection["pymongo_test"] def test_exceptions(self): - self.assertRaises(TypeError, Connection, self.host, self.port, - pool_size=None) self.assertRaises(TypeError, Connection, self.host, self.port, pool_size="one") self.assertRaises(TypeError, Connection, self.host, self.port, @@ -51,6 +49,14 @@ class TestPooling(unittest.TestCase): self.assertRaises(ValueError, Connection, self.host, self.port, pool_size=0) + def test_constants(self): + Connection.POOL_SIZE = -1 + self.assertRaises(ValueError, Connection, self.host, self.port) + + Connection.POOL_SIZE = 1 + self.assert_(Connection(self.host, self.port)) + + # NOTE this test is non-deterministic def test_end_request(self): count = 0