1
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
using Jiaowu.Api.Domain.Academic;
|
||||
using Jiaowu.Api.Infrastructure.Persistence;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Jiaowu.Api.Tests;
|
||||
|
||||
public sealed class PersistenceTests : IAsyncLifetime
|
||||
{
|
||||
private readonly SqliteConnection _connection = new("Data Source=:memory:");
|
||||
private AppDbContext _db = null!;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
await _connection.OpenAsync();
|
||||
var options = new DbContextOptionsBuilder<AppDbContext>()
|
||||
.UseSqlite(_connection)
|
||||
.Options;
|
||||
_db = new AppDbContext(options);
|
||||
await _db.Database.EnsureCreatedAsync();
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await _db.DisposeAsync();
|
||||
await _connection.DisposeAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Catalog_codes_are_unique()
|
||||
{
|
||||
_db.Campuses.Add(new Campus { Code = "MAIN", Name = "主校区" });
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
_db.Campuses.Add(new Campus { Code = "MAIN", Name = "另一个校区" });
|
||||
|
||||
await Assert.ThrowsAsync<DbUpdateException>(() => _db.SaveChangesAsync());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Organization_relations_can_be_persisted()
|
||||
{
|
||||
var campus = new Campus { Code = "EAST", Name = "东校区" };
|
||||
var college = new College
|
||||
{
|
||||
Code = "ART",
|
||||
Name = "艺术学院",
|
||||
CampusId = campus.Id
|
||||
};
|
||||
var major = new Major
|
||||
{
|
||||
Code = "1305",
|
||||
Name = "设计学",
|
||||
CollegeId = college.Id,
|
||||
DegreeType = "艺术学学士"
|
||||
};
|
||||
_db.AddRange(campus, college, major);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
var saved = await _db.Majors
|
||||
.Include(x => x.College)
|
||||
.ThenInclude(x => x!.Campus)
|
||||
.SingleAsync();
|
||||
|
||||
Assert.Equal("东校区", saved.College!.Campus!.Name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user