using ZB.MOM.WW.CBDD.Bson;
using ZB.MOM.WW.CBDD.Core.Collections;
using Xunit;
using System.Collections.Generic;
using System;
using System.Linq;
namespace ZB.MOM.WW.CBDD.Tests;
public class BsonSchemaTests
{
public class SimpleEntity
{
///
/// Gets or sets the identifier.
///
public ObjectId Id { get; set; }
///
/// Gets or sets the name.
///
public string Name { get; set; } = string.Empty;
///
/// Gets or sets the age.
///
public int Age { get; set; }
///
/// Gets or sets a value indicating whether the entity is active.
///
public bool IsActive { get; set; }
}
///
/// Verifies schema generation for a simple entity.
///
[Fact]
public void GenerateSchema_SimpleEntity()
{
var schema = BsonSchemaGenerator.FromType();
schema.Title.ShouldBe("SimpleEntity");
schema.Fields.Count.ShouldBe(4);
var idField = schema.Fields.First(f => f.Name == "_id");
idField.Type.ShouldBe(BsonType.ObjectId);
var nameField = schema.Fields.First(f => f.Name == "name");
nameField.Type.ShouldBe(BsonType.String);
var ageField = schema.Fields.First(f => f.Name == "age");
ageField.Type.ShouldBe(BsonType.Int32);
}
public class CollectionEntity
{
///
/// Gets or sets tags.
///
public List Tags { get; set; } = new();
///
/// Gets or sets scores.
///
public int[] Scores { get; set; } = Array.Empty();
}
///
/// Verifies schema generation for collection fields.
///
[Fact]
public void GenerateSchema_Collections()
{
var schema = BsonSchemaGenerator.FromType();
var tags = schema.Fields.First(f => f.Name == "tags");
tags.Type.ShouldBe(BsonType.Array);
tags.ArrayItemType.ShouldBe(BsonType.String);
var scores = schema.Fields.First(f => f.Name == "scores");
scores.Type.ShouldBe(BsonType.Array);
scores.ArrayItemType.ShouldBe(BsonType.Int32);
}
public class NestedEntity
{
///
/// Gets or sets the parent entity.
///
public SimpleEntity Parent { get; set; } = new();
}
///
/// Verifies schema generation for nested document fields.
///
[Fact]
public void GenerateSchema_Nested()
{
var schema = BsonSchemaGenerator.FromType();
var parent = schema.Fields.First(f => f.Name == "parent");
parent.Type.ShouldBe(BsonType.Document);
parent.NestedSchema.ShouldNotBeNull();
parent.NestedSchema.Fields.ShouldContain(f => f.Name == "_id");
}
public class ComplexCollectionEntity
{
///
/// Gets or sets items.
///
public List Items { get; set; } = new();
}
///
/// Verifies schema generation for collections of complex types.
///
[Fact]
public void GenerateSchema_ComplexCollection()
{
var schema = BsonSchemaGenerator.FromType();
var items = schema.Fields.First(f => f.Name == "items");
items.Type.ShouldBe(BsonType.Array);
// items.ArrayItemType.ShouldBe(BsonType.Document); // Wait, my generator logic might return Array here? No, item type logic...
// Let's verify generator logic for complex array item type
// In generator: (BsonType.Array, itemNested, itemBsonType)
// itemBsonType for SimpleEntity should be Document
items.ArrayItemType.ShouldBe(BsonType.Document);
items.NestedSchema.ShouldNotBeNull();
items.NestedSchema.Fields.ShouldContain(f => f.Name == "_id");
}
}