Skip to content

Commit

Permalink
fix self.assertRegex test
Browse files Browse the repository at this point in the history
  • Loading branch information
deniszh committed Aug 25, 2024
1 parent 6a794ac commit 3308e60
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 45 deletions.
8 changes: 4 additions & 4 deletions webapp/tests/test_finders_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def _test_find_nodes(self, http_request):
responseObject = HTTPResponse(body=BytesIO(b'error'), status=200, preload_content=False)
http_request.return_value = responseObject

with self.assertRaisesRegexp(Exception, 'Error decoding response from https://[^ ]+: .+'):
with self.assertRaisesRegex(Exception, 'Error decoding response from https://[^ ]+: .+'):
finder.find_nodes(query)

@patch('graphite.finders.remote.cache.get')
Expand Down Expand Up @@ -300,7 +300,7 @@ def test_get_index(self, http_request):
responseObject = HTTPResponse(body=BytesIO(b'error'), status=200, preload_content=False)
http_request.return_value = responseObject

with self.assertRaisesRegexp(Exception, 'Error decoding index response from http://[^ ]+: .+'):
with self.assertRaisesRegex(Exception, 'Error decoding index response from http://[^ ]+: .+'):
result = finder.get_index({})

@patch('urllib3.PoolManager.request')
Expand Down Expand Up @@ -379,7 +379,7 @@ def test_auto_complete_tags(self, http_request):
responseObject = HTTPResponse(body=BytesIO(b'error'), status=200, preload_content=False)
http_request.return_value = responseObject

with self.assertRaisesRegexp(Exception, 'Error decoding autocomplete tags response from http://[^ ]+: .+'):
with self.assertRaisesRegex(Exception, 'Error decoding autocomplete tags response from http://[^ ]+: .+'):
result = finder.auto_complete_tags(['name=test'], 'tag')

@patch('urllib3.PoolManager.request')
Expand Down Expand Up @@ -460,5 +460,5 @@ def test_auto_complete_values(self, http_request):
responseObject = HTTPResponse(body=BytesIO(b'error'), status=200, preload_content=False)
http_request.return_value = responseObject

with self.assertRaisesRegexp(Exception, 'Error decoding autocomplete values response from http://[^ ]+: .+'):
with self.assertRaisesRegex(Exception, 'Error decoding autocomplete values response from http://[^ ]+: .+'):
result = finder.auto_complete_values(['name=test'], 'tag1', 'value')
20 changes: 10 additions & 10 deletions webapp/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def test_normalize_different_steps(self):
#
def test_matchSeries_assert(self):
seriesList = self._generate_series_list()
with self.assertRaisesRegexp(AssertionError, 'The number of series in each argument must be the same'):
with self.assertRaisesRegex(AssertionError, 'The number of series in each argument must be the same'):
functions.matchSeries(seriesList[0], [])

def test_matchSeries_empty(self):
Expand Down Expand Up @@ -682,7 +682,7 @@ def test_aggregate_stripSeries(self):
self.assertEqual(result, expectedList)

def test_aggregate_invalidFunc(self):
with self.assertRaisesRegexp(Exception, "Unsupported aggregation function: blahSeries"):
with self.assertRaisesRegex(Exception, "Unsupported aggregation function: blahSeries"):
functions.aggregate({}, [], 'blahSeries')

def test_aggregate_emptySeries(self):
Expand Down Expand Up @@ -730,7 +730,7 @@ def test_rangeOfSeries(self):
self.assertEqual(result, expectedList)

def test_percentileOfSeries_0th_percentile(self):
with self.assertRaisesRegexp(ValueError, 'The requested percent is required to be greater than 0'):
with self.assertRaisesRegex(ValueError, 'The requested percent is required to be greater than 0'):
functions.percentileOfSeries({}, [], 0)

def test_percentileOfSeries_empty_series(self):
Expand Down Expand Up @@ -960,7 +960,7 @@ def test_asPercent_error(self):
]
)

with self.assertRaisesRegexp(ValueError, "asPercent second argument must be missing, a single digit, reference exactly 1 series or reference the same number of series as the first argument"):
with self.assertRaisesRegex(ValueError, "asPercent second argument must be missing, a single digit, reference exactly 1 series or reference the same number of series as the first argument"):
functions.asPercent({}, seriesList, seriesList2)

def test_asPercent_no_seriesList2(self):
Expand Down Expand Up @@ -1312,7 +1312,7 @@ def test_divideSeries_error(self):
)

message = r"divideSeries second argument must reference exactly 1 series \(got 2\)"
with self.assertRaisesRegexp(ValueError, message):
with self.assertRaisesRegex(ValueError, message):
functions.divideSeries({}, seriesList, seriesList2)

def test_divideSeries_seriesList2_single(self):
Expand Down Expand Up @@ -2725,7 +2725,7 @@ def test_vertical_line_before_start(self):
tzinfo=pytz.utc
)
message = r"verticalLine\(\): timestamp 3600 exists before start of range"
with self.assertRaisesRegexp(ValueError, message):
with self.assertRaisesRegex(ValueError, message):
_ = functions.verticalLine(requestContext, "01:0019700101", "foo")

def test_vertical_line_after_end(self):
Expand All @@ -2735,7 +2735,7 @@ def test_vertical_line_after_end(self):
tzinfo=pytz.utc
)
message = r"verticalLine\(\): timestamp 31539600 exists after end of range"
with self.assertRaisesRegexp(ValueError, message):
with self.assertRaisesRegex(ValueError, message):
_ = functions.verticalLine(requestContext, "01:0019710101", "foo")

def test_line_width(self):
Expand Down Expand Up @@ -3823,7 +3823,7 @@ def test_aggregateLine_bad(self):
]
)

with self.assertRaisesRegexp(ValueError, '^Invalid parameters \\(Unsupported aggregation function: bad\\)$'):
with self.assertRaisesRegex(ValueError, '^Invalid parameters \\(Unsupported aggregation function: bad\\)$'):
functions.aggregateLine(
self._build_requestContext(
startTime=datetime(1970,1,1,1,0,0,0,pytz.timezone(settings.TIME_ZONE)),
Expand Down Expand Up @@ -4611,7 +4611,7 @@ def mock_evaluateTarget(requestContext, targets):
)

with patch('graphite.render.functions.evaluateTarget', mock_evaluateTarget):
with self.assertRaisesRegexp(ValueError, '^Invalid parameters \\(Unsupported aggregation function: invalid\\)$'):
with self.assertRaisesRegex(ValueError, '^Invalid parameters \\(Unsupported aggregation function: invalid\\)$'):
functions.movingWindow(request_context, seriesList, 5, 'invalid')

def test_movingWindow_xFilesFactor(self):
Expand Down Expand Up @@ -6455,7 +6455,7 @@ def parse(path):
self.assertEqual(result, [])

with patch('graphite.storage.STORE.tagdb', MockTagDB()):
with self.assertRaisesRegexp(ValueError, 'groupByTags\\(\\): no tags specified'):
with self.assertRaisesRegex(ValueError, 'groupByTags\\(\\): no tags specified'):
functions.groupByTags({}, [], 'sum')

result = functions.groupByTags({}, seriesList, 'sum', 'server')
Expand Down
8 changes: 4 additions & 4 deletions webapp/tests/test_readers_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_RemoteReader_fetch_multi(self, http_request):
responseObject = HTTPResponse(body=BytesIO(b'error'), status=200, preload_content=False)
http_request.return_value = responseObject

with self.assertRaisesRegexp(Exception, 'Error decoding response from http://[^ ]+: .+'):
with self.assertRaisesRegex(Exception, 'Error decoding response from http://[^ ]+: .+'):
reader.fetch(startTime, endTime)

# invalid response data
Expand All @@ -165,20 +165,20 @@ def test_RemoteReader_fetch_multi(self, http_request):
)
http_request.return_value = responseObject

