Files
CBDD/tests/CBDD.Tests/SetMethodTests.cs

230 lines
6.5 KiB
C#
Executable File

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<ObjectId, User>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.Users);
}
[Fact]
public void Set_Shorthand_ReturnsCorrectCollection()
{
var collection = _db.Set<User>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.Users);
}
[Fact]
public void Set_Int_ReturnsCorrectCollection()
{
var collection = _db.Set<int, Person>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.People);
}
[Fact]
public void Set_String_ReturnsCorrectCollection()
{
var collection = _db.Set<string, StringEntity>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.StringEntities);
}
[Fact]
public void Set_Guid_ReturnsCorrectCollection()
{
var collection = _db.Set<Guid, GuidEntity>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.GuidEntities);
}
[Fact]
public void Set_CustomKey_ReturnsCorrectCollection()
{
var collection = _db.Set<OrderId, Order>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.Orders);
}
[Fact]
public void Set_AllObjectIdCollections_ReturnCorrectInstances()
{
_db.Set<ObjectId, AnnotatedUser>().ShouldBeSameAs(_db.AnnotatedUsers);
_db.Set<ObjectId, ComplexUser>().ShouldBeSameAs(_db.ComplexUsers);
_db.Set<ObjectId, TestDocument>().ShouldBeSameAs(_db.TestDocuments);
_db.Set<ObjectId, OrderDocument>().ShouldBeSameAs(_db.OrderDocuments);
_db.Set<ObjectId, ComplexDocument>().ShouldBeSameAs(_db.ComplexDocuments);
_db.Set<ObjectId, PersonV2>().ShouldBeSameAs(_db.PeopleV2);
_db.Set<ObjectId, VectorEntity>().ShouldBeSameAs(_db.VectorItems);
_db.Set<ObjectId, GeoEntity>().ShouldBeSameAs(_db.GeoItems);
}
[Fact]
public void Set_AllIntCollections_ReturnCorrectInstances()
{
_db.Set<int, AutoInitEntity>().ShouldBeSameAs(_db.AutoInitEntities);
_db.Set<int, Product>().ShouldBeSameAs(_db.Products);
_db.Set<int, IntEntity>().ShouldBeSameAs(_db.IntEntities);
_db.Set<int, AsyncDoc>().ShouldBeSameAs(_db.AsyncDocs);
_db.Set<int, SchemaUser>().ShouldBeSameAs(_db.SchemaUsers);
}
[Fact]
public void Set_StringKeyCollections_ReturnCorrectInstances()
{
_db.Set<string, CustomKeyEntity>().ShouldBeSameAs(_db.CustomKeyEntities);
}
[Fact]
public void Set_UnregisteredEntity_ThrowsInvalidOperationException()
{
Should.Throw<InvalidOperationException>(() => _db.Set<ObjectId, Address>());
}
[Fact]
public void Set_WrongKeyType_ThrowsInvalidOperationException()
{
Should.Throw<InvalidOperationException>(() => _db.Set<string, User>());
}
[Fact]
public void Set_CanPerformOperations()
{
var users = _db.Set<User>();
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<int, Product>();
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<int, ExtendedEntity>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.ExtendedEntities);
}
[Fact]
public void Set_ParentCollection_ReturnsCorrectInstance()
{
var collection = _db.Set<ObjectId, User>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.Users);
}
[Fact]
public void Set_ParentShorthand_ReturnsCorrectInstance()
{
var collection = _db.Set<User>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.Users);
}
[Fact]
public void Set_ParentIntCollection_ReturnsCorrectInstance()
{
_db.Set<int, Person>().ShouldBeSameAs(_db.People);
_db.Set<int, Product>().ShouldBeSameAs(_db.Products);
}
[Fact]
public void Set_ParentCustomKey_ReturnsCorrectInstance()
{
var collection = _db.Set<OrderId, Order>();
collection.ShouldNotBeNull();
collection.ShouldBeSameAs(_db.Orders);
}
[Fact]
public void Set_UnregisteredEntity_ThrowsInvalidOperationException()
{
Should.Throw<InvalidOperationException>(() => _db.Set<ObjectId, Address>());
}
[Fact]
public void Set_OwnCollection_CanPerformOperations()
{
var entities = _db.Set<int, ExtendedEntity>();
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<User>();
var user = new User { Name = "Bob", Age = 25 };
var id = users.Insert(user);
var found = users.FindById(id);
found.ShouldNotBeNull();
found.Name.ShouldBe("Bob");
}
}