Skip to content

Commit 2faf095

Browse files
prune998jvshahid
authored andcommitted
Close influxdata#30. Convert non dictionary type to json and fix the tests
1 parent 936d963 commit 2faf095

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

influxdb/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ def request(self, url, method='GET', params=None, data=None,
7171
"""
7272
url = "{0}/{1}".format(self._baseurl, url)
7373

74-
if isinstance(data, dict):
75-
data = json.dumps(data)
76-
7774
if params is None:
7875
params = {}
7976

@@ -84,6 +81,9 @@ def request(self, url, method='GET', params=None, data=None,
8481

8582
params.update(auth)
8683

84+
if not isinstance(data, dict):
85+
data = json.dumps(data)
86+
8787
response = session.request(
8888
method=method,
8989
url=url,
@@ -163,7 +163,7 @@ def write_points_with_precision(self, data, time_precision='s'):
163163
url=url,
164164
method='POST',
165165
params=params,
166-
data=json.dumps(data),
166+
data=data,
167167
status_code=200
168168
)
169169

tests/influxdb/client_test.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"""
33
unit tests
44
"""
5+
import json
56
import requests
67
from nose.tools import raises
78
from mock import patch
@@ -24,7 +25,14 @@ def _mocked_session( method="GET", status_code=200, content=""):
2425
def check_method(*args, **kwargs):
2526
# Check method
2627
assert method == kwargs.get('method', 'GET')
27-
return _build_response_object(status_code=status_code, content=content)
28+
c = content
29+
if method == 'POST':
30+
if not isinstance(c, dict):
31+
c = json.dumps(c)
32+
assert c == kwargs.get('data')
33+
c = ''
34+
35+
return _build_response_object(status_code=status_code, content=c)
2836

2937
mocked = patch.object(
3038
session,
@@ -67,7 +75,7 @@ def test_write_points(self):
6775
}
6876
]
6977

70-
with _mocked_session('post', 200) as mocked:
78+
with _mocked_session('post', 200, data) as mocked:
7179
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
7280
assert cli.write_points(data) is True
7381

@@ -89,7 +97,7 @@ def test_write_points_with_precision(self):
8997
}
9098
]
9199

92-
with _mocked_session('post', 200) as mocked:
100+
with _mocked_session('post', 200, data) as mocked:
93101
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
94102
assert cli.write_points_with_precision(data) is True
95103

@@ -148,7 +156,7 @@ def test_query_fail(self):
148156
cli.query('select column_one from foo;')
149157

150158
def test_create_database(self):
151-
with _mocked_session('post', 201) as mocked:
159+
with _mocked_session('post', 201, {"name": "new_db"}) as mocked:
152160
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
153161
assert cli.create_database('new_db') is True
154162

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy