mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2026-07-31 01:40:42 +00:00
Esiur CLI
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Esiur.Core;
|
||||
using Esiur.Resource;
|
||||
using Esiur.Stores.EntityCore;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Esiur.Tests.EntityCore;
|
||||
|
||||
public sealed class EntityCoreProviderTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Sqlite_PersistsAndMaterializesEsiurResources()
|
||||
{
|
||||
await using var connection = new SqliteConnection("Data Source=:memory:");
|
||||
await connection.OpenAsync();
|
||||
|
||||
var warehouse = new Warehouse();
|
||||
var store = await warehouse.Put("database", new EntityStore());
|
||||
DbContextOptions<ResourceDbContext>? options = null;
|
||||
options = new DbContextOptionsBuilder<ResourceDbContext>()
|
||||
.UseSqlite(connection)
|
||||
.UseEsiur(store, () => new ResourceDbContext(options!))
|
||||
.Options;
|
||||
|
||||
try
|
||||
{
|
||||
await warehouse.Open();
|
||||
|
||||
await using var context = new ResourceDbContext(options);
|
||||
await context.Database.EnsureCreatedAsync();
|
||||
|
||||
var added = await context.Resources.AddResourceAsync(new ProviderResource
|
||||
{
|
||||
Name = "SQLite resource",
|
||||
});
|
||||
|
||||
Assert.True(added.Id > 0);
|
||||
Assert.NotNull(added.Instance);
|
||||
Assert.Same(store, added.Instance!.Store);
|
||||
|
||||
context.ChangeTracker.Clear();
|
||||
|
||||
var loaded = await context.Resources.SingleAsync(x => x.Id == added.Id);
|
||||
|
||||
Assert.Same(added, loaded);
|
||||
Assert.Equal("SQLite resource", loaded.Name);
|
||||
Assert.NotNull(await warehouse.Get<IResource>($"database/{nameof(ProviderResource)}/{added.Id}"));
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (warehouse.IsOpen)
|
||||
await warehouse.Close();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PostgreSql_BuildsModelAndTranslatesQuery()
|
||||
{
|
||||
await AssertProviderCompatibility(
|
||||
options => options.UseNpgsql(
|
||||
"Host=localhost;Database=esiur_tests;Username=esiur;Password=unused"),
|
||||
"Npgsql.EntityFrameworkCore.PostgreSQL");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MySql_BuildsModelAndTranslatesQuery()
|
||||
{
|
||||
await AssertProviderCompatibility(
|
||||
options => options.UseMySQL(
|
||||
"Server=localhost;Database=esiur_tests;User=esiur;Password=unused"),
|
||||
"MySql.EntityFrameworkCore");
|
||||
}
|
||||
|
||||
private static async Task AssertProviderCompatibility(
|
||||
Action<DbContextOptionsBuilder<ResourceDbContext>> configureProvider,
|
||||
string expectedProvider)
|
||||
{
|
||||
var warehouse = new Warehouse();
|
||||
var store = await warehouse.Put("database", new EntityStore());
|
||||
var builder = new DbContextOptionsBuilder<ResourceDbContext>();
|
||||
|
||||
configureProvider(builder);
|
||||
builder.UseEsiur(store);
|
||||
|
||||
await using var context = new ResourceDbContext(builder.Options);
|
||||
var entity = context.Model.FindEntityType(typeof(ProviderResource));
|
||||
var sql = context.Resources
|
||||
.Where(x => x.Name == "provider check")
|
||||
.ToQueryString();
|
||||
|
||||
Assert.NotNull(entity);
|
||||
Assert.Equal(expectedProvider, context.Database.ProviderName);
|
||||
Assert.Contains(entity!.GetTableName()!, sql, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
private sealed class ResourceDbContext(DbContextOptions<ResourceDbContext> options)
|
||||
: DbContext(options)
|
||||
{
|
||||
public DbSet<ProviderResource> Resources => Set<ProviderResource>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class ProviderResource : IResource
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
public Instance? Instance { get; set; }
|
||||
|
||||
public event DestroyedEvent? OnDestroy;
|
||||
|
||||
public AsyncReply<bool> Handle(
|
||||
ResourceOperation operation,
|
||||
IResourceContext? context = null)
|
||||
=> new(true);
|
||||
|
||||
public void Destroy() => OnDestroy?.Invoke(this);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.10" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="MySql.EntityFrameworkCore" Version="10.0.7" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="10.0.3" />
|
||||
<PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="3.0.3" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\Stores\Esiur.Stores.EntityCore\Esiur.Stores.EntityCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user