From bfa4a4c1d751b5390c30c6c1f86c2f422b687cca Mon Sep 17 00:00:00 2001 From: HueByte Date: Sat, 17 Sep 2022 01:07:55 +0200 Subject: [PATCH] T #5 Basic implementation of tests for repositories --- Huppy/Huppy.App/Huppy.App.csproj | 1 + Huppy/Huppy.App/HuppyHostedService.cs | 7 ++ .../Huppy.IntegrationTests/Data/UserData.cs | 46 ++++++++++++- .../Huppy.IntegrationTests/Huppy.Tests.csproj | 1 + .../Repositories/DefaultRepositoryTests.cs | 67 +++++++++++++------ .../Repositories/UserRepositoryTests.cs | 55 +++++++-------- .../Huppy.IntegrationTests/Startup.cs | 5 +- .../Utilities/DatabaseSupport.cs | 17 +++++ 8 files changed, 149 insertions(+), 50 deletions(-) create mode 100644 Huppy/Huppy.Tests/Huppy.IntegrationTests/Utilities/DatabaseSupport.cs diff --git a/Huppy/Huppy.App/Huppy.App.csproj b/Huppy/Huppy.App/Huppy.App.csproj index 7bc2c14..c5f152e 100644 --- a/Huppy/Huppy.App/Huppy.App.csproj +++ b/Huppy/Huppy.App/Huppy.App.csproj @@ -9,6 +9,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all + diff --git a/Huppy/Huppy.App/HuppyHostedService.cs b/Huppy/Huppy.App/HuppyHostedService.cs index 8577684..3879bf7 100644 --- a/Huppy/Huppy.App/HuppyHostedService.cs +++ b/Huppy/Huppy.App/HuppyHostedService.cs @@ -77,6 +77,13 @@ public Task StopAsync(CancellationToken cancellationToken) public async Task CreateDatabase() { _logger.LogInformation("Verifying database"); + + if (_dbContext.Database.IsInMemory()) + { + _logger.LogInformation("Database in memory, skipping migration"); + return; + } + await _dbContext.Database.MigrateAsync(); } diff --git a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Data/UserData.cs b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Data/UserData.cs index f2fc50d..fc077d3 100644 --- a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Data/UserData.cs +++ b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Data/UserData.cs @@ -1,4 +1,5 @@ using Huppy.Core.Models; +using Huppy.Tests.Utilities; using System; using System.Collections; using System.Collections.Generic; @@ -12,15 +13,58 @@ public class UserData : IEnumerable { public IEnumerator GetEnumerator() { + yield return new object[] + { + new User() + { + Id = DatabaseSupport.GenerateUlongDiscordId(), + JoinDate = DateTime.Now, + Username = Guid.NewGuid().ToString() + } + }; yield return new object[] { new User() { - + Id = DatabaseSupport.GenerateUlongDiscordId(), + JoinDate = DateTime.Now, + Username = Guid.NewGuid().ToString() + } + }; + yield return new object[] + { + new User() + { + Id = DatabaseSupport.GenerateUlongDiscordId(), + JoinDate = DateTime.Now, + Username = Guid.NewGuid().ToString() } }; } + public static IEnumerable Data => + new List + { + new object[] + { + new User() + { + Id = DatabaseSupport.GenerateUlongDiscordId(), + JoinDate = DateTime.Now, + Username = Guid.NewGuid().ToString() + } + }, + new object[] + { + new User() + { + Id = DatabaseSupport.GenerateUlongDiscordId(), + JoinDate = DateTime.Now, + Username = Guid.NewGuid().ToString() + } + } + }; + IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); diff --git a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Huppy.Tests.csproj b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Huppy.Tests.csproj index fbb5307..2c9456d 100644 --- a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Huppy.Tests.csproj +++ b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Huppy.Tests.csproj @@ -9,6 +9,7 @@ + diff --git a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/DefaultRepositoryTests.cs b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/DefaultRepositoryTests.cs index 8d5f7a8..6c00ad0 100644 --- a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/DefaultRepositoryTests.cs +++ b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/DefaultRepositoryTests.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json; using System.Threading.Tasks; namespace Huppy.Tests.Repositories; @@ -24,8 +25,10 @@ public virtual async Task AddAsync(TEntity? entity) { var result = await _repository.AddAsync(entity); await _repository.SaveChangesAsync(); - + Assert.True(result); + + await AssertEntityExistAsync(entity); } public virtual async Task AddRangeAsync(IEnumerable entities) @@ -34,6 +37,11 @@ public virtual async Task AddRangeAsync(IEnumerable entities) await _repository.SaveChangesAsync(); Assert.True(result); + + foreach(var entity in entities) + { + await AssertEntityExistAsync(entity); + } } public virtual async Task GetAllAsync() @@ -47,38 +55,32 @@ public virtual async Task GetAllAsync() Assert.NotNull(resultEntities); } - public virtual async Task GetAsync(TKey id) + public virtual async Task GetAsync(TEntity entity) { - var result = await _repository.GetAsync(id); + await SeedEntityAsync(entity); + var result = await _repository.GetAsync(entity.Id); Assert.NotNull(result); } - public virtual async Task RemoveAsync(TKey id) + public virtual async Task RemoveAsync(TEntity? entity) { - var result = await _repository.RemoveAsync(id); + await SeedEntityAsync(entity); + + var result = await _repository.RemoveAsync(entity.Id); await _repository.SaveChangesAsync(); Assert.True(result); var getAllQuery = await _repository.GetAllAsync(); - var doesExist = await getAllQuery.AnyAsync(entity => entity.Id.Equals(id)); + var doesExist = await getAllQuery.AnyAsync(entity => entity.Id.Equals(entity.Id)); Assert.False(doesExist); } - public virtual async Task RemoveAsync(TEntity? entity) - { - var result = await _repository.RemoveAsync(entity); - await _repository.SaveChangesAsync(); - - - Assert.True(result); - } - public virtual async Task SaveChangesAsync(TEntity testSaveEntity) { - await _repository.AddAsync(testSaveEntity); + await SeedEntityAsync(testSaveEntity); await _repository.SaveChangesAsync(); var entity = await _repository.GetAsync(testSaveEntity.Id); @@ -93,15 +95,38 @@ public virtual async Task SaveChangesAsync(TEntity testSaveEntity) public virtual async Task UpdateAsync(TEntity? entity) { - await _repository.AddAsync(entity); + await SeedEntityAsync(entity!); + + await _repository.UpdateAsync(entity); + + // TODO + Assert.True(true); + } + + public virtual async Task UpdateRangeAsync(IEnumerable entities) + { + foreach(var entity in entities) await SeedEntityAsync(entity); + + await _repository.UpdateRange(entities); + + // TODO + Assert.True(true); + } + + private async Task SeedEntityAsync(TEntity entity) + { + var addResult = await _repository.AddAsync(entity); await _repository.SaveChangesAsync(); - var resultEntity = await _repository.GetAsync(entity.Id); - Assert.NotNull(resultEntity); + Assert.True(addResult); } - public virtual Task UpdateRange(IEnumerable entities) + private async Task AssertEntityExistAsync(TEntity? entity) { - throw new NotImplementedException(); + Assert.NotNull(entity); + + var result = await _repository.GetAsync(entity.Id); + + Assert.NotNull(result); } } diff --git a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/UserRepositoryTests.cs b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/UserRepositoryTests.cs index 60e2ef5..d1d4511 100644 --- a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/UserRepositoryTests.cs +++ b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/UserRepositoryTests.cs @@ -1,29 +1,10 @@ -using Huppy.Core.Interfaces.IRepositories; +using Discord; +using Huppy.Core.Interfaces.IRepositories; using Huppy.Core.Models; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Huppy.Tests.Data; namespace Huppy.Tests.Repositories { - public class UserData : IEnumerable - { - public IEnumerator GetEnumerator() - { - yield return new object[] - { - new User() - }; - } - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - } public class UserRepositoryTests : DefaultRepositoryTests { private readonly IUserRepository _userRepository; @@ -34,11 +15,31 @@ public UserRepositoryTests(IServiceProvider serviceProvider, IUserRepository use [Theory] [ClassData(typeof(UserData))] - public override async Task AddAsync(User? entity) => await base.AddAsync(entity); + public override async Task AddAsync(User? user) => await base.AddAsync(user); - public override Task AddRangeAsync(IEnumerable entities) - { - throw new NotImplementedException(); - } + [Theory] + [ClassData(typeof(UserData))] + public override async Task AddRangeAsync(IEnumerable users) => await base.AddRangeAsync(users); + + [Theory] + [ClassData(typeof(UserData))] + public override async Task GetAsync(User entity) => await base.GetAsync(entity); + + + [Theory] + [ClassData(typeof(UserData))] + public override async Task RemoveAsync(User? entity) => await base.RemoveAsync(entity); + + [Theory] + [ClassData(typeof(UserData))] + public override async Task SaveChangesAsync(User? entity) => await base.SaveChangesAsync(entity!); + + [Theory] + [ClassData(typeof(UserData))] + public override async Task UpdateAsync(User? entity) => await base.UpdateAsync(entity!); + + [Theory] + [MemberData(nameof(UserData.Data))] + public override async Task UpdateRangeAsync(IEnumerable entities) => await base.UpdateRangeAsync(entities); } } diff --git a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Startup.cs b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Startup.cs index 7613f5a..5a0e280 100644 --- a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Startup.cs +++ b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Startup.cs @@ -1,7 +1,9 @@ using Huppy.App.Configuration; using Huppy.Core.Services.Huppy; using Huppy.Core.Utilities; +using Huppy.Infrastructure; using Huppy.Kernel; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -32,11 +34,12 @@ public void ConfigureHost(IHostBuilder hostBuilder) .AddLogger(Log.Logger) .AddDiscord() .AddServices() - .AddDatabase() + // .AddDatabase() .AddHttpClients() .AddMiddlewares(); services.AddHostedService(); + services.AddDbContext((optionsBuilder) => optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString())); }) .ConfigureLogging(ctx => ctx.ClearProviders()) .UseSerilog(Log.Logger); diff --git a/Huppy/Huppy.Tests/Huppy.IntegrationTests/Utilities/DatabaseSupport.cs b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Utilities/DatabaseSupport.cs new file mode 100644 index 0000000..0806c42 --- /dev/null +++ b/Huppy/Huppy.Tests/Huppy.IntegrationTests/Utilities/DatabaseSupport.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Huppy.Tests.Utilities +{ + public static class DatabaseSupport + { + public static ulong GenerateUlongDiscordId() + { + Random rnd = new(); + return Convert.ToUInt64(rnd.NextInt64(100000000000000000, 999999999999999999)); + } + } +}