refactor(FactoryTests): created unit tests for Factory
This commit is contained in:
parent
fbf9488c55
commit
ea2340974a
@ -54,15 +54,15 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.10" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.10" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.10" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.10" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.10" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.10" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.5" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
173
DigitalData.Core.Tests/Abstractions/FactoryTests.cs
Normal file
173
DigitalData.Core.Tests/Abstractions/FactoryTests.cs
Normal file
@ -0,0 +1,173 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using DigitalData.Core.Abstractions;
|
||||
|
||||
namespace DigitalData.Core.Tests.Abstractions
|
||||
{
|
||||
[TestFixture]
|
||||
public class FactoryTests
|
||||
{
|
||||
private Factory _factory;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_factory = new Factory();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Add_ServiceDescriptor_ShouldIncreaseCount()
|
||||
{
|
||||
// Arrange
|
||||
var descriptor = ServiceDescriptor.Singleton(typeof(string), "test");
|
||||
|
||||
// Act
|
||||
_factory.Add(descriptor);
|
||||
|
||||
// Assert
|
||||
Assert.That(_factory.Count, Is.EqualTo(1));
|
||||
Assert.That(_factory.Contains(descriptor), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Clear_ShouldRemoveAllServices()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "test"));
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(int), 42));
|
||||
|
||||
// Act
|
||||
_factory.Clear();
|
||||
|
||||
// Assert
|
||||
Assert.That(_factory.Count, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetService_ShouldReturnRegisteredInstance()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "Hello World"));
|
||||
|
||||
// Act
|
||||
var result = _factory.GetService(typeof(string));
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.EqualTo("Hello World"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetService_UnregisteredType_ShouldReturnNull()
|
||||
{
|
||||
// Act
|
||||
var result = _factory.GetService(typeof(int));
|
||||
|
||||
// Assert
|
||||
Assert.That(result, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ModifyAfterBuild_ShouldThrowInvalidOperationException()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "x"));
|
||||
var _ = _factory.GetService(typeof(string)); // trigger build
|
||||
|
||||
// Act & Assert
|
||||
Assert.Throws<InvalidOperationException>(() => _factory.Add(ServiceDescriptor.Singleton(typeof(int), 1)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove_ShouldWorkBeforeBuild()
|
||||
{
|
||||
// Arrange
|
||||
var descriptor = ServiceDescriptor.Singleton(typeof(string), "Test");
|
||||
_factory.Add(descriptor);
|
||||
|
||||
// Act
|
||||
var removed = _factory.Remove(descriptor);
|
||||
|
||||
// Assert
|
||||
Assert.That(removed, Is.True);
|
||||
Assert.That(_factory.Count, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Insert_ShouldInsertAtCorrectIndex()
|
||||
{
|
||||
// Arrange
|
||||
var d1 = ServiceDescriptor.Singleton(typeof(string), "A");
|
||||
var d2 = ServiceDescriptor.Singleton(typeof(int), 1);
|
||||
|
||||
_factory.Add(d1);
|
||||
_factory.Insert(0, d2);
|
||||
|
||||
// Act
|
||||
var first = _factory[0];
|
||||
|
||||
// Assert
|
||||
Assert.That(first.ServiceType, Is.EqualTo(typeof(int)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Enumerator_ShouldIterateOverServices()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "a"));
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(int), 5));
|
||||
|
||||
// Act
|
||||
var list = _factory.ToList();
|
||||
|
||||
// Assert
|
||||
Assert.That(list.Count, Is.EqualTo(2));
|
||||
Assert.That(list.Any(sd => sd.ServiceType == typeof(string)), Is.True);
|
||||
Assert.That(list.Any(sd => sd.ServiceType == typeof(int)), Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CopyTo_ShouldCopyElements()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "x"));
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(int), 10));
|
||||
var array = new ServiceDescriptor[2];
|
||||
|
||||
// Act
|
||||
_factory.CopyTo(array, 0);
|
||||
|
||||
// Assert
|
||||
Assert.That(array[0].ServiceType, Is.EqualTo(typeof(string)));
|
||||
Assert.That(array[1].ServiceType, Is.EqualTo(typeof(int)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void RemoveAt_ShouldRemoveItemAtIndex()
|
||||
{
|
||||
// Arrange
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(string), "x"));
|
||||
_factory.Add(ServiceDescriptor.Singleton(typeof(int), 5));
|
||||
|
||||
// Act
|
||||
_factory.RemoveAt(0);
|
||||
|
||||
// Assert
|
||||
Assert.That(_factory.Count, Is.EqualTo(1));
|
||||
Assert.That(_factory[0].ServiceType, Is.EqualTo(typeof(int)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Indexer_Set_ShouldReplaceItem()
|
||||
{
|
||||
// Arrange
|
||||
var original = ServiceDescriptor.Singleton(typeof(string), "A");
|
||||
var replacement = ServiceDescriptor.Singleton(typeof(string), "B");
|
||||
_factory.Add(original);
|
||||
|
||||
// Act
|
||||
_factory[0] = replacement;
|
||||
|
||||
// Assert
|
||||
Assert.That(_factory[0], Is.EqualTo(replacement));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,115 +0,0 @@
|
||||
using DigitalData.Core.Tests.Mock;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Reflection;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Infrastructure;
|
||||
|
||||
namespace DigitalData.Core.Tests.Infrastructure;
|
||||
|
||||
public class DbRepositoryTests
|
||||
{
|
||||
private IHost _host;
|
||||
|
||||
private IRepository Repo;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
var builder = Host.CreateApplicationBuilder();
|
||||
|
||||
builder.Services.AddDbContext<MockDbContext>(opt => opt.UseInMemoryDatabase("MockDB"));
|
||||
|
||||
builder.Services.AddDbRepository(opt =>
|
||||
{
|
||||
opt.RegisterFromAssembly<MockDbContext>(typeof(User).Assembly);
|
||||
});
|
||||
|
||||
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
||||
|
||||
_host = builder.Build();
|
||||
|
||||
Repo = _host.Services.GetRequiredService<IRepository>();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
if (_host is IDisposable disposableHost)
|
||||
disposableHost.Dispose();
|
||||
}
|
||||
|
||||
[TestCase(true, TestName = "WhenGivenMultipleUsers")]
|
||||
[TestCase(false, TestName = "WhenGivenSingleUser")]
|
||||
public void CreateAsync_ShouldNotThrow(bool multiple)
|
||||
{
|
||||
// Arrange
|
||||
var faker = Fake.CreateUserFaker();
|
||||
|
||||
// Act & Assert
|
||||
if (multiple)
|
||||
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10))));
|
||||
else
|
||||
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate()));
|
||||
}
|
||||
|
||||
[TestCase(true, TestName = "WhenDtoUsed")]
|
||||
[TestCase(false, TestName = "WhenEntityUsed")]
|
||||
public async Task ReadAsync_ShouldReturnCreated(bool useDto)
|
||||
{
|
||||
// Act
|
||||
var createdUser = useDto
|
||||
? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
|
||||
: await Repo.CreateAsync(Fake.User);
|
||||
|
||||
var readUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(readUser, Is.Not.Null);
|
||||
Assert.That(readUser, Is.EqualTo(createdUser));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReadAsync_ShouldReturnUpdated()
|
||||
{
|
||||
// Arrange
|
||||
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
|
||||
var userUpdateDto = new UserUpdateDto() { Age = 10, Email = "Bar", FirstName = "Foo" };
|
||||
|
||||
// Act
|
||||
await Repo.UpdateAsync<User, UserUpdateDto>(userUpdateDto, u => u.Id == createdUser!.Id);
|
||||
var upToDateUser = await Repo.Where<User>(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(upToDateUser, Is.Not.Null);
|
||||
Assert.That(upToDateUser?.FirstName, Is.EqualTo(userUpdateDto.FirstName));
|
||||
Assert.That(upToDateUser?.Email, Is.EqualTo(userUpdateDto.Email));
|
||||
Assert.That(upToDateUser?.Age, Is.EqualTo(userUpdateDto.Age));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ReadAsync_ShouldNotReturnDeleted()
|
||||
{
|
||||
// Arrange
|
||||
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
|
||||
var readUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Act
|
||||
await Repo.DeleteAsync<User>(u => u.Id == createdUser.Id);
|
||||
var deletedUser = await Repo.Where<User>(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(readUser, Is.Not.Null);
|
||||
Assert.That(deletedUser, Is.Null);
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Abstractions.Interfaces;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace DigitalData.Core.Tests.Mock;
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Abstractions.Interfaces;
|
||||
|
||||
namespace DigitalData.Core.Tests.Mock;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Abstractions.Interfaces;
|
||||
|
||||
namespace DigitalData.Core.Tests.Mock;
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using DigitalData.Core.Abstractions.Interfaces;
|
||||
|
||||
namespace DigitalData.Core.Tests.Mock;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user