190 lines
5.2 KiB
C#
190 lines
5.2 KiB
C#
using System.Reflection;
|
|
using FluentAssertions;
|
|
using HaDbMigration.Models;
|
|
using HaDbMigration.Services;
|
|
using NpgsqlTypes;
|
|
|
|
namespace HaDbMigration.Tests;
|
|
|
|
public sealed class PostgresWriterServiceTests
|
|
{
|
|
private const BindingFlags PrivateStatic = BindingFlags.NonPublic | BindingFlags.Static;
|
|
|
|
private static object? InvokeConvertValue(object value, ColumnDescriptor column)
|
|
{
|
|
return typeof(PostgresWriterService)
|
|
.GetMethod("ConvertValue", PrivateStatic)!
|
|
.Invoke(null, [value, column]);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0L, false)] // SQLite integer 0 -> PostgreSQL false
|
|
[InlineData(1L, true)] // SQLite integer 1 -> PostgreSQL true
|
|
[InlineData(42L, true)] // any non-zero -> true
|
|
public void ConvertValue_WithBooleanConversion_ShouldConvertIntToBool(
|
|
long input, bool expected)
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("test", "test", NpgsqlDbType.Boolean, IsBooleanConversion: true);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue(input, column);
|
|
|
|
// Assert
|
|
result.Should().Be(expected);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithBigintColumn_ShouldConvertToLong()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("id", "id", NpgsqlDbType.Bigint);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue(42, column);
|
|
|
|
// Assert
|
|
result.Should().Be(42L);
|
|
result.Should().BeOfType<long>();
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithIntegerColumn_ShouldConvertToInt()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("ver", "ver", NpgsqlDbType.Integer);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue(42L, column);
|
|
|
|
// Assert
|
|
result.Should().Be(42);
|
|
result.Should().BeOfType<int>();
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithSmallintColumn_ShouldConvertToShort()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("idx", "idx", NpgsqlDbType.Smallint);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue(5, column);
|
|
|
|
// Assert
|
|
result.Should().Be((short)5);
|
|
result.Should().BeOfType<short>();
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithDoubleColumn_ShouldConvertToDouble()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("ts", "ts", NpgsqlDbType.Double);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue(1234567890.123, column);
|
|
|
|
// Assert
|
|
result.Should().BeOfType<double>();
|
|
((double)result!).Should().BeApproximately(1234567890.123, 0.001);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithTextColumn_ShouldConvertToString()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("data", "data", NpgsqlDbType.Text);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue("{\"key\": \"value\"}", column);
|
|
|
|
// Assert
|
|
result.Should().Be("{\"key\": \"value\"}");
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithVarcharColumn_ShouldConvertToString()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("name", "name", NpgsqlDbType.Varchar);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue("test_entity", column);
|
|
|
|
// Assert
|
|
result.Should().Be("test_entity");
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithCharColumnAndEmptyString_ShouldReturnNull()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("legacy", "legacy", NpgsqlDbType.Char);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue("", column);
|
|
|
|
// Assert
|
|
result.Should().BeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithByteaColumn_ShouldReturnByteArray()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("bin", "bin", NpgsqlDbType.Bytea);
|
|
var bytes = new byte[] { 0x01, 0x02, 0x03 };
|
|
|
|
// Act
|
|
var result = InvokeConvertValue(bytes, column);
|
|
|
|
// Assert
|
|
result.Should().BeOfType<byte[]>();
|
|
((byte[])result!).Should().Equal(0x01, 0x02, 0x03);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithTimestampTzAndStringDate_ShouldConvertToUtcDateTime()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("ts", "ts", NpgsqlDbType.TimestampTz);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue("2024-01-15 10:30:00", column);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
result.Should().BeOfType<DateTime>();
|
|
((DateTime)result!).Kind.Should().Be(DateTimeKind.Utc);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithTimestampTzAndDateTime_ShouldEnsureUtcKind()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("ts", "ts", NpgsqlDbType.TimestampTz);
|
|
var unspecified = new DateTime(2024, 1, 15, 10, 30, 0, DateTimeKind.Unspecified);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue(unspecified, column);
|
|
|
|
// Assert
|
|
result.Should().NotBeNull();
|
|
((DateTime)result!).Kind.Should().Be(DateTimeKind.Utc);
|
|
}
|
|
|
|
[Fact]
|
|
public void ConvertValue_WithTimestampTzAndInvalidString_ShouldReturnNull()
|
|
{
|
|
// Arrange
|
|
var column = new ColumnDescriptor("ts", "ts", NpgsqlDbType.TimestampTz);
|
|
|
|
// Act
|
|
var result = InvokeConvertValue("not-a-date", column);
|
|
|
|
// Assert
|
|
result.Should().BeNull();
|
|
}
|
|
}
|