Skip to content

Commit cd44d9e

Browse files
author
Grégory Starck
committed
Enh: updated tests to be less strict / more robust.
We had spurious/random test failures with travis because there had other warnings generated. (see for instance: https://travis-ci.org/savoirfairelinux/influxdb-python/jobs/59929464) Unfortunately we couldn't reproduce. So this just changed the assert to be less strict: search for the expected message/warning in the rec_warnings list. example of the failure: ====================================================================== FAIL: Tests warning for an invalid bulk size. ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/travis/build/savoirfairelinux/influxdb-python/tests/influxdb/influxdb08/helper_test.py", line 172, in testWarnBulkSizeZero .format(WarnBulkSizeZero, '\n'.join(map(str, w)))) nose.proxy.AssertionError: 5 != 1 : <class 'tests.influxdb.influxdb08.helper_test.WarnBulkSizeZero'> call should have generated one warning.Actual generated warnings: {message : UserWarning('Definition of bulk_size in WarnBulkSizeZero forced to 1, was less than 1.',), category : 'UserWarning', filename : '/home/travis/build/savoirfairelinux/influxdb-python/influxdb/influxdb08/helper.py', lineno : 79, line : None} {message : ResourceWarning('unclosed <socket.socket object at 0x0000000005c63820>',), category : 'ResourceWarning', filename : '/home/travis/build/savoirfairelinux/influxdb-python/.tox/pypy3/lib-python/3/os.py', lineno : 477, line : None} {message : ResourceWarning('unclosed <socket.socket object at 0x0000000005e1ace0>',), category : 'ResourceWarning', filename : '/home/travis/build/savoirfairelinux/influxdb-python/.tox/pypy3/lib-python/3/os.py', lineno : 477, line : None} {message : ResourceWarning('unclosed <socket.socket object at 0x0000000005e1b100>',), category : 'ResourceWarning', filename : '/home/travis/build/savoirfairelinux/influxdb-python/.tox/pypy3/lib-python/3/os.py', lineno : 477, line : None} {message : ResourceWarning('unclosed <socket.socket object at 0x0000000005e1b130>',), category : 'ResourceWarning', filename : '/home/travis/build/savoirfairelinux/influxdb-python/.tox/pypy3/lib-python/3/os.py', lineno : 477, line : None}
1 parent b58aeed commit cd44d9e

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

tests/influxdb/influxdb08/helper_test.py

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,26 @@ class Meta:
158158
bulk_size = 0
159159
autocommit = True
160160

161-
with warnings.catch_warnings(record=True) as w:
161+
with warnings.catch_warnings(record=True) as rec_warnings:
162162
warnings.simplefilter("always")
163-
try:
163+
# Server defined in the client is invalid, we're testing
164+
# the warning only.
165+
with self.assertRaises(ConnectionError):
164166
WarnBulkSizeZero(time=159, server_name='us.east-1')
165-
except ConnectionError:
166-
# Server defined in the client is invalid, we're testing
167-
# the warning only.
168-
pass
169-
self.assertEqual(len(w), 1,
170-
'{} call should have generated one warning.'
171-
.format(WarnBulkSizeZero))
172-
self.assertIn('forced to 1', str(w[-1].message),
173-
'Warning message did not contain "forced to 1".')
167+
168+
self.assertGreaterEqual(
169+
len(rec_warnings), 1,
170+
'{} call should have generated one warning.'
171+
'Actual generated warnings: {}'.format(
172+
WarnBulkSizeZero, '\n'.join(map(str, rec_warnings))))
173+
174+
expected_msg = (
175+
'Definition of bulk_size in WarnBulkSizeZero forced to 1, '
176+
'was less than 1.')
177+
178+
self.assertIn(expected_msg, list(w.message.args[0]
179+
for w in rec_warnings),
180+
'Warning message did not contain "forced to 1".')
174181

175182
def testWarnBulkSizeNoEffect(self):
176183
"""
@@ -184,11 +191,24 @@ class Meta:
184191
bulk_size = 5
185192
autocommit = False
186193

187-
with warnings.catch_warnings(record=True) as w:
194+
with warnings.catch_warnings(record=True) as rec_warnings:
188195
warnings.simplefilter("always")
189196
WarnBulkSizeNoEffect(time=159, server_name='us.east-1')
190-
self.assertEqual(len(w), 1,
191-
'{} call should have generated one warning.'
192-
.format(WarnBulkSizeNoEffect))
193-
self.assertIn('has no affect', str(w[-1].message),
194-
'Warning message did not contain "has not affect".')
197+
198+
self.assertGreaterEqual(
199+
len(rec_warnings), 1,
200+
'{} call should have generated one warning.'
201+
'Actual generated warnings: {}'.format(
202+
WarnBulkSizeNoEffect, '\n'.join(map(str, rec_warnings))))
203+
204+
expected_msg = (
205+
'Definition of bulk_size in WarnBulkSizeNoEffect has no affect '
206+
'because autocommit is false.')
207+
208+
self.assertIn(expected_msg, list(w.message.args[0]
209+
for w in rec_warnings),
210+
'Warning message did not contain the expected_msg.')
211+
212+
213+
if __name__ == '__main__':
214+
unittest.main()

0 commit comments

Comments
 (0)