server-monitor-manager/tests/ServerMonitorManager.Control.Tests/SchemaCompatibilityTests.cs

43 lines
1.6 KiB
C#

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Options;
using Xunit;
namespace ServerMonitorManager.Control.Tests;
public sealed class SchemaCompatibilityTests
{
[Fact]
public async Task CanOpenAlpha7DatabaseWithoutReapplyingMigrations()
{
// Try to find the alpha7.db file which should be generated by CI
var dbPath = Path.GetFullPath(Path.Combine(System.AppContext.BaseDirectory, "..", "..", "..", "..", "..", "tests", "acceptance", "alpha7.db"));
if (!File.Exists(dbPath))
{
// Skip the test locally if the file hasn't been generated
return;
}
// 1. Verify PRAGMA user_version is readable and hasn't changed abruptly
await using var connection = new SqliteConnection($"Data Source={dbPath}");
await connection.OpenAsync();
var command = connection.CreateCommand();
command.CommandText = "PRAGMA user_version;";
var userVersion = Convert.ToInt64(await command.ExecuteScalarAsync());
Assert.True(userVersion > 0, "Database user_version should be greater than 0");
// 2. Open it with the current ControlStore to see if data reads correctly and it doesn't crash
var store = new ControlStore(Options.Create(new ControlOptions { DatabasePath = dbPath }));
await store.InitializeAsync(CancellationToken.None);
// 3. Test that we can read from it
var links = await store.ListEffectiveLinksForNodeAsync("nonexistent", CancellationToken.None);
Assert.Empty(links);
}
}