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

Commit 514387f

Browse files
authored
Merge pull request #232 from cloudant/221-json-dumps
Honour the custom encoder parameter for json encoding.
2 parents dd912bc + 9a4fb82 commit 514387f

File tree

7 files changed

+82
-11
lines changed

7 files changed

+82
-11
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
2.3.0 (Unreleased)
22
==================
3+
- [FIXED] Issue where the custom JSON encoder was not used when transforming
4+
data.
35

46
2.2.0 (2016-10-20)
57
==================

src/cloudant/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ def _write_cors_configuration(self, config):
727727
)
728728
resp = self.r_session.put(
729729
endpoint,
730-
data=json.dumps(config),
730+
data=json.dumps(config, cls=self.encoder),
731731
headers={'Content-Type': 'application/json'}
732732
)
733733
resp.raise_for_status()

src/cloudant/database.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ def bulk_docs(self, docs):
660660
headers = {'Content-Type': 'application/json'}
661661
resp = self.r_session.post(
662662
url,
663-
data=json.dumps(data),
663+
data=json.dumps(data, cls=self.client.encoder),
664664
headers=headers
665665
)
666666
resp.raise_for_status()
@@ -684,7 +684,7 @@ def missing_revisions(self, doc_id, *revisions):
684684
resp = self.r_session.post(
685685
url,
686686
headers={'Content-Type': 'application/json'},
687-
data=json.dumps(data)
687+
data=json.dumps(data, cls=self.client.encoder)
688688
)
689689
resp.raise_for_status()
690690

@@ -713,7 +713,7 @@ def revisions_diff(self, doc_id, *revisions):
713713
resp = self.r_session.post(
714714
url,
715715
headers={'Content-Type': 'application/json'},
716-
data=json.dumps(data)
716+
data=json.dumps(data, cls=self.client.encoder)
717717
)
718718
resp.raise_for_status()
719719

@@ -752,7 +752,7 @@ def set_revision_limit(self, limit):
752752
"""
753753
url = posixpath.join(self.database_url, '_revs_limit')
754754

755-
resp = self.r_session.put(url, data=json.dumps(limit))
755+
resp = self.r_session.put(url, data=json.dumps(limit, self.client.encoder))
756756
resp.raise_for_status()
757757

758758
return resp.json()
@@ -991,7 +991,7 @@ def share_database(self, username, roles=None):
991991
doc['cloudant'] = data
992992
resp = self.r_session.put(
993993
self.security_url,
994-
data=json.dumps(doc),
994+
data=json.dumps(doc, cls=self.client.encoder),
995995
headers={'Content-Type': 'application/json'}
996996
)
997997
resp.raise_for_status()
@@ -1016,7 +1016,7 @@ def unshare_database(self, username):
10161016
doc['cloudant'] = data
10171017
resp = self.r_session.put(
10181018
self.security_url,
1019-
data=json.dumps(doc),
1019+
data=json.dumps(doc, cls=self.client.encoder),
10201020
headers={'Content-Type': 'application/json'}
10211021
)
10221022
resp.raise_for_status()

src/cloudant/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def create(self):
144144
headers = {'Content-Type': 'application/json'}
145145
resp = self._r_session.post(
146146
self.index_url,
147-
data=json.dumps(payload),
147+
data=json.dumps(payload, cls=self._database.client.encoder),
148148
headers=headers
149149
)
150150
resp.raise_for_status()

tests/unit/database_tests.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,41 @@ def test_constructor(self):
6767
self.assertEqual(self.db.r_session, self.client.r_session)
6868
self.assertIsInstance(self.db.result, Result)
6969

70+
def test_bulk_docs_uses_custom_encoder(self):
71+
"""
72+
Test that the bulk_docs method uses the custom encoder
73+
"""
74+
self.set_up_client(auto_connect=True, encoder="AEncoder")
75+
docs = [
76+
{'_id': 'julia{0:03d}'.format(i), 'name': 'julia', 'age': i}
77+
for i in range(3)
78+
]
79+
database = self.client[self.test_dbname]
80+
with self.assertRaises(TypeError):
81+
# since the encoder is a str a type error should be thrown.
82+
database.bulk_docs(docs)
83+
84+
def test_missing_revisions_uses_custom_encoder(self):
85+
"""
86+
Test that missing_revisions uses the custom encoder.
87+
"""
88+
revs = ['1-1', '2-1', '3-1']
89+
self.set_up_client(auto_connect=True, encoder="AEncoder")
90+
database = self.client[self.test_dbname]
91+
with self.assertRaises(TypeError):
92+
# since the encoder is a str a type error should be thrown.
93+
database.missing_revisions('no-such-doc', *revs)
94+
95+
def test_revs_diff_uses_custom_encoder(self):
96+
"""
97+
Test that revisions_diff uses the custom encoder.
98+
"""
99+
revs = ['1-1', '2-1', '3-1']
100+
self.set_up_client(auto_connect=True, encoder="AEncoder")
101+
database = self.client[self.test_dbname]
102+
with self.assertRaises(TypeError):
103+
database.revisions_diff('no-such-doc', *revs)
104+
70105
def test_retrieve_db_url(self):
71106
"""
72107
Test retrieving the database URL
@@ -822,6 +857,27 @@ def tearDown(self):
822857
self.db_tear_down()
823858
super(CloudantDatabaseTests, self).tearDown()
824859

