Skip to content

Commit

Permalink
T #5 unfinished implementation of repository tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Sep 16, 2022
1 parent ee26f71 commit 82951f1
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Huppy/Huppy.Tests/Huppy.IntegrationTests/Data/UserData.cs
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();
}
}
}
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
{
}
}
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();
}
}
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();
}


}
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();
}
}
}
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;
}
}
}

0 comments on commit 82951f1

Please sign in to comment.