Skip to content

Commit

Permalink
Use non-deprecated assertion methods
Browse files Browse the repository at this point in the history
Both assertDictContainsSubset and assertRaisesRegexp have been
deprecated, and removed in Python 3.12. Use assertGreaterEqual on the
dictionary items to compare dictionaries instead of
assertDictContainsSubset.

Fixes census-instrumentation#1242
  • Loading branch information
s-t-e-v-e-n-k committed Apr 9, 2024
1 parent 2e0710b commit 9fc5f15
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def test_collector_to_metric_invalid_dist(self):
collector.register_view(view)
desc = collector.registered_views[list(REGISTERED_VIEW)[0]]

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
ValueError,
'unsupported aggregation type <class \'mock.mock.Mock\'>'):
collector.to_metric(desc=desc, tag_values=[None], agg_data=agg)
Expand Down Expand Up @@ -278,8 +278,8 @@ def test_collector_collect_with_none_label_value(self):

class TestPrometheusStatsExporter(unittest.TestCase):
def test_exporter_constructor_no_namespace(self):
with self.assertRaisesRegexp(ValueError,
'Namespace can not be empty string.'):
with self.assertRaisesRegex(ValueError,
'Namespace can not be empty string.'):
prometheus.new_stats_exporter(prometheus.Options())

def test_emit(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ def test_gcp_gce_monitored_resource(self, gcp_md_mock):
with mock_gce_env():
resource = monitored_resource.get_instance()
self.assertEqual(resource.get_type(), 'mock_resource_type')
self.assertDictContainsSubset(
{'mock_label_key': 'mock_label_value'}, resource.get_labels())
self.assertDictContainsSubset(mocked_labels, resource.get_labels())
self.assertGreaterEqual(
resource.get_labels().items(),
{'mock_label_key': 'mock_label_value'}.items())
self.assertGreaterEqual(
resource.get_labels().items(), mocked_labels.items())

@mock.patch('opencensus.common.monitored_resource.monitored_resource'
'.gcp_metadata_config.GcpMetadataConfig')
Expand All @@ -130,16 +132,19 @@ def test_gcp_k8s_monitored_resource(self, gcp_md_mock):

gcp_md_mock.get_attribute.assert_called_once_with(cluster_name_key)
self.assertEqual(r1.get_type(), 'k8s_container')
self.assertDictContainsSubset(mocked_labels, r1.get_labels())
self.assertGreaterEqual(
r1.get_labels().items(), mocked_labels.items())

with mock_oc_env():
with mock_k8s_env():
r2 = monitored_resource.get_instance()

self.assertEqual(r1.get_type(), 'k8s_container')
self.assertDictContainsSubset(mocked_labels, r1.get_labels())
self.assertDictContainsSubset(
{'mock_label_key': 'mock_label_value'}, r2.get_labels())
self.assertGreaterEqual(
r1.get_labels().items(), mocked_labels.items())
self.assertGreaterEqual(
r2.get_labels().items(),
{'mock_label_key': 'mock_label_value'}.items())

@mock.patch('opencensus.common.monitored_resource.monitored_resource'
'.aws_identity_doc_utils.AwsIdentityDocumentUtils')
Expand All @@ -163,9 +168,11 @@ def test_aws_monitored_resource(self, aws_md_mock):
with mock_aws_env():
resource = monitored_resource.get_instance()
self.assertEqual(resource.get_type(), 'mock_resource_type')
self.assertDictContainsSubset(
{'mock_label_key': 'mock_label_value'}, resource.get_labels())
self.assertDictContainsSubset(mocked_labels, resource.get_labels())
self.assertGreaterEqual(
resource.get_labels().items(),
{'mock_label_key': 'mock_label_value'}.items())
self.assertGreaterEqual(
resource.get_labels().items(), mocked_labels.items())

def test_k8s_environment(self):
patch = mock.patch.dict(os.environ,
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/stats/test_aggregation_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,15 @@ def test_exemplar(self):
def test_exemplar_null_attachments(self):
timestamp = time.time()

with self.assertRaisesRegexp(TypeError,
'attachments should not be empty'):
with self.assertRaisesRegex(TypeError,
'attachments should not be empty'):
aggregation_data_module.Exemplar(6, timestamp, None)

def test_exemplar_null_attachment_key(self):
timestamp = time.time()
attachment = {None: "one", "Two": "two"}

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError,
'attachment key should not be empty and should be a string'):
aggregation_data_module.Exemplar(6, timestamp, attachment)
Expand All @@ -312,7 +312,7 @@ def test_exemplar_null_attachment_value(self):
timestamp = time.time()
attachment = {"One": "one", "Two": None}

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError,
'attachment value should not be empty and should be a string'):
aggregation_data_module.Exemplar(6, timestamp, attachment)
Expand All @@ -321,7 +321,7 @@ def test_exemplar_int_attachment_key(self):
timestamp = time.time()
attachment = {1: "one", "Two": "two"}

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError,
'attachment key should not be empty and should be a string'):
aggregation_data_module.Exemplar(6, timestamp, attachment)
Expand All @@ -330,7 +330,7 @@ def test_exemplar_int_attachment_value(self):
timestamp = time.time()
attachment = {"One": "one", "Two": 2}

with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError,
'attachment value should not be empty and should be a string'):
aggregation_data_module.Exemplar(6, timestamp, attachment)
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/stats/test_measurement_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_put_attachment_none_key(self):
test_value = 'testValue'
measurement_map = measurement_map_module.MeasurementMap(
measure_to_view_map=measure_to_view_map, attachments={})
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError,
'attachment key should not be empty and should be a string'):
measurement_map.measure_put_attachment(test_key, test_value)
Expand All @@ -69,7 +69,7 @@ def test_put_attachment_none_value(self):
test_value = None
measurement_map = measurement_map_module.MeasurementMap(
measure_to_view_map=measure_to_view_map, attachments={})
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError,
'attachment value should not be empty and should be a string'):
measurement_map.measure_put_attachment(test_key, test_value)
Expand All @@ -80,7 +80,7 @@ def test_put_attachment_int_key(self):
test_value = 'testValue'
measurement_map = measurement_map_module.MeasurementMap(
measure_to_view_map=measure_to_view_map, attachments={})
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError,
'attachment key should not be empty and should be a string'):
measurement_map.measure_put_attachment(test_key, test_value)
Expand All @@ -91,7 +91,7 @@ def test_put_attachment_int_value(self):
test_value = 42
measurement_map = measurement_map_module.MeasurementMap(
measure_to_view_map=measure_to_view_map, attachments={})
with self.assertRaisesRegexp(
with self.assertRaisesRegex(
TypeError,
'attachment value should not be empty and should be a string'):
measurement_map.measure_put_attachment(test_key, test_value)
Expand Down

0 comments on commit 9fc5f15

Please sign in to comment.