updated tests, added Testcontainers, added ApplicationDbContext

This commit is contained in:
Mia Rose Winter 2024-04-30 14:49:14 +02:00
parent e1cab9b53f
commit 2c44951e13
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
2 changed files with 81 additions and 4 deletions

View file

@ -0,0 +1,68 @@
using Microsoft.EntityFrameworkCore;
using Testcontainers.PostgreSql;
using Wave.Data;
namespace Wave.Tests.Data;
[TestFixture, FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
[TestOf(typeof(ApplicationDbContext))]
public class ApplicationDbContextTest {
private PostgreSqlContainer PostgresContainer { get; } = new PostgreSqlBuilder().WithImage("postgres:16.1-alpine").Build();
[SetUp]
public async Task SetUp() {
await PostgresContainer.StartAsync();
}
[TearDown]
public async Task TearDown() {
await PostgresContainer.DisposeAsync();
}
private ApplicationDbContext GetContext() {
return new ApplicationDbContext(
new DbContextOptionsBuilder<ApplicationDbContext>()
.UseNpgsql(PostgresContainer.GetConnectionString())
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
.EnableThreadSafetyChecks()
.Options);
}
[Test]
public async Task Migration() {
await using var context = GetContext();
Assert.DoesNotThrowAsync(() => context.Database.MigrateAsync());
}
[Test]
public async Task CreateArticle() {
await using var context = GetContext();
await context.Database.EnsureCreatedAsync();
var author = new ApplicationUser {
FullName = "Test User"
};
Article article = new() {
Title = "Testing Article",
Body = "This is a *test* Article",
Author = author
};
article.UpdateSlug(null);
article.UpdateBody();
await context.AddAsync(article);
Assert.DoesNotThrowAsync(() => context.SaveChangesAsync());
var dbArticle = await context.Set<Article>()
.IgnoreQueryFilters().FirstOrDefaultAsync();
Assert.That(dbArticle, Is.Not.Null);
Assert.That(dbArticle.Title, Is.EqualTo("Testing Article"));
Assert.That(dbArticle.Slug, Is.EqualTo("testing-article"));
Assert.That(dbArticle.Body, Is.EqualTo("This is a *test* Article"));
Assert.That(dbArticle.BodyPlain, Is.EqualTo("This is a test Article"));
Assert.That(dbArticle.BodyHtml, Is.EqualTo("<p>This is a <em>test</em> Article</p>"));
}
}

View file

@ -10,11 +10,20 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" /> <PackageReference Include="coverlet.collector" Version="6.0.2">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> <PrivateAssets>all</PrivateAssets>
<PackageReference Include="NUnit" Version="3.14.0" /> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> </PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="Testcontainers" Version="3.8.0" />
<PackageReference Include="Testcontainers.PostgreSql" Version="3.8.0" />
<PackageReference Include="Testcontainers.Redis" Version="3.8.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>