forked from xiongyuxing/tiku-backend.net
103 lines
3.5 KiB
C#
103 lines
3.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Metadata;
|
|
using Tiku.Domain.Content;
|
|
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(28, tableNames.Count);
|
|
Assert.Contains("tenants", tableNames);
|
|
Assert.Contains("tenant_settings", tableNames);
|
|
Assert.Contains("content_entries", tableNames);
|
|
Assert.Contains("content_nodes", tableNames);
|
|
Assert.Contains("question_collections", tableNames);
|
|
Assert.Contains("practice_blueprints", tableNames);
|
|
Assert.Contains("questions", tableNames);
|
|
Assert.Contains("question_versions", tableNames);
|
|
Assert.Contains("answer_records", tableNames);
|
|
}
|
|
|
|
[Fact]
|
|
public void Content_node_path_is_mapped_to_ltree()
|
|
{
|
|
using var context = new TikuDbContext(Options);
|
|
|
|
var path = context.Model.FindEntityType(typeof(ContentNode))!
|
|
.FindProperty(nameof(ContentNode.Path))!;
|
|
|
|
Assert.Equal("ltree", path.GetColumnType());
|
|
}
|
|
|
|
[Fact]
|
|
public void Collection_relations_include_tenant_in_foreign_keys()
|
|
{
|
|
using var context = new TikuDbContext(Options);
|
|
|
|
var foreignKeys = context.Model.FindEntityType(typeof(QuestionCollection))!
|
|
.GetForeignKeys()
|
|
.Where(foreignKey => foreignKey.Properties.Count > 1)
|
|
.Select(foreignKey => foreignKey.Properties
|
|
.Select(property => property.Name)
|
|
.ToArray());
|
|
|
|
Assert.All(
|
|
foreignKeys,
|
|
properties => Assert.Contains("TenantId", properties));
|
|
}
|
|
|
|
[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"]));
|
|
}
|
|
}
|