860+
def test_share_database_uses_custom_encoder(self):
861+
"""
862+
Test that share_database uses custom encoder
863+
"""
864+
share = 'user-{0}'.format(unicode_(uuid.uuid4()))
865+
self.set_up_client(auto_connect=True, encoder="AEncoder")
866+
database = self.client[self.test_dbname]
867+
with self.assertRaises(TypeError):
868+
database.share_database(share)
869+
870+
871+
def test_unshare_database_uses_custom_encoder(self):
872+
"""
873+
Test that unshare_database uses custom encoder
874+
"""
875+
share = 'user-{0}'.format(unicode_(uuid.uuid4()))
876+
self.set_up_client(auto_connect=True, encoder="AEncoder")
877+
database = self.client[self.test_dbname]
878+
with self.assertRaises(TypeError):
879+
database.unshare_database(share)
880+
825881
def test_get_security_document(self):
826882
"""
827883
Test the retrieval of the security document.

tests/unit/index_tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,17 @@ def test_create_an_index_using_design_prefix(self):
241241
}
242242
)
243243

244+
def test_create_uses_custom_encoder(self):
245+
"""
246+
Test that the create method uses the custom encoder
247+
"""
248+
self.set_up_client(auto_connect=True, encoder="AEncoder")
249+
database = self.client[self.test_dbname]
250+
index = Index(database, '_design/ddoc001', 'index001', fields=['name', 'age'])
251+
with self.assertRaises(TypeError):
252+
index.create()
253+
254+
244255
def test_create_fails_due_to_ddocid_validation(self):
245256
"""
246257
Ensure that if the design doc id is not a string the create call fails.

tests/unit/unit_t_db_base.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def setUp(self):
134134
"""
135135
self.set_up_client()
136136

137-
def set_up_client(self, auto_connect=False):
137+
def set_up_client(self, auto_connect=False, encoder=None):
138138
if os.environ.get('RUN_CLOUDANT_TESTS') is None:
139139
admin_party = False
140140
if (os.environ.get('ADMIN_PARTY') and
@@ -148,7 +148,8 @@ def set_up_client(self, auto_connect=False):
148148
self.pwd,
149149
admin_party,
150150
url=self.url,
151-
connect=auto_connect
151+
connect=auto_connect,
152+
encoder=encoder
152153
)
153154
else:
154155
self.account = os.environ.get('CLOUDANT_ACCOUNT')
@@ -162,7 +163,8 @@ def set_up_client(self, auto_connect=False):
162163
self.pwd,
163164
url=self.url,
164165
x_cloudant_user=self.account,
165-
connect=auto_connect
166+
connect=auto_connect,
167+
encoder=encoder
166168
)
167169

168170

0 commit comments

Comments
 (0)