Skip to content

Commit 77924a8

Browse files
committed
Add importlib metadata tests
1 parent a618dac commit 77924a8

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

test/test_importlib_metadata.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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_importlib_metadata_available(self):
22+
"""Test that importlib.metadata is available and working"""
23+
try:
24+
# Test that we can import importlib.metadata
25+
import importlib.metadata
26+
27+
self.assertTrue(True, "importlib.metadata is available")
28+
except ImportError:
29+
self.fail("importlib.metadata should be available in Python 3.8+")
30+
31+
def test_metrics_string_generation_mqtt_connection_builder(self):
32+
"""Test that mqtt_connection_builder uses importlib.metadata for version detection"""
33+
from awsiot import mqtt_connection_builder
34+
35+
# Mock importlib.metadata.version to return a known version
36+
with patch("importlib.metadata.version") as mock_version:
37+
mock_version.return_value = "1.2.3"
38+
39+
# Call the function that uses version detection
40+
# We need to access the private function for testing
41+
result = mqtt_connection_builder._get_metrics_str("test_username")
42+
43+
# Verify that importlib.metadata.version was called
44+
mock_version.assert_called_once_with("awsiotsdk")
45+
46+
# Verify the result contains the expected format
47+
self.assertIn("SDK=PythonV2&Version=1.2.3", result)
48+
49+
def test_metrics_string_generation_mqtt5_client_builder(self):
50+
"""Test that mqtt5_client_builder uses importlib.metadata for version detection"""
51+
from awsiot import mqtt5_client_builder
52+
53+
# Mock importlib.metadata.version to return a known version
54+
with patch("importlib.metadata.version") as mock_version:
55+
mock_version.return_value = "1.2.3"
56+
57+
# Call the function that uses version detection
58+
# We need to access the private function for testing
59+
result = mqtt5_client_builder._get_metrics_str("test_username")
60+
61+
# Verify that importlib.metadata.version was called
62+
mock_version.assert_called_once_with("awsiotsdk")
63+
64+
# Verify the result contains the expected format
65+
self.assertIn("SDK=PythonV2&Version=1.2.3", result)
66+
67+
def test_package_not_found_handling_mqtt_connection_builder(self):
68+
"""Test that PackageNotFoundError is handled correctly in mqtt_connection_builder"""
69+
import importlib.metadata
70+
71+
from awsiot import mqtt_connection_builder
72+
73+
# Mock importlib.metadata.version to raise PackageNotFoundError
74+
with patch("importlib.metadata.version") as mock_version:
75+
mock_version.side_effect = importlib.metadata.PackageNotFoundError("Package not found")
76+
77+
# Call the function that uses version detection
78+
result = mqtt_connection_builder._get_metrics_str("test_username")
79+
80+
# Verify that the fallback version is used
81+
self.assertIn("SDK=PythonV2&Version=dev", result)
82+
83+
def test_package_not_found_handling_mqtt5_client_builder(self):
84+
"""Test that PackageNotFoundError is handled correctly in mqtt5_client_builder"""
85+
import importlib.metadata
86+
87+
from awsiot import mqtt5_client_builder
88+
89+
# Mock importlib.metadata.version to raise PackageNotFoundError
90+
with patch("importlib.metadata.version") as mock_version:
91+
mock_version.side_effect = importlib.metadata.PackageNotFoundError("Package not found")
92+
93+
# Call the function that uses version detection
94+
result = mqtt5_client_builder._get_metrics_str("test_username")
95+
96+
# Verify that the fallback version is used
97+
self.assertIn("SDK=PythonV2&Version=dev", result)
98+
99+
def test_general_exception_handling_mqtt_connection_builder(self):
100+
"""Test that general exceptions are handled correctly in mqtt_connection_builder"""
101+
from awsiot import mqtt_connection_builder
102+
103+
# Mock importlib.metadata.version to raise a general exception
104+
with patch("importlib.metadata.version") as mock_version:
105+
mock_version.side_effect = Exception("Some other error")
106+
107+
# Call the function that uses version detection
108+
result = mqtt_connection_builder._get_metrics_str("test_username")
109+
110+
# Verify that empty string is returned on general exception
111+
self.assertEqual(result, "")
112+
113+
def test_general_exception_handling_mqtt5_client_builder(self):
114+
"""Test that general exceptions are handled correctly in mqtt5_client_builder"""
115+
from awsiot import mqtt5_client_builder
116+
117+
# Mock importlib.metadata.version to raise a general exception
118+
with patch("importlib.metadata.version") as mock_version:
119+
mock_version.side_effect = Exception("Some other error")
120+
121+
# Call the function that uses version detection
122+
result = mqtt5_client_builder._get_metrics_str("test_username")
123+
124+
# Verify that empty string is returned on general exception
125+
self.assertEqual(result, "")
126+
127+
def test_no_pkg_resources_import(self):
128+
"""Test that pkg_resources is not imported in the modified files"""
129+
import awsiot.mqtt5_client_builder
130+
import awsiot.mqtt_connection_builder
131+
132+
# Check that pkg_resources is not in the module's globals
133+
self.assertNotIn("pkg_resources", awsiot.mqtt_connection_builder.__dict__)
134+
self.assertNotIn("pkg_resources", awsiot.mqtt5_client_builder.__dict__)
135+
136+
def test_importlib_metadata_import(self):
137+
"""Test that importlib.metadata is properly imported in the modified files"""
138+
# Check that importlib.metadata is available (though it might be imported locally)
139+
# We can't directly check if it's imported since it's done inside the function
140+
# But we can verify the function works with importlib.metadata
141+
try:
142+
import importlib.metadata
143+
144+
self.assertTrue(True, "importlib.metadata is available for import")
145+
except ImportError:
146+
self.fail("importlib.metadata should be available")
147+
148+
149+
if __name__ == "__main__":
150+
unittest.main()

0 commit comments

Comments
 (0)