junction/tests/Junction.Tests/Unit/SqliteSchemaTests.cs
dtrentin 54f8b3be25 feat: history & trends (v0.6)
Time-series history persisted + per-item trend sparklines in detail view.

Data (Domain+Persistence): HistoryPoint model; IMachineRepository +Append/
Get/Prune history; snapshot_history table (append-only) + index; DeleteAsync
cascades history. Port purity kept (Result/Cancelled, no throw).

Core: MachineMonitor.PersistAsync appends history on Connected snapshots +
throttled retention prune (7-day window, <=1/hour via Interlocked-CAS), never
blocks/undoes latest-snapshot save.

App: custom Sparkline Control (StreamGeometry polyline, min/max-scaled,
net48-safe, no charting dep); DataItemRowViewModel observable +Trend/HasTrend +
pure TryParseNumeric; MachineDetailViewModel loads per-item history (1h window,
60 pts) on load + each snapshot, parses numeric, sets Trend on UI thread;
detail view Trend column.

Projects: Domain, Persistence, Core, App, Tests (+31: 196 pass + 2 skip).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-22 08:57:52 +02:00

145 lines
4.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Junction.Domain;
using Junction.Persistence;
using Xunit;
namespace Junction.Tests.Unit
{
/// <summary>
/// Integration-style tests against a REAL SQLite file. Proves the native e_sqlite3
/// library loads and runs on the Linux net8.0 test host (relevant to the net48 story).
/// </summary>
public sealed class SqliteSchemaTests : IDisposable
{
private readonly string _dbPath;
private readonly string _connectionString;
public SqliteSchemaTests()
{
_dbPath = Path.Combine(Path.GetTempPath(), "junction_test_" + Guid.NewGuid().ToString("N") + ".db");
_connectionString = "Data Source=" + _dbPath;
}
public void Dispose()
{
try
{
if (File.Exists(_dbPath))
{
File.Delete(_dbPath);
}
}
catch
{
// best-effort cleanup
}
}
[Fact]
public void ConnectionFactory_OpensUsableConnection()
{
var factory = new SqliteConnectionFactory(_connectionString);
using (IDbConnection connection = factory.CreateOpenConnection())
{
Assert.Equal(ConnectionState.Open, connection.State);
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "SELECT 1;";
var value = Convert.ToInt64(cmd.ExecuteScalar());
Assert.Equal(1L, value);
}
}
}
[Fact]
public void EnsureCreated_CreatesAllThreeTables()
{
var factory = new SqliteConnectionFactory(_connectionString);
Result result = SqliteSchema.EnsureCreated(factory);
Assert.True(result.IsSuccess, DescribeErrors(result));
var tables = QueryTableNames();
Assert.Contains("machines", tables);
Assert.Contains("latest_snapshots", tables);
Assert.Contains("snapshot_items", tables);
}
[Fact]
public void EnsureCreated_CreatesSnapshotHistoryTable()
{
var factory = new SqliteConnectionFactory(_connectionString);
Result result = SqliteSchema.EnsureCreated(factory);
Assert.True(result.IsSuccess, DescribeErrors(result));
Assert.Contains("snapshot_history", QueryTableNames());
}
[Fact]
public void EnsureCreated_IsIdempotent()
{
var factory = new SqliteConnectionFactory(_connectionString);
Result first = SqliteSchema.EnsureCreated(factory);
Result second = SqliteSchema.EnsureCreated(factory);
Assert.True(first.IsSuccess, DescribeErrors(first));
Assert.True(second.IsSuccess, DescribeErrors(second));
var tables = QueryTableNames();
Assert.Contains("machines", tables);
Assert.Contains("latest_snapshots", tables);
Assert.Contains("snapshot_items", tables);
}
[Fact]
public void EnsureCreated_OnOpenConnection_Works()
{
var factory = new SqliteConnectionFactory(_connectionString);
using (IDbConnection connection = factory.CreateOpenConnection())
{
Result result = SqliteSchema.EnsureCreated(connection);
Assert.True(result.IsSuccess, DescribeErrors(result));
}
}
private List<string> QueryTableNames()
{
var names = new List<string>();
var factory = new SqliteConnectionFactory(_connectionString);
using (IDbConnection connection = factory.CreateOpenConnection())
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "SELECT name FROM sqlite_master WHERE type = 'table';";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
names.Add(reader.GetString(0));
}
}
}
return names;
}
private static string DescribeErrors(Result result)
{
if (result.IsSuccess)
{
return "";
}
return string.Join("; ", System.Linq.Enumerable.Select(result.Errors, e => e.ToString()));
}
}
}