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;
|
||||
|
||||
@@ -419,4 +420,45 @@ public class ChatMessageEntityTests
|
||||
foreignKeyAttribute.Should().NotBeNull();
|
||||
foreignKeyAttribute!.Name.Should().Be("SessionId");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonSerialization_RoundTrip_ShouldPreserveScalarProperties()
|
||||
{
|
||||
// Arrange
|
||||
var original = new ChatMessageEntity
|
||||
{
|
||||
Id = 42,
|
||||
SessionId = 7,
|
||||
Content = "Hello JSON",
|
||||
Role = "user",
|
||||
CreatedAt = DateTime.UtcNow.AddMinutes(-5),
|
||||
MessageOrder = 3,
|
||||
Session = null!, // ensure navigation not required for serialization
|
||||
};
|
||||
|
||||
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<ChatMessageEntity>(json, options)!;
|
||||
|
||||
// Assert
|
||||
deserialized.Id.Should().Be(original.Id);
|
||||
deserialized.SessionId.Should().Be(original.SessionId);
|
||||
deserialized.Content.Should().Be(original.Content);
|
||||
deserialized.Role.Should().Be(original.Role);
|
||||
deserialized.MessageOrder.Should().Be(original.MessageOrder);
|
||||
// Allow a small delta due to serialization precision
|
||||
deserialized.CreatedAt.Should().BeCloseTo(original.CreatedAt, TimeSpan.FromSeconds(1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user