add tests
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user