forked from xiongyuxing/tiku-backend.net
feat: add core persistence model
This commit is contained in:
68
Tiku.IntegrationTests/PersistenceModelTests.cs
Normal file
68
Tiku.IntegrationTests/PersistenceModelTests.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Tiku.Domain.QuestionBanks;
|
||||
using Tiku.Infrastructure.Persistence;
|
||||
|
||||
namespace Tiku.IntegrationTests;
|
||||
|
||||
public sealed class PersistenceModelTests
|
||||
{
|
||||
private static readonly DbContextOptions<TikuDbContext> Options =
|
||||
new DbContextOptionsBuilder<TikuDbContext>()
|
||||
.UseNpgsql("Host=localhost;Database=tiku_model_tests;Username=postgres")
|
||||
.Options;
|
||||
|
||||
[Fact]
|
||||
public void Core_model_contains_expected_tables()
|
||||
{
|
||||
using var context = new TikuDbContext(Options);
|
||||
|
||||
var tableNames = context.Model.GetEntityTypes()
|
||||
.Select(entity => entity.GetTableName())
|
||||
.ToHashSet(StringComparer.Ordinal);
|
||||
|
||||
Assert.Equal(20, tableNames.Count);
|
||||
Assert.Contains("tenants", tableNames);
|
||||
Assert.Contains("questions", tableNames);
|
||||
Assert.Contains("question_versions", tableNames);
|
||||
Assert.Contains("answer_records", tableNames);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(nameof(Question.Tags), "'[]'::jsonb")]
|
||||
public void Question_json_properties_are_mapped_to_jsonb(
|
||||
string propertyName,
|
||||
string expectedDefaultValueSql)
|
||||
{
|
||||
using var context = new TikuDbContext(Options);
|
||||
|
||||
var property = context.Model.FindEntityType(typeof(Question))!
|
||||
.FindProperty(propertyName)!;
|
||||
|
||||
Assert.Equal("jsonb", property.GetColumnType());
|
||||
Assert.Equal(expectedDefaultValueSql, property.GetDefaultValueSql());
|
||||
Assert.Equal(typeof(System.Text.Json.JsonElement), property.ClrType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Question_relations_include_tenant_in_foreign_keys()
|
||||
{
|
||||
using var context = new TikuDbContext(Options);
|
||||
|
||||
var foreignKeys = context.Model.FindEntityType(typeof(Question))!
|
||||
.GetForeignKeys()
|
||||
.Select(foreignKey => foreignKey.Properties
|
||||
.Select(property => property.Name)
|
||||
.ToArray())
|
||||
.ToArray();
|
||||
|
||||
Assert.All(
|
||||
foreignKeys.Where(properties => properties.Length > 1),
|
||||
properties => Assert.Contains("TenantId", properties));
|
||||
|
||||
Assert.Contains(
|
||||
foreignKeys,
|
||||
properties => properties.SequenceEqual(
|
||||
["TenantId", "Id", "CurrentVersionId"]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user