Skip to content

Commit

Permalink
Add tests for DatabasePersistenceService
Browse files Browse the repository at this point in the history
  • Loading branch information
izzat5233 committed Mar 28, 2024
1 parent 71f48c3 commit ace9bfd
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
17 changes: 17 additions & 0 deletions AirportTicketBookingSystem.Test/Domain/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using AirportTicketBookingSystem.Domain.Interfaces;

namespace AirportTicketBookingSystem.Test.Domain;

/// <summary>
/// Empty entity for testing purposes
/// </summary>
public class Entity : IEntity
{
public int Id { get; set; }

public Entity(int id) => Id = id;

public override bool Equals(object? obj) => obj is Entity other && other.Id == Id;

public override int GetHashCode() => Id.GetHashCode();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using AirportTicketBookingSystem.Infrastructure.Interfaces;
using AirportTicketBookingSystem.Infrastructure.Service.Database;
using AirportTicketBookingSystem.Test.Common;
using AirportTicketBookingSystem.Test.Domain;
using AutoFixture.Xunit2;
using FluentAssertions;
using Moq;
using Xunit;

namespace AirportTicketBookingSystem.Test.Infrastructure.Service.Database;

public class DatabasePersistenceServiceTests
{
[Theory, AutoMoqData]
public void Exists_ShouldReturnTrue_IfAndOnlyIfEntityExists(
List<Entity> existingEntities,
Entity newEntity,
[Frozen] Mock<IFileService<Entity>> fileServiceMock,
DatabasePersistenceService<Entity> persistenceService)
{
var existingEntity = existingEntities.First();
fileServiceMock.Setup(s => s.ReadAll()).Returns(existingEntities);

persistenceService.Exists(existingEntity)
.Should().BeTrue();

persistenceService.Exists(newEntity)
.Should().BeFalse();
}

[Theory, AutoMoqData]
public void GetAll_ShouldCallReadAll(
List<Entity> entities,
[Frozen] Mock<IFileService<Entity>> fileServiceMock,
DatabasePersistenceService<Entity> persistenceService)
{
fileServiceMock.Setup(s => s.ReadAll()).Returns(entities);

var result = persistenceService.GetAll();

result.Should().BeSameAs(entities);
fileServiceMock.Verify(s => s.ReadAll(), Times.Once);
}

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

[Theory, AutoMoqData]
public async Task Update_ShouldReplaceExistingEntityAndCallWriteAllAsync(
List<Entity> entities,
Entity newEntity,
[Frozen] Mock<IFileService<Entity>> fileServiceMock,
DatabasePersistenceService<Entity> persistenceService)
{
var entityToUpdate = entities.First();
newEntity.Id = entityToUpdate.Id;
fileServiceMock
.Setup(s => s.ReadAll())
.Returns(entities.ToList());

await persistenceService.Update(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(
List<Entity> entities,
[Frozen] Mock<IFileService<Entity>> fileServiceMock,
DatabasePersistenceService<Entity> persistenceService)
{
var entityToDelete = entities.First();
fileServiceMock.Setup(s
=> s.ReadAll()).Returns(entities);

await persistenceService.Delete(entityToDelete);

fileServiceMock
.Verify(s => s.WriteAllAsync(It.Is<IEnumerable<Entity>>(e => !e.Contains(entityToDelete))),
Times.Once);
}
}

0 comments on commit ace9bfd

Please sign in to comment.