Merge lp:~elopio/cloudspacesclient/return_json into lp:cloudspacesclient

Proposed by Leo Arias
Status: Merged
Merged at revision: 26
Proposed branch: lp:~elopio/cloudspacesclient/return_json
Merge into: lp:cloudspacesclient
Diff against target: 194 lines (+78/-30)
4 files modified
src/cloudspacesclient/client.py (+2/-2)
src/cloudspacesclient/tests/conformance/test_cloudspaces_api.py (+16/-19)
src/cloudspacesclient/tests/unit/test_client.py (+46/-6)
src/cloudspacesclient/tests/unit/utils.py (+14/-3)
To merge this branch: bzr merge lp:~elopio/cloudspacesclient/return_json
Reviewer Review Type Date Requested Status
Łukasz Czyżykowski (community) Approve
Richard Huddie Pending
Review via email: mp+196632@code.launchpad.net

Commit message

Refactor the client to return the json after each request.

To post a comment you must log in.
27. By Leo Arias

Make a better check of the returned json.

Revision history for this message
Łukasz Czyżykowski (lukasz-czyzykowski) wrote :

LGTM

review: Approve
28. By Leo Arias

Merged with trunk.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/cloudspacesclient/client.py'
2--- src/cloudspacesclient/client.py 2013-11-21 04:32:56 +0000
3+++ src/cloudspacesclient/client.py 2013-11-27 06:07:02 +0000
4@@ -45,7 +45,7 @@
5 if not response.ok:
6 raise APIException(
7 "{0}: {1}".format(response.status_code, response.reason))
8- return response
9+ return response.json()
10
11 def _get_headers(self):
12 return {
13@@ -66,4 +66,4 @@
14 if not response.ok:
15 raise APIException(
16 "{0}: {1}".format(response.status_code, response.reason))
17- return response
18+ return response.json()
19
20=== modified file 'src/cloudspacesclient/tests/conformance/test_cloudspaces_api.py'
21--- src/cloudspacesclient/tests/conformance/test_cloudspaces_api.py 2013-11-26 16:34:44 +0000
22+++ src/cloudspacesclient/tests/conformance/test_cloudspaces_api.py 2013-11-27 06:07:02 +0000
23@@ -56,12 +56,10 @@
24 return parser.parse(rfc3339_now)
25
26 def test_get_user_representation(self):
27- response = self.cloudspaces_api_client.get_user_representation()
28- self.assertTrue(response.ok)
29- response_json = response.json()
30+ user_rep = self.cloudspaces_api_client.get_user_representation()
31 validictory.validate(
32- response_json, schemas.GET_USER_REPRESENTATION_RESPONSE_SCHEMA)
33- volumes = response_json.get('volumes')
34+ user_rep, schemas.GET_USER_REPRESENTATION_RESPONSE_SCHEMA)
35+ volumes = user_rep.get('volumes')
36 # Assert that the volumes have just been created.
37 self._assert_volumes_when_created(
38 volumes, self.start_datetime, self._get_now_datetime())
39@@ -93,32 +91,31 @@
40 resource_path, '{0}metadata/{1}'.format(path, node_id))
41
42 def test_get_root_metadata(self):
43- user_rep = self.cloudspaces_api_client.get_user_representation().json()
44+ user_rep = self.cloudspaces_api_client.get_user_representation()
45 root_volume = self._get_root_volume(user_rep.get('volumes'))
46 node_id = root_volume.get('node_id')
47 filename = os.path.split(root_volume.get('name'))[-1]
48- response = self.cloudspaces_api_client.get_metadata(node_id)
49- self.assertTrue(response.ok)
50- response_json = response.json()
51+
52+ metadata = self.cloudspaces_api_client.get_metadata(node_id)
53 validictory.validate(
54- response_json, schemas.GET_METADATA_RESPONSE_SCHEMA)
55- self._assert_normal_folder_keys_not_in_root(response_json)
56- self._assert_content_path(node_id, response_json.get('content_path'))
57+ metadata, schemas.GET_METADATA_RESPONSE_SCHEMA)
58+ self._assert_normal_folder_keys_not_in_root(metadata)
59+ self._assert_content_path(node_id, metadata.get('content_path'))
60 # TODO Check that the contents are not present because the folder is
61 # empty. --elopio - 2013-11-25
62- self.assertTrue(response_json.get('is_folder'))
63- self.assertFalse(response_json.get('is_deleted'))
64- self.assertTrue(response_json.get('is_root'))
65+ self.assertTrue(metadata.get('is_folder'))
66+ self.assertFalse(metadata.get('is_deleted'))
67+ self.assertTrue(metadata.get('is_root'))
68 # TODO The spec doesn't include it in the filename in the examples.
69 # --elopio - 2013-11-25
70- self.assertEqual(response_json.get('filename'), filename)
71- self.assertEqual(response_json.get('node_id'), node_id)
72+ self.assertEqual(metadata.get('filename'), filename)
73+ self.assertEqual(metadata.get('node_id'), node_id)
74 # TODO The spec says the path shouldn't be returned on the root
75 # metadata, but I think it's useful. Should we change the spec?
76 # --elopio - 2013-11-25
77 self.assertEqual(
78- response_json.get('path'), self.test_server.get_root_path())
79- self._assert_resource_path(node_id, response_json.get('resource_path'))
80+ metadata.get('path'), self.test_server.get_root_path())
81+ self._assert_resource_path(node_id, metadata.get('resource_path'))
82 # TODO Check the volume_id field. --elopio - 2013-11-25
83
84 def _get_root_volume(self, volumes):
85
86=== modified file 'src/cloudspacesclient/tests/unit/test_client.py'
87--- src/cloudspacesclient/tests/unit/test_client.py 2013-11-20 05:57:50 +0000
88+++ src/cloudspacesclient/tests/unit/test_client.py 2013-11-27 06:07:02 +0000
89@@ -17,6 +17,7 @@
90
91 """Unit tests for the Cloudspaces API client."""
92
93+import json
94 import unittest
95
96 import mock
97@@ -30,17 +31,19 @@
98 def setUp(self):
99 super(CloudspacesAPIClientTestCase, self).setUp()
100 self.api_client = cloudspacesclient.CloudspacesAPIClient(
101- 'dummy url', 'dummy auth')
102+ 'http://example.com/cloudspaces/', 'test auth')
103+ self.request_headers = {
104+ 'Content-Type': 'application/json',
105+ 'Accept': 'application/json'
106+ }
107
108- def test_get_user_representation_should_return_request(self):
109+ def test_get_user_representation_should_return_json(self):
110 with mock.patch('requests.get') as get_mock:
111 get_mock.return_value = utils.RequestResponseMock(
112- ok=True, status_code=200, content='test content')
113+ ok=True, content='{"test": "content"}')
114 response = self.api_client.get_user_representation()
115
116- self.assertTrue(response.ok)
117- self.assertEqual(response.status_code, 200)
118- self.assertEqual(response.content, 'test content')
119+ self.assertEqual(response.get('test'), 'content')
120
121 def test_get_user_representation_failure(self):
122 with mock.patch('requests.get') as get_mock:
123@@ -50,3 +53,40 @@
124 self.api_client.get_user_representation()
125
126 self.assertEqual(str(e.exception), 'test error code: test reason')
127+
128+ def test_get_user_representation_request(self):
129+ with mock.patch('requests.get') as get_mock:
130+ get_mock.return_value = utils.RequestResponseMock(
131+ ok=True, content=json.dumps("{'test': 'content'}"))
132+ self.api_client.get_user_representation()
133+
134+ get_mock.assert_called_once_with(
135+ 'http://example.com/cloudspaces/', auth='test auth',
136+ headers=self.request_headers)
137+
138+ def test_get_metadata_should_return_json(self):
139+ with mock.patch('requests.get') as get_mock:
140+ get_mock.return_value = utils.RequestResponseMock(
141+ ok=True, content=json.dumps("{'test': 'content'}"))
142+ response = self.api_client.get_metadata('dummy node')
143+
144+ self.assertEqual(response, "{'test': 'content'}")
145+
146+ def test_get_metadata_failure(self):
147+ with mock.patch('requests.get') as get_mock:
148+ get_mock.return_value = utils.RequestResponseMock(
149+ ok=False, status_code='test error code', reason='test reason')
150+ with self.assertRaises(cloudspacesclient.APIException) as e:
151+ self.api_client.get_metadata('dummy node')
152+
153+ self.assertEqual(str(e.exception), 'test error code: test reason')
154+
155+ def test_get_metadata_request(self):
156+ with mock.patch('requests.get') as get_mock:
157+ get_mock.return_value = utils.RequestResponseMock(
158+ ok=True, content=json.dumps("{'test': 'content'}"))
159+ self.api_client.get_metadata('testnodeid')
160+
161+ get_mock.assert_called_once_with(
162+ 'http://example.com/cloudspaces/metadata/testnodeid',
163+ auth='test auth', headers=self.request_headers)
164
165=== modified file 'src/cloudspacesclient/tests/unit/utils.py'
166--- src/cloudspacesclient/tests/unit/utils.py 2013-11-20 05:06:14 +0000
167+++ src/cloudspacesclient/tests/unit/utils.py 2013-11-27 06:07:02 +0000
168@@ -18,12 +18,23 @@
169 """Utilities used in multiple unit tests."""
170
171
172-class RequestResponseMock(object):
173+from requests import models
174+
175+
176+class RequestResponseMock(models.Response):
177
178 def __init__(self, ok=True, status_code=200, reason='OK',
179 content='mock content'):
180 super(RequestResponseMock, self).__init__()
181- self.ok = ok
182+ self._ok = ok
183 self.status_code = status_code
184 self.reason = reason
185- self.content = content
186+ self._content = content
187+
188+ @property
189+ def content(self):
190+ return self._content
191+
192+ @property
193+ def ok(self):
194+ return self._ok

Subscribers

People subscribed via source and target branches

to all changes: