Greenfield C# app reading industrial machine data over pluggable protocols. M1 walking skeleton: MTConnect plugin polls a machine, Avalonia dashboard shows live last datum. Build 0 warn/0 err on net48 + net8.0 (Linux), 115 unit tests + 2 docker-gated integration tests. Projects: - Junction.Domain (netstandard2.0): Result<T>, models, IProtocolDriver, IMachineRepository, plugin manifest. Zero package deps. - Junction.Core (netstandard2.0): PollingEngine, PluginLoader (Assembly.LoadFrom), MachineMonitor, DI extensions. - Junction.Persistence (netstandard2.0): SqliteMachineRepository (Dapper), schema, connection factory. Provider-swap seam to SQL Server. - Junction.Protocols.MTConnect (netstandard2.0): HTTP driver + namespace- version-agnostic parser (MTConnect 1.7 + 2.0). Runtime plugin. - Junction.App (net48;net8.0): Avalonia MVVM, live dashboard, NLog, DI root. - Junction.Tests (net8.0): xUnit + Moq, 117 tests. - mock/: docker-compose MTConnect agent (ladder99/agent). Target net48 for Windows 7/8 fleet compatibility. Avalonia pinned 11.3.x. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
134 lines
4.1 KiB
C#
134 lines
4.1 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_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()));
|
|
}
|
|
}
|
|
}
|