Skip to content

Commit a9cdd9e

Browse files
committed
Redo type tests
1 parent edc1790 commit a9cdd9e

File tree

17 files changed

+838
-479
lines changed

17 files changed

+838
-479
lines changed

test/EFCore.Cosmos.FunctionalTests/StoreTypeCosmosTest.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.Types;
5+
6+
public class BoolTypeTest(BoolTypeTest.BoolTypeFixture fixture)
7+
: TypeTestBase<bool, BoolTypeTest.BoolTypeFixture>(fixture)
8+
{
9+
public class BoolTypeFixture() : TypeTestFixture(true, false)
10+
{
11+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
12+
13+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
14+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
15+
}
16+
}
17+
18+
public class StringTypeTest(StringTypeTest.StringTypeFixture fixture)
19+
: TypeTestBase<string, StringTypeTest.StringTypeFixture>(fixture)
20+
{
21+
public class StringTypeFixture() : TypeTestFixture("foo", "bar")
22+
{
23+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
24+
25+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
26+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
27+
}
28+
}
29+
30+
public class GuidTypeTest(GuidTypeTest.GuidTypeFixture fixture)
31+
: TypeTestBase<Guid, GuidTypeTest.GuidTypeFixture>(fixture)
32+
{
33+
public class GuidTypeFixture() : TypeTestFixture(
34+
new Guid("8f7331d6-cde9-44fb-8611-81fff686f280"),
35+
new Guid("ae192c36-9004-49b2-b785-8be10d169627"))
36+
{
37+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
38+
39+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
40+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
41+
}
42+
}
43+
44+
public class ByteArrayTypeTest(ByteArrayTypeTest.ByteArrayTypeFixture fixture)
45+
: TypeTestBase<byte[], ByteArrayTypeTest.ByteArrayTypeFixture>(fixture)
46+
{
47+
public class ByteArrayTypeFixture() : TypeTestFixture([1, 2, 3], [4, 5, 6])
48+
{
49+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
50+
51+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
52+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
53+
54+
public override Func<byte[], byte[], bool> Comparer { get; } = (a, b) => a.SequenceEqual(b);
55+
}
56+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.Types;
5+
6+
public class ByteTypeTest(ByteTypeTest.ByteTypeFixture fixture) : TypeTestBase<byte, ByteTypeTest.ByteTypeFixture>(fixture)
7+
{
8+
public class ByteTypeFixture() : TypeTestFixture(byte.MinValue, byte.MaxValue)
9+
{
10+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
11+
12+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
13+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
14+
}
15+
}
16+
17+
public class ShortTypeTest(ShortTypeTest.ShortTypeFixture fixture) : TypeTestBase<short, ShortTypeTest.ShortTypeFixture>(fixture)
18+
{
19+
public class ShortTypeFixture() : TypeTestFixture(short.MinValue, short.MaxValue)
20+
{
21+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
22+
23+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
24+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
25+
}
26+
}
27+
28+
public class IntTypeTest(IntTypeTest.IntTypeFixture fixture) : TypeTestBase<int, IntTypeTest.IntTypeFixture>(fixture)
29+
{
30+
public class IntTypeFixture() : TypeTestFixture(int.MinValue, int.MaxValue)
31+
{
32+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
33+
34+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
35+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
36+
}
37+
}
38+
39+
public class LongTypeTest(LongTypeTest.LongTypeFixture fixture) : TypeTestBase<long, LongTypeTest.LongTypeFixture>(fixture)
40+
{
41+
public class LongTypeFixture() : TypeTestFixture(long.MinValue, long.MaxValue)
42+
{
43+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
44+
45+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
46+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
47+
}
48+
}
49+
50+
public class DecimalTypeTest(DecimalTypeTest.DecimalTypeFixture fixture) : TypeTestBase<decimal, DecimalTypeTest.DecimalTypeFixture>(fixture)
51+
{
52+
public class DecimalTypeFixture() : TypeTestFixture(30.5m, 30m)
53+
{
54+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
55+
56+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
57+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
58+
}
59+
}
60+
61+
public class DoubleTypeTest(DoubleTypeTest.DoubleTypeFixture fixture) : TypeTestBase<double, DoubleTypeTest.DoubleTypeFixture>(fixture)
62+
{
63+
public class DoubleTypeFixture() : TypeTestFixture(30.5d, 30d)
64+
{
65+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
66+
67+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
68+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
69+
}
70+
}
71+
72+
public class FloatTypeTest(FloatTypeTest.FloatTypeFixture fixture) : TypeTestBase<float, FloatTypeTest.FloatTypeFixture>(fixture)
73+
{
74+
public class FloatTypeFixture() : TypeTestFixture(30.5f, 30f)
75+
{
76+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
77+
78+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
79+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
80+
}
81+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore.Types;
5+
6+
public class DateTimeTypeTest(DateTimeTypeTest.DateTimeTypeFixture fixture)
7+
: TypeTestBase<DateTime, DateTimeTypeTest.DateTimeTypeFixture>(fixture)
8+
{
9+
public class DateTimeTypeFixture() : TypeTestFixture(
10+
new DateTime(2020, 1, 5, 12, 30, 45, DateTimeKind.Unspecified),
11+
new DateTime(2022, 5, 3, 0, 0, 0, DateTimeKind.Unspecified))
12+
{
13+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
14+
15+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
16+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
17+
}
18+
}
19+
20+
public class DateTimeOffsetTypeTest(DateTimeOffsetTypeTest.DateTimeOffsetTypeFixture fixture)
21+
: TypeTestBase<DateTimeOffset, DateTimeOffsetTypeTest.DateTimeOffsetTypeFixture>(fixture)
22+
{
23+
public class DateTimeOffsetTypeFixture() : TypeTestFixture(
24+
new DateTimeOffset(2020, 1, 5, 12, 30, 45, TimeSpan.FromHours(2)),
25+
new DateTimeOffset(2020, 1, 5, 12, 30, 45, TimeSpan.FromHours(3)))
26+
{
27+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
28+
29+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
30+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
31+
}
32+
}
33+
34+
public class DateOnlyTypeTest(DateOnlyTypeTest.DateTypeFixture fixture) : TypeTestBase<DateOnly, DateOnlyTypeTest.DateTypeFixture>(fixture)
35+
{
36+
public class DateTypeFixture() : TypeTestFixture(
37+
new DateOnly(2020, 1, 5),
38+
new DateOnly(2022, 5, 3))
39+
{
40+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
41+
42+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
43+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
44+
}
45+
}
46+
47+
public class TimeOnlyTypeTest(TimeOnlyTypeTest.TimeTypeFixture fixture)
48+
: TypeTestBase<TimeOnly, TimeOnlyTypeTest.TimeTypeFixture>(fixture)
49+
{
50+
public class TimeTypeFixture() : TypeTestFixture(
51+
new TimeOnly(12, 30, 45),
52+
new TimeOnly(14, 0, 0))
53+
{
54+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
55+
56+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
57+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
58+
}
59+
}
60+
61+
public class TimeSpanTypeTest(TimeSpanTypeTest.TimeSpanTypeFixture fixture) : TypeTestBase<TimeSpan, TimeSpanTypeTest.TimeSpanTypeFixture>(fixture)
62+
{
63+
public class TimeSpanTypeFixture() : TypeTestFixture(
64+
new TimeSpan(12, 30, 45),
65+
new TimeSpan(14, 0, 0))
66+
{
67+
protected override ITestStoreFactory TestStoreFactory => CosmosTestStoreFactory.Instance;
68+
69+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
70+
=> base.AddOptions(builder).ConfigureWarnings(c => c.Log(CosmosEventId.NoPartitionKeyDefined));
71+
}
72+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore;
5+
6+
public abstract class RelationalTypeTestBase<T, TFixture>(TFixture fixture) : TypeTestBase<T, TFixture>(fixture)
7+
where TFixture : RelationalTypeTestBase<T, TFixture>.RelationalTypeTestFixture
8+
where T : notnull
9+
{
10+
[ConditionalFact]
11+
public virtual async Task ExecuteUpdate_within_json_to_parameter()
12+
=> await TestHelpers.ExecuteWithStrategyInTransactionAsync(
13+
Fixture.CreateContext,
14+
Fixture.UseTransaction,
15+
async context =>
16+
{
17+
await context.Set<JsonTypeEntity>().ExecuteUpdateAsync(s => s.SetProperty(e => e.JsonContainer.Value, e => Fixture.OtherValue));
18+
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
19+
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);
20+
});
21+
22+
[ConditionalFact]
23+
public virtual async Task ExecuteUpdate_within_json_to_constant()
24+
=> await TestHelpers.ExecuteWithStrategyInTransactionAsync(
25+
Fixture.CreateContext,
26+
Fixture.UseTransaction,
27+
async context =>
28+
{
29+
// Manually inject a constant node into the query tree
30+
var parameter = Expression.Parameter(typeof(JsonTypeEntity));
31+
var valueExpression = Expression.Lambda<Func<JsonTypeEntity, T>>(
32+
Expression.Constant(Fixture.OtherValue, typeof(T)),
33+
parameter);
34+
35+
await context.Set<JsonTypeEntity>().ExecuteUpdateAsync(s => s.SetProperty(e => e.JsonContainer.Value, valueExpression));
36+
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
37+
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);
38+
});
39+
40+
[ConditionalFact]
41+
public virtual async Task ExecuteUpdate_within_json_to_another_json_property()
42+
=> await TestHelpers.ExecuteWithStrategyInTransactionAsync(
43+
Fixture.CreateContext,
44+
Fixture.UseTransaction,
45+
async context =>
46+
{
47+
await context.Set<JsonTypeEntity>().ExecuteUpdateAsync(s => s.SetProperty(e => e.JsonContainer.Value, e => e.JsonContainer.OtherValue));
48+
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
49+
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);
50+
});
51+
52+
[ConditionalFact]
53+
public virtual async Task ExecuteUpdate_within_json_to_nonjson_column()
54+
=> await TestHelpers.ExecuteWithStrategyInTransactionAsync(
55+
Fixture.CreateContext,
56+
Fixture.UseTransaction,
57+
async context =>
58+
{
59+
await context.Set<JsonTypeEntity>().ExecuteUpdateAsync(s => s.SetProperty(e => e.JsonContainer.Value, e => e.OtherValue));
60+
var result = await context.Set<JsonTypeEntity>().Where(e => e.Id == 1).SingleAsync();
61+
Assert.Equal(Fixture.OtherValue, result.JsonContainer.Value, Fixture.Comparer);
62+
});
63+
64+
protected class JsonTypeEntity
65+
{
66+
public int Id { get; set; }
67+
68+
public required T Value { get; set; }
69+
public required T OtherValue { get; set; }
70+
71+
public required JsonContainer JsonContainer { get; set; }
72+
}
73+
74+
public class JsonContainer
75+
{
76+
public required T Value { get; set; }
77+
public required T OtherValue { get; set; }
78+
}
79+
80+
public abstract class RelationalTypeTestFixture(T value, T otherValue)
81+
: TypeTestFixture(value, otherValue)
82+
{
83+
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
84+
{
85+
base.OnModelCreating(modelBuilder, context);
86+
87+
modelBuilder.Entity<JsonTypeEntity>(b =>
88+
{
89+
modelBuilder.Entity<JsonTypeEntity>().Property(e => e.Id).ValueGeneratedNever();
90+
b.ComplexProperty(e => e.JsonContainer, cb => cb.ToJson());
91+
});
92+
}
93+
94+
protected override async Task SeedAsync(DbContext context)
95+
{
96+
await base.SeedAsync(context);
97+
98+
context.Set<JsonTypeEntity>().AddRange(
99+
new()
100+
{
101+
Id = 1,
102+
Value = Value,
103+
OtherValue = OtherValue,
104+
JsonContainer = new()
105+
{
106+
Value = Value,
107+
OtherValue = OtherValue
108+
}
109+
},
110+
new()
111+
{
112+
Id = 2,
113+
Value = OtherValue,
114+
OtherValue = Value,
115+
JsonContainer = new()
116+
{
117+
Value = OtherValue,
118+
OtherValue = Value
119+
}
120+
});
121+
122+
await context.SaveChangesAsync();
123+
}
124+
125+
public virtual void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
126+
=> facade.UseTransaction(transaction.GetDbTransaction());
127+
}
128+
}

0 commit comments

Comments
 (0)