using ZB.MOM.WW.CBDD.Bson; using ZB.MOM.WW.CBDD.Core.Collections; using ZB.MOM.WW.CBDD.Shared; namespace ZB.MOM.WW.CBDD.Tests; public class SetMethodTests : IDisposable { private readonly string _dbPath; private readonly Shared.TestDbContext _db; public SetMethodTests() { _dbPath = Path.Combine(Path.GetTempPath(), $"cbdd_set_{Guid.NewGuid()}.db"); _db = new Shared.TestDbContext(_dbPath); } public void Dispose() { _db.Dispose(); if (File.Exists(_dbPath)) File.Delete(_dbPath); } [Fact] public void Set_ObjectId_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Users); } [Fact] public void Set_Shorthand_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Users); } [Fact] public void Set_Int_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.People); } [Fact] public void Set_String_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.StringEntities); } [Fact] public void Set_Guid_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.GuidEntities); } [Fact] public void Set_CustomKey_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Orders); } [Fact] public void Set_AllObjectIdCollections_ReturnCorrectInstances() { _db.Set().ShouldBeSameAs(_db.AnnotatedUsers); _db.Set().ShouldBeSameAs(_db.ComplexUsers); _db.Set().ShouldBeSameAs(_db.TestDocuments); _db.Set().ShouldBeSameAs(_db.OrderDocuments); _db.Set().ShouldBeSameAs(_db.ComplexDocuments); _db.Set().ShouldBeSameAs(_db.PeopleV2); _db.Set().ShouldBeSameAs(_db.VectorItems); _db.Set().ShouldBeSameAs(_db.GeoItems); } [Fact] public void Set_AllIntCollections_ReturnCorrectInstances() { _db.Set().ShouldBeSameAs(_db.AutoInitEntities); _db.Set().ShouldBeSameAs(_db.Products); _db.Set().ShouldBeSameAs(_db.IntEntities); _db.Set().ShouldBeSameAs(_db.AsyncDocs); _db.Set().ShouldBeSameAs(_db.SchemaUsers); } [Fact] public void Set_StringKeyCollections_ReturnCorrectInstances() { _db.Set().ShouldBeSameAs(_db.CustomKeyEntities); } [Fact] public void Set_UnregisteredEntity_ThrowsInvalidOperationException() { Should.Throw(() => _db.Set()); } [Fact] public void Set_WrongKeyType_ThrowsInvalidOperationException() { Should.Throw(() => _db.Set()); } [Fact] public void Set_CanPerformOperations() { var users = _db.Set(); var user = new User { Name = "Alice", Age = 30 }; var id = users.Insert(user); var found = users.FindById(id); found.ShouldNotBeNull(); found.Name.ShouldBe("Alice"); found.Age.ShouldBe(30); } [Fact] public void Set_WithIntKey_CanPerformOperations() { var products = _db.Set(); var product = new Product { Id = 1, Title = "Widget", Price = 9.99m }; products.Insert(product); var found = products.FindById(1); found.ShouldNotBeNull(); found.Title.ShouldBe("Widget"); found.Price.ShouldBe(9.99m); } } public class SetMethodInheritanceTests : IDisposable { private readonly string _dbPath; private readonly Shared.TestExtendedDbContext _db; public SetMethodInheritanceTests() { _dbPath = Path.Combine(Path.GetTempPath(), $"cbdd_set_inherit_{Guid.NewGuid()}.db"); _db = new Shared.TestExtendedDbContext(_dbPath); } public void Dispose() { _db.Dispose(); if (File.Exists(_dbPath)) File.Delete(_dbPath); } [Fact] public void Set_OwnCollection_ReturnsCorrectInstance() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.ExtendedEntities); } [Fact] public void Set_ParentCollection_ReturnsCorrectInstance() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Users); } [Fact] public void Set_ParentShorthand_ReturnsCorrectInstance() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Users); } [Fact] public void Set_ParentIntCollection_ReturnsCorrectInstance() { _db.Set().ShouldBeSameAs(_db.People); _db.Set().ShouldBeSameAs(_db.Products); } [Fact] public void Set_ParentCustomKey_ReturnsCorrectInstance() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Orders); } [Fact] public void Set_UnregisteredEntity_ThrowsInvalidOperationException() { Should.Throw(() => _db.Set()); } [Fact] public void Set_OwnCollection_CanPerformOperations() { var entities = _db.Set(); var entity = new ExtendedEntity { Id = 1, Description = "Test", CreatedAt = DateTime.UtcNow }; entities.Insert(entity); var found = entities.FindById(1); found.ShouldNotBeNull(); found.Description.ShouldBe("Test"); } [Fact] public void Set_ParentCollection_CanPerformOperations() { var users = _db.Set(); var user = new User { Name = "Bob", Age = 25 }; var id = users.Insert(user); var found = users.FindById(id); found.ShouldNotBeNull(); found.Name.ShouldBe("Bob"); } }