Skip to content

Commit

Permalink
Merge pull request #29 from grupadotnet/add-unit-tests
Browse files Browse the repository at this point in the history
Add unit tests configuration and actual unit tests for MessageService and AttachmentService
  • Loading branch information
Gromate authored Jan 25, 2025
2 parents fcf65e7 + aa27447 commit 2d89637
Show file tree
Hide file tree
Showing 26 changed files with 470 additions and 68 deletions.
10 changes: 7 additions & 3 deletions appsettings_example.Tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
"ConnectionStrings": {
"pimi-connect-postgresql-test": "Host=localhost; Port=5432; Database=pimi-connect-test; Username=postgres; Password=password123"
},
"TestSettings": {
"EntitiesCount": 6,
"IntegrationTestsSettings": {
"EntitiesCount": 5,
"ConnectionStringName": "pimi-connect-postgresql-test",
"MigrationsAssemblyName": "pimi-connect-api.API"
}
},
"UnitTestsSettings": {
"DummyCount": 10,
"DummyStringMaxLength": 20
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace pimi_connect_api.Attachment;

public class AttachmentRepository : IRepository<AttachmentEntity>
public class AttachmentRepository : IAttachmentRepository
{
private readonly AppDbContext _dbContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ namespace pimi_connect_api.Attachment;
public class AttachmentService : IAttachmentService
{
private readonly IMapper _mapper;
private readonly AttachmentRepository _repository;
private readonly IAttachmentRepository _repository;

public AttachmentService(AppDbContext dbContext, IMapper mapper)
public AttachmentService(IMapper mapper, IAttachmentRepository repository)
{
_mapper = mapper;
_repository = new AttachmentRepository(dbContext);
_repository = repository;
}

public async Task<AttachmentDto> GetAttachmentAsync(Guid id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using pimi_connect_app.Data.Entities;
using pimi_connect_app.Data.Repository;

namespace pimi_connect_api.Attachment;

public interface IAttachmentRepository : IRepository<AttachmentEntity>
{

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using pimi_connect_app.Data.Entities;
using pimi_connect_app.Data.Repository;

namespace pimi_connect_api.Message;

public interface IMessageRepository
public interface IMessageRepository : IRepository<MessageEntity>
{
Task<bool> MessageExists(Guid id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace pimi_connect_api.Message;

public class MessageRepository : IRepository<MessageEntity>, IMessageRepository
public class MessageRepository : IMessageRepository
{
private readonly AppDbContext _dbContext;

Expand Down
13 changes: 5 additions & 8 deletions pimi-connect-api/pimi-connect-api.API/Message/MessageService.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using pimi_connect_api.Exceptions;
using pimi_connect_api.Message;
using pimi_connect_api.Services.Interfaces;
using pimi_connect_app.Data.AppDbContext;
using pimi_connect_app.Data.Entities;
using pimi_connect_app.Data.Models;
using pimi_connect_app.Data.Repository;

namespace pimi_connect_api.Services
namespace pimi_connect_api.Message
{
public class MessageService : IMessageService
{
private readonly IMapper _mapper;
private readonly MessageRepository _repository;
private readonly IMessageRepository _repository;

public MessageService(AppDbContext dbContext, IMapper mapper)
public MessageService(IMapper mapper, IMessageRepository repository)
{
_mapper = mapper;
_repository = new MessageRepository(dbContext);
_repository = repository;
}

public Task<MessageDto> AddMessageAsync(MessageDto messageDto)
{
throw new NotImplementedException();
Expand Down
9 changes: 9 additions & 0 deletions pimi-connect-api/pimi-connect-api.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
using Microsoft.EntityFrameworkCore;
using pimi_connect_api.Attachment;
using pimi_connect_api.Hubs;
using pimi_connect_api.Message;
using pimi_connect_api.Middleware;
using pimi_connect_api.Services;
using pimi_connect_api.Services.Interfaces;
using pimi_connect_api.User;
using pimi_connect_api.UserChat;
using pimi_connect_app.Data.AppDbContext;
using pimi_connect_app.Data.Entities;
using pimi_connect_app.Data.Models;
using pimi_connect_app.Data.Models.Validators;
using pimi_connect_app.Data.Repository;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -29,6 +32,12 @@
builder.Services.AddScoped<IMessageService, MessageService>();
builder.Services.AddScoped<IUserChatService, UserChatService>();

// Repositories
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IAttachmentRepository, AttachmentRepository>();
builder.Services.AddScoped<IMessageRepository, MessageRepository>();
builder.Services.AddScoped<IUserChatRepository, UserChatRepository>();

// Validators
builder.Services.AddScoped<IValidator<UserDto>, UserDtoValidator>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using pimi_connect_app.Data.Entities;
using pimi_connect_app.Data.Repository;

namespace pimi_connect_api.User;

public interface IUserRepository
public interface IUserRepository : IRepository<UserEntity>
{
Task<UserEntity?> GetByEmail(string email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace pimi_connect_api.User;

public class UserRepository : IRepository<UserEntity>, IUserRepository
public class UserRepository : IUserRepository
{
private readonly AppDbContext _dbContext;

Expand Down
6 changes: 3 additions & 3 deletions pimi-connect-api/pimi-connect-api.API/User/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace pimi_connect_api.User;
public class UserService : IUserService
{
private readonly IMapper _mapper;
private readonly UserRepository _repository;
private readonly IUserRepository _repository;

public UserService(AppDbContext dbContext, IMapper mapper)
public UserService(IMapper mapper, IUserRepository repository)
{
_mapper = mapper;
_repository = new UserRepository(dbContext);
_repository = repository;
}

public async Task<UserDto> GetUserAsync(Guid id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using pimi_connect_app.Data.Entities;
using pimi_connect_app.Data.Repository;

namespace pimi_connect_api.UserChat;

public interface IUserChatRepository : IRepository<UserChatEntity>
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace pimi_connect_api.UserChat;

public class UserChatRepository : IRepository<UserChatEntity>
public class UserChatRepository : IUserChatRepository
{
private readonly AppDbContext _dbContext;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace pimi_connect_api.UserChat;
public class UserChatService : IUserChatService
{
private readonly IMapper _mapper;
private readonly UserChatRepository _repository;
private readonly IUserChatRepository _repository;

public UserChatService(AppDbContext dbContext, IMapper mapper)
public UserChatService(IMapper mapper, IUserChatRepository repository)
{
_mapper = mapper;
_repository = new UserChatRepository(dbContext);
_repository = repository;
}

public async Task<UserChatDto> GetUserChatAsync(Guid id)
Expand Down
2 changes: 2 additions & 0 deletions pimi-connect-api/pimi-connect-api.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
global using Microsoft.EntityFrameworkCore;
global using Microsoft.AspNetCore.Mvc;

global using Moq;

global using pimi_connect_api.Controllers;
global using pimi_connect_api.Services;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Http;
using pimi_connect_api.Attachment;
using pimi_connect_api.Exceptions;
using pimi_connect_api.UnitTests.Base;
using pimi_connect_api.Tests.IntegrationTests.Base;

namespace pimi_connect_api.UnitTests.ControllersTests;
namespace pimi_connect_api.Tests.IntegrationTests;

[Collection("TestsWhichUseDatabase_pimi-connect-test")]
public class AttachmentControllerTests : ControllerTestsBase<AttachmentDto>
public class AttachmentIntegrationTests : IntegrationTestsBase<AttachmentDto>
{
private AttachmentController _attachmentController;

Check warning on line 11 in pimi-connect-api/pimi-connect-api.Tests/IntegrationTests/AttachmentControllerTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable field '_attachmentController' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Expand All @@ -20,7 +20,8 @@ public class AttachmentControllerTests : ControllerTestsBase<AttachmentDto>
}

// initialize Controller
var attachmentService = new AttachmentService(TestDbContext, Mapper);
var attachmentRepository = new AttachmentRepository(TestDbContext);
var attachmentService = new AttachmentService(Mapper, attachmentRepository);
_attachmentController = new AttachmentController(attachmentService);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
using AutoMapper;
using Microsoft.Extensions.Configuration;
using pimi_connect_api.Tests.Shared;
using pimi_connect_api.UnitTests.Shared;
using pimi_connect_app.Data.AppDbContext;
using pimi_connect_app.Data.MappingProfiles;

namespace pimi_connect_api.UnitTests.Base;
namespace pimi_connect_api.Tests.IntegrationTests.Base;

public abstract class ControllerTestsBase<TDtoType> where TDtoType : class
public abstract class IntegrationTestsBase<TDtoType> where TDtoType : class
{
#region Properties
protected AppDbContext TestDbContext { get; private set; }
protected IConfiguration Configuration { get; private set; }
protected TestSettings Settings { get; private set; }
protected IntegrationTestsSettings Settings { get; private set; }
protected IMapper Mapper { get; private set; }
protected TestHelper Helper { get; }
protected List<Guid> ExistingIds { get; private set; }
protected Guid ExistingId { get; private set; }
protected Guid NotExistingId { get; private set; }
#endregion

protected ControllerTestsBase()
protected IntegrationTestsBase()

Check warning on line 22 in pimi-connect-api/pimi-connect-api.Tests/IntegrationTests/Base/IntegrationTestsBase.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable property 'Configuration' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 22 in pimi-connect-api/pimi-connect-api.Tests/IntegrationTests/Base/IntegrationTestsBase.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable property 'Settings' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 22 in pimi-connect-api/pimi-connect-api.Tests/IntegrationTests/Base/IntegrationTestsBase.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable property 'ExistingIds' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
SetupConfiguration();
SetupSettings();
Expand Down Expand Up @@ -85,30 +85,23 @@ private void SetupConfiguration()
.Build();

Configuration = configuration;


}

private void SetupSettings()
{
Settings = new TestSettings();
Configuration.GetSection("TestSettings")
Settings = new IntegrationTestsSettings();
Configuration.GetSection("IntegrationTestsSettings")
.Bind(Settings, c => c.BindNonPublicProperties = true);

if (Settings == null)
{
throw new InvalidOperationException("Could not get TestSettings correctly.");
throw new InvalidOperationException("Could not get IntegrationTestsSettings correctly.");
}
}

private void SetupMapper()
{
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MappingProfile());
});

Mapper = new Mapper(mapperConfig);
Mapper = Utils.CreateMapper();
}

private void SetupTestDbContext()
Expand All @@ -133,12 +126,7 @@ private void SetIds()

private void SetExistingIds()
{
ExistingIds = new List<Guid>();

for (var i = 0; i < Settings.EntitiesCount; i++)
{
ExistingIds.Add(Guid.NewGuid());
}
ExistingIds = Utils.CreateGuids(Settings.EntitiesCount);
}

private void SetExistingId()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace pimi_connect_api.UnitTests.Shared;
namespace pimi_connect_api.Tests.IntegrationTests.Base;

public class TestSettings
public class IntegrationTestsSettings
{
public int EntitiesCount { get; set; }
public string ConnectionStringName { get; set; }

Check warning on line 6 in pimi-connect-api/pimi-connect-api.Tests/IntegrationTests/Base/IntegrationTestsSettings.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable property 'ConnectionStringName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using AutoMapper;
using pimi_connect_app.Data.AppDbContext;
using pimi_connect_app.Data.Entities;
using pimi_connect_app.Data.Enums;

namespace pimi_connect_api.UnitTests.Shared;
namespace pimi_connect_api.Tests.IntegrationTests.Base;

public class TestHelper
{
Expand All @@ -21,7 +19,7 @@ public async Task ResetDbContext()
await _testDbContext.Database.EnsureDeletedAsync();
await _testDbContext.Database.EnsureCreatedAsync();
}

#region Fill tables
public async Task FillUsersTable(List<Guid> idsToAdd)
{
Expand Down Expand Up @@ -51,5 +49,4 @@ await _testDbContext
_testDbContext.ChangeTracker.Clear();
}
#endregion

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Microsoft.AspNetCore.Http;
using pimi_connect_api.Exceptions;
using pimi_connect_api.UnitTests.Base;
using pimi_connect_api.Tests.IntegrationTests.Base;
using pimi_connect_api.User;

namespace pimi_connect_api.UnitTests.ControllersTests;
namespace pimi_connect_api.Tests.IntegrationTests;

[Collection("TestsWhichUseDatabase_pimi-connect-test")]
public class UserControllerTests : ControllerTestsBase<UserDto>
public class UserControllerTests : IntegrationTestsBase<UserDto>
{
private UserController _userController;

Check warning on line 11 in pimi-connect-api/pimi-connect-api.Tests/IntegrationTests/UserControllerTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Non-nullable field '_userController' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Expand All @@ -21,7 +21,8 @@ public class UserControllerTests : ControllerTestsBase<UserDto>
}

// initialize Controller
var userService = new UserService(TestDbContext, Mapper);
var userRepository = new UserRepository(TestDbContext);
var userService = new UserService(Mapper, userRepository);
_userController = new UserController(userService);
}

Expand Down
Loading

0 comments on commit 2d89637

Please sign in to comment.