Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
test(back-end): Refactor and add new tests for the classes in the rep…
Browse files Browse the repository at this point in the history
…ositories
  • Loading branch information
CarlosPavajeau committed Feb 9, 2021
1 parent b275669 commit 0ece616
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 208 deletions.
29 changes: 3 additions & 26 deletions Infrastructure.Test/Repositories/ActivitiesRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,33 +47,10 @@ public async Task Save_Valid_Activity()

Assert.Pass();
}
catch (DbUpdateException)
catch (DbUpdateException e)
{
Assert.Fail();
}
}

[Test]
public async Task Save_Invalid_Activity()
{
try
{
Activity activity = new Activity
{
State = ActivityState.Pending,
ClientId = "123456789",
Periodicity = PeriodicityType.Casual
};

_activitiesRepository.Insert(activity);

await _dbContext.SaveChangesAsync();

Assert.Fail();
}
catch (DbUpdateException)
{
Assert.Pass();
Console.WriteLine(e.InnerException?.Message);
Assert.Fail(e.Message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public async Task Save_Valid_User()

Assert.IsTrue(result.Succeeded);
}
catch (DbUpdateException)
catch (DbUpdateException e)
{
Assert.Fail();
Assert.Fail(e.Message);
}
}

Expand Down Expand Up @@ -113,9 +113,9 @@ public async Task Assign_Role_To_User()

Assert.IsTrue(result.Succeeded);
}
catch (DbUpdateException)
catch (DbUpdateException e)
{
Assert.Fail();
Assert.Fail(e.Message);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Infrastructure.Test/Repositories/BaseRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public void Init()
services.AddSingleton(mockHostingEnvironment.Object);

ServiceProvider = services.BuildServiceProvider();
ServiceProvider.GetService<ApplicationDbContext>().Database.Migrate();
ServiceProvider.GetService<ApplicationDbContext>()?.Database.Migrate();
}

