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

Preserve unicodeness and fixed-lengthiness in compiled model. #32662

Merged
merged 1 commit into from
Jan 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ public virtual void Initialize(IDbContextOptions options)
Region = cosmosOptions.Region;
PreferredRegions = cosmosOptions.PreferredRegions;
LimitToEndpoint = cosmosOptions.LimitToEndpoint;
EnableContentResponseOnWrite = cosmosOptions.EnableContentResponseOnWrite;
ConnectionMode = cosmosOptions.ConnectionMode;
WebProxy = cosmosOptions.WebProxy;
RequestTimeout = cosmosOptions.RequestTimeout;
Expand Down Expand Up @@ -208,7 +207,6 @@ public virtual void Validate(IDbContextOptions options)
|| GatewayModeMaxConnectionLimit != cosmosOptions.GatewayModeMaxConnectionLimit
|| MaxTcpConnectionsPerEndpoint != cosmosOptions.MaxTcpConnectionsPerEndpoint
|| MaxRequestsPerTcpConnection != cosmosOptions.MaxRequestsPerTcpConnection
|| EnableContentResponseOnWrite != cosmosOptions.EnableContentResponseOnWrite
|| HttpClientFactory != cosmosOptions.HttpClientFactory
))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2154,7 +2154,15 @@ public override bool Create(
&& relationalTypeMapping.Scale != defaultInstance.Scale;
var dbTypeDifferent = relationalTypeMapping.DbType != null
&& relationalTypeMapping.DbType != defaultInstance.DbType;
if (storeTypeDifferent || sizeDifferent || precisionDifferent || scaleDifferent || dbTypeDifferent)
var isUnicodeDifferent = relationalTypeMapping.IsUnicode != defaultInstance.IsUnicode;
var isFixedLengthDifferent = relationalTypeMapping.IsFixedLength != defaultInstance.IsFixedLength;
if (storeTypeDifferent
|| sizeDifferent
|| precisionDifferent
|| scaleDifferent
|| dbTypeDifferent
|| isUnicodeDifferent
|| isFixedLengthDifferent)
{
AddNamespace(typeof(RelationalTypeMappingInfo), parameters.Namespaces);
mainBuilder.AppendLine(",")
Expand All @@ -2174,6 +2182,18 @@ public override bool Create(
"size", code.Literal(relationalTypeMapping.Size), mainBuilder, ref firstParameter);
}

if (isUnicodeDifferent)
{
GenerateArgument(
"unicode", code.Literal(relationalTypeMapping.IsUnicode), mainBuilder, ref firstParameter);
}

if (isFixedLengthDifferent)
{
GenerateArgument(
"fixedLength", code.Literal(relationalTypeMapping.IsFixedLength), mainBuilder, ref firstParameter);
}

if (precisionDifferent)
{
GenerateArgument(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ public SqlServerStringTypeMapping(
private static string GetDefaultStoreName(bool unicode, bool fixedLength)
=> unicode
? fixedLength ? "nchar" : "nvarchar"
: fixedLength
? "char"
: "varchar";
: fixedLength ? "char" : "varchar";

private static DbType? GetDbType(bool unicode, bool fixedLength)
=> unicode
Expand Down
95 changes: 9 additions & 86 deletions test/EFCore.Cosmos.FunctionalTests/CosmosConcurrencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,91 +45,21 @@ public virtual Task Updating_then_updating_the_same_entity_results_in_DbUpdateCo
ctx => ctx.Customers.Single(c => c.Id == "3").Name = "Updated",
ctx => ctx.Customers.Single(c => c.Id == "3").Name = "Updated");

[ConditionalFact]
public async Task Etag_will_return_when_content_response_enabled_false()
{
await using var testDatabase = CosmosTestStore.CreateInitialized(
DatabaseName,
o => o.ContentResponseOnWriteEnabled(false));

var customer = new Customer
{
Id = "4", Name = "Theon",
};

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
await context.Database.EnsureCreatedAsync();

await context.AddAsync(customer);

await context.SaveChangesAsync();
}

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
var customerFromStore = await context.Set<Customer>().SingleAsync();

Assert.Equal(customer.Id, customerFromStore.Id);
Assert.Equal("Theon", customerFromStore.Name);
Assert.Equal(customer.ETag, customerFromStore.ETag);

context.Remove(customerFromStore);

await context.SaveChangesAsync();
}
}

[ConditionalFact]
public async Task Etag_will_return_when_content_response_enabled_true()
{
await using var testDatabase = CosmosTestStore.CreateInitialized(
DatabaseName,
o => o.ContentResponseOnWriteEnabled());

var customer = new Customer
{
Id = "3", Name = "Theon",
};

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
await context.Database.EnsureCreatedAsync();

await context.AddAsync(customer);

await context.SaveChangesAsync();
}

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
{
var customerFromStore = await context.Set<Customer>().SingleAsync();

Assert.Equal(customer.Id, customerFromStore.Id);
Assert.Equal("Theon", customerFromStore.Name);
Assert.Equal(customer.ETag, customerFromStore.ETag);

context.Remove(customerFromStore);

await context.SaveChangesAsync();
}
}

