EFcoreExtension
All checks were successful
Publish NuGet packages / publish (push) Successful in 1m15s

This commit is contained in:
2026-06-13 20:25:29 +07:00
parent e3ab850ec2
commit 7c0f89ebbb
10 changed files with 1332 additions and 1169 deletions

View File

@@ -7,16 +7,16 @@
private static readonly double scalarDouble = 500.0; // прибавляемое значение в мм
// Коллекции единиц
private static readonly Length[] unitsArray = new[] { Length.Meter, Length._MilliMeter };
private static readonly Length?[] nullableUnitsArray = new Length?[] { Length.Meter, null, Length._MilliMeter };
private static readonly List<Length> unitsList = new List<Length> { Length.Meter, Length._MilliMeter };
private static readonly List<Length?> nullableUnitsList = new List<Length?> { Length.Meter, null, Length._MilliMeter };
private static readonly Length[] unitsArray = [Length.Meter, Length._MilliMeter];
private static readonly Length?[] nullableUnitsArray = [Length.Meter, null, Length._MilliMeter];
private static readonly List<Length> unitsList = [Length.Meter, Length._MilliMeter];
private static readonly List<Length?> nullableUnitsList = [Length.Meter, null, Length._MilliMeter];
// Коллекции double
private static readonly double[] doubleArray = new double[] { 200.0, 300.0 };
private static readonly double?[] nullableDoubleArray = new double?[] { 200.0, null, 300.0 };
private static readonly List<double> doubleList = new List<double> { 200.0, 300.0 };
private static readonly List<double?> nullableDoubleList = new List<double?> { 200.0, null, 300.0 };
private static readonly double[] doubleArray = [200.0, 300.0];
private static readonly double?[] nullableDoubleArray = [200.0, null, 300.0];
private static readonly List<double> doubleList = [200.0, 300.0];
private static readonly List<double?> nullableDoubleList = [200.0, null, 300.0];
// ====================== 1. T[] + double ======================
[Fact]
@@ -303,7 +303,7 @@
[Fact]
public void Plus_NullArray_ReturnsNull()
{
Length[] nullArray = null;
Length[] nullArray = null!;
var result = nullArray.Plus<Length>(5.0);
Assert.Null(result);
}
@@ -311,7 +311,7 @@
[Fact]
public void Plus_NullList_ReturnsNull()
{
List<Length> nullList = null;
List<Length> nullList = null!;
var result = nullList.Plus<Length>(5.0);
Assert.Null(result);
}

View File

@@ -20,6 +20,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="10.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.9" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
@@ -31,8 +34,6 @@
<ItemGroup>
<ProjectReference Include="..\QWERTYkez.Mensura\QWERTYkez.Mensura.csproj" />
<InternalsVisibleTo Include="..\QWERTYkez.Mensura\QWERTYkez.Mensura.csproj" />
<ProjectReference Include="..\QWERTYkez.Mensura.Generator\QWERTYkez.Mensura.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>

View File

@@ -0,0 +1,98 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace QWERTYkez.Mensura.Tests
{
public class TestEntity1
{
public int Id { get; set; }
public Length Length { get; set; }
}
public class TestEntity2
{
public int Id { get; set; }
public double Length { get; set; }
}
public class SharedDbContext(DbContextOptions<SharedDbContext> options) : DbContext(options)
{
public DbSet<TestEntity1> Entities1 { get; set; }
public DbSet<TestEntity2> Entities2 { get; set; }
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
configurationBuilder.UseMensuraUnits();
}
}
public class IdentityConversionTest : IDisposable
{
private readonly SqliteConnection _connection;
public IdentityConversionTest()
{
_connection = new SqliteConnection("Filename=:memory:");
_connection.Open();
}
public void Dispose()
{
_connection?.Dispose();
GC.SuppressFinalize(this);
}
[Fact]
public void Length_And_Double_Are_Stored_Identically()
{
// 1. Сохраняем Length → читаем как double
double savedDoubleFromLength;
using (var context = new SharedDbContext(new DbContextOptionsBuilder<SharedDbContext>().UseSqlite(_connection).Options))
{
context.Database.EnsureCreated();
var expectedLength = Length.Meter; // 1000 мм
context.Entities1.Add(new TestEntity1 { Length = expectedLength });
context.SaveChanges();
using var cmd = context.Database.GetDbConnection().CreateCommand();
cmd.CommandText = "SELECT Length FROM Entities1";
savedDoubleFromLength = (double)cmd.ExecuteScalar()!;
}
// 2. Сохраняем double → читаем как Length
double savedDoubleFromDouble = 1234.5;
using (var context = new SharedDbContext(new DbContextOptionsBuilder<SharedDbContext>().UseSqlite(_connection).Options))
{
context.Database.EnsureCreated();
context.Entities2.Add(new TestEntity2 { Length = savedDoubleFromDouble });
context.SaveChanges();
using var cmd = context.Database.GetDbConnection().CreateCommand();
cmd.CommandText = "SELECT Length FROM Entities2";
savedDoubleFromDouble = (double)cmd.ExecuteScalar()!; // должно быть 1234.5
}
// 3. Проверяем, что сырые значения сохранены корректно
Assert.Equal(1000.0, savedDoubleFromLength);
Assert.Equal(1234.5, savedDoubleFromDouble);
// 4. Проверяем обратимость: читаем из Entities1 как TestEntity2
using (var context = new SharedDbContext(new DbContextOptionsBuilder<SharedDbContext>().UseSqlite(_connection).Options))
{
var fromLengthTable = context.Entities2
.FromSqlRaw("SELECT Id, Length FROM Entities1")
.First();
Assert.Equal(1000.0, fromLengthTable.Length);
}
// 5. Проверяем обратимость: читаем из Entities2 как TestEntity1
using (var context = new SharedDbContext(new DbContextOptionsBuilder<SharedDbContext>().UseSqlite(_connection).Options))
{
var fromDoubleTable = context.Entities1
.FromSqlRaw("SELECT Id, Length FROM Entities2")
.First();
Assert.Equal(1234.5, (double)fromDoubleTable.Length);
}
}
}
}