Skip to content

Commit f06f8e2

Browse files
committed
test: add EF decimal parameterization test
1 parent 61ff1a0 commit f06f8e2

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using EntityFrameworkCore.Ydb.FunctionalTests.TestUtilities;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.EntityFrameworkCore.Diagnostics;
4+
using Microsoft.EntityFrameworkCore.TestUtilities;
5+
6+
namespace EntityFrameworkCore.Ydb.FunctionalTests.Query;
7+
8+
public class DecimalParameterQueryYdbFixture
9+
: SharedStoreFixtureBase<DecimalParameterQueryYdbFixture.DecimalContext>
10+
{
11+
private static DbCommandInterceptor? CurrentInterceptor => null;
12+
13+
protected override string StoreName => "DecimalParameter";
14+
protected override ITestStoreFactory TestStoreFactory => YdbTestStoreFactory.Instance;
15+
16+
public override DbContextOptionsBuilder AddOptions(DbContextOptionsBuilder builder)
17+
{
18+
var b = base.AddOptions(builder);
19+
if (CurrentInterceptor is not null)
20+
b.AddInterceptors(CurrentInterceptor);
21+
return b;
22+
}
23+
24+
protected override void OnModelCreating(ModelBuilder b, DbContext ctx)
25+
{
26+
b.Entity<ItemDefault>(e => e.HasKey(x => x.Id));
27+
28+
b.Entity<ItemExplicit>(e =>
29+
{
30+
e.HasKey(x => x.Id);
31+
e.Property(x => x.Price).HasPrecision(22, 9);
32+
});
33+
}
34+
35+
public class DecimalContext(DbContextOptions options) : DbContext(options);
36+
}
37+
38+
public class ItemDefault { public int Id { get; init; } public decimal Price { get; init; } }
39+
public class ItemExplicit { public int Id { get; init; } public decimal Price { get; init; } }
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Xunit;
3+
4+
namespace EntityFrameworkCore.Ydb.FunctionalTests.Query;
5+
6+
public class DecimalParameterYdbTest(DecimalParameterQueryYdbFixture fixture)
7+
: IClassFixture<DecimalParameterQueryYdbFixture>
8+
{
9+
private DecimalParameterQueryYdbFixture Fixture { get; } = fixture;
10+
11+
[ConditionalFact]
12+
public async Task Parameter_decimal_uses_default_22_9_and_roundtrips()
13+
{
14+
await using var ctx = Fixture.CreateContext();
15+
await ctx.Database.EnsureCreatedAsync();
16+
17+
decimal v = 1.23456789m;
18+
ctx.Add(new ItemDefault { Price = v });
19+
await ctx.SaveChangesAsync();
20+
21+
var got = await ctx.Set<ItemDefault>().Where(x => x.Price == v).ToListAsync();
22+
Assert.Single(got);
23+
Assert.Equal(v, got[0].Price);
24+
}
25+
26+
[ConditionalFact]
27+
public async Task Parameter_decimal_respects_explicit_22_9_and_roundtrips()
28+
{
29+
await using var ctx = Fixture.CreateContext();
30+
await ctx.Database.EnsureCreatedAsync();
31+
32+
decimal v = 123.456789012m;
33+
ctx.Add(new ItemExplicit { Price = v });
34+
await ctx.SaveChangesAsync();
35+
36+
var got = await ctx.Set<ItemExplicit>().Where(x => x.Price == v).ToListAsync();
37+
Assert.Single(got);
38+
Assert.Equal(v, got[0].Price);
39+
}
40+
41+
[ConditionalFact]
42+
public async Task Decimal_out_of_range_bubbles_up()
43+
{
44+
await using var ctx = Fixture.CreateContext();
45+
await ctx.Database.EnsureCreatedAsync();
46+
47+
var tooBig = new ItemExplicit { Price = 10_000_000_000_000m }; // 14 целых цифр -> вне 22,9
48+
ctx.Add(tooBig);
49+
50+
var ex = await Assert.ThrowsAsync<DbUpdateException>(() => ctx.SaveChangesAsync());
51+
Assert.Contains("Decimal", ex.InnerException?.Message ?? "");
52+
}
53+
}

0 commit comments

Comments
 (0)