98 lines
3.8 KiB
C#
98 lines
3.8 KiB
C#
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|