[ConditionalTheory]
[InlineData(null)]
[InlineData(true)]
[InlineData(false)]
public async Task Etag_is_updated_in_entity_after_SaveChanges(bool? contentResponseOnWriteEnabled)
{
await using var testDatabase = CosmosTestStore.CreateInitialized(
DatabaseName,
o =>
var options = new DbContextOptionsBuilder(Fixture.CreateOptions())
.UseCosmos(o =>
{
if (contentResponseOnWriteEnabled.HasValue)
if (contentResponseOnWriteEnabled != null)
{
o.ContentResponseOnWriteEnabled(contentResponseOnWriteEnabled.Value);
}
});
})
.Options;

var customer = new Customer
{
Expand All @@ -139,10 +69,9 @@ public async Task Etag_is_updated_in_entity_after_SaveChanges(bool? contentRespo
};

string etag = null;

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
await using (var context = new ConcurrencyContext(options))
{
await context.Database.EnsureCreatedAsync();
await Fixture.TestStore.CleanAsync(context);

await context.AddAsync(customer);

Expand All @@ -151,7 +80,7 @@ public async Task Etag_is_updated_in_entity_after_SaveChanges(bool? contentRespo
etag = customer.ETag;
}

await using (var context = new ConcurrencyContext(CreateOptions(testDatabase)))
await using (var context = new ConcurrencyContext(options))
{
var customerFromStore = await context.Set<Customer>().SingleAsync();

Expand Down Expand Up @@ -208,7 +137,7 @@ protected virtual async Task ConcurrencyTestAsync<TException>(
where TException : DbUpdateException
{
using var outerContext = CreateContext();
await outerContext.Database.EnsureCreatedAsync();
await Fixture.TestStore.CleanAsync(outerContext);
seedAction?.Invoke(outerContext);
await outerContext.SaveChangesAsync();

Expand Down Expand Up @@ -258,12 +187,6 @@ protected override void OnModelCreating(ModelBuilder builder)
});
}

private DbContextOptions CreateOptions(CosmosTestStore testDatabase)
=> testDatabase.AddProviderOptions(new DbContextOptionsBuilder())
.ConfigureWarnings(w => w.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning))
.EnableDetailedErrors()
.Options;

public class Customer
{
public string Id { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
namespace Microsoft.EntityFrameworkCore;

public class MaterializationInterceptionCosmosTest :
MaterializationInterceptionTestBase<MaterializationInterceptionCosmosTest.CosmosLibraryContext>,
IClassFixture<MaterializationInterceptionCosmosTest.MaterializationInterceptionCosmosFixture>
MaterializationInterceptionTestBase<MaterializationInterceptionCosmosTest.CosmosLibraryContext>
{
public MaterializationInterceptionCosmosTest(MaterializationInterceptionCosmosFixture fixture)
: base(fixture)
{
}

public override Task Intercept_query_materialization_with_owned_types_projecting_collection(bool async)
public override Task Intercept_query_materialization_with_owned_types_projecting_collection(bool async, bool usePooling)
=> Task.CompletedTask;

public class CosmosLibraryContext : LibraryContext
Expand All @@ -30,20 +24,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
}

public override LibraryContext CreateContext(IEnumerable<ISingletonInterceptor> interceptors, bool inject)
=> new CosmosLibraryContext(Fixture.CreateOptions(interceptors, inject));

public class MaterializationInterceptionCosmosFixture : SingletonInterceptorsFixtureBase
{
protected override string StoreName
=> "MaterializationInterception";

protected override ITestStoreFactory TestStoreFactory
=> CosmosTestStoreFactory.Instance;

protected override IServiceCollection InjectInterceptors(
IServiceCollection serviceCollection,
IEnumerable<ISingletonInterceptor> injectedInterceptors)
=> base.InjectInterceptors(serviceCollection.AddEntityFrameworkCosmos(), injectedInterceptors);
}
protected override ITestStoreFactory TestStoreFactory
=> CosmosTestStoreFactory.Instance;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ protected CosmosTestStoreFactory()
public IServiceCollection AddProviderServices(IServiceCollection serviceCollection)
=> serviceCollection
.AddEntityFrameworkCosmos()
.AddSingleton<ILoggerFactory>(new TestSqlLoggerFactory())
.AddSingleton<TestStoreIndex>();
.AddSingleton<ILoggerFactory>(new TestSqlLoggerFactory());

public TestStore Create(string storeName)
=> CosmosTestStore.Create(storeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@
namespace Microsoft.EntityFrameworkCore;

public class MaterializationInterceptionInMemoryTest :
MaterializationInterceptionTestBase<MaterializationInterceptionInMemoryTest.InMemoryLibraryContext>,
IClassFixture<MaterializationInterceptionInMemoryTest.MaterializationInterceptionInMemoryFixture>
MaterializationInterceptionTestBase<MaterializationInterceptionInMemoryTest.InMemoryLibraryContext>
{
public MaterializationInterceptionInMemoryTest(MaterializationInterceptionInMemoryFixture fixture)
: base(fixture)
{
}

public class InMemoryLibraryContext : LibraryContext
{
public InMemoryLibraryContext(DbContextOptions options)
Expand All @@ -29,23 +23,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}
}

public override LibraryContext CreateContext(IEnumerable<ISingletonInterceptor> interceptors, bool inject)
=> new InMemoryLibraryContext(Fixture.CreateOptions(interceptors, inject));

public class MaterializationInterceptionInMemoryFixture : SingletonInterceptorsFixtureBase
{
protected override string StoreName
=> "MaterializationInterception";

protected override ITestStoreFactory TestStoreFactory
=> InMemoryTestStoreFactory.Instance;
protected override ITestStoreFactory TestStoreFactory
=> InMemoryTestStoreFactory.Instance;

protected override IServiceCollection InjectInterceptors(
IServiceCollection serviceCollection,
IEnumerable<ISingletonInterceptor> injectedInterceptors)
=> base.InjectInterceptors(serviceCollection.AddEntityFrameworkInMemoryDatabase(), injectedInterceptors);

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base.AddOptions(builder).ConfigureWarnings(c => c.Ignore(InMemoryEventId.TransactionIgnoredWarning));
}
protected override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
=> base.AddOptions(builder).ConfigureWarnings(c => c.Ignore(InMemoryEventId.TransactionIgnoredWarning));
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public InMemoryTestStore InitializeInMemory(
protected override TestStoreIndex GetTestStoreIndex(IServiceProvider serviceProvider)
=> serviceProvider == null
? base.GetTestStoreIndex(null)
: serviceProvider.GetRequiredService<TestStoreIndex>();
: serviceProvider.GetService<TestStoreIndex>() ?? base.GetTestStoreIndex(serviceProvider);

public override DbContextOptionsBuilder AddProviderOptions(DbContextOptionsBuilder builder)
=> builder.UseInMemoryDatabase(Name);
Expand Down
Loading
Loading