Skip to content

Implement missing unit tests for MemoryContext and ProductService in 01-SemanticSearch scenario#48

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/fix-47
Closed

Implement missing unit tests for MemoryContext and ProductService in 01-SemanticSearch scenario#48
Copilot wants to merge 3 commits intomainfrom
copilot/fix-47

Conversation

Copy link
Contributor

Copilot AI commented Sep 1, 2025

This PR implements comprehensive unit tests for the semantic search functionality in the scenarios/01-SemanticSearch project, addressing the missing test coverage for core components.

Changes Made

Products.Tests

  • Added Moq dependency to enable proper mocking of external dependencies
  • Implemented MemoryContextTests.cs with the required InitMemoryContextAsync_PopulatesCollection test method
  • Created custom TestEmbeddingClient to handle the IEmbeddingGenerator interface since extension methods cannot be directly mocked
  • Validates core functionality: Products are read from database, embeddings are generated, vectors are upserted to memory collection, and initialization returns true
  • Includes proper logging verification to ensure initialization and completion messages are logged

Store.Tests

  • Converted from xUnit to MSTest framework for consistency with existing Products.Tests
  • Added comprehensive ProductService tests covering both semantic and standard search functionality:
    • Search_WithSemanticSearch_CallsAISearchEndpoint: Tests AI-powered semantic search endpoint calls
    • Search_WithStandardSearch_CallsProductSearchEndpoint: Tests standard product search endpoint calls
    • Search_WhenHttpCallFails_ReturnsDefaultResponse: Tests error handling when HTTP calls fail
  • Uses Moq to mock HttpClient behavior for reliable and isolated testing

Key Implementation Details

The MemoryContext.InitMemoryContextAsync method testing required special handling because it uses the GenerateVectorAsync extension method from Microsoft.Extensions.AI. Since Moq cannot mock extension methods directly, a custom TestEmbeddingClient was implemented that satisfies the IEmbeddingGenerator<string, Embedding<float>> interface.

The Store tests mock HTTP calls using Mock<HttpMessageHandler> to verify that the correct endpoints are called for semantic vs. standard search, and that error conditions are handled gracefully.

Test Results

  • Products.Tests: 7 tests passing (including 1 new MemoryContext test)
  • Store.Tests: 3 tests passing (all new ProductService tests)
  • Total: 10/10 tests successful

Both test projects now use MSTest framework consistently and provide comprehensive coverage of the semantic search initialization and search functionality.

Fixes #47.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits September 1, 2025 16:51
Co-authored-by: elbruno <3533489+elbruno@users.noreply.github.com>
Co-authored-by: elbruno <3533489+elbruno@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement missing Unit Tests Implement missing unit tests for MemoryContext and ProductService in 01-SemanticSearch scenario Sep 1, 2025
Copilot AI requested a review from elbruno September 1, 2025 16:56
@elbruno elbruno closed this Jan 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement missing Unit Tests

3 participants