Skip to content

Commit 45f4b41

Browse files
committed
Refactor tests.
Use `DSN_STRING` and `CLUSTER_DSN_STRING` instead of rewriting the whole string. Makes it easier to change, if and when tests are developed. Change all `assert .* == .*`, `assert .* is True` and `assert .* is False` to use `assertEqual`, `assertTrue` and `assertFalse`. This is mainly for consistency, no "real" reason.
1 parent 0667705 commit 45f4b41

File tree

4 files changed

+59
-58
lines changed

4 files changed

+59
-58
lines changed

tests/influxdb/client_test.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
from influxdb import InfluxDBClient, InfluxDBClusterClient
3030
from influxdb.client import InfluxDBServerError
3131

32+
DSN_STRING = 'influxdb://uSr:pWd@host:1886/db'
33+
CLUSTER_DSN_STRING = 'influxdb://uSr:pWd@host1:8086,uSr:pWd@host2:8086/db'
34+
3235

3336
def _build_response_object(status_code=200, content=""):
3437
resp = requests.Response()
@@ -105,20 +108,20 @@ def test_scheme(self):
105108
self.assertEqual('https://host:8086', cli._baseurl)
106109

107110
def test_dsn(self):
108-
cli = InfluxDBClient.from_DSN('influxdb://usr:pwd@host:1886/db')
111+
cli = InfluxDBClient.from_DSN(DSN_STRING)
109112
self.assertEqual('http://host:1886', cli._baseurl)
110-
self.assertEqual('usr', cli._username)
111-
self.assertEqual('pwd', cli._password)
113+
self.assertEqual('uSr', cli._username)
114+
self.assertEqual('pWd', cli._password)
112115
self.assertEqual('db', cli._database)
113116
self.assertFalse(cli.use_udp)
114117

115-
cli = InfluxDBClient.from_DSN('udp+influxdb://usr:pwd@host:1886/db')
118+
cli = InfluxDBClient.from_DSN('udp+' + DSN_STRING)
116119
self.assertTrue(cli.use_udp)
117120

118-
cli = InfluxDBClient.from_DSN('https+influxdb://usr:pwd@host:1886/db')
121+
cli = InfluxDBClient.from_DSN('https+' + DSN_STRING)
119122
self.assertEqual('https://host:1886', cli._baseurl)
120123

121-
cli = InfluxDBClient.from_DSN('https+influxdb://usr:pwd@host:1886/db',
124+
cli = InfluxDBClient.from_DSN('https+' + DSN_STRING,
122125
**{'ssl': False})
123126
self.assertEqual('http://host:1886', cli._baseurl)
124127

@@ -808,33 +811,29 @@ def test_recovery(self):
808811
self.assertEqual(2, len(cluster.bad_clients))
809812

810813
def test_dsn(self):
811-
cli = InfluxDBClusterClient.from_DSN(
812-
'influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db')
814+
cli = InfluxDBClusterClient.from_DSN(CLUSTER_DSN_STRING)
813815
self.assertEqual(2, len(cli.clients))
814816
self.assertEqual('http://host1:8086', cli.clients[0]._baseurl)
815-
self.assertEqual('usr', cli.clients[0]._username)
816-
self.assertEqual('pwd', cli.clients[0]._password)
817+
self.assertEqual('uSr', cli.clients[0]._username)
818+
self.assertEqual('pWd', cli.clients[0]._password)
817819
self.assertEqual('db', cli.clients[0]._database)
818820
self.assertFalse(cli.clients[0].use_udp)
819821
self.assertEqual('http://host2:8086', cli.clients[1]._baseurl)
820-
self.assertEqual('usr', cli.clients[1]._username)
821-
self.assertEqual('pwd', cli.clients[1]._password)
822+
self.assertEqual('uSr', cli.clients[1]._username)
823+
self.assertEqual('pWd', cli.clients[1]._password)
822824
self.assertEqual('db', cli.clients[1]._database)
823825
self.assertFalse(cli.clients[1].use_udp)
824826

825-
cli = InfluxDBClusterClient.from_DSN(
826-
'udp+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db')
827+
cli = InfluxDBClusterClient.from_DSN('udp+' + CLUSTER_DSN_STRING)
827828
self.assertTrue(cli.clients[0].use_udp)
828829
self.assertTrue(cli.clients[1].use_udp)
829830

830-
cli = InfluxDBClusterClient.from_DSN(
831-
'https+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db')
831+
cli = InfluxDBClusterClient.from_DSN('https+' + CLUSTER_DSN_STRING)
832832
self.assertEqual('https://host1:8086', cli.clients[0]._baseurl)
833833
self.assertEqual('https://host2:8086', cli.clients[1]._baseurl)
834834

835-
cli = InfluxDBClusterClient.from_DSN(
836-
'https+influxdb://usr:pwd@host1:8086,usr:pwd@host2:8086/db',
837-
**{'ssl': False})
835+
cli = InfluxDBClusterClient.from_DSN('https+' + CLUSTER_DSN_STRING,
836+
**{'ssl': False})
838837
self.assertEqual('http://host1:8086', cli.clients[0]._baseurl)
839838
self.assertEqual('http://host2:8086', cli.clients[1]._baseurl)
840839

tests/influxdb/dataframe_client_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_write_points_from_dataframe_in_batches(self):
7575
status_code=204)
7676

7777
cli = DataFrameClient(database='db')
78-
assert cli.write_points(dataframe, "foo", batch_size=1) is True
78+
self.assertTrue(cli.write_points(dataframe, "foo", batch_size=1))
7979

8080
def test_write_points_from_dataframe_with_numeric_column_names(self):
8181
now = pd.Timestamp('1970-01-01 00:00+00:00')
@@ -266,7 +266,7 @@ def test_query_with_empty_result(self):
266266
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
267267
with _mocked_session(cli, 'GET', 200, {"results": [{}]}):
268268
result = cli.query('select column_one from foo;')
269-
assert result == {}
269+
self.assertEqual(result, {})
270270

271271
def test_list_series(self):
272272
response = {

tests/influxdb/influxdb08/client_test.py

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def u(x):
2727
def u(x):
2828
return x
2929

30+
DSN_STRING = 'influxdb://uSr:pWd@host:1886/db'
31+
3032

3133
def _build_response_object(status_code=200, content=""):
3234
resp = requests.Response()
@@ -91,47 +93,47 @@ def setUp(self):
9193

9294
def test_scheme(self):
9395
cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')
94-
assert cli._baseurl == 'http://host:8086'
96+
self.assertEqual(cli._baseurl, 'http://host:8086')
9597

9698
cli = InfluxDBClient(
9799
'host', 8086, 'username', 'password', 'database', ssl=True
98100
)
99-
assert cli._baseurl == 'https://host:8086'
101+
self.assertEqual(cli._baseurl, 'https://host:8086')
100102

101103
def test_dsn(self):
102-
cli = InfluxDBClient.from_DSN('influxdb://usr:pwd@host:1886/db')
103-
assert cli._baseurl == 'http://host:1886'
104-
assert cli._username == 'usr'
105-
assert cli._password == 'pwd'
106-
assert cli._database == 'db'
107-
assert cli.use_udp is False
104+
cli = InfluxDBClient.from_DSN(DSN_STRING)
105+
self.assertEqual('http://host:1886', cli._baseurl)
106+
self.assertEqual('uSr', cli._username)
107+
self.assertEqual('pWd', cli._password)
108+
self.assertEqual('db', cli._database)
109+
self.assertFalse(cli.use_udp)
108110

109-
cli = InfluxDBClient.from_DSN('udp+influxdb://usr:pwd@host:1886/db')
110-
assert cli.use_udp is True
111+
cli = InfluxDBClient.from_DSN('udp+' + DSN_STRING)
112+
self.assertTrue(cli.use_udp)
111113

112-
cli = InfluxDBClient.from_DSN('https+influxdb://usr:pwd@host:1886/db')
113-
assert cli._baseurl == 'https://host:1886'
114+
cli = InfluxDBClient.from_DSN('https+' + DSN_STRING)
115+
self.assertEqual('https://host:1886', cli._baseurl)
114116

115-
cli = InfluxDBClient.from_DSN('https+influxdb://usr:pwd@host:1886/db',
117+
cli = InfluxDBClient.from_DSN('https+' + DSN_STRING,
116118
**{'ssl': False})
117-
assert cli._baseurl == 'http://host:1886'
119+
self.assertEqual('http://host:1886', cli._baseurl)
118120

119121
def test_switch_database(self):
120122
cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')
121123
cli.switch_database('another_database')
122-
assert cli._database == 'another_database'
124+
self.assertEqual(cli._database, 'another_database')
123125

124126
@raises(FutureWarning)
125127
def test_switch_db_deprecated(self):
126128
cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')
127129
cli.switch_db('another_database')
128-
assert cli._database == 'another_database'
130+
self.assertEqual(cli._database, 'another_database')
129131

130132
def test_switch_user(self):
131133
cli = InfluxDBClient('host', 8086, 'username', 'password', 'database')
132134
cli.switch_user('another_username', 'another_password')
133-
assert cli._username == 'another_username'
134-
assert cli._password == 'another_password'
135+
self.assertEqual(cli._username, 'another_username')
136+
self.assertEqual(cli._password, 'another_password')
135137

136138
def test_write(self):
137139
with requests_mock.Mocker() as m:
@@ -250,8 +252,8 @@ def test_write_points_udp(self):
250252

251253
received_data, addr = s.recvfrom(1024)
252254

253-
assert self.dummy_points == \
254-
json.loads(received_data.decode(), strict=True)
255+
self.assertEqual(self.dummy_points,
256+
json.loads(received_data.decode(), strict=True))
255257

256258
def test_write_bad_precision_udp(self):
257259
cli = InfluxDBClient(
@@ -277,7 +279,7 @@ def test_write_points_fails(self):
277279
def test_write_points_with_precision(self):
278280
with _mocked_session('post', 200, self.dummy_points):
279281
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
280-
assert cli.write_points(self.dummy_points) is True
282+
self.assertTrue(cli.write_points(self.dummy_points))
281283

282284
def test_write_points_bad_precision(self):
283285
cli = InfluxDBClient()
@@ -299,13 +301,14 @@ def test_write_points_with_precision_fails(self):
299301
def test_delete_points(self):
300302
with _mocked_session('delete', 204) as mocked:
301303
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
302-
assert cli.delete_points("foo") is True
304+
self.assertTrue(cli.delete_points("foo"))
303305

304-
assert len(mocked.call_args_list) == 1
306+
self.assertEqual(len(mocked.call_args_list), 1)
305307
args, kwds = mocked.call_args_list[0]
306308

307-
assert kwds['params'] == {'u': 'username', 'p': 'password'}
308-
assert kwds['url'] == 'http://host:8086/db/db/series/foo'
309+
self.assertEqual(kwds['params'],
310+
{'u': 'username', 'p': 'password'})
311+
self.assertEqual(kwds['url'], 'http://host:8086/db/db/series/foo')
309312

310313
@raises(Exception)
311314
def test_delete_points_with_wrong_name(self):
@@ -342,7 +345,7 @@ def test_query(self):
342345
with _mocked_session('get', 200, data):
343346
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
344347
result = cli.query('select column_one from foo;')
345-
assert len(result[0]['points']) == 4
348+
self.assertEqual(len(result[0]['points']), 4)
346349

347350
def test_query_chunked(self):
348351
cli = InfluxDBClient(database='db')
@@ -422,7 +425,7 @@ def test_query_bad_precision(self):
422425
def test_create_database(self):
423426
with _mocked_session('post', 201, {"name": "new_db"}):
424427
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
425-
assert cli.create_database('new_db') is True
428+
self.assertTrue(cli.create_database('new_db'))
426429

427430
@raises(Exception)
428431
def test_create_database_fails(self):
@@ -433,7 +436,7 @@ def test_create_database_fails(self):
433436
def test_delete_database(self):
434437
with _mocked_session('delete', 204):
435438
cli = InfluxDBClient('host', 8086, 'username', 'password', 'db')
436-
assert cli.delete_database('old_db') is True
439+
self.assertTrue(cli.delete_database('old_db'))
437440

438441
@raises(Exception)
439442
def test_delete_database_fails(self):
@@ -447,8 +450,8 @@ def test_get_list_database(self):
447450
]
448451
with _mocked_session('get', 200, data):
449452
cli = InfluxDBClient('host', 8086, 'username', 'password')
450-
assert len(cli.get_list_database()) == 1
451-
assert cli.get_list_database()[0]['name'] == 'a_db'
453+
self.assertEqual(len(cli.get_list_database()), 1)
454+
self.assertEqual(cli.get_list_database()[0]['name'], 'a_db')
452455

453456
@raises(Exception)
454457
def test_get_list_database_fails(self):
@@ -463,8 +466,8 @@ def test_get_database_list_deprecated(self):
463466
]
464467
with _mocked_session('get', 200, data):
465468
cli = InfluxDBClient('host', 8086, 'username', 'password')
466-
assert len(cli.get_database_list()) == 1
467-
assert cli.get_database_list()[0]['name'] == 'a_db'
469+
self.assertEqual(len(cli.get_database_list()), 1)
470+
self.assertEqual(cli.get_database_list()[0]['name'], 'a_db')
468471

469472
def test_delete_series(self):
470473
with _mocked_session('delete', 204):

tests/influxdb/influxdb08/dataframe_client_test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ def test_write_points_from_dataframe_in_batches(self):
6363
"http://localhost:8086/db/db/series")
6464

6565
cli = DataFrameClient(database='db')
66-
assert cli.write_points({"foo": dataframe},
67-
batch_size=1) is True
66+
self.assertTrue(cli.write_points({"foo": dataframe}, batch_size=1))
6867

6968
def test_write_points_from_dataframe_with_numeric_column_names(self):
7069
now = pd.Timestamp('1970-01-01 00:00+00:00')
@@ -239,15 +238,15 @@ def test_query_multiple_time_series(self):
239238
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
240239
result = cli.query("""select mean(value), min(value), max(value),
241240
stddev(value) from series1, series2, series3""")
242-
assert dataframes.keys() == result.keys()
241+
self.assertEqual(dataframes.keys(), result.keys())
243242
for key in dataframes.keys():
244243
assert_frame_equal(dataframes[key], result[key])
245244

246245
def test_query_with_empty_result(self):
247246
with _mocked_session('get', 200, []):
248247
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
249248
result = cli.query('select column_one from foo;')
250-
assert result == []
249+
self.assertEqual(result, [])
251250

252251
def test_list_series(self):
253252
response = [
@@ -260,7 +259,7 @@ def test_list_series(self):
260259
with _mocked_session('get', 200, response):
261260
cli = DataFrameClient('host', 8086, 'username', 'password', 'db')
262261
series_list = cli.get_list_series()
263-
assert series_list == ['seriesA', 'seriesB']
262+
self.assertEqual(series_list, ['seriesA', 'seriesB'])
264263

265264
def test_datetime_to_epoch(self):
266265
timestamp = pd.Timestamp('2013-01-01 00:00:00.000+00:00')

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