Skip to content

Commit

Permalink
Add explicit Async suffix to CrudDatabaseService methods
Browse files Browse the repository at this point in the history
  • Loading branch information
izzat5233 committed Apr 1, 2024
1 parent e8b92e5 commit cb37321
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ public void GetAll_ShouldCallReadAll(
}

[Theory, AutoMoqData]
public async Task Add_ShouldCallAppendAllAsync_WithEntity(
public async Task AddAsync_ShouldCallAppendAllAsync_WithEntity(
Entity entity,
[Frozen] Mock<IFileService<Entity>> fileServiceMock,
DatabasePersistenceService<Entity> persistenceService)
{
await persistenceService.Add(entity);
await persistenceService.AddAsync(entity);
fileServiceMock.Verify(s => s.AppendAllAsync(It.Is<IEnumerable<Entity>>(e => e.Contains(entity))), Times.Once);
}

[Theory, AutoMoqData]
public async Task Update_ShouldReplaceExistingEntityAndCallWriteAllAsync(
public async Task UpdateAsync_ShouldReplaceExistingEntityAndCallWriteAllAsync(
List<Entity> entities,
Entity newEntity,
[Frozen] Mock<IFileService<Entity>> fileServiceMock,
Expand All @@ -65,15 +65,15 @@ public async Task Update_ShouldReplaceExistingEntityAndCallWriteAllAsync(
.Setup(s => s.ReadAll())
.Returns(entities.ToList());

await persistenceService.Update(newEntity);
await persistenceService.UpdateAsync(newEntity);

fileServiceMock.Verify(s => s.WriteAllAsync(It.Is<IEnumerable<Entity>>(e =>
e.Contains(newEntity) && !e.Any(e => e.Id == entityToUpdate.Id && !e.Equals(newEntity)))),
Times.Once);
}

[Theory, AutoMoqData]
public async Task Delete_ShouldCallWriteAllAsync_WithoutDeletedEntity(
public async Task DeleteAsync_ShouldCallWriteAllAsync_WithoutDeletedEntity(
List<Entity> entities,
[Frozen] Mock<IFileService<Entity>> fileServiceMock,
DatabasePersistenceService<Entity> persistenceService)
Expand All @@ -82,7 +82,7 @@ public async Task Delete_ShouldCallWriteAllAsync_WithoutDeletedEntity(
fileServiceMock.Setup(s
=> s.ReadAll()).Returns(entities);

await persistenceService.Delete(entityToDelete);
await persistenceService.DeleteAsync(entityToDelete);

fileServiceMock
.Verify(s => s.WriteAllAsync(It.Is<IEnumerable<Entity>>(e => !e.Contains(entityToDelete))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service.Database;
public class DatabaseUniquenessLayerTests
{
[Theory, AutoMoqData]
public async Task Add_EntityExists_ShouldThrow(
public async Task AddAsync_EntityExists_ShouldThrow(
Entity entity,
[Frozen] Mock<IQueryDatabaseService<Entity>> queryServiceMock,
DatabaseUniquenessLayer<Entity> uniquenessLayer)
Expand All @@ -22,13 +22,13 @@ public async Task Add_EntityExists_ShouldThrow(
.Setup(s => s.Exists(entity))
.Returns(true);

var action = () => uniquenessLayer.Add(entity);
var action = () => uniquenessLayer.AddAsync(entity);

await action.Should().ThrowAsync<DatabaseOperationException>();
}

[Theory, AutoMoqData]
public async Task Add_EntityDoesNotExist_ShouldCallCrud(
public async Task AddAsync_EntityDoesNotExist_ShouldCallCrud(
Entity entity,
[Frozen] Mock<IQueryDatabaseService<Entity>> queryServiceMock,
[Frozen] Mock<ICrudDatabaseService<Entity>> crudServiceMock,
Expand All @@ -38,14 +38,14 @@ public async Task Add_EntityDoesNotExist_ShouldCallCrud(
.Setup(s => s.Exists(entity))
.Returns(false);

var action = () => uniquenessLayer.Add(entity);
var action = () => uniquenessLayer.AddAsync(entity);

await action.Should().NotThrowAsync();
crudServiceMock.Verify(s => s.Add(entity), Times.Once);
crudServiceMock.Verify(s => s.AddAsync(entity), Times.Once);
}

[Theory, AutoMoqData]
public async Task UpdateOrDelete_EntityDoesNotExist_ShouldThrow(
public async Task UpdateAsyncOrDeleteAsync_EntityDoesNotExist_ShouldThrow(
Entity entity,
[Frozen] Mock<IQueryDatabaseService<Entity>> queryServiceMock,
DatabaseUniquenessLayer<Entity> uniquenessLayer)
Expand All @@ -54,15 +54,15 @@ public async Task UpdateOrDelete_EntityDoesNotExist_ShouldThrow(
.Setup(s => s.Exists(entity))
.Returns(false);

var updateAction = () => uniquenessLayer.Update(entity);
var deleteAction = () => uniquenessLayer.Delete(entity);
var updateAction = () => uniquenessLayer.UpdateAsync(entity);
var deleteAction = () => uniquenessLayer.DeleteAsync(entity);

await updateAction.Should().ThrowAsync<DatabaseOperationException>();
await deleteAction.Should().ThrowAsync<DatabaseOperationException>();
}

[Theory, AutoMoqData]
public async Task UpdateOrDelete_EntityExists_ShouldCallCrud(
public async Task UpdateAsyncOrDeleteAsync_EntityExists_ShouldCallCrud(
Entity entity,
[Frozen] Mock<IQueryDatabaseService<Entity>> queryServiceMock,
[Frozen] Mock<ICrudDatabaseService<Entity>> crudServiceMock,
Expand All @@ -72,12 +72,12 @@ public async Task UpdateOrDelete_EntityExists_ShouldCallCrud(
.Setup(s => s.Exists(entity))
.Returns(true);

var updateAction = () => uniquenessLayer.Update(entity);
var deleteAction = () => uniquenessLayer.Delete(entity);
var updateAction = () => uniquenessLayer.UpdateAsync(entity);
var deleteAction = () => uniquenessLayer.DeleteAsync(entity);

await updateAction.Should().NotThrowAsync();
crudServiceMock.Verify(s => s.Update(entity), Times.Once);
crudServiceMock.Verify(s => s.UpdateAsync(entity), Times.Once);
await deleteAction.Should().NotThrowAsync();
crudServiceMock.Verify(s => s.Delete(entity), Times.Once);
crudServiceMock.Verify(s => s.DeleteAsync(entity), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,41 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service.Database;
public class DatabaseValidationLayerTests
{
[Theory, AutoMoqData]
public async Task Add_ShouldValidateBeforeCall(
public async Task AddAsync_ShouldValidateBeforeCall(
Entity entity,
[Frozen] Mock<IValidationService> validationServiceMock,
[Frozen] Mock<ICrudDatabaseService<Entity>> crudServiceMock,
DatabaseValidationLayer<Entity> validationLayer)
{
await validationLayer.Add(entity);
await validationLayer.AddAsync(entity);

validationServiceMock.Verify(s => s.ValidateEntityOrThrow(entity), Times.Once);
crudServiceMock.Verify(s => s.Add(entity), Times.Once);
crudServiceMock.Verify(s => s.AddAsync(entity), Times.Once);
}

[Theory, AutoMoqData]
public async Task Update_ShouldValidateBeforeCall(
public async Task UpdateAsync_ShouldValidateBeforeCall(
Entity entity,
[Frozen] Mock<IValidationService> validationServiceMock,
[Frozen] Mock<ICrudDatabaseService<Entity>> crudServiceMock,
DatabaseValidationLayer<Entity> validationLayer)
{
await validationLayer.Update(entity);
await validationLayer.UpdateAsync(entity);

validationServiceMock.Verify(s => s.ValidateEntityOrThrow(entity), Times.Once);
crudServiceMock.Verify(s => s.Update(entity), Times.Once);
crudServiceMock.Verify(s => s.UpdateAsync(entity), Times.Once);
}

[Theory, AutoMoqData]
public async Task Delete_ShouldJustCall(
public async Task DeleteAsync_ShouldJustCall(
Entity entity,
[Frozen] Mock<IValidationService> validationServiceMock,
[Frozen] Mock<ICrudDatabaseService<Entity>> crudServiceMock,
DatabaseValidationLayer<Entity> validationLayer)
{
await validationLayer.Delete(entity);
await validationLayer.DeleteAsync(entity);

validationServiceMock.Verify(s => s.ValidateEntityOrThrow(entity), Times.Never);
crudServiceMock.Verify(s => s.Delete(entity), Times.Once);
crudServiceMock.Verify(s => s.DeleteAsync(entity), Times.Once);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service.Database.Relati
public class AirportRelationalLayerTests
{
[Theory, AutoMoqData]
public async Task AddOrUpdate_ShouldNeverThrow(
public async Task AddAsyncOrUpdateAsync_ShouldNeverThrow(
Airport airport,
AirportRelationalLayer relationalLayer)
{
var addAction = () => relationalLayer.Add(airport);
var updateAction = () => relationalLayer.Update(airport);
var addAction = () => relationalLayer.AddAsync(airport);
var updateAction = () => relationalLayer.UpdateAsync(airport);

await addAction.Should().NotThrowAsync();
await updateAction.Should().NotThrowAsync();
}

[Theory, AutoMoqData]
public async Task Delete_ShouldThrowIfRelatedFlightExist(
public async Task DeleteAsync_ShouldThrowIfRelatedFlightExist(
[Frozen] string fixedAirportId,
Flight flight,
Airport airport,
Expand All @@ -36,7 +36,7 @@ public async Task Delete_ShouldThrowIfRelatedFlightExist(
.Setup(s => s.GetAll())
.Returns([flight]);

var action = () => relationalLayer.Delete(airport);
var action = () => relationalLayer.DeleteAsync(airport);

await action.Should().ThrowAsync<DatabaseRelationalException>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service.Database.Relati
public class BookingRelationalLayerTests
{
[Theory, AutoMoqData]
public async Task AddOrUpdate_ShouldThrowIfFlightDoesNotExist(
public async Task AddAsyncOrUpdateAsync_ShouldThrowIfFlightDoesNotExist(
Booking booking,
[Frozen] Mock<IQueryDatabaseService<Flight>> flightQueryServiceMock,
BookingRelationalLayer relationalLayer)
Expand All @@ -22,15 +22,15 @@ public async Task AddOrUpdate_ShouldThrowIfFlightDoesNotExist(
.Setup(s => s.GetAll())
.Returns([]);

var addAction = () => relationalLayer.Add(booking);
var updateAction = () => relationalLayer.Update(booking);
var addAction = () => relationalLayer.AddAsync(booking);
var updateAction = () => relationalLayer.UpdateAsync(booking);

await addAction.Should().ThrowAsync<DatabaseRelationalException>();
await updateAction.Should().ThrowAsync<DatabaseRelationalException>();
}

[Theory, AutoMoqData]
public async Task AddOrUpdate_ShouldThrowIfPassengerDoesNotExist(
public async Task AddAsyncOrUpdateAsync_ShouldThrowIfPassengerDoesNotExist(
Booking booking,
[Frozen] Mock<IQueryDatabaseService<Passenger>> passengerQueryServiceMock,
BookingRelationalLayer relationalLayer)
Expand All @@ -39,8 +39,8 @@ public async Task AddOrUpdate_ShouldThrowIfPassengerDoesNotExist(
.Setup(s => s.GetAll())
.Returns([]);

var addAction = () => relationalLayer.Add(booking);
var updateAction = () => relationalLayer.Update(booking);
var addAction = () => relationalLayer.AddAsync(booking);
var updateAction = () => relationalLayer.UpdateAsync(booking);

await addAction.Should().ThrowAsync<DatabaseRelationalException>();
await updateAction.Should().ThrowAsync<DatabaseRelationalException>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service.Database.Relati
public class FlightRelationalLayerTests
{
[Theory, AutoMoqData]
public async Task AddOrUpdate_ShouldThrowIfAirportDoesNotExist(
public async Task AddAsyncOrUpdateAsync_ShouldThrowIfAirportDoesNotExist(
Flight flight,
[Frozen] Mock<IQueryDatabaseService<Airport>> airportQueryServiceMock,
FlightRelationalLayer relationalLayer)
Expand All @@ -22,15 +22,15 @@ public async Task AddOrUpdate_ShouldThrowIfAirportDoesNotExist(
.Setup(s => s.GetAll())
.Returns([]);

var addAction = () => relationalLayer.Add(flight);
var updateAction = () => relationalLayer.Update(flight);
var addAction = () => relationalLayer.AddAsync(flight);
var updateAction = () => relationalLayer.UpdateAsync(flight);

await addAction.Should().ThrowAsync<DatabaseRelationalException>();
await updateAction.Should().ThrowAsync<DatabaseRelationalException>();
}

[Theory, AutoMoqData]
public async Task Delete_ShouldThrowIfRelatedBookingExist(
public async Task DeleteAsync_ShouldThrowIfRelatedBookingExist(
[Frozen] int fixedBookingId,
Booking booking,
Flight flight,
Expand All @@ -41,7 +41,7 @@ public async Task Delete_ShouldThrowIfRelatedBookingExist(
.Setup(s => s.GetAll())
.Returns([booking]);

var action = () => relationalLayer.Delete(flight);
var action = () => relationalLayer.DeleteAsync(flight);

await action.Should().ThrowAsync<DatabaseRelationalException>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ namespace AirportTicketBookingSystem.Test.Infrastructure.Service.Database.Relati
public class PassengerRelationalLayerTests
{
[Theory, AutoMoqData]
public async Task AddOrUpdate_ShouldNeverThrow(
public async Task AddAsyncOrUpdateAsync_ShouldNeverThrow(
Passenger passenger,
PassengerRelationalLayer relationalLayer)
{
var addAction = () => relationalLayer.Add(passenger);
var updateAction = () => relationalLayer.Update(passenger);
var addAction = () => relationalLayer.AddAsync(passenger);
var updateAction = () => relationalLayer.UpdateAsync(passenger);

await addAction.Should().NotThrowAsync();
await updateAction.Should().NotThrowAsync();
}

[Theory, AutoMoqData]
public async Task Delete_ShouldThrowIfRelatedBookingExist(
public async Task DeleteAsync_ShouldThrowIfRelatedBookingExist(
[Frozen] int fixedBookingId,
Booking booking,
Passenger passenger,
Expand All @@ -36,7 +36,7 @@ public async Task Delete_ShouldThrowIfRelatedBookingExist(
.Setup(s => s.GetAll())
.Returns([booking]);

var action = () => relationalLayer.Delete(passenger);
var action = () => relationalLayer.DeleteAsync(passenger);

await action.Should().ThrowAsync<DatabaseRelationalException>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface ICrudDatabaseService<in TEntity>
/// <exception cref="DatabaseOperationException">Thrown when an entity already exists in the database.
/// Or when entity is invalid (against its own attributes).</exception>
/// <exception cref="DatabaseRelationalException">Thrown when a violation of relational constraints occurs.</exception>
Task Add(TEntity entity);
Task AddAsync(TEntity entity);

/// <summary>
/// Updates an existing entity with a new entity in the database.
Expand All @@ -29,7 +29,7 @@ public interface ICrudDatabaseService<in TEntity>
/// Or when entity is invalid (against its own attributes).</exception>
/// <exception cref="DatabaseRelationalException">Thrown when a violation of relational constraints occurs.</exception>
/// <returns>A task representing the update operation.</returns>
Task Update(TEntity newEntity);
Task UpdateAsync(TEntity newEntity);

/// <summary>
/// Deletes an entity of type <typeparamref name="TEntity"/> from the database.
Expand All @@ -38,5 +38,5 @@ public interface ICrudDatabaseService<in TEntity>
/// <exception cref="DatabaseOperationException">Thrown when the entity does not exist.</exception>
/// <exception cref="DatabaseRelationalException">Thrown when a violation of relational constraints occurs.</exception>
/// <returns>A task representing the delete operation.</returns>
Task Delete(TEntity entity);
Task DeleteAsync(TEntity entity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public AirportRepository(
_crudDatabaseService = crudDatabaseService;
}

public void Add(Airport airport) => _crudDatabaseService.Add(airport);
public void Add(Airport airport) => _crudDatabaseService.AddAsync(airport);

public IEnumerable<Airport> GetAll() => _queryDatabaseService.GetAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public BookingRepository(
_crudDatabaseService = crudDatabaseService;
}

public void Add(Booking booking) => _crudDatabaseService.Add(booking);
public void Add(Booking booking) => _crudDatabaseService.AddAsync(booking);

public void Update(Booking booking) => _crudDatabaseService.Update(booking);
public void Update(Booking booking) => _crudDatabaseService.UpdateAsync(booking);

public void Delete(Booking booking) => _crudDatabaseService.Delete(booking);
public void Delete(Booking booking) => _crudDatabaseService.DeleteAsync(booking);

public IEnumerable<Booking> GetAll() => _queryDatabaseService.GetAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public FlightRepository(
_crudDatabaseService = crudDatabaseService;
}

public void Add(Flight flight) => _crudDatabaseService.Add(flight);
public void Add(Flight flight) => _crudDatabaseService.AddAsync(flight);

public IEnumerable<Flight> GetAll() => _queryDatabaseService.GetAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public PassengerRepository(
_crudDatabaseService = crudDatabaseService;
}

public void Add(Passenger passenger) => _crudDatabaseService.Add(passenger);
public void Add(Passenger passenger) => _crudDatabaseService.AddAsync(passenger);

public Passenger? GetById(int id) => _queryDatabaseService
.GetAll()
Expand Down
Loading

0 comments on commit cb37321

Please sign in to comment.