Skip to content
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
@@ -1,13 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Types;
namespace Microsoft.EntityFrameworkCore.Types.Miscellaneous;

public class BoolTypeTest(BoolTypeTest.BoolTypeFixture fixture)
: TypeTestBase<bool, BoolTypeTest.BoolTypeFixture>(fixture)
{
public class BoolTypeFixture() : TypeTestFixture(true, false)
public class BoolTypeFixture : TypeTestFixture
{
public override bool Value { get; } = true;
public override bool OtherValue { get; } = false;

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -18,8 +21,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build
public class StringTypeTest(StringTypeTest.StringTypeFixture fixture)
: TypeTestBase<string, StringTypeTest.StringTypeFixture>(fixture)
{
public class StringTypeFixture() : TypeTestFixture("foo", "bar")
public class StringTypeFixture : TypeTestFixture
{
public override string Value { get; } = "foo";
public override string OtherValue { get; } = "bar";

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -30,10 +36,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build
public class GuidTypeTest(GuidTypeTest.GuidTypeFixture fixture)
: TypeTestBase<Guid, GuidTypeTest.GuidTypeFixture>(fixture)
{
public class GuidTypeFixture() : TypeTestFixture(
new Guid("8f7331d6-cde9-44fb-8611-81fff686f280"),
new Guid("ae192c36-9004-49b2-b785-8be10d169627"))
public class GuidTypeFixture : TypeTestFixture
{
public override Guid Value { get; } = new("8f7331d6-cde9-44fb-8611-81fff686f280");
public override Guid OtherValue { get; } = new("ae192c36-9004-49b2-b785-8be10d169627");

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -44,8 +51,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build
public class ByteArrayTypeTest(ByteArrayTypeTest.ByteArrayTypeFixture fixture)
: TypeTestBase<byte[], ByteArrayTypeTest.ByteArrayTypeFixture>(fixture)
{
public class ByteArrayTypeFixture() : TypeTestFixture([1, 2, 3], [4, 5, 6])
public class ByteArrayTypeFixture : TypeTestFixture
{
public override byte[] Value { get; } = [1, 2, 3];
public override byte[] OtherValue { get; } = [4, 5, 6, 7];

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand Down
37 changes: 29 additions & 8 deletions test/EFCore.Cosmos.FunctionalTests/Types/CosmosNumericTypeTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Types;
namespace Microsoft.EntityFrameworkCore.Types.Numeric;

public class ByteTypeTest(ByteTypeTest.ByteTypeFixture fixture) : TypeTestBase<byte, ByteTypeTest.ByteTypeFixture>(fixture)
{
public class ByteTypeFixture() : TypeTestFixture(byte.MinValue, byte.MaxValue)
public class ByteTypeFixture : TypeTestFixture
{
public override byte Value { get; } = byte.MinValue;
public override byte OtherValue { get; } = byte.MaxValue;

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -16,8 +19,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build

public class ShortTypeTest(ShortTypeTest.ShortTypeFixture fixture) : TypeTestBase<short, ShortTypeTest.ShortTypeFixture>(fixture)
{
public class ShortTypeFixture() : TypeTestFixture(short.MinValue, short.MaxValue)
public class ShortTypeFixture : TypeTestFixture
{
public override short Value { get; } = short.MinValue;
public override short OtherValue { get; } = short.MaxValue;

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -27,8 +33,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build

public class IntTypeTest(IntTypeTest.IntTypeFixture fixture) : TypeTestBase<int, IntTypeTest.IntTypeFixture>(fixture)
{
public class IntTypeFixture() : TypeTestFixture(int.MinValue, int.MaxValue)
public class IntTypeFixture : TypeTestFixture
{
public override int Value { get; } = int.MinValue;
public override int OtherValue { get; } = int.MaxValue;

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -38,8 +47,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build

public class LongTypeTest(LongTypeTest.LongTypeFixture fixture) : TypeTestBase<long, LongTypeTest.LongTypeFixture>(fixture)
{
public class LongTypeFixture() : TypeTestFixture(long.MinValue, long.MaxValue)
public class LongTypeFixture : TypeTestFixture
{
public override long Value { get; } = long.MinValue;
public override long OtherValue { get; } = long.MaxValue;

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -49,8 +61,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build

public class DecimalTypeTest(DecimalTypeTest.DecimalTypeFixture fixture) : TypeTestBase<decimal, DecimalTypeTest.DecimalTypeFixture>(fixture)
{
public class DecimalTypeFixture() : TypeTestFixture(30.5m, 30m)
public class DecimalTypeFixture : TypeTestFixture
{
public override decimal Value { get; } = 30.5m;
public override decimal OtherValue { get; } = 30m;

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -60,8 +75,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build

public class DoubleTypeTest(DoubleTypeTest.DoubleTypeFixture fixture) : TypeTestBase<double, DoubleTypeTest.DoubleTypeFixture>(fixture)
{
public class DoubleTypeFixture() : TypeTestFixture(30.5d, 30d)
public class DoubleTypeFixture : TypeTestFixture
{
public override double Value { get; } = 30.5d;
public override double OtherValue { get; } = 30d;

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -71,8 +89,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build

public class FloatTypeTest(FloatTypeTest.FloatTypeFixture fixture) : TypeTestBase<float, FloatTypeTest.FloatTypeFixture>(fixture)
{
public class FloatTypeFixture() : TypeTestFixture(30.5f, 30f)
public class FloatTypeFixture : TypeTestFixture
{
public override float Value { get; } = 30.5f;
public override float OtherValue { get; } = 30f;

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand Down
37 changes: 21 additions & 16 deletions test/EFCore.Cosmos.FunctionalTests/Types/CosmosTemporalTypeTest.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.EntityFrameworkCore.Types;
namespace Microsoft.EntityFrameworkCore.Types.Temporal;

public class DateTimeTypeTest(DateTimeTypeTest.DateTimeTypeFixture fixture)
: TypeTestBase<DateTime, DateTimeTypeTest.DateTimeTypeFixture>(fixture)
{
public class DateTimeTypeFixture() : TypeTestFixture(
new DateTime(2020, 1, 5, 12, 30, 45, DateTimeKind.Unspecified),
new DateTime(2022, 5, 3, 0, 0, 0, DateTimeKind.Unspecified))
public class DateTimeTypeFixture : TypeTestFixture
{
public override DateTime Value { get; } = new DateTime(2020, 1, 5, 12, 30, 45, DateTimeKind.Unspecified);
public override DateTime OtherValue { get; } = new DateTime(2022, 5, 3, 0, 0, 0, DateTimeKind.Unspecified);

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -20,10 +21,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build
public class DateTimeOffsetTypeTest(DateTimeOffsetTypeTest.DateTimeOffsetTypeFixture fixture)
: TypeTestBase<DateTimeOffset, DateTimeOffsetTypeTest.DateTimeOffsetTypeFixture>(fixture)
{
public class DateTimeOffsetTypeFixture() : TypeTestFixture(
new DateTimeOffset(2020, 1, 5, 12, 30, 45, TimeSpan.FromHours(2)),
new DateTimeOffset(2020, 1, 5, 12, 30, 45, TimeSpan.FromHours(3)))
public class DateTimeOffsetTypeFixture : TypeTestFixture
{
public override DateTimeOffset Value { get; } = new DateTimeOffset(2020, 1, 5, 12, 30, 45, TimeSpan.FromHours(2));
public override DateTimeOffset OtherValue { get; } = new DateTimeOffset(2020, 1, 5, 12, 30, 45, TimeSpan.FromHours(3));

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -33,10 +35,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build

public class DateOnlyTypeTest(DateOnlyTypeTest.DateTypeFixture fixture) : TypeTestBase<DateOnly, DateOnlyTypeTest.DateTypeFixture>(fixture)
{
public class DateTypeFixture() : TypeTestFixture(
new DateOnly(2020, 1, 5),
new DateOnly(2022, 5, 3))
public class DateTypeFixture : TypeTestFixture
{
public override DateOnly Value { get; } = new DateOnly(2020, 1, 5);
public override DateOnly OtherValue { get; } = new DateOnly(2022, 5, 3);

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -47,10 +50,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build
public class TimeOnlyTypeTest(TimeOnlyTypeTest.TimeTypeFixture fixture)
: TypeTestBase<TimeOnly, TimeOnlyTypeTest.TimeTypeFixture>(fixture)
{
public class TimeTypeFixture() : TypeTestFixture(
new TimeOnly(12, 30, 45),
new TimeOnly(14, 0, 0))
public class TimeTypeFixture : TypeTestFixture
{
public override TimeOnly Value { get; } = new TimeOnly(12, 30, 45);
public override TimeOnly OtherValue { get; } = new TimeOnly(14, 0, 0);

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand All @@ -60,10 +64,11 @@ public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder build

public class TimeSpanTypeTest(TimeSpanTypeTest.TimeSpanTypeFixture fixture) : TypeTestBase<TimeSpan, TimeSpanTypeTest.TimeSpanTypeFixture>(fixture)
{
public class TimeSpanTypeFixture() : TypeTestFixture(
new TimeSpan(12, 30, 45),
new TimeSpan(14, 0, 0))
public class TimeSpanTypeFixture : TypeTestFixture
{
public override TimeSpan Value { get; } = new TimeSpan(12, 30, 45);
public override TimeSpan OtherValue { get; } = new TimeSpan(14, 0, 0);

protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;

public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
Expand Down
100 changes: 89 additions & 11 deletions test/EFCore.Relational.Specification.Tests/RelationalTypeTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,43 @@ public abstract class RelationalTypeTestBase<T, TFixture>(TFixture fixture) : Ty
where TFixture : RelationalTypeTestBase<T, TFixture>.RelationalTypeTestFixture
where T : notnull
{
public RelationalTypeTestBase(TFixture fixture, ITestOutputHelper testOutputHelper)
: this(fixture)
{
Fixture.TestSqlLoggerFactory.Clear();
Fixture.TestSqlLoggerFactory.SetTestOutputHelper(testOutputHelper);
}

#region SaveChanges

[ConditionalFact]
public virtual async Task SaveChanges_within_json()
=> await TestHelpers.ExecuteWithStrategyInTransactionAsync(
Fixture.CreateContext,
Fixture.UseTransaction,
async context =>
{
JsonTypeEntity entity;

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
entity = await context.Set<JsonTypeEntity>().SingleAsync(e => e.Id == 1);
}

entity.JsonContainer.Value = Fixture.OtherValue;
await context.SaveChangesAsync();

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);
}
});

#endregion SaveChanges

#region ExecuteUpdate

[ConditionalFact]
public virtual async Task ExecuteUpdate_within_json_to_parameter()
=> await TestHelpers.ExecuteWithStrategyInTransactionAsync(
Expand All @@ -15,8 +52,12 @@ public virtual async Task ExecuteUpdate_within_json_to_parameter()
async context =>
{
await context.Set<JsonTypeEntity>().ExecuteUpdateAsync(s => s.SetProperty(e => e.JsonContainer.Value, e => Fixture.OtherValue));
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);
}
});

[ConditionalFact]
Expand All @@ -33,8 +74,12 @@ public virtual async Task ExecuteUpdate_within_json_to_constant()
parameter);

await context.Set<JsonTypeEntity>().ExecuteUpdateAsync(s => s.SetProperty(e => e.JsonContainer.Value, valueExpression));
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);
}
});

[ConditionalFact]
Expand All @@ -45,8 +90,12 @@ public virtual async Task ExecuteUpdate_within_json_to_another_json_property()
async context =>
{
await context.Set<JsonTypeEntity>().ExecuteUpdateAsync(s => s.SetProperty(e => e.JsonContainer.Value, e => e.JsonContainer.OtherValue));
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);
}
});

[ConditionalFact]
Expand All @@ -57,10 +106,16 @@ public virtual async Task ExecuteUpdate_within_json_to_nonjson_column()
async context =>
{
await context.Set<JsonTypeEntity>().ExecuteUpdateAsync(s => s.SetProperty(e => e.JsonContainer.Value, e => e.OtherValue));
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);

using (Fixture.TestSqlLoggerFactory.SuspendRecordingEvents())
{
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);
}
});

#endregion ExecuteUpdate

protected class JsonTypeEntity
{
public int Id { get; set; }
Expand All @@ -77,17 +132,37 @@ public class JsonContainer
public required T OtherValue { get; set; }
}

public abstract class RelationalTypeTestFixture(T value, T otherValue)
: TypeTestFixture(value, otherValue)
protected void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);

protected void AssertExecuteUpdateSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected, forUpdate: true);

public abstract class RelationalTypeTestFixture : TypeTestFixture, ITestSqlLoggerFactory
{
public virtual string? StoreType => null;

protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
base.OnModelCreating(modelBuilder, context);

modelBuilder.Entity<TypeEntity>(b =>
{
b.Property(e => e.Value).HasColumnType(StoreType);
b.Property(e => e.OtherValue).HasColumnType(StoreType);
});

modelBuilder.Entity<JsonTypeEntity>(b =>
{
modelBuilder.Entity<JsonTypeEntity>().Property(e => e.Id).ValueGeneratedNever();
b.ComplexProperty(e => e.JsonContainer, cb => cb.ToJson());

b.ComplexProperty(e => e.JsonContainer, jc =>
{
jc.ToJson();

jc.Property(e => e.Value).HasColumnType(StoreType);
jc.Property(e => e.OtherValue).HasColumnType(StoreType);
});
});
}

Expand Down Expand Up @@ -122,6 +197,9 @@ protected override async Task SeedAsync(DbContext context)
await context.SaveChangesAsync();
}

public TestSqlLoggerFactory TestSqlLoggerFactory
=> (TestSqlLoggerFactory)ListLoggerFactory;

public virtual void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());
}
Expand Down
Loading
Loading