-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
T #5 unfinished implementation of repository tests
- Loading branch information
HueByte
committed
Sep 16, 2022
1 parent
ee26f71
commit 82951f1
Showing
6 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Huppy.Core.Models; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Huppy.Tests.Data | ||
{ | ||
public class UserData : IEnumerable<object[]> | ||
{ | ||
public IEnumerator<object[]> GetEnumerator() | ||
{ | ||
yield return new object[] | ||
{ | ||
new User() | ||
{ | ||
|
||
} | ||
}; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Huppy/Huppy.Tests/Huppy.IntegrationTests/Fixtures/PriorityFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Huppy.Tests.Fixtures | ||
{ | ||
internal class PriorityFixture | ||
{ | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/DefaultRepositoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using Discord; | ||
using Huppy.Kernel; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Huppy.Tests.Repositories; | ||
|
||
public abstract class DefaultRepositoryTests<TKey, TEntity> where TKey : IConvertible where TEntity : DbModel<TKey> | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly IRepository<TKey, TEntity> _repository; | ||
|
||
public DefaultRepositoryTests(IServiceProvider serviceProvider, IRepository<TKey, TEntity> repository) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_repository = repository; | ||
} | ||
|
||
public virtual async Task AddAsync(TEntity? entity) | ||
{ | ||
var result = await _repository.AddAsync(entity); | ||
await _repository.SaveChangesAsync(); | ||
|
||
Assert.True(result); | ||
} | ||
|
||
public virtual async Task AddRangeAsync(IEnumerable<TEntity> entities) | ||
{ | ||
var result = await _repository.AddRangeAsync(entities); | ||
await _repository.SaveChangesAsync(); | ||
|
||
Assert.True(result); | ||
} | ||
|
||
public virtual async Task GetAllAsync() | ||
{ | ||
var allEntities = await _repository.GetAllAsync(); | ||
|
||
Assert.NotNull(allEntities); | ||
|
||
var resultEntities = await allEntities.ToListAsync(); | ||
|
||
Assert.NotNull(resultEntities); | ||
} | ||
|
||
public virtual async Task GetAsync(TKey id) | ||
{ | ||
var result = await _repository.GetAsync(id); | ||
|
||
Assert.NotNull(result); | ||
} | ||
|
||
public virtual async Task RemoveAsync(TKey id) | ||
{ | ||
var result = await _repository.RemoveAsync(id); | ||
await _repository.SaveChangesAsync(); | ||
|
||
Assert.True(result); | ||
|
||
var getAllQuery = await _repository.GetAllAsync(); | ||
var doesExist = await getAllQuery.AnyAsync(entity => entity.Id.Equals(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 _repository.SaveChangesAsync(); | ||
|
||
var entity = await _repository.GetAsync(testSaveEntity.Id); | ||
Assert.NotNull(entity); | ||
|
||
await _repository.RemoveAsync(testSaveEntity); | ||
await _repository.SaveChangesAsync(); | ||
|
||
entity = await _repository.GetAsync(testSaveEntity.Id); | ||
Assert.Null(entity); | ||
} | ||
|
||
public virtual async Task UpdateAsync(TEntity? entity) | ||
{ | ||
await _repository.AddAsync(entity); | ||
await _repository.SaveChangesAsync(); | ||
|
||
var resultEntity = await _repository.GetAsync(entity.Id); | ||
Assert.NotNull(resultEntity); | ||
} | ||
|
||
public virtual Task UpdateRange(IEnumerable<TEntity> entities) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/RepositoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Huppy.Core.Interfaces.IRepositories; | ||
using Huppy.Kernel; | ||
using Huppy.Tests.Utilities; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Huppy.Tests.Repositories; | ||
|
||
public class RepositoryTests | ||
{ | ||
private List<Type> _repositoriesTypes; | ||
|
||
private readonly IServiceProvider _serviceProvider; | ||
public RepositoryTests(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
|
||
_repositoriesTypes = AppDomain.CurrentDomain.GetAssemblies() | ||
.SelectMany(asm => asm.GetTypes()) | ||
.Where(type => type.IsInterface && type.IsAssignableToGenericType(typeof(IRepository<,>)) && type != typeof(IRepository<,>)) | ||
.ToList(); | ||
} | ||
|
||
[Fact] | ||
public async Task Test() | ||
{ | ||
var repository = _serviceProvider.GetRequiredService<IUserRepository>(); | ||
|
||
//foreach (var type in _repositoriesTypes) | ||
//{ | ||
//var repositoryInstance = _serviceProvider.GetService(type); | ||
//if (repositoryInstance is null) continue; | ||
|
||
//var test = Convert.ChangeType(repositoryInstance, typeof(IRepository<IConvertible, DbModel<IConvertible>>)); | ||
|
||
await GetAllTest(repository); | ||
//var test = await repositoryInstance.GetAllAsync(); | ||
|
||
//} | ||
} | ||
|
||
private async Task GetAllTest<Tkey, TEntity>(IRepository<Tkey, TEntity> repository) where Tkey : IConvertible where TEntity : DbModel<Tkey> | ||
{ | ||
var x = await repository.GetAllAsync(); | ||
} | ||
|
||
|
||
} |
44 changes: 44 additions & 0 deletions
44
Huppy/Huppy.Tests/Huppy.IntegrationTests/Repositories/UserRepositoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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; | ||
|
||
namespace Huppy.Tests.Repositories | ||
{ | ||
public class UserData : IEnumerable<object[]> | ||
{ | ||
public IEnumerator<object[]> GetEnumerator() | ||
{ | ||
yield return new object[] | ||
{ | ||
new User() | ||
}; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
public class UserRepositoryTests : DefaultRepositoryTests<ulong, User> | ||
{ | ||
private readonly IUserRepository _userRepository; | ||
public UserRepositoryTests(IServiceProvider serviceProvider, IUserRepository userRepository) : base(serviceProvider, userRepository) | ||
{ | ||
_userRepository = userRepository; | ||
} | ||
|
||
[Theory] | ||
[ClassData(typeof(UserData))] | ||
public override async Task<bool> AddAsync(User? entity) => await base.AddAsync(entity); | ||
|
||
public override Task<bool> AddRangeAsync(IEnumerable<User> entities) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Huppy/Huppy.Tests/Huppy.IntegrationTests/Utilities/ReflectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Huppy.Tests.Utilities | ||
{ | ||
public static class ReflectionExtensions | ||
{ | ||
public static bool IsAssignableToGenericType(this Type givenType, Type genericType) | ||
{ | ||
if (givenType == null || genericType == null) | ||
{ | ||
return false; | ||
} | ||
|
||
return givenType == genericType | ||
|| givenType.MapsToGenericTypeDefinition(genericType) | ||
|| givenType.HasInterfaceThatMapsToGenericTypeDefinition(genericType) | ||
|| givenType.BaseType.IsAssignableToGenericType(genericType); | ||
} | ||
|
||
private static bool HasInterfaceThatMapsToGenericTypeDefinition(this Type givenType, Type genericType) | ||
{ | ||
return givenType | ||
.GetInterfaces() | ||
.Where(it => it.IsGenericType) | ||
.Any(it => it.GetGenericTypeDefinition() == genericType); | ||
} | ||
|
||
private static bool MapsToGenericTypeDefinition(this Type givenType, Type genericType) | ||
{ | ||
return genericType.IsGenericTypeDefinition | ||
&& givenType.IsGenericType | ||
&& givenType.GetGenericTypeDefinition() == genericType; | ||
} | ||
} | ||
} |