|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0. |
| 3 | + |
| 4 | +import unittest |
| 5 | +from unittest.mock import patch |
| 6 | + |
| 7 | + |
| 8 | +class TestImportlibMetadata(unittest.TestCase): |
| 9 | + """Test that importlib.metadata is used instead of pkg_resources""" |
| 10 | + |
| 11 | + def setUp(self): |
| 12 | + """Reset the metrics string cache before each test""" |
| 13 | + # Reset the cached metrics string in both modules |
| 14 | + import awsiot.mqtt5_client_builder |
| 15 | + import awsiot.mqtt_connection_builder |
| 16 | + |
| 17 | + # Reset the global _metrics_str variable |
| 18 | + awsiot.mqtt_connection_builder._metrics_str = None |
| 19 | + awsiot.mqtt5_client_builder._metrics_str = None |
| 20 | + |
| 21 | + def test_metrics_string_generation_mqtt_connection_builder(self): |
| 22 | + """Test that mqtt_connection_builder uses importlib.metadata for version detection""" |
| 23 | + from awsiot import mqtt_connection_builder |
| 24 | + |
| 25 | + # Mock importlib.metadata.version to return a known version |
| 26 | + with patch("importlib.metadata.version") as mock_version: |
| 27 | + mock_version.return_value = "1.2.3" |
| 28 | + |
| 29 | + # Call the function that uses version detection |
| 30 | + # We need to access the private function for testing |
| 31 | + result = mqtt_connection_builder._get_metrics_str("test_username") |
| 32 | + |
| 33 | + # Verify that importlib.metadata.version was called |
| 34 | + mock_version.assert_called_once_with("awsiotsdk") |
| 35 | + |
| 36 | + # Verify the result contains the expected format |
| 37 | + self.assertIn("SDK=PythonV2&Version=1.2.3", result) |
| 38 | + |
| 39 | + def test_metrics_string_generation_mqtt5_client_builder(self): |
| 40 | + """Test that mqtt5_client_builder uses importlib.metadata for version detection""" |
| 41 | + from awsiot import mqtt5_client_builder |
| 42 | + |
| 43 | + # Mock importlib.metadata.version to return a known version |
| 44 | + with patch("importlib.metadata.version") as mock_version: |
| 45 | + mock_version.return_value = "1.2.3" |
| 46 | + |
| 47 | + # Call the function that uses version detection |
| 48 | + # We need to access the private function for testing |
| 49 | + result = mqtt5_client_builder._get_metrics_str("test_username") |
| 50 | + |
| 51 | + # Verify that importlib.metadata.version was called |
| 52 | + mock_version.assert_called_once_with("awsiotsdk") |
| 53 | + |
| 54 | + # Verify the result contains the expected format |
| 55 | + self.assertIn("SDK=PythonV2&Version=1.2.3", result) |
| 56 | + |
| 57 | + def test_package_not_found_handling_mqtt_connection_builder(self): |
| 58 | + """Test that PackageNotFoundError is handled correctly in mqtt_connection_builder""" |
| 59 | + import importlib.metadata |
| 60 | + |
| 61 | + from awsiot import mqtt_connection_builder |
| 62 | + |
| 63 | + # Mock importlib.metadata.version to raise PackageNotFoundError |
| 64 | + with patch("importlib.metadata.version") as mock_version: |
| 65 | + mock_version.side_effect = importlib.metadata.PackageNotFoundError("Package not found") |
| 66 | + |
| 67 | + # Call the function that uses version detection |
| 68 | + result = mqtt_connection_builder._get_metrics_str("test_username") |
| 69 | + |
| 70 | + # Verify that the fallback version is used |
| 71 | + self.assertIn("SDK=PythonV2&Version=dev", result) |
| 72 | + |
| 73 | + def test_package_not_found_handling_mqtt5_client_builder(self): |
| 74 | + """Test that PackageNotFoundError is handled correctly in mqtt5_client_builder""" |
| 75 | + import importlib.metadata |
| 76 | + |
| 77 | + from awsiot import mqtt5_client_builder |
| 78 | + |
| 79 | + # Mock importlib.metadata.version to raise PackageNotFoundError |
| 80 | + with patch("importlib.metadata.version") as mock_version: |
| 81 | + mock_version.side_effect = importlib.metadata.PackageNotFoundError("Package not found") |
| 82 | + |
| 83 | + # Call the function that uses version detection |
| 84 | + result = mqtt5_client_builder._get_metrics_str("test_username") |
| 85 | + |
| 86 | + # Verify that the fallback version is used |
| 87 | + self.assertIn("SDK=PythonV2&Version=dev", result) |
| 88 | + |
| 89 | + def test_general_exception_handling_mqtt_connection_builder(self): |
| 90 | + """Test that general exceptions are handled correctly in mqtt_connection_builder""" |
| 91 | + from awsiot import mqtt_connection_builder |
| 92 | + |
| 93 | + # Mock importlib.metadata.version to raise a general exception |
| 94 | + with patch("importlib.metadata.version") as mock_version: |
| 95 | + mock_version.side_effect = Exception("Some other error") |
| 96 | + |
| 97 | + # Call the function that uses version detection |
| 98 | + result = mqtt_connection_builder._get_metrics_str("test_username") |
| 99 | + |
| 100 | + # Verify that empty string is returned on general exception |
| 101 | + self.assertEqual(result, "") |
| 102 | + |
| 103 | + def test_general_exception_handling_mqtt5_client_builder(self): |
| 104 | + """Test that general exceptions are handled correctly in mqtt5_client_builder""" |
| 105 | + from awsiot import mqtt5_client_builder |
| 106 | + |
| 107 | + # Mock importlib.metadata.version to raise a general exception |
| 108 | + with patch("importlib.metadata.version") as mock_version: |
| 109 | + mock_version.side_effect = Exception("Some other error") |
| 110 | + |
| 111 | + # Call the function that uses version detection |
| 112 | + result = mqtt5_client_builder._get_metrics_str("test_username") |
| 113 | + |
| 114 | + # Verify that empty string is returned on general exception |
| 115 | + self.assertEqual(result, "") |
| 116 | + |
| 117 | + def test_no_pkg_resources_import(self): |
| 118 | + """Test that pkg_resources is not imported in the modified files""" |
| 119 | + import awsiot.mqtt5_client_builder |
| 120 | + import awsiot.mqtt_connection_builder |
| 121 | + |
| 122 | + # Check that pkg_resources is not in the module's globals |
| 123 | + self.assertNotIn("pkg_resources", awsiot.mqtt_connection_builder.__dict__) |
| 124 | + self.assertNotIn("pkg_resources", awsiot.mqtt5_client_builder.__dict__) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + unittest.main() |
0 commit comments