Format ./web/backend/Features/Features.Breweries.Tests

This commit is contained in:
Aaron Po
2026-06-20 15:09:42 -04:00
parent 0f77ae43b5
commit aadeab3afa
8 changed files with 48 additions and 47 deletions

View File

@@ -19,24 +19,13 @@ public class CreateBreweryHandlerTests
private static CreateBreweryLocation ValidLocation() private static CreateBreweryLocation ValidLocation()
{ {
return new CreateBreweryLocation( return new CreateBreweryLocation(Guid.NewGuid(), "123 Main St", null, "12345", null);
Guid.NewGuid(),
"123 Main St",
null,
"12345",
null
);
} }
[Fact] [Fact]
public async Task Handle_PersistsEntity_WithNewIdsAndCreatedAt() public async Task Handle_PersistsEntity_WithNewIdsAndCreatedAt()
{ {
CreateBreweryCommand command = new( CreateBreweryCommand command = new(Guid.NewGuid(), "MyBrew", "Desc", ValidLocation());
Guid.NewGuid(),
"MyBrew",
"Desc",
ValidLocation()
);
BreweryPost? persisted = null; BreweryPost? persisted = null;
_repoMock _repoMock
@@ -68,4 +57,4 @@ public class CreateBreweryHandlerTests
result.BreweryName.Should().Be("MyBrew"); result.BreweryName.Should().Be("MyBrew");
result.Location.Should().NotBeNull(); result.Location.Should().NotBeNull();
} }
} }

View File

@@ -18,4 +18,4 @@ public class DeleteBreweryHandlerTests
repoMock.Verify(r => r.DeleteAsync(id), Times.Once); repoMock.Verify(r => r.DeleteAsync(id), Times.Once);
} }
} }

View File

@@ -96,11 +96,13 @@ public class UpdateBreweryHandlerTests
await _handler.Handle(command, CancellationToken.None); await _handler.Handle(command, CancellationToken.None);
persisted!.Location.Should().NotBeNull(); persisted!.Location.Should().NotBeNull();
persisted.Location!.BreweryPostLocationId.Should().Be(locationCommand.BreweryPostLocationId); persisted
.Location!.BreweryPostLocationId.Should()
.Be(locationCommand.BreweryPostLocationId);
persisted.Location.BreweryPostId.Should().Be(command.BreweryPostId); persisted.Location.BreweryPostId.Should().Be(command.BreweryPostId);
persisted.Location.CityId.Should().Be(locationCommand.CityId); persisted.Location.CityId.Should().Be(locationCommand.CityId);
persisted.Location.AddressLine1.Should().Be(locationCommand.AddressLine1); persisted.Location.AddressLine1.Should().Be(locationCommand.AddressLine1);
persisted.Location.AddressLine2.Should().Be(locationCommand.AddressLine2); persisted.Location.AddressLine2.Should().Be(locationCommand.AddressLine2);
persisted.Location.PostalCode.Should().Be(locationCommand.PostalCode); persisted.Location.PostalCode.Should().Be(locationCommand.PostalCode);
} }
} }

View File

@@ -8,19 +8,19 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1"/> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="xunit" Version="2.9.2"/> <PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2"/> <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
<PackageReference Include="Moq" Version="4.20.72"/> <PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="FluentAssertions" Version="6.9.0"/> <PackageReference Include="FluentAssertions" Version="6.9.0" />
<PackageReference Include="DbMocker" Version="1.26.0"/> <PackageReference Include="DbMocker" Version="1.26.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Using Include="Xunit"/> <Using Include="Xunit" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Features.Breweries\Features.Breweries.csproj"/> <ProjectReference Include="..\Features.Breweries\Features.Breweries.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -20,10 +20,12 @@ public class GetAllBreweriesHandlerTests
[Fact] [Fact]
public async Task Handle_PassesLimitAndOffset_ToRepository() public async Task Handle_PassesLimitAndOffset_ToRepository()
{ {
_repoMock.Setup(r => r.GetAllAsync(10, 5)) _repoMock.Setup(r => r.GetAllAsync(10, 5)).ReturnsAsync(Array.Empty<BreweryPost>());
.ReturnsAsync(Array.Empty<BreweryPost>());
IEnumerable<BreweryDto> result = await _handler.Handle(new GetAllBreweriesQuery(10, 5), CancellationToken.None); IEnumerable<BreweryDto> result = await _handler.Handle(
new GetAllBreweriesQuery(10, 5),
CancellationToken.None
);
result.Should().BeEmpty(); result.Should().BeEmpty();
_repoMock.Verify(r => r.GetAllAsync(10, 5), Times.Once); _repoMock.Verify(r => r.GetAllAsync(10, 5), Times.Once);
@@ -35,13 +37,18 @@ public class GetAllBreweriesHandlerTests
BreweryPost[] breweries = new[] BreweryPost[] breweries = new[]
{ {
new BreweryPost { BreweryPostId = Guid.NewGuid(), BreweryName = "A" }, new BreweryPost { BreweryPostId = Guid.NewGuid(), BreweryName = "A" },
new BreweryPost { BreweryPostId = Guid.NewGuid(), BreweryName = "B" } new BreweryPost { BreweryPostId = Guid.NewGuid(), BreweryName = "B" },
}; };
_repoMock.Setup(r => r.GetAllAsync(null, null)) _repoMock.Setup(r => r.GetAllAsync(null, null)).ReturnsAsync(breweries);
.ReturnsAsync(breweries);
IEnumerable<BreweryDto> result = await _handler.Handle(new GetAllBreweriesQuery(null, null), CancellationToken.None); IEnumerable<BreweryDto> result = await _handler.Handle(
new GetAllBreweriesQuery(null, null),
CancellationToken.None
);
result.Select(b => b.BreweryPostId).Should().BeEquivalentTo(breweries.Select(b => b.BreweryPostId)); result
.Select(b => b.BreweryPostId)
.Should()
.BeEquivalentTo(breweries.Select(b => b.BreweryPostId));
} }
} }

View File

@@ -21,10 +21,12 @@ public class GetBreweryByIdHandlerTests
public async Task Handle_ReturnsBrewery_WhenFound() public async Task Handle_ReturnsBrewery_WhenFound()
{ {
BreweryPost brewery = new() { BreweryPostId = Guid.NewGuid(), BreweryName = "Test" }; BreweryPost brewery = new() { BreweryPostId = Guid.NewGuid(), BreweryName = "Test" };
_repoMock.Setup(r => r.GetByIdAsync(brewery.BreweryPostId)) _repoMock.Setup(r => r.GetByIdAsync(brewery.BreweryPostId)).ReturnsAsync(brewery);
.ReturnsAsync(brewery);
BreweryDto? result = await _handler.Handle(new GetBreweryByIdQuery(brewery.BreweryPostId), CancellationToken.None); BreweryDto? result = await _handler.Handle(
new GetBreweryByIdQuery(brewery.BreweryPostId),
CancellationToken.None
);
result.Should().NotBeNull(); result.Should().NotBeNull();
result!.BreweryPostId.Should().Be(brewery.BreweryPostId); result!.BreweryPostId.Should().Be(brewery.BreweryPostId);
@@ -34,11 +36,13 @@ public class GetBreweryByIdHandlerTests
public async Task Handle_ReturnsNull_WhenNotFound() public async Task Handle_ReturnsNull_WhenNotFound()
{ {
Guid id = Guid.NewGuid(); Guid id = Guid.NewGuid();
_repoMock.Setup(r => r.GetByIdAsync(id)) _repoMock.Setup(r => r.GetByIdAsync(id)).ReturnsAsync((BreweryPost?)null);
.ReturnsAsync((BreweryPost?)null);
BreweryDto? result = await _handler.Handle(new GetBreweryByIdQuery(id), CancellationToken.None); BreweryDto? result = await _handler.Handle(
new GetBreweryByIdQuery(id),
CancellationToken.None
);
result.Should().BeNull(); result.Should().BeNull();
} }
} }

View File

@@ -81,8 +81,7 @@ public class BreweryRepositoryTests
public async Task CreateAsync_ExecutesSuccessfully() public async Task CreateAsync_ExecutesSuccessfully()
{ {
MockDbConnection conn = new(); MockDbConnection conn = new();
conn.Mocks.When(cmd => cmd.CommandText == "USP_CreateBrewery") conn.Mocks.When(cmd => cmd.CommandText == "USP_CreateBrewery").ReturnsScalar(1);
.ReturnsScalar(1);
BreweryRepository repo = CreateRepo(conn); BreweryRepository repo = CreateRepo(conn);
BreweryPost brewery = new() BreweryPost brewery = new()
{ {
@@ -97,12 +96,12 @@ public class BreweryRepositoryTests
CityId = Guid.NewGuid(), CityId = Guid.NewGuid(),
AddressLine1 = "123 Main St", AddressLine1 = "123 Main St",
PostalCode = "12345", PostalCode = "12345",
Coordinates = [0x00, 0x01] Coordinates = [0x00, 0x01],
} },
}; };
// Should not throw // Should not throw
Func<Task> act = async () => await repo.CreateAsync(brewery); Func<Task> act = async () => await repo.CreateAsync(brewery);
await act.Should().NotThrowAsync(); await act.Should().NotThrowAsync();
} }
} }

View File

@@ -11,4 +11,4 @@ internal class TestConnectionFactory(DbConnection conn) : ISqlConnectionFactory
{ {
return _conn; return _conn;
} }
} }