add tests
All checks were successful
SonarQube / Build and analyze (push) Successful in 3m54s
Unit Tests / Run Tests (push) Successful in 2m23s

This commit is contained in:
Leonid Pershin
2025-10-20 15:11:42 +03:00
parent f8fd16edb2
commit 1c910d7b7f
8 changed files with 549 additions and 154 deletions

View File

@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json;
using ChatBot.Models.Entities;
using FluentAssertions;
@@ -634,4 +635,55 @@ public class ChatSessionEntityTests
columnAttribute.Should().NotBeNull();
columnAttribute!.Name.Should().Be("max_history_length");
}
[Fact]
public void JsonSerialization_RoundTrip_ShouldPreserveScalarProperties()
{
// Arrange
var original = new ChatSessionEntity
{
Id = 101,
SessionId = "session-json-1",
ChatId = 123456789L,
ChatType = "group",
ChatTitle = "Test Group",
Model = "llama3.1:8b",
CreatedAt = DateTime.UtcNow.AddMinutes(-10),
LastUpdatedAt = DateTime.UtcNow.AddMinutes(-5),
MaxHistoryLength = 77,
Messages = new List<ChatMessageEntity>(),
};
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = false,
DefaultIgnoreCondition = System
.Text
.Json
.Serialization
.JsonIgnoreCondition
.WhenWritingNull,
};
// Act
var json = JsonSerializer.Serialize(original, options);
var deserialized = JsonSerializer.Deserialize<ChatSessionEntity>(json, options)!;
// Assert
deserialized.Id.Should().Be(original.Id);
deserialized.SessionId.Should().Be(original.SessionId);
deserialized.ChatId.Should().Be(original.ChatId);
deserialized.ChatType.Should().Be(original.ChatType);
deserialized.ChatTitle.Should().Be(original.ChatTitle);
deserialized.Model.Should().Be(original.Model);
deserialized.MaxHistoryLength.Should().Be(original.MaxHistoryLength);
// Allow small delta for DateTime serialization
deserialized.CreatedAt.Should().BeCloseTo(original.CreatedAt, TimeSpan.FromSeconds(1));
deserialized
.LastUpdatedAt.Should()
.BeCloseTo(original.LastUpdatedAt, TimeSpan.FromSeconds(1));
deserialized.Messages.Should().NotBeNull();
deserialized.Messages.Should().BeEmpty();
}
}