fix/partitioning-small-fix #21
@ -9,6 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" />
|
||||
<PackageReference Include="Mapster" Version="7.4.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
|
40
DD.Persistence.Database.Postgres.Test/DbFixture.cs
Normal file
40
DD.Persistence.Database.Postgres.Test/DbFixture.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using DD.Persistence.Database.Model;
|
||||
using DD.Persistence.Database.Postgres.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace DD.Persistence.Database.Postgres.Test;
|
||||
public class DbFixture : IDisposable
|
||||
{
|
||||
public string connectionString { get; }
|
||||
public ServiceProvider serviceProvider { get; private set; }
|
||||
|
||||
public DbFixture()
|
||||
{
|
||||
connectionString = $"Host=localhost;Port=5462;Username=postgres;Password=postgres;Database={Guid.CreateVersion7()}";
|
||||
|
||||
var serviceCollection = new ServiceCollection();
|
||||
serviceCollection
|
||||
.AddDbContext<PersistenceDbContext>(options => options.UseNpgsql(connectionString),
|
||||
ServiceLifetime.Transient);
|
||||
|
||||
serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
|
||||
var context = serviceProvider.GetRequiredService<PersistenceDbContext>();
|
||||
context.Database.EnsureCreated();
|
||||
context.Database.AddPartitioning();
|
||||
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
var dbContext = new PersistencePostgresContext(
|
||||
new DbContextOptionsBuilder<PersistencePostgresContext>()
|
||||
.UseNpgsql(connectionString)
|
||||
.Options);
|
||||
|
||||
dbContext.Database.EnsureDeleted();
|
||||
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
}
|
@ -1,85 +1,70 @@
|
||||
using DD.Persistence.Database.Entity;
|
||||
using DD.Persistence.Database.Model;
|
||||
using DD.Persistence.Database.Postgres.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Mapster;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Npgsql;
|
||||
|
||||
namespace DD.Persistence.Database.Postgres.Test;
|
||||
|
||||
public class UnitTest1
|
||||
public class UnitTest1 : IClassFixture<DbFixture>
|
||||
{
|
||||
private string connectionString;
|
||||
private IHost host;
|
||||
private ServiceProvider _serviceProvider;
|
||||
private string _connectionString;
|
||||
|
||||
public UnitTest1()
|
||||
public UnitTest1(DbFixture fixture)
|
||||
{
|
||||
connectionString = "Host=localhost;Port=5462;Username=postgres;Password=postgres;Database=persistence5";
|
||||
|
||||
|
||||
|
||||
var builder = Host.CreateApplicationBuilder();
|
||||
builder.Services.AddDbContext<PersistencePostgresContext>(options =>
|
||||
options.UseNpgsql(connectionString));
|
||||
|
||||
builder.Services.AddScoped<DbContext>(provider => provider.GetRequiredService<PersistencePostgresContext>());
|
||||
|
||||
host = builder.Build();
|
||||
using var scope = host.Services.CreateScope();
|
||||
var provider = scope.ServiceProvider;
|
||||
|
||||
var context = provider.GetRequiredService<PersistencePostgresContext>();
|
||||
context.Database.EnsureCreatedAndMigrated();
|
||||
context.Database.AddPartitioning();
|
||||
context.SaveChanges();
|
||||
|
||||
|
||||
//using var scope2 = builder..CreateScope();
|
||||
//var scopedServices = scope.ServiceProvider;
|
||||
|
||||
//var dbContext = scopedServices.GetRequiredService<PersistencePostgresContext>();
|
||||
//dbContext.Database.EnsureCreatedAndMigrated();
|
||||
//dbContext.SaveChanges();
|
||||
_serviceProvider = fixture.serviceProvider;
|
||||
_connectionString = fixture.connectionString;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CreateHyperTable()
|
||||
{
|
||||
var entity = new ParameterData() {
|
||||
var entity = new ParameterData()
|
||||
{
|
||||
DiscriminatorId = Guid.NewGuid(),
|
||||
ParameterId = 1,
|
||||
Timestamp = DateTime.UtcNow,
|
||||
Value = "123"
|
||||
};
|
||||
|
||||
var scope = host.Services.CreateScope();
|
||||
using (var context = _serviceProvider.GetService<PersistenceDbContext>()!)
|
||||
{
|
||||
context.ParameterData.Add(entity);
|
||||
|
||||
var context = scope.ServiceProvider.GetRequiredService<PersistencePostgresContext>();
|
||||
var entity2 = entity.Adapt<ParameterData>();
|
||||
entity2.ParameterId = 2;
|
||||
context.ParameterData.Add(entity2);
|
||||
|
||||
context.ParameterData.Add(entity);
|
||||
context.SaveChanges();
|
||||
var entity3 = entity2.Adapt<ParameterData>();
|
||||
entity3.ParameterId = 3;
|
||||
entity3.Timestamp = DateTime.UtcNow.AddDays(2);
|
||||
context.ParameterData.Add(entity3);
|
||||
|
||||
entity.Timestamp = DateTime.UtcNow.AddDays(1);
|
||||
context.ParameterData.Add(entity);
|
||||
context.SaveChanges();
|
||||
var entity4 = entity3.Adapt<ParameterData>();
|
||||
entity4.ParameterId = 4;
|
||||
context.ParameterData.Add(entity4);
|
||||
|
||||
entity.Timestamp = DateTime.UtcNow.AddDays(2);
|
||||
context.ParameterData.Add(entity);
|
||||
context.SaveChanges();
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
entity.Timestamp = DateTime.UtcNow.AddDays(3);
|
||||
context.ParameterData.Add(entity);
|
||||
context.SaveChanges();
|
||||
var chunksCount = 0;
|
||||
using (var connection = new NpgsqlConnection(_connectionString))
|
||||
{
|
||||
connection.Open();
|
||||
string sql = "select count(*) from (select show_chunks('parameter_data'));";
|
||||
|
||||
entity.Timestamp = DateTime.UtcNow.AddDays(4);
|
||||
context.ParameterData.Add(entity);
|
||||
context.SaveChanges();
|
||||
using (var command = new NpgsqlCommand(sql, connection))
|
||||
{
|
||||
using (var reader = command.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
chunksCount += reader.GetInt32(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Equal(2, chunksCount);
|
||||
}
|
||||
|
||||
//public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
// Host.CreateDefaultBuilder(args)
|
||||
// .ConfigureWebHostDefaults(webBuilder =>
|
||||
// {
|
||||
// webBuilder.UseStartup<Startup>();
|
||||
// });
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user
Мы договаривались делать это в PersistenceDbContext.OnModelCreating(..) и именно этот момент и проверяет текст.