213 lines
6.7 KiB
C#
213 lines
6.7 KiB
C#
using FluentAssertions;
|
|
using HaDbMigration.Configuration;
|
|
using HaDbMigration.Models;
|
|
using HaDbMigration.Services;
|
|
using HaDbMigration.Services.Interfaces;
|
|
using Microsoft.Extensions.Logging;
|
|
using Moq;
|
|
using NpgsqlTypes;
|
|
|
|
namespace HaDbMigration.Tests;
|
|
|
|
public sealed class ValidationServiceTests
|
|
{
|
|
private readonly Mock<ISqliteReaderService> _sqliteReaderMock;
|
|
private readonly Mock<IPostgresWriterService> _postgresWriterMock;
|
|
private readonly Mock<ILogger<ValidationService>> _loggerMock;
|
|
|
|
public ValidationServiceTests()
|
|
{
|
|
_sqliteReaderMock = new Mock<ISqliteReaderService>();
|
|
_postgresWriterMock = new Mock<IPostgresWriterService>();
|
|
_loggerMock = new Mock<ILogger<ValidationService>>();
|
|
}
|
|
|
|
private ValidationService CreateSut(bool dryRun = false)
|
|
{
|
|
var options = new MigrationOptions
|
|
{
|
|
SqlitePath = "test.db",
|
|
PostgresConnectionString = "Host=localhost",
|
|
DryRun = dryRun,
|
|
};
|
|
|
|
return new ValidationService(
|
|
_sqliteReaderMock.Object,
|
|
_postgresWriterMock.Object,
|
|
options,
|
|
_loggerMock.Object);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidateRowCountsAsync_WhenCountsMatch_ShouldReturnAllMatched()
|
|
{
|
|
// Arrange
|
|
var table = new TableDescriptor(
|
|
"test_table",
|
|
[new ColumnDescriptor("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true)],
|
|
"id");
|
|
|
|
_sqliteReaderMock
|
|
.Setup(x => x.GetRowCountAsync("test_table", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1000);
|
|
|
|
_postgresWriterMock
|
|
.Setup(x => x.GetRowCountAsync("test_table", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1000);
|
|
|
|
var sut = CreateSut();
|
|
|
|
// Act
|
|
var results = await sut.ValidateRowCountsAsync([table], CancellationToken.None);
|
|
|
|
// Assert
|
|
results.Should().HaveCount(1);
|
|
results[0].IsMatch.Should().BeTrue();
|
|
results[0].SourceRowCount.Should().Be(1000);
|
|
results[0].TargetRowCount.Should().Be(1000);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidateRowCountsAsync_WhenCountsMismatch_ShouldReturnMismatch()
|
|
{
|
|
// Arrange
|
|
var table = new TableDescriptor(
|
|
"test_table",
|
|
[new ColumnDescriptor("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true)],
|
|
"id");
|
|
|
|
_sqliteReaderMock
|
|
.Setup(x => x.GetRowCountAsync("test_table", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1000);
|
|
|
|
_postgresWriterMock
|
|
.Setup(x => x.GetRowCountAsync("test_table", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(500);
|
|
|
|
var sut = CreateSut();
|
|
|
|
// Act
|
|
var results = await sut.ValidateRowCountsAsync([table], CancellationToken.None);
|
|
|
|
// Assert
|
|
results.Should().HaveCount(1);
|
|
results[0].IsMatch.Should().BeFalse();
|
|
results[0].SourceRowCount.Should().Be(1000);
|
|
results[0].TargetRowCount.Should().Be(500);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidateRowCountsAsync_InDryRunMode_ShouldAlwaysReturnMatch()
|
|
{
|
|
// Arrange
|
|
var table = new TableDescriptor(
|
|
"test_table",
|
|
[new ColumnDescriptor("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true)],
|
|
"id");
|
|
|
|
_sqliteReaderMock
|
|
.Setup(x => x.GetRowCountAsync("test_table", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(1000);
|
|
|
|
var sut = CreateSut(dryRun: true);
|
|
|
|
// Act
|
|
var results = await sut.ValidateRowCountsAsync([table], CancellationToken.None);
|
|
|
|
// Assert
|
|
results.Should().HaveCount(1);
|
|
results[0].IsMatch.Should().BeTrue();
|
|
results[0].TargetRowCount.Should().Be(0);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidateRowCountsAsync_InDryRunMode_ShouldNotQueryPostgres()
|
|
{
|
|
// Arrange
|
|
var table = new TableDescriptor(
|
|
"test_table",
|
|
[new ColumnDescriptor("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true)],
|
|
"id");
|
|
|
|
_sqliteReaderMock
|
|
.Setup(x => x.GetRowCountAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(100);
|
|
|
|
var sut = CreateSut(dryRun: true);
|
|
|
|
// Act
|
|
await sut.ValidateRowCountsAsync([table], CancellationToken.None);
|
|
|
|
// Assert
|
|
_postgresWriterMock.Verify(
|
|
x => x.GetRowCountAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()),
|
|
Times.Never);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidateRowCountsAsync_WithMultipleTables_ShouldReturnResultForEach()
|
|
{
|
|
// Arrange
|
|
var table1 = new TableDescriptor(
|
|
"table1",
|
|
[new ColumnDescriptor("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true)],
|
|
"id");
|
|
var table2 = new TableDescriptor(
|
|
"table2",
|
|
[new ColumnDescriptor("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true)],
|
|
"id");
|
|
|
|
_sqliteReaderMock
|
|
.Setup(x => x.GetRowCountAsync("table1", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(100);
|
|
_sqliteReaderMock
|
|
.Setup(x => x.GetRowCountAsync("table2", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(200);
|
|
|
|
_postgresWriterMock
|
|
.Setup(x => x.GetRowCountAsync("table1", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(100);
|
|
_postgresWriterMock
|
|
.Setup(x => x.GetRowCountAsync("table2", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(200);
|
|
|
|
var sut = CreateSut();
|
|
|
|
// Act
|
|
var results = await sut.ValidateRowCountsAsync([table1, table2], CancellationToken.None);
|
|
|
|
// Assert
|
|
results.Should().HaveCount(2);
|
|
results.Should().AllSatisfy(r => r.IsMatch.Should().BeTrue());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ValidateRowCountsAsync_WithEmptyTable_ShouldHandleZeroCounts()
|
|
{
|
|
// Arrange
|
|
var table = new TableDescriptor(
|
|
"empty_table",
|
|
[new ColumnDescriptor("id", "id", NpgsqlDbType.Bigint, IsPrimaryKey: true)],
|
|
"id");
|
|
|
|
_sqliteReaderMock
|
|
.Setup(x => x.GetRowCountAsync("empty_table", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(0);
|
|
|
|
_postgresWriterMock
|
|
.Setup(x => x.GetRowCountAsync("empty_table", It.IsAny<CancellationToken>()))
|
|
.ReturnsAsync(0);
|
|
|
|
var sut = CreateSut();
|
|
|
|
// Act
|
|
var results = await sut.ValidateRowCountsAsync([table], CancellationToken.None);
|
|
|
|
// Assert
|
|
results.Should().HaveCount(1);
|
|
results[0].IsMatch.Should().BeTrue();
|
|
results[0].SourceRowCount.Should().Be(0);
|
|
results[0].TargetRowCount.Should().Be(0);
|
|
}
|
|
}
|