protected void DetachAllEntities()
{
ApplicationDbContext dbContext = ServiceProvider.GetService<ApplicationDbContext>();
dbContext.ChangeTracker.Entries()
dbContext?.ChangeTracker.Entries()
.Where(e => e.State == EntityState.Added || e.State == EntityState.Modified || e.State == EntityState.Deleted)
.ToList()
.ForEach(e =>
Expand Down
64 changes: 32 additions & 32 deletions Infrastructure.Test/Repositories/ClientsRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,31 @@ namespace Infrastructure.Test.Repositories
[Order(0)]
public class ClientsRepositoryTest : BaseRepositoryTest
{
private IClientsRepository clientsRepository;
private ApplicationDbContext dbContext;
private IClientsRepository _clientsRepository;
private ApplicationDbContext _dbContext;

[SetUp]
public void InitRepository()
public void SetUp()
{
DetachAllEntities();

clientsRepository = ServiceProvider.GetService<IClientsRepository>();
dbContext = ServiceProvider.GetService<ApplicationDbContext>();
_clientsRepository = ServiceProvider.GetService<IClientsRepository>();
_dbContext = ServiceProvider.GetService<ApplicationDbContext>();
}

[Test]
public void CheckClientRepository()
{
Assert.IsNotNull(clientsRepository);
Assert.IsNotNull(_clientsRepository);
}

[Test]
public async Task CheckClientTableAreEmpty()
{
List<Client> clients = await clientsRepository.GetAll().ToListAsync();

Assert.IsTrue(clients.Count == 0);
}

[Test]
public void SaveEmptyClient()
public void Save_Invalid_Client()
{
try
{
Client client = new Client();
clientsRepository.Insert(client);
_clientsRepository.Insert(client);
}
catch (Exception)
{
Expand All @@ -55,7 +47,7 @@ public void SaveEmptyClient()
}

[Test]
public async Task SaveClient()
public async Task Save_Valid_Client()
{
try
{
Expand Down Expand Up @@ -84,53 +76,61 @@ public async Task SaveClient()
}
};

clientsRepository.Insert(client);
_clientsRepository.Insert(client);

await dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync();

Assert.Pass();
}
catch (DbUpdateException)
catch (DbUpdateException e)
{
Assert.Fail();
Assert.Fail(e.Message);
}
}

[Test]
public async Task SearchClient()
public async Task Search_Non_Existent_Client()
{
Client client = await _clientsRepository.FindByIdAsync("123456789");

Assert.IsNull(client);
}

[Test]
public async Task Search_Existing_Client()
{
try
{
Client client = await clientsRepository.FindByIdAsync("12345678");
Client client = await _clientsRepository.FindByIdAsync("12345678");
Assert.IsNotNull(client);
Assert.AreEqual("12345678", client.Id);
Assert.AreEqual("Manolo", client.FirstName);
}
catch (DbUpdateException)
catch (DbUpdateException e)
{
Assert.Fail();
Assert.Fail(e.Message);
}
}

[Test]
public async Task UpdateClient()
public async Task Update_Existing_Client()
{
try
{
Client client = await clientsRepository.FindByIdAsync("12345678");
Client client = await _clientsRepository.FindByIdAsync("12345678");
Assert.IsNotNull(client);

client.SecondName = "Jesus";
clientsRepository.Update(client);
_clientsRepository.Update(client);

await dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync();

client = await clientsRepository.FindByIdAsync("12345678");
client = await _clientsRepository.FindByIdAsync("12345678");
Assert.AreEqual("Jesus", client.SecondName);
}
catch (DbUpdateException)
catch (DbUpdateException e)
{
Assert.Fail();
Assert.Fail(e.Message);
}
}
}
Expand Down
84 changes: 52 additions & 32 deletions Infrastructure.Test/Repositories/EmployeesRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,59 @@ namespace Infrastructure.Test.Repositories
[Order(1)]
public class EmployeesRepositoryTest : BaseRepositoryTest
{
private IEmployeesRepository employeesRepository;
private ApplicationDbContext dbContext;
private IEmployeesRepository _employeesRepository;
private ApplicationDbContext _dbContext;

[SetUp]
public void SetUp()
{
DetachAllEntities();

employeesRepository = ServiceProvider.GetService<IEmployeesRepository>();
dbContext = ServiceProvider.GetService<ApplicationDbContext>();
_employeesRepository = ServiceProvider.GetService<IEmployeesRepository>();
_dbContext = ServiceProvider.GetService<ApplicationDbContext>();
}

[Test]
public void CheckEmployeesRepository()
{
Assert.IsNotNull(employeesRepository);
Assert.IsNotNull(_employeesRepository);
}

[Test]
public async Task LoadEmployeeCharges()
public async Task Get_All_Employee_Charges()
{
try
{
List<EmployeeCharge> employeeCharges = await employeesRepository.GetAllEmployeeCharges().ToListAsync();
List<EmployeeCharge> employeeCharges = await _employeesRepository.GetAllEmployeeCharges().ToListAsync();
Assert.IsTrue(employeeCharges.Count > 0);
}
catch (Exception)
catch (Exception e)
{
Assert.Fail(e.Message);
}
}

[Test]
public async Task Save_Invalid_Employee()
{
try
{
Employee employee = new Employee();

_employeesRepository.Insert(employee);

await _dbContext.SaveChangesAsync();

Assert.Fail();
}
catch (Exception)
{
Assert.Pass();
}
}

[Test]
public async Task SaveEmployee()
public async Task Save_Valid_Employee()
{
try
{
Expand All @@ -65,54 +84,55 @@ public async Task SaveEmployee()
}
};

employeesRepository.Insert(employee);
_employeesRepository.Insert(employee);

await dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync();

Assert.Pass();
}
catch (DbUpdateException)
catch (DbUpdateException e)
{
Assert.Fail();
Assert.Fail(e.Message);
}
}

[Test]
public async Task SearchEmployee()
public async Task Search_Non_Existent_Employee()
{
try
{
Employee employee = await employeesRepository.FindByIdAsync("123456789");
Employee employee = await _employeesRepository.FindByIdAsync("123456788");

Assert.IsNotNull(employee);
Assert.AreEqual("123456789", employee.Id);
Assert.AreEqual("Juan", employee.FirstName);
}
catch (Exception)
{
Assert.Fail();
}
Assert.IsNull(employee);
}

[Test]
public async Task UpdateEmployee()
public async Task Search_Existing_Employee()
{
Employee employee = await _employeesRepository.FindByIdAsync("123456789");

Assert.IsNotNull(employee);
Assert.AreEqual("123456789", employee.Id);
Assert.AreEqual("Juan", employee.FirstName);
}

[Test]
public async Task Update_Existing_Employee()
{
try
{
Employee employee = await employeesRepository.FindByIdAsync("123456789");
Employee employee = await _employeesRepository.FindByIdAsync("123456789");
Assert.IsNotNull(employee);

employee.LastName = "Manuel";
employeesRepository.Update(employee);
_employeesRepository.Update(employee);

await dbContext.SaveChangesAsync();
await _dbContext.SaveChangesAsync();

employee = await employeesRepository.FindByIdAsync("123456789");
employee = await _employeesRepository.FindByIdAsync("123456789");
Assert.AreEqual("Manuel", employee.LastName);
}
catch (DbUpdateException)
catch (DbUpdateException e)
{
Assert.Fail();
Assert.Fail(e.Message);
}
}
}
Expand Down
Loading

0 comments on commit 0ece616

Please sign in to comment.