|
| 1 | +using DecentDB.EntityFrameworkCore; |
| 2 | +using Microsoft.EntityFrameworkCore; |
| 3 | +using Microsoft.EntityFrameworkCore.Infrastructure; |
| 4 | +using Microsoft.EntityFrameworkCore.Storage; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace DecentDB.EntityFrameworkCore.Tests; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Verifies that the EF Core provider respects decimal precision and scale |
| 11 | +/// from ConfigureConventions, HasPrecision, and HasColumnType. |
| 12 | +/// </summary> |
| 13 | +public sealed class DecimalPrecisionTests : IDisposable |
| 14 | +{ |
| 15 | + private readonly string _tempDir = Path.Combine(Path.GetTempPath(), $"decentdb_prec_{Guid.NewGuid():N}"); |
| 16 | + |
| 17 | + public DecimalPrecisionTests() |
| 18 | + { |
| 19 | + Directory.CreateDirectory(_tempDir); |
| 20 | + } |
| 21 | + |
| 22 | + public void Dispose() |
| 23 | + { |
| 24 | + if (Directory.Exists(_tempDir)) |
| 25 | + { |
| 26 | + Directory.Delete(_tempDir, true); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void DefaultDecimalMapping_UsesDefaultPrecisionAndScale() |
| 32 | + { |
| 33 | + var dbPath = Path.Combine(_tempDir, "default.ddb"); |
| 34 | + using var context = CreateContext<DefaultDecimalContext>(dbPath); |
| 35 | + var mappingSource = context.GetService<IRelationalTypeMappingSource>(); |
| 36 | + |
| 37 | + var mapping = (RelationalTypeMapping)mappingSource.FindMapping(typeof(decimal))!; |
| 38 | + Assert.Equal("DECIMAL(18,4)", mapping.StoreType); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void ConfigureConventions_HavePrecision_IsRespected() |
| 43 | + { |
| 44 | + var dbPath = Path.Combine(_tempDir, "conventions.ddb"); |
| 45 | + using var context = CreateContext<CustomPrecisionConventionContext>(dbPath); |
| 46 | + |
| 47 | + context.Database.EnsureCreated(); |
| 48 | + |
| 49 | + context.Items.Add(new PrecisionItem { Value = 123.456789m }); |
| 50 | + context.SaveChanges(); |
| 51 | + |
| 52 | + var item = context.Items.First(); |
| 53 | + Assert.Equal(123.456789m, item.Value); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void HasPrecision_OnProperty_IsRespected() |
| 58 | + { |
| 59 | + var dbPath = Path.Combine(_tempDir, "hasprecision.ddb"); |
| 60 | + using var context = CreateContext<PropertyPrecisionContext>(dbPath); |
| 61 | + |
| 62 | + context.Database.EnsureCreated(); |
| 63 | + |
| 64 | + context.Items.Add(new PrecisionItem { Value = 99.12m }); |
| 65 | + context.SaveChanges(); |
| 66 | + |
| 67 | + var item = context.Items.First(); |
| 68 | + Assert.Equal(99.12m, item.Value); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void HasColumnType_WithPrecisionScale_IsRespected() |
| 73 | + { |
| 74 | + var dbPath = Path.Combine(_tempDir, "columntype.ddb"); |
| 75 | + using var context = CreateContext<ColumnTypeContext>(dbPath); |
| 76 | + |
| 77 | + context.Database.EnsureCreated(); |
| 78 | + |
| 79 | + context.Items.Add(new PrecisionItem { Value = 12345.67m }); |
| 80 | + context.SaveChanges(); |
| 81 | + |
| 82 | + var item = context.Items.First(); |
| 83 | + Assert.Equal(12345.67m, item.Value); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void NullableDecimal_WithPrecision_IsRespected() |
| 88 | + { |
| 89 | + var dbPath = Path.Combine(_tempDir, "nullable.ddb"); |
| 90 | + using var context = CreateContext<NullableDecimalContext>(dbPath); |
| 91 | + |
| 92 | + context.Database.EnsureCreated(); |
| 93 | + |
| 94 | + context.Items.Add(new NullableDecimalItem { Value = 42.123456m }); |
| 95 | + context.Items.Add(new NullableDecimalItem { Value = null }); |
| 96 | + context.SaveChanges(); |
| 97 | + |
| 98 | + var items = context.Items.OrderBy(i => i.Id).ToList(); |
| 99 | + Assert.Equal(42.123456m, items[0].Value); |
| 100 | + Assert.Null(items[1].Value); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void MultipleDecimalProperties_WithDifferentPrecisions() |
| 105 | + { |
| 106 | + var dbPath = Path.Combine(_tempDir, "multi.ddb"); |
| 107 | + using var context = CreateContext<MultiPrecisionContext>(dbPath); |
| 108 | + |
| 109 | + context.Database.EnsureCreated(); |
| 110 | + |
| 111 | + context.Items.Add(new MultiDecimalItem |
| 112 | + { |
| 113 | + Price = 99.99m, |
| 114 | + TaxRate = 0.0825m, |
| 115 | + Weight = 1234.5m |
| 116 | + }); |
| 117 | + context.SaveChanges(); |
| 118 | + |
| 119 | + var item = context.Items.First(); |
| 120 | + Assert.Equal(99.99m, item.Price); |
| 121 | + Assert.Equal(0.0825m, item.TaxRate); |
| 122 | + Assert.Equal(1234.5m, item.Weight); |
| 123 | + } |
| 124 | + |
| 125 | + private static TContext CreateContext<TContext>(string dbPath) where TContext : DbContext |
| 126 | + { |
| 127 | + var options = new DbContextOptionsBuilder<TContext>() |
| 128 | + .UseDecentDB($"Data Source={dbPath}") |
| 129 | + .Options; |
| 130 | + return (TContext)Activator.CreateInstance(typeof(TContext), options)!; |
| 131 | + } |
| 132 | + |
| 133 | + #region Test entities and contexts |
| 134 | + |
| 135 | + public class PrecisionItem |
| 136 | + { |
| 137 | + public int Id { get; set; } |
| 138 | + public decimal Value { get; set; } |
| 139 | + } |
| 140 | + |
| 141 | + public class NullableDecimalItem |
| 142 | + { |
| 143 | + public int Id { get; set; } |
| 144 | + public decimal? Value { get; set; } |
| 145 | + } |
| 146 | + |
| 147 | + public class MultiDecimalItem |
| 148 | + { |
| 149 | + public int Id { get; set; } |
| 150 | + public decimal Price { get; set; } |
| 151 | + public decimal TaxRate { get; set; } |
| 152 | + public decimal Weight { get; set; } |
| 153 | + } |
| 154 | + |
| 155 | + private sealed class DefaultDecimalContext : DbContext |
| 156 | + { |
| 157 | + public DefaultDecimalContext(DbContextOptions<DefaultDecimalContext> options) : base(options) { } |
| 158 | + public DbSet<PrecisionItem> Items { get; set; } = null!; |
| 159 | + } |
| 160 | + |
| 161 | + private sealed class CustomPrecisionConventionContext : DbContext |
| 162 | + { |
| 163 | + public CustomPrecisionConventionContext(DbContextOptions<CustomPrecisionConventionContext> options) : base(options) { } |
| 164 | + public DbSet<PrecisionItem> Items { get; set; } = null!; |
| 165 | + |
| 166 | + protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) |
| 167 | + { |
| 168 | + configurationBuilder.Properties<decimal>().HavePrecision(18, 6); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private sealed class PropertyPrecisionContext : DbContext |
| 173 | + { |
| 174 | + public PropertyPrecisionContext(DbContextOptions<PropertyPrecisionContext> options) : base(options) { } |
| 175 | + public DbSet<PrecisionItem> Items { get; set; } = null!; |
| 176 | + |
| 177 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 178 | + { |
| 179 | + modelBuilder.Entity<PrecisionItem>().Property(e => e.Value).HasPrecision(10, 2); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private sealed class ColumnTypeContext : DbContext |
| 184 | + { |
| 185 | + public ColumnTypeContext(DbContextOptions<ColumnTypeContext> options) : base(options) { } |
| 186 | + public DbSet<PrecisionItem> Items { get; set; } = null!; |
| 187 | + |
| 188 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 189 | + { |
| 190 | + modelBuilder.Entity<PrecisionItem>().Property(e => e.Value).HasColumnType("DECIMAL(10,2)"); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private sealed class NullableDecimalContext : DbContext |
| 195 | + { |
| 196 | + public NullableDecimalContext(DbContextOptions<NullableDecimalContext> options) : base(options) { } |
| 197 | + public DbSet<NullableDecimalItem> Items { get; set; } = null!; |
| 198 | + |
| 199 | + protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) |
| 200 | + { |
| 201 | + configurationBuilder.Properties<decimal>().HavePrecision(18, 6); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private sealed class MultiPrecisionContext : DbContext |
| 206 | + { |
| 207 | + public MultiPrecisionContext(DbContextOptions<MultiPrecisionContext> options) : base(options) { } |
| 208 | + public DbSet<MultiDecimalItem> Items { get; set; } = null!; |
| 209 | + |
| 210 | + protected override void OnModelCreating(ModelBuilder modelBuilder) |
| 211 | + { |
| 212 | + modelBuilder.Entity<MultiDecimalItem>().Property(e => e.Price).HasPrecision(10, 2); |
| 213 | + modelBuilder.Entity<MultiDecimalItem>().Property(e => e.TaxRate).HasPrecision(8, 4); |
| 214 | + modelBuilder.Entity<MultiDecimalItem>().Property(e => e.Weight).HasPrecision(12, 1); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + #endregion |
| 219 | +} |
0 commit comments