From b75d0add7d26b6abcf02caf1cced291a356ba7e7 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Mon, 7 Sep 2026 20:02:56 +0100 Subject: [PATCH] db testing --- src/Yavsc.Org.Tests/NonRegression/Database.cs | 28 +++++++++++++------ src/Yavsc.Org.Tests/WebServerFixture.cs | 14 ++++++++++ .../Migrations/20260309015232_init.cs | 6 +++- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/Yavsc.Org.Tests/NonRegression/Database.cs b/src/Yavsc.Org.Tests/NonRegression/Database.cs index 01cd0a5dc..4633ce177 100644 --- a/src/Yavsc.Org.Tests/NonRegression/Database.cs +++ b/src/Yavsc.Org.Tests/NonRegression/Database.cs @@ -1,34 +1,44 @@ +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Diagnostics; +using Yavsc.Models; namespace Yavsc.Org.Tests.Mandatory -{ - - [Collection("Database")] +{[Collection("Database")] [Trait("regression", "II")] [Trait("dev", "wip")] public class Database: IClassFixture, IDisposable { - readonly WebServerFixture _serverFixture; readonly ITestOutputHelper output; - public Database(WebServerFixture serverFixture, ITestOutputHelper output) + readonly WebServerFixture _serverFixture; + + public Database(ITestOutputHelper output, WebServerFixture _serverFixture) { this.output = output; - _serverFixture = serverFixture; - + this._serverFixture = _serverFixture; } /// - /// Assuming we're using an account that may create databases, + /// Assuming we're using an account that may create databases, /// Install all our migrations in a fresh new database. /// public void Dispose() { - if (_serverFixture!=null) + if (_serverFixture!=null) { _serverFixture.Dispose(); } } + + [Fact] + public void TestDatabaseMigration() + { + // Test logic goes here + _serverFixture.ResetAndMigrateDatabase(); + } } + } diff --git a/src/Yavsc.Org.Tests/WebServerFixture.cs b/src/Yavsc.Org.Tests/WebServerFixture.cs index 4f736ecfe..f82771ec6 100644 --- a/src/Yavsc.Org.Tests/WebServerFixture.cs +++ b/src/Yavsc.Org.Tests/WebServerFixture.cs @@ -189,6 +189,20 @@ public sealed class WebServerFixture : WebHostFixture return app; } + public void ResetAndMigrateDatabase() + { + using var scope = Services.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService(); + db.Database.EnsureDeleted(); + if (db.Database.IsRelational()) + { + db.Database.Migrate(); + return; + } + + db.Database.EnsureCreated(); + } + protected override async Task ConfigurePipelineAsync(WebApplication app) { // The MSBuild target CopyYavscOrgStaticAssets in diff --git a/src/Yavsc.Org/Migrations/20260309015232_init.cs b/src/Yavsc.Org/Migrations/20260309015232_init.cs index 8780da0d5..a61679882 100644 --- a/src/Yavsc.Org/Migrations/20260309015232_init.cs +++ b/src/Yavsc.Org/Migrations/20260309015232_init.cs @@ -11,6 +11,10 @@ namespace Yavsc.Migrations /// protected override void Up(MigrationBuilder migrationBuilder) { + var declarationDateDefaultSql = ActiveProvider == "Microsoft.EntityFrameworkCore.Sqlite" + ? "CURRENT_TIMESTAMP" + : "LOCALTIMESTAMP"; + migrationBuilder.CreateTable( name: "Activities", columns: table => new @@ -1484,7 +1488,7 @@ namespace Yavsc.Migrations Platform = table.Column(type: "text", nullable: true), Version = table.Column(type: "text", nullable: true), DeviceOwnerId = table.Column(type: "text", nullable: true), - DeclarationDate = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "LOCALTIMESTAMP"), + DeclarationDate = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: declarationDateDefaultSql), LatestActivityUpdate = table.Column(type: "timestamp with time zone", nullable: true) }, constraints: table =>