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

Use ARM to create containers for Cosmos tests. #33994

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 4 additions & 17 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,24 +212,11 @@ public virtual Task<bool> CanConnectAsync(CancellationToken cancellationToken =
=> throw new NotSupportedException(CosmosStrings.CanConnectNotSupported);

/// <summary>
/// Returns the store name of the property that is used to store the partition key.
/// </summary>
/// <param name="entityType">The entity type to get the partition key property name for.</param>
/// <returns>The name of the partition key property.</returns>
[Obsolete("Use GetPartitionKeyStoreNames")]
private static string GetPartitionKeyStoreName(IEntityType entityType)
{
var name = entityType.GetPartitionKeyPropertyName();
return name != null
? entityType.FindProperty(name)!.GetJsonPropertyName()
: CosmosClientWrapper.DefaultPartitionKey;
}

/// <summary>
/// Returns the store names of the properties that is used to store the partition keys.
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
/// <param name="entityType">The entity type to get the partition key property names for.</param>
/// <returns>The names of the partition key property.</returns>
private static IReadOnlyList<string> GetPartitionKeyStoreNames(IEntityType entityType)
{
var properties = entityType.GetPartitionKeyProperties();
Expand Down
16 changes: 16 additions & 0 deletions test/EFCore.Cosmos.FunctionalTests/F1CosmosFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ protected override ITestStoreFactory TestStoreFactory
public override TestHelpers TestHelpers
=> CosmosTestHelpers.Instance;

public override async Task ReseedAsync()
{
await base.ReseedAsync();

using var context = CreateContext();
try
{
await context.Teams.SingleAsync(t => t.Id == Team.Ferrari);
}
catch (Exception)
{
// Recreating the containers without using CosmosClient causes cached metadata in CosmosClient to be out of sync
// and causes the first query to fail. This is a workaround for that.
}
}

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base.AddOptions(builder).ConfigureWarnings(w => w.Ignore(CosmosEventId.NoPartitionKeyDefined));

Expand Down
70 changes: 39 additions & 31 deletions test/EFCore.Cosmos.FunctionalTests/ReloadTest.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Azure.Core;
using Newtonsoft.Json.Linq;

namespace Microsoft.EntityFrameworkCore;

#nullable disable

public class ReloadTest
public class ReloadTest : IClassFixture<ReloadTest.CosmosReloadTestFixture>
{
public static IEnumerable<object[]> IsAsyncData = new object[][] { [false], [true] };
public static IEnumerable<object[]> IsAsyncData = [[false], [true]];

private void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

protected void ClearLog()
=> Fixture.TestSqlLoggerFactory.Clear();

protected CosmosReloadTestFixture Fixture { get; }

public ReloadTest(CosmosReloadTestFixture fixture)
{
Fixture = fixture;
ClearLog();
}

[ConditionalFact]
public async Task Entity_reference_can_be_reloaded()
{
await using var testDatabase = await CosmosTestStore.CreateInitializedAsync("ReloadTest");

using var context = new ReloadTestContext(testDatabase);
await context.Database.EnsureCreatedAsync();
using var context = CreateContext();

var entry = await context.AddAsync(new Item { Id = 1337 });

Expand All @@ -33,35 +43,33 @@ public async Task Entity_reference_can_be_reloaded()
Assert.Null(itemJson["unmapped"]);
}

public class ReloadTestContext(CosmosTestStore testStore) : DbContext
protected ReloadTestContext CreateContext()
=> Fixture.CreateContext();

public class CosmosReloadTestFixture : SharedStoreFixtureBase<ReloadTestContext>
{
private readonly string _connectionUri = testStore.ConnectionUri;
private readonly string _authToken = testStore.AuthToken;
private readonly string _name = testStore.Name;
private readonly TokenCredential _tokenCredential = testStore.TokenCredential;
protected override string StoreName
=> nameof(ReloadTest);

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (TestEnvironment.UseTokenCredential)
{
optionsBuilder.UseCosmos(
_connectionUri,
_tokenCredential,
_name,
b => b.ApplyConfiguration());
}
else
{
optionsBuilder.UseCosmos(
_connectionUri,
_authToken,
_name,
b => b.ApplyConfiguration());
}
}
protected override bool UsePooling
=> false;

protected override ITestStoreFactory TestStoreFactory
=> CosmosTestStoreFactory.Instance;

public TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ServiceProvider.GetRequiredService<ILoggerFactory>();
}

public class ReloadTestContext(DbContextOptions dbContextOptions) : DbContext(dbContextOptions)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>(
b =>
{
b.HasPartitionKey(e => e.Id);
});
}

public DbSet<Item> Items { get; set; }
Expand Down
Loading