Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 938e997

Browse files
authored
Merge pull request #231 from cloudant/230-add-auto-connect-feature
Add auto connect feature to client
2 parents 1939f6f + 2131369 commit 938e997

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

src/cloudant/client.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ class CouchDB(dict):
4040
4141
:param str user: Username used to connect to CouchDB.
4242
:param str auth_token: Authentication token used to connect to CouchDB.
43-
:param str url: URL for CouchDB server.
4443
:param bool admin_party: Setting to allow the use of Admin Party mode in
4544
CouchDB. Defaults to ``False``.
45+
:param str url: URL for CouchDB server.
4646
:param str encoder: Optional json Encoder object used to encode
4747
documents for storage. Defaults to json.JSONEncoder.
48-
:param requests.HTTPAdapter adapter: Optional adapter to use for configuring requests.
48+
:param requests.HTTPAdapter adapter: Optional adapter to use for
49+
configuring requests.
50+
:param bool connect: Keyword argument, if set to True performs the call to
51+
connect as part of client construction. Default is False.
4952
"""
5053
_DATABASE_CLASS = CouchDatabase
5154

@@ -60,12 +63,17 @@ def __init__(self, user, auth_token, admin_party=False, **kwargs):
6063
self.encoder = kwargs.get('encoder') or json.JSONEncoder
6164
self.adapter = kwargs.get('adapter')
6265
self.r_session = None
66+
if kwargs.get('connect', False):
67+
self.connect()
6368

6469
def connect(self):
6570
"""
6671
Starts up an authentication session for the client using cookie
67-
authentication.
72+
authentication if necessary.
6873
"""
74+
if self.r_session:
75+
return
76+
6977
self.r_session = requests.Session()
7078
# If a Transport Adapter was supplied add it to the session
7179
if self.adapter is not None:

tests/unit/client_tests.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,44 @@ def test_connect(self):
103103
self.client.disconnect()
104104
self.assertIsNone(self.client.r_session)
105105

106+
def test_auto_connect(self):
107+
"""
108+
Test connect during client instantiation option.
109+
"""
110+
try:
111+
self.set_up_client(auto_connect=True)
112+
self.assertIsInstance(self.client.r_session, requests.Session)
113+
if self.client.admin_party:
114+
self.assertIsNone(self.client.r_session.auth)
115+
else:
116+
self.assertEqual(
117+
self.client.r_session.auth, (self.user, self.pwd)
118+
)
119+
finally:
120+
self.client.disconnect()
121+
self.assertIsNone(self.client.r_session)
122+
123+
def test_multiple_connect(self):
124+
"""
125+
Test that issuing a connect call to an already connected client does
126+
not cause any issue.
127+
"""
128+
try:
129+
self.client.connect()
130+
self.set_up_client(auto_connect=True)
131+
self.client.connect()
132+
self.assertIsInstance(self.client.r_session, requests.Session)
133+
if self.client.admin_party:
134+
self.assertIsNone(self.client.r_session.auth)
135+
else:
136+
self.assertEqual(
137+
self.client.r_session.auth, (self.user, self.pwd)
138+
)
139+
finally:
140+
self.client.disconnect()
141+
self.assertIsNone(self.client.r_session)
142+
143+
106144
def test_session(self):
107145
"""
108146
Test getting session information.

tests/unit/unit_t_db_base.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ def setUp(self):
132132
"""
133133
Set up test attributes for unit tests targeting a database
134134
"""
135+
self.set_up_client()
136+
137+
def set_up_client(self, auto_connect=False):
135138
if os.environ.get('RUN_CLOUDANT_TESTS') is None:
136139
admin_party = False
137140
if (os.environ.get('ADMIN_PARTY') and
@@ -140,7 +143,13 @@ def setUp(self):
140143
self.user = os.environ.get('DB_USER', None)
141144
self.pwd = os.environ.get('DB_PASSWORD', None)
142145
self.url = os.environ['DB_URL']
143-
self.client = CouchDB(self.user, self.pwd, admin_party, url=self.url)
146+
self.client = CouchDB(
147+
self.user,
148+
self.pwd,
149+
admin_party,
150+
url=self.url,
151+
connect=auto_connect
152+
)
144153
else:
145154
self.account = os.environ.get('CLOUDANT_ACCOUNT')
146155
self.user = os.environ.get('DB_USER')
@@ -152,7 +161,10 @@ def setUp(self):
152161
self.user,
153162
self.pwd,
154163
url=self.url,
155-
x_cloudant_user=self.account)
164+
x_cloudant_user=self.account,
165+
connect=auto_connect
166+
)
167+
156168

157169
def tearDown(self):
158170
"""

0 commit comments

Comments
 (0)