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; /// /// Initializes a new instance of the class. /// public SetMethodTests() { _dbPath = Path.Combine(Path.GetTempPath(), $"cbdd_set_{Guid.NewGuid()}.db"); _db = new Shared.TestDbContext(_dbPath); } /// /// Disposes the resources used by this instance. /// public void Dispose() { _db.Dispose(); if (File.Exists(_dbPath)) File.Delete(_dbPath); } /// /// Tests set object id returns correct collection. /// [Fact] public void Set_ObjectId_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Users); } /// /// Tests set shorthand returns correct collection. /// [Fact] public void Set_Shorthand_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Users); } /// /// Tests set int returns correct collection. /// [Fact] public void Set_Int_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.People); } /// /// Tests set string returns correct collection. /// [Fact] public void Set_String_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.StringEntities); } /// /// Tests set guid returns correct collection. /// [Fact] public void Set_Guid_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.GuidEntities); } /// /// Tests set custom key returns correct collection. /// [Fact] public void Set_CustomKey_ReturnsCorrectCollection() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Orders); } /// /// Tests set all object id collections return correct instances. /// [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); } /// /// Tests set all int collections return correct instances. /// [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); } /// /// Tests set string key collections return correct instances. /// [Fact] public void Set_StringKeyCollections_ReturnCorrectInstances() { _db.Set().ShouldBeSameAs(_db.CustomKeyEntities); } /// /// Tests set unregistered entity throws invalid operation exception. /// [Fact] public void Set_UnregisteredEntity_ThrowsInvalidOperationException() { Should.Throw(() => _db.Set()); } /// /// Tests set wrong key type throws invalid operation exception. /// [Fact] public void Set_WrongKeyType_ThrowsInvalidOperationException() { Should.Throw(() => _db.Set()); } /// /// Tests set can perform operations. /// [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); } /// /// Tests set with int key can perform operations. /// [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; /// /// Initializes a new instance of the class. /// public SetMethodInheritanceTests() { _dbPath = Path.Combine(Path.GetTempPath(), $"cbdd_set_inherit_{Guid.NewGuid()}.db"); _db = new Shared.TestExtendedDbContext(_dbPath); } /// /// Disposes the resources used by this instance. /// public void Dispose() { _db.Dispose(); if (File.Exists(_dbPath)) File.Delete(_dbPath); } /// /// Tests set own collection returns correct instance. /// [Fact] public void Set_OwnCollection_ReturnsCorrectInstance() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.ExtendedEntities); } /// /// Tests set parent collection returns correct instance. /// [Fact] public void Set_ParentCollection_ReturnsCorrectInstance() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Users); } /// /// Tests set parent shorthand returns correct instance. /// [Fact] public void Set_ParentShorthand_ReturnsCorrectInstance() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Users); } /// /// Tests set parent int collection returns correct instance. /// [Fact] public void Set_ParentIntCollection_ReturnsCorrectInstance() { _db.Set().ShouldBeSameAs(_db.People); _db.Set().ShouldBeSameAs(_db.Products); } /// /// Tests set parent custom key returns correct instance. /// [Fact] public void Set_ParentCustomKey_ReturnsCorrectInstance() { var collection = _db.Set(); collection.ShouldNotBeNull(); collection.ShouldBeSameAs(_db.Orders); } /// /// Tests set unregistered entity throws invalid operation exception. /// [Fact] public void Set_UnregisteredEntity_ThrowsInvalidOperationException() { Should.Throw(() => _db.Set()); } /// /// Tests set own collection can perform operations. /// [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"); } /// /// Tests set parent collection can perform operations. /// [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"); } }