Skip to content

Commit 483c955

Browse files
committed
test fastapi tests
1 parent d060eca commit 483c955

File tree

1 file changed

+66
-27
lines changed

1 file changed

+66
-27
lines changed

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,50 +1095,89 @@ def test_instruments_with_fastapi_installed(self, mock_logger):
10951095
mock_distro = Mock()
10961096
mock_distro.load_instrumentor.return_value = None
10971097
_load_instrumentors(mock_distro)
1098+
print(
1099+
"mock_distro.load_instrumentor.call_args_list: %s"
1100+
% mock_distro.load_instrumentor.call_args_list
1101+
)
10981102
self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 1)
10991103
(ep,) = mock_distro.load_instrumentor.call_args.args
11001104
self.assertEqual(ep.name, "fastapi")
11011105
mock_logger.debug.assert_has_calls(
11021106
[self._instrumentation_loaded_successfully_call()]
11031107
)
11041108

1109+
# TODO: fails
1110+
@patch(
1111+
"opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts"
1112+
)
11051113
@patch("opentelemetry.instrumentation.auto_instrumentation._load._logger")
1106-
def test_instruments_with_old_fastapi_installed(self, mock_logger): # pylint: disable=no-self-use
1114+
def test_instruments_with_old_fastapi_installed(
1115+
self, mock_logger, mock_dep
1116+
): # pylint: disable=no-self-use
11071117
dependency_conflict = DependencyConflict("0.58", "0.57")
11081118
mock_distro = Mock()
1109-
mock_distro.load_instrumentor.side_effect = DependencyConflictError(
1110-
dependency_conflict
1111-
)
1119+
# mock_distro.load_instrumentor.side_effect = DependencyConflictError(
1120+
# dependency_conflict
1121+
# )
1122+
# TODO: return conflict instead of raising
1123+
mock_dep.return_value = DependencyConflictError(dependency_conflict)
11121124
_load_instrumentors(mock_distro)
1113-
self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 1)
1114-
(ep,) = mock_distro.load_instrumentor.call_args.args
1115-
self.assertEqual(ep.name, "fastapi")
1116-
assert (
1117-
self._instrumentation_loaded_successfully_call()
1118-
not in mock_logger.debug.call_args_list
1119-
)
1120-
mock_logger.debug.assert_has_calls(
1121-
[self._instrumentation_failed_to_load_call(dependency_conflict)]
1125+
mock_distro.load_instrumentor.assert_not_called()
1126+
self.assertEqual(
1127+
mock_logger.debug.call_args.args,
1128+
(
1129+
"Skipping instrumentation %s: %s",
1130+
"fastapi",
1131+
dependency_conflict,
1132+
),
11221133
)
1123-
1134+
# print("mock_distro.load_instrumentor.call_args_list: %s" % mock_distro.load_instrumentor.call_args_list)
1135+
# self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 1)
1136+
# (ep,) = mock_distro.load_instrumentor.call_args.args
1137+
# self.assertEqual(ep.name, "fastapi")
1138+
# assert (
1139+
# self._instrumentation_loaded_successfully_call()
1140+
# not in mock_logger.debug.call_args_list
1141+
# )
1142+
# mock_logger.debug.assert_has_calls(
1143+
# [self._instrumentation_failed_to_load_call(dependency_conflict)]
1144+
# )
1145+
1146+
# TODO: fails
1147+
@patch(
1148+
"opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts"
1149+
)
11241150
@patch("opentelemetry.instrumentation.auto_instrumentation._load._logger")
1125-
def test_instruments_without_fastapi_installed(self, mock_logger): # pylint: disable=no-self-use
1151+
def test_instruments_without_fastapi_installed(
1152+
self, mock_logger, mock_dep
1153+
): # pylint: disable=no-self-use
11261154
dependency_conflict = DependencyConflict("0.58", None)
11271155
mock_distro = Mock()
1128-
mock_distro.load_instrumentor.side_effect = DependencyConflictError(
1129-
dependency_conflict
1130-
)
1156+
# mock_distro.load_instrumentor.side_effect = DependencyConflictError(
1157+
# dependency_conflict
1158+
# )
1159+
mock_dep.return_value = DependencyConflictError(dependency_conflict)
11311160
_load_instrumentors(mock_distro)
1132-
self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 1)
1133-
(ep,) = mock_distro.load_instrumentor.call_args.args
1134-
self.assertEqual(ep.name, "fastapi")
1135-
assert (
1136-
self._instrumentation_loaded_successfully_call()
1137-
not in mock_logger.debug.call_args_list
1138-
)
1139-
mock_logger.debug.assert_has_calls(
1140-
[self._instrumentation_failed_to_load_call(dependency_conflict)]
1161+
mock_distro.load_instrumentor.assert_not_called()
1162+
self.assertEqual(
1163+
mock_logger.debug.call_args.args,
1164+
(
1165+
"Skipping instrumentation %s: %s",
1166+
"fastapi",
1167+
dependency_conflict,
1168+
),
11411169
)
1170+
# print("mock_distro.load_instrumentor.call_args_list: %s" % mock_distro.load_instrumentor.call_args_list)
1171+
# self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 1)
1172+
# (ep,) = mock_distro.load_instrumentor.call_args.args
1173+
# self.assertEqual(ep.name, "fastapi")
1174+
# assert (
1175+
# self._instrumentation_loaded_successfully_call()
1176+
# not in mock_logger.debug.call_args_list
1177+
# )
1178+
# mock_logger.debug.assert_has_calls(
1179+
# [self._instrumentation_failed_to_load_call(dependency_conflict)]
1180+
# )
11421181

11431182
def _create_app(self):
11441183
# instrumentation is handled by the instrument call

0 commit comments

Comments
 (0)