Skip to content

Commit

Permalink
Use ARM to create containers for Cosmos tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Jun 14, 2024
1 parent 22fa9d5 commit 41cae8a
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 114 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace Microsoft.EntityFrameworkCore;
public class OptimisticConcurrencyCosmosTest(F1CosmosFixture<byte[]> fixture)
: OptimisticConcurrencyTestBase<F1CosmosFixture<byte[]>, byte[]>(fixture), IAsyncLifetime
{
[ConditionalFact(Skip = "Issue #33993")]
public override Task Change_in_independent_association_results_in_independent_association_exception() => base.Change_in_independent_association_results_in_independent_association_exception();

// Non-persisted property in query
// Issue #17670
public override Task Calling_GetDatabaseValues_on_owned_entity_works(bool async)
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

0 comments on commit 41cae8a

Please sign in to comment.