Skip to content

Fix Cosmos tests when running against a real endpoint - #38710

Merged
AndriySvyryd merged 2 commits into
mainfrom
copilot/fix-cosmos-tests-endpoint
Jul 30, 2026
Merged

AndriySvyryd merged 2 commits into
mainfrom
copilot/fix-cosmos-tests-endpoint

Conversation

Copilot AI commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Three tests fail when running against a real Azure Cosmos DB endpoint (token credential auth or non-emulator key).

Changes

  • CosmosSessionTokensTest.Read_item_uses_session_token: The Cosmos service changed the error message for invalid session tokens on point reads (ReadItemAsync), while the query API path still returns the old wording. Replaced the brittle ResponseBody string check with Assert.Equal(HttpStatusCode.BadRequest, ex.StatusCode).

  • CosmosTransactionalBatchTest (two tests): Both tests manually constructed DbContextOptionsBuilder hardcoded to use AuthToken. When UseTokenCredential=true, AuthToken is still the emulator default key, causing 401 Unauthorized against a real endpoint. Fixed by mirroring the CosmosTestStore.AddProviderOptions pattern — conditionally selecting TokenCredential or AuthToken based on CosmosTestEnvironment.UseTokenCredential:

var store = (CosmosTestStore)Fixture.TestStore;
var optionsBuilder = new DbContextOptionsBuilder<TransactionalBatchContext>();
if (CosmosTestEnvironment.UseTokenCredential)
{
    optionsBuilder.UseCosmos(store.ConnectionUri, store.TokenCredential, Fixture.TestStore.Name, cfg => ...);
}
else
{
    optionsBuilder.UseCosmos(store.ConnectionUri, store.AuthToken, Fixture.TestStore.Name, cfg => ...);
}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR improves the reliability of EF Core Cosmos functional tests when running against a real Azure Cosmos DB endpoint (token credential auth or non-emulator keys), avoiding emulator-specific assumptions and brittle service error message checks.

Changes:

  • Updates CosmosSessionTokensTest.Read_item_uses_session_token to assert on CosmosException.StatusCode (BadRequest) instead of matching a specific ResponseBody message.
  • Fixes two CosmosTransactionalBatchTest cases by selecting TokenCredential vs AuthToken based on CosmosTestEnvironment.UseTokenCredential, preventing 401s against real endpoints.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
test/EFCore.Cosmos.FunctionalTests/CosmosTransactionalBatchTest.cs Adjusts options setup to work with token-credential auth when running against real Cosmos endpoints.
test/EFCore.Cosmos.FunctionalTests/CosmosSessionTokensTest.cs Makes the point-read session token assertion resilient to service-side error message wording changes.
Comments suppressed due to low confidence (1)

test/EFCore.Cosmos.FunctionalTests/CosmosTransactionalBatchTest.cs:107

  • This test can use CosmosTestStore.AddProviderOptions to handle the token-credential vs auth-key selection and ApplyConfiguration, avoiding duplicated branching and keeping the setup consistent with other Cosmos functional tests.
        var store = (CosmosTestStore)Fixture.TestStore;
        var optionsBuilder = new DbContextOptionsBuilder<TransactionalBatchContext>();
        if (CosmosTestEnvironment.UseTokenCredential)
        {
            optionsBuilder.UseCosmos(store.ConnectionUri, store.TokenCredential, Fixture.TestStore.Name, cfg => cfg.ApplyConfiguration());

Copilot AI requested a review from AndriySvyryd July 29, 2026 00:42
@AndriySvyryd
AndriySvyryd requested a review from cincuranet July 29, 2026 01:00
@AndriySvyryd
AndriySvyryd marked this pull request as ready for review July 29, 2026 01:00
@AndriySvyryd
AndriySvyryd requested a review from a team as a code owner July 29, 2026 01:00
Copilot AI review requested due to automatic review settings July 29, 2026 01:00
@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) July 29, 2026 01:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@AndriySvyryd
AndriySvyryd merged commit af85674 into main Jul 30, 2026
16 checks passed
@AndriySvyryd
AndriySvyryd deleted the copilot/fix-cosmos-tests-endpoint branch July 30, 2026 09:03
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 6, 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.

4 participants