Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/unittest/userrepo #110

Merged
merged 9 commits into from
May 25, 2020
176 changes: 176 additions & 0 deletions 3_Repositories.Tests/UserRepositoryTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using Models;
using NUnit.Framework;
using Repositories.Tests.Base;
using Repositories.Tests.DataSources;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Repositories.Tests
{
[TestFixture]
public class UserRepositoryTest : RepositoryTest<User, UserRepository>
{
protected new IUserRepository Repository => (IUserRepository)base.Repository;

/// <summary>
/// User is retrieved correctly
/// </summary>
[Test]
public async Task GetUserAsync_GoodFlow([UserDataSource]User entity)
{
DbContext.Add(entity);
await DbContext.SaveChangesAsync();

//Test
User retrieved = await Repository.GetUserAsync(entity.Id);

Assert.AreEqual(entity, retrieved);
}

/// <summary>
/// User not added and thus not found in repo
/// </summary>
[Test]
public async Task GetUserAsync_NotFound()
{
int randomId = 74;

//Test
User retrieved = await Repository.GetUserAsync(randomId);

Assert.IsNull(retrieved);
}

/// <summary>
/// User correctly removed from repo
/// </summary>
[Test]
public async Task RemoveUserAsync_GoodFlow([UserDataSource]User entity)
{
DbContext.Add(entity);
await DbContext.SaveChangesAsync();

//Test
bool removed = await Repository.RemoveUserAsync(entity.Id);
await DbContext.SaveChangesAsync();

User retrieved = await Repository.GetUserAsync(entity.Id);

Assert.IsTrue(removed);
Assert.IsNull(retrieved);
}

/// <summary>
/// User that was not in repo not found
/// </summary>
[Test]
public async Task RemoveUserAsync_NotFound()
{
int randomId = 74;

//Test
bool removed = await Repository.RemoveUserAsync(randomId);
await DbContext.SaveChangesAsync();

Assert.IsFalse(removed);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task AddAsyncTest_GoodFlow([UserDataSource]User entity)
{
return base.AddAsyncTest_GoodFlow(entity);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override void AddRangeTest_BadFlow_EmptyList()
{
base.AddRangeTest_BadFlow_EmptyList();
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override void AddRangeTest_BadFlow_Null()
{
base.AddRangeTest_BadFlow_Null();
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task AddRangeTest_GoodFlow([UserDataSource(50)]List<User> entities)
{
return base.AddRangeTest_GoodFlow(entities);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override void AddTest_BadFlow_Null()
{
base.AddTest_BadFlow_Null();
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task FindAsyncTest_BadFlow_NotExists([UserDataSource]User entity)
{
return base.FindAsyncTest_BadFlow_NotExists(entity);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task FindAsyncTest_GoodFlow([UserDataSource]User entity)
{
return base.FindAsyncTest_GoodFlow(entity);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task GetAllAsyncTest_Badflow_Empty()
{
return base.GetAllAsyncTest_Badflow_Empty();
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task GetAllAsyncTest_GoodFlow([UserDataSource(50)]List<User> entities)
{
return base.GetAllAsyncTest_GoodFlow(entities);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task RemoveAsyncTest_BadFlow_NotExists([UserDataSource]User entity)
{
return base.RemoveAsyncTest_BadFlow_NotExists(entity);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task RemoveAsyncTest_GoodFlow([UserDataSource]User entity)
{
return base.RemoveAsyncTest_GoodFlow(entity);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task UpdateTest_BadFlow_NotExists([UserDataSource]User entity, [UserDataSource]User updateEntity)
{
return base.UpdateTest_BadFlow_NotExists(entity, updateEntity);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task UpdateTest_BadFlow_Null([UserDataSource]User entity)
{
return base.UpdateTest_BadFlow_Null(entity);
}

///<inheritdoc cref="RepositoryTest{TDomain, TRepository}"/>
[Test]
public override Task UpdateTest_GoodFlow([UserDataSource]User entity, [UserDataSource]User updateEntity)
{
return base.UpdateTest_GoodFlow(entity, updateEntity);
}
}
}
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added highlighted filter to search endpoint - [#57](https://github.com/DigitalExcellence/dex-backend/issues/57)
- Setup basic unit test framework to ensure that the core functionality of the application works [#65](https://github.com/DigitalExcellence/dex-backend/issues/65)
- Added roles and authorization validation. - [#107](https://github.com/DigitalExcellence/dex-backend/issues/107)
- Added unittests for UserRepository - [#121] (https://github.com/DigitalExcellence/dex-backend/issues/121)

### Changed

Expand All @@ -26,9 +27,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Deprecated

### Removed
- Removed user from search result resource - [#129](https://github.com/DigitalExcellence/dex-backend/issues/129)

- Removed user from search result resource - [#129](https://github.com/DigitalExcellence/dex-backend/issues/129)

### Fixed

### Security
Expand Down