Skip to content

Commit

Permalink
T #5 Basic implementation of tests for repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
HueByte committed Sep 16, 2022
1 parent bc2be76 commit 3ad987e
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 50 deletions.
1 change: 1 addition & 0 deletions Huppy/Huppy.App/Huppy.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.9" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Discord.Net" Version="3.7.2" />
Expand Down
7 changes: 7 additions & 0 deletions Huppy/Huppy.App/HuppyHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
46 changes: 45 additions & 1 deletion Huppy/Huppy.Tests/Huppy.IntegrationTests/Data/UserData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Huppy.Core.Models;
using Huppy.Tests.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
Expand All @@ -12,15 +13,58 @@ public class UserData : IEnumerable<object[]>
{
public IEnumerator<object[]> 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<object[]> Data =>
new List<object[]>
{
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.9" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TEntity> entities)
Expand All @@ -34,6 +37,11 @@ public virtual async Task AddRangeAsync(IEnumerable<TEntity> entities)
await _repository.SaveChangesAsync();

Assert.True(result);

foreach(var entity in entities)
{
await AssertEntityExistAsync(entity);
}
}

public virtual async Task GetAllAsync()
Expand All @@ -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);
Expand All @@ -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<TEntity> 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<TEntity> entities)
private async Task AssertEntityExistAsync(TEntity? entity)
{
throw new NotImplementedException();
Assert.NotNull(entity);

var result = await _repository.GetAsync(entity.Id);

Assert.NotNull(result);
}
}
Original file line number Diff line number Diff line change
@@ -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<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;
Expand All @@ -34,11 +15,31 @@ public UserRepositoryTests(IServiceProvider serviceProvider, IUserRepository use

[Theory]
[ClassData(typeof(UserData))]
public override async Task<bool> AddAsync(User? entity) => await base.AddAsync(entity);
public override async Task AddAsync(User? user) => await base.AddAsync(user);

public override Task<bool> AddRangeAsync(IEnumerable<User> entities)
{
throw new NotImplementedException();
}
[Theory]
[ClassData(typeof(UserData))]
public override async Task AddRangeAsync(IEnumerable<User> 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<User> entities) => await base.UpdateRangeAsync(entities);
}
}
5 changes: 4 additions & 1 deletion Huppy/Huppy.Tests/Huppy.IntegrationTests/Startup.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,11 +34,12 @@ public void ConfigureHost(IHostBuilder hostBuilder)
.AddLogger(Log.Logger)
.AddDiscord()
.AddServices()
.AddDatabase()
// .AddDatabase()
.AddHttpClients()
.AddMiddlewares();

services.AddHostedService<HuppyHostedService>();
services.AddDbContext<HuppyDbContext>((optionsBuilder) => optionsBuilder.UseInMemoryDatabase(Guid.NewGuid().ToString()));
})
.ConfigureLogging(ctx => ctx.ClearProviders())
.UseSerilog(Log.Logger);
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

0 comments on commit 3ad987e

Please sign in to comment.