Files
CBDD/tests/CBDD.Tests/BsonSchemaTests.cs

97 lines
2.9 KiB
C#
Executable File

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
{
public ObjectId Id { get; set; }
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
public bool IsActive { get; set; }
}
[Fact]
public void GenerateSchema_SimpleEntity()
{
var schema = BsonSchemaGenerator.FromType<SimpleEntity>();
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
{
public List<string> Tags { get; set; } = new();
public int[] Scores { get; set; } = Array.Empty<int>();
}
[Fact]
public void GenerateSchema_Collections()
{
var schema = BsonSchemaGenerator.FromType<CollectionEntity>();
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
{
public SimpleEntity Parent { get; set; } = new();
}
[Fact]
public void GenerateSchema_Nested()
{
var schema = BsonSchemaGenerator.FromType<NestedEntity>();
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
{
public List<SimpleEntity> Items { get; set; } = new();
}
[Fact]
public void GenerateSchema_ComplexCollection()
{
var schema = BsonSchemaGenerator.FromType<ComplexCollectionEntity>();
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");
}
}