Skip to content

Commit 5767c53

Browse files
committed
Merge pull request influxdata#31 from william-p/request_payload
In all cases, serialise Data if not a string
2 parents 2faf095 + 24cac85 commit 5767c53

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

influxdb/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def request(self, url, method='GET', params=None, data=None,
8181

8282
params.update(auth)
8383

84-
if not isinstance(data, dict):
84+
if data is not None and not isinstance(data, str):
8585
data = json.dumps(data)
8686

8787
response = session.request(

tests/influxdb/client_test.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,38 @@ def _build_response_object(status_code=200, content=""):
1818
return resp
1919

2020

21-
def _mocked_session( method="GET", status_code=200, content=""):
21+
def _mocked_session(method="GET", status_code=200, content=""):
2222

2323
method = method.upper()
2424

25-
def check_method(*args, **kwargs):
25+
def request(*args, **kwargs):
26+
c = content
27+
2628
# Check method
2729
assert method == kwargs.get('method', 'GET')
28-
c = content
30+
2931
if method == 'POST':
30-
if not isinstance(c, dict):
31-
c = json.dumps(c)
32-
assert c == kwargs.get('data')
33-
c = ''
32+
data = kwargs.get('data', None)
33+
34+
if data is not None:
35+
# Data must be a string
36+
assert isinstance(data, str)
37+
38+
# Data must be a JSON string
39+
assert c == json.loads(data)
40+
41+
c = data
42+
43+
# Anyway, Content must be a JSON string (or empty string)
44+
if not isinstance(c, str):
45+
c = json.dumps(c)
3446

3547
return _build_response_object(status_code=status_code, content=c)
3648

3749
mocked = patch.object(
3850
session,
3951
'request',
40-
side_effect = check_method
52+
side_effect = request
4153
)
4254

4355
return mocked
@@ -140,11 +152,16 @@ def test_remove_scheduled_delete(self):
140152
cli.remove_scheduled_delete(1)
141153

142154
def test_query(self):
143-
expected = ('[{"name":"foo",'
144-
'"columns":["time","sequence_number","column_one"],'
145-
'"points":[[1383876043,16,"2"],[1383876043,15,"1"],'
146-
'[1383876035,14,"2"],[1383876035,13,"1"]]}]')
147-
with _mocked_session('get', 200, expected) as mocked:
155+
data = [
156+
{ "name":"foo",
157+
"columns": ["time", "sequence_number", "column_one"],
158+
"points": [
159+
[1383876043, 16, "2"], [1383876043, 15, "1"],
160+
[1383876035, 14, "2"], [1383876035, 13, "1"]
161+
]
162+
}
163+
]
164+
with _mocked_session('get', 200, data) as mocked:
148165
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
149166
result = cli.query('select column_one from foo;')
150167
assert len(result[0]['points']) == 4
@@ -161,7 +178,7 @@ def test_create_database(self):
161178
assert cli.create_database('new_db') is True
162179

163180
@raises(Exception)
164-
def test_creata_database_fails(self):
181+
def test_create_database_fails(self):
165182
with _mocked_session('post', 401) as mocked:
166183
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
167184
cli.create_database('new_db')
@@ -178,8 +195,10 @@ def test_delete_database_fails(self):
178195
cli.delete_database('old_db')
179196

180197
def test_get_database_list(self):
181-
expected = ('[{"name": "a_db"}]')
182-
with _mocked_session('get', 200, expected) as mocked:
198+
data = [
199+
{"name": "a_db"}
200+
]
201+
with _mocked_session('get', 200, data) as mocked:
183202
cli = InfluxDBClient('host', 8086, 'username', 'password')
184203
assert len(cli.get_database_list()) == 1
185204
assert cli.get_database_list()[0]['name'] == 'a_db'

0 commit comments

Comments
 (0)