with self.assertRaisesRegexp(Exception, r'Invalid render response from http://[^ ]+: KeyError\(\'name\',?\)'):
with self.assertRaisesRegex(Exception, r'Invalid render response from http://[^ ]+: KeyError\(\'name\',?\)'):
reader.fetch(startTime, endTime)

# non-200 response
responseObject = HTTPResponse(body=BytesIO(b'error'), status=500, preload_content=False)
http_request.return_value = responseObject

with self.assertRaisesRegexp(Exception, 'Error response 500 from http://[^ ]+'):
with self.assertRaisesRegex(Exception, 'Error response 500 from http://[^ ]+'):
reader.fetch(startTime, endTime)

# exception raised by request()
http_request.side_effect = Exception('error')

with self.assertRaisesRegexp(Exception, 'Error requesting http://[^ ]+: error'):
with self.assertRaisesRegex(Exception, 'Error requesting http://[^ ]+: error'):
reader.fetch(startTime, endTime)

#
Expand Down
2 changes: 1 addition & 1 deletion webapp/tests/test_readers_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def test_merge_with_cache_with_different_step_bad(self):
cache_results.append((i, 1))

# merge the db results with the cached results
with self.assertRaisesRegexp(Exception, "Invalid consolidation function: 'bad_function'"):
with self.assertRaisesRegex(Exception, "Invalid consolidation function: 'bad_function'"):
values = merge_with_cache(
cached_datapoints=cache_results,
start=start,
Expand Down
6 changes: 3 additions & 3 deletions webapp/tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def test_render_evaluateTokens_template(self):
]

message = r'invalid template\(\) syntax, only string/numeric arguments are allowed'
with self.assertRaisesRegexp(ValueError, message):
with self.assertRaisesRegex(ValueError, message):
evaluateTarget({}, test_input)

@patch('graphite.render.evaluator.prefetchData', lambda *_: None)
Expand Down Expand Up @@ -180,11 +180,11 @@ class ScalarTokenNumber(object):
float = None
scientific = None

with self.assertRaisesRegexp(ValueError, 'unknown token in target evaluator'):
with self.assertRaisesRegex(ValueError, 'unknown token in target evaluator'):
tokens = ScalarToken()
evaluateScalarTokens(tokens)

with self.assertRaisesRegexp(ValueError, 'unknown numeric type in target evaluator'):
with self.assertRaisesRegex(ValueError, 'unknown numeric type in target evaluator'):
tokens = ScalarToken()
tokens.number = ScalarTokenNumber()
evaluateScalarTokens(tokens)
Expand Down
4 changes: 2 additions & 2 deletions webapp/tests/test_render_datalib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_TimeSeries_init_no_args(self):
msg = r'__init__\(\) missing 5 required positional arguments'
else:
msg = r'__init__\(\) takes at least 6 arguments \(1 given\)'
with self.assertRaisesRegexp(TypeError, msg):
with self.assertRaisesRegex(TypeError, msg):
TimeSeries()

def test_TimeSeries_init_string_values(self):
Expand Down Expand Up @@ -252,7 +252,7 @@ def test_TimeSeries_iterate_valuesPerPoint_2_invalid(self):

series.consolidate(2)
self.assertEqual(series.valuesPerPoint, 2)
with self.assertRaisesRegexp(Exception, "Invalid consolidation function: 'bogus'"):
with self.assertRaisesRegex(Exception, "Invalid consolidation function: 'bogus'"):
_ = list(series)


Expand Down
38 changes: 19 additions & 19 deletions webapp/tests/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def mock_pool_exec(pool, jobs, timeout):
message = r'Timed out after [-.e0-9]+s for fetch for \[\'a\'\]'
with patch('graphite.storage.pool_exec', mock_pool_exec):
with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.fetch(['a'], 1, 2, 3, {}))
self.assertEqual(log_info.call_count, 1)
self.assertRegex(log_info.call_args[0][0], message)
Expand All @@ -86,7 +86,7 @@ def test_fetch_all_failed(self):

message = r'All requests failed for fetch for \[\'a\'\] \(1\)'
with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.fetch(['a'], 1, 2, 3, {}))
self.assertEqual(log_info.call_count, 1)
self.assertRegex(
Expand All @@ -100,7 +100,7 @@ def test_fetch_all_failed(self):

message = r'All requests failed for fetch for \[\'a\'\] \(2\)'
with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.fetch(['a'], 1, 2, 3, {}))
self.assertEqual(log_info.call_count, 2)
self.assertRegex(
Expand All @@ -124,7 +124,7 @@ def test_fetch_some_failed(self):

message = r'All requests failed for fetch for \[\'a\'\] \(2\)'
with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.fetch(['a'], 1, 2, 3, {}))
self.assertEqual(log_info.call_count, 2)
self.assertRegex(
Expand All @@ -141,7 +141,7 @@ def test_fetch_some_failed_hard_fail_enabled(self):

message = r'1 request\(s\) failed for fetch for \[\'a\'\] \(2\)'
with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.fetch(['a'], 1, 2, 3, {}))
self.assertEqual(log_info.call_count, 1)
self.assertRegex(
Expand All @@ -155,7 +155,7 @@ def test_fetch_some_failed_hard_fail_enabled(self):

message = r'All requests failed for fetch for \[\'a\'\] \(2\)'
with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.fetch(['a'], 1, 2, 3, {}))
self.assertEqual(log_info.call_count, 2)
self.assertRegex(
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_find(self):
# failure threshold
message = r'Query a yields too many results and failed \(failure threshold is 1\)'
with self.settings(METRICS_FIND_FAILURE_THRESHOLD=1):
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.find('a'))

# warning threshold
Expand All @@ -221,7 +221,7 @@ def mock_pool_exec(pool, jobs, timeout):
message = r'Timed out after [-.e0-9]+s for find <FindQuery: a from \* until \*>'
with patch('graphite.storage.pool_exec', mock_pool_exec):
with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.find('a'))
self.assertEqual(log_info.call_count, 1)
self.assertRegex(log_info.call_args[0][0], message)
Expand All @@ -234,7 +234,7 @@ def test_find_all_failed(self):

message = r'All requests failed for find <FindQuery: a from \* until \*>'
with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.find('a'))
self.assertEqual(log_info.call_count, 1)
self.assertRegex(
Expand All @@ -247,7 +247,7 @@ def test_find_all_failed(self):
)

with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, message):
with self.assertRaisesRegex(Exception, message):
list(store.find('a'))
self.assertEqual(log_info.call_count, 2)
self.assertRegex(
Expand Down Expand Up @@ -296,7 +296,7 @@ def mock_pool_exec(pool, jobs, timeout):

with patch('graphite.storage.pool_exec', mock_pool_exec):
with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, 'Timed out after .*'):
with self.assertRaisesRegex(Exception, 'Timed out after .*'):
store.get_index()
self.assertEqual(log_info.call_count, 1)
self.assertRegex(log_info.call_args[0][0], 'Timed out after [-.e0-9]+s')
Expand All @@ -308,7 +308,7 @@ def test_get_index_all_failed(self):
)

with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, 'All requests failed for get_index'):
with self.assertRaisesRegex(Exception, 'All requests failed for get_index'):
store.get_index()
self.assertEqual(log_info.call_count, 1)
self.assertRegex(
Expand All @@ -321,7 +321,7 @@ def test_get_index_all_failed(self):
)

with patch('graphite.storage.log.info') as log_info:
with self.assertRaisesRegexp(Exception, r'All requests failed for get_index \(2\)'):
with self.assertRaisesRegex(Exception, r'All requests failed for get_index \(2\)'):
store.get_index()
self.assertEqual(log_info.call_count, 2)
self.assertRegex(
Expand Down Expand Up @@ -577,29 +577,29 @@ def mockAutoCompleteValues(exprs, tag, valuePrefix=None, limit=None, requestCont
# test exception handling with one finder
store = mockStore([TestFinderTagsException()])

with self.assertRaisesRegexp(Exception, r'All requests failed for tags for \[\'tag1=value1\'\] test.*'):
with self.assertRaisesRegex(Exception, r'All requests failed for tags for \[\'tag1=value1\'\] test.*'):
store.tagdb_auto_complete_tags(['tag1=value1'], 'test', 100, request_context)

with self.assertRaisesRegexp(Exception, r'All requests failed for values for \[\'tag1=value1\'\] tag2 test.*'):
with self.assertRaisesRegex(Exception, r'All requests failed for values for \[\'tag1=value1\'\] tag2 test.*'):
store.tagdb_auto_complete_values(['tag1=value1'], 'tag2', 'test', 100, request_context)

# test exception handling with more than one finder
store = mockStore([TestFinderTagsException(), TestFinderTagsException()])

with self.assertRaisesRegexp(Exception, r'All requests failed for tags for \[\'tag1=value1\'\] test'):
with self.assertRaisesRegex(Exception, r'All requests failed for tags for \[\'tag1=value1\'\] test'):
store.tagdb_auto_complete_tags(['tag1=value1'], 'test', 100, request_context)

with self.assertRaisesRegexp(Exception, r'All requests failed for values for \[\'tag1=value1\'\] tag2 test'):
with self.assertRaisesRegex(Exception, r'All requests failed for values for \[\'tag1=value1\'\] tag2 test'):
store.tagdb_auto_complete_values(['tag1=value1'], 'tag2', 'test', 100, request_context)

# test pool timeout handling
store = mockStore([TestFinderTagsTimeout()])

with self.settings(USE_WORKER_POOL=True, FIND_TIMEOUT=0):
with self.assertRaisesRegexp(Exception, r'Timed out after [-.e0-9]+s for tags for \[\'tag1=value1\'\]'):
with self.assertRaisesRegex(Exception, r'Timed out after [-.e0-9]+s for tags for \[\'tag1=value1\'\]'):
store.tagdb_auto_complete_tags(['tag1=value1'], 'test', 100, request_context)

with self.assertRaisesRegexp(Exception, r'Timed out after [-.e0-9]+s for values for \[\'tag1=value1\'\] tag2 test'):
with self.assertRaisesRegex(Exception, r'Timed out after [-.e0-9]+s for values for \[\'tag1=value1\'\] tag2 test'):
store.tagdb_auto_complete_values(['tag1=value1'], 'tag2', 'test', 100, request_context)

# test write_index
Expand Down
2 changes: 1 addition & 1 deletion webapp/tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def mockRequest(method, url, fields=None, headers=None, timeout=None):
with patch('graphite.http_pool.http.request', mockRequest):
self._test_tagdb(db)

with self.assertRaisesRegexp(Exception, 'HTTP Error from remote tagdb: 405'):
with self.assertRaisesRegex(Exception, 'HTTP Error from remote tagdb: 405'):
db.get_tag('delSeries')

db.username = 'test'
Expand Down
2 changes: 1 addition & 1 deletion webapp/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_logtime(ok, custom=None, timer=None):
self.assertEqual(log.info.call_count, 2)
self.assertRegex(log.info.call_args[0][0], r'test :: custom [-.e0-9]+s')

with self.assertRaisesRegexp(Exception, 'testException'):
with self.assertRaisesRegex(Exception, 'testException'):
test_logtime(False)
self.assertEqual(log.info.call_count, 3)
self.assertRegex(log.info.call_args[0][0], r'test :: failed in [-.e0-9]+s')
Expand Down

0 comments on commit 3308e60

Please sign in to comment.