Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: Improve test coverage #10681

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

rracanicci
Copy link
Contributor

Motivation and Context

Adding more tests to improve overall coverage.

Description

  • adding tests for bedrock agent channel
  • adding tests for bedrock model provider utils
  • adding tests for mistral ai chat completion
  • tests for bing search
  • tests for google search

Contribution Checklist

@rracanicci rracanicci requested a review from a team as a code owner February 25, 2025 17:55
@markwallace-microsoft markwallace-microsoft added the python Pull requests for the Python Semantic Kernel label Feb 25, 2025
@github-actions github-actions bot changed the title Improve test coverage Python: Improve test coverage Feb 25, 2025
@markwallace-microsoft
Copy link
Member

markwallace-microsoft commented Feb 25, 2025

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
TOTAL19616216689% 
report-only-changed-files is enabled. No files were changed during this commit :)

Python Unit Test Overview

Tests Skipped Failures Errors Time
3274 5 💤 0 ❌ 0 🔥 1m 23s ⏱️

assert "lr=lang_en" in query_str


@pytest.mark.asyncio
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, we have default mark for this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed


a = channel.messages

print(a)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(a)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

mock_agent.agent_model.foundation_model = "mock-foundation-model"

# Mock create_session_id to return a fixed session id
mock_agent.create_session_id.return_value = "test-session-id"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just removed this :)

assert "No chat history available" in str(exc_info.value)


@patch("semantic_kernel.agents.bedrock.bedrock_agent.BedrockAgent.create_session_id", return_value="session-id")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: probably don't need to mock this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed this one as well :)

assert "No chat history available." in str(exc_info.value)


@patch("semantic_kernel.agents.bedrock.bedrock_agent.BedrockAgent.create_session_id", return_value="session-id")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed :)

"""Test get_history yields messages in reverse order."""
channel, _ = channel_and_agent

channel.messages.append(ChatMessageContent(role=AuthorRole.USER, content="User1"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chat_history fixture can be used here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

def test_format_system_message() -> None:
"""Test that system message is formatted correctly."""
content = ChatMessageContent(role=AuthorRole.SYSTEM, content="System message")
formatted = _format_system_message(content)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should not test any private method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main idea: https://stackoverflow.com/a/250719. We generally make sure these methods behave correctly via integration tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating this to test through the public MESSAGE_CONVERTERS as in other unit tests.

@@ -372,3 +385,365 @@ async def test_with_different_execution_settings_stream(
continue
assert mock_mistral_ai_client_completion_stream.chat.stream_async.call_args.kwargs["temperature"] == 0.2
assert mock_mistral_ai_client_completion_stream.chat.stream_async.call_args.kwargs["seed"] == 2


async def test_mistral_ai_chat_completion_initialization_valid():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can set the environment variables in a fixture so that the setting object can be instantiated. Please refer to the sample here: https://github.com/microsoft/semantic-kernel/blob/main/python/tests/unit/agents/bedrock_agent/conftest.py#L14

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the goal of this test would be to actually validate both the settings creation and mistral base are being called. Which is why they are being patched, I am happy to update otherwise though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this one actually, this seems to be redundant with other test.

assert "The MistralAI chat model ID is required." in str(exc_info.value)


async def test_mistral_ai_chat_completion_initialization_validation_error():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated :)

assert "Failed to create MistralAI settings." in str(exc_info.value)


async def test_mistral_ai_chat_completion_inner_get_chat_message_contents_success():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, we shouldn't be testing the _inner_xxx method in unit tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update those to use the proper public methods.

async def test_bing_search_init_success():
"""Test that BingSearch initializes successfully with valid parameters."""
# Arrange/Act
with patch.object(BingSettings, "create", return_value=MagicMock(spec=BingSettings)) as mock_settings_create:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a fixture for the environment variable to instantiate the settings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we have bing_unit_test_env in the base level conftest

Copy link
Contributor Author

@rracanicci rracanicci Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests updated to use bing_unit_test_env :). Thank you

Copy link
Contributor

@moonbox3 moonbox3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, @rracanicci.

mock_channel.messages.append(ChatMessageContent(role=AuthorRole.USER, content="User message"))

# Create a dummy agent that is not BedrockAgent
non_bedrock_agent = Agent()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will now fail because in the last few days we updated the Agent to be an ABC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests have been updated accordingly :)

@rracanicci rracanicci requested a review from a team as a code owner March 5, 2025 16:00
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel kernel.core documentation memory labels Mar 5, 2025
@github-actions github-actions bot changed the title Python: Improve test coverage .Net: Python: Improve test coverage Mar 5, 2025
@rracanicci rracanicci force-pushed the feature/improve-test-coverage branch from 50cd0c6 to 4fb3101 Compare March 5, 2025 16:03
@markwallace-microsoft markwallace-microsoft removed .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel kernel.core documentation memory labels Mar 5, 2025
@rracanicci rracanicci changed the title .Net: Python: Improve test coverage Python: Improve test coverage Mar 5, 2025
@rracanicci rracanicci force-pushed the feature/improve-test-coverage branch 2 times, most recently from 19bb678 to a815b23 Compare March 7, 2025 10:28
@rracanicci rracanicci force-pushed the feature/improve-test-coverage branch from 4476f53 to 8f62a96 Compare March 7, 2025 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
python Pull requests for the Python Semantic Kernel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants