Skip to content

Commit

Permalink
Add GenericControllerFunctionalTests and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
izzat5233 committed Apr 30, 2024
1 parent 66bb02a commit 3909b50
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
62 changes: 62 additions & 0 deletions RestaurantReservation.Test/GenericControllerFunctionalTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Microsoft.Extensions.DependencyInjection;
using RestaurantReservation.Presentation.Interfaces;
using RestaurantReservation.Test.Data;
using RestaurantReservation.Test.Fixtures;
using FluentAssertions;

namespace RestaurantReservation.Test;

public class ProgramFunctionalTests : IClassFixture<FullTestSetupFixture>
{
private readonly IGenericController _genericController;

public ProgramFunctionalTests(FullTestSetupFixture fullTestSetupFixture)
{
_genericController = fullTestSetupFixture.ServiceProvider.GetRequiredService<IGenericController>();
}

[Fact]
public async Task ListManagers_ShouldReturnCorrectData()
{
var managers = await _genericController.ListManagers().ToListAsync();
managers.Count.Should().Be(1);
managers[0].Should().BeEquivalentTo(ModelsData.Employees().ToList()[0]);
}

[Fact]
public async Task GetReservationsByCustomer_ShouldReturnCorrectData()
{
var reservations = await _genericController.GetReservationsByCustomer(1).ToListAsync();
reservations.Count.Should().Be(1);
reservations[0].Should().BeEquivalentTo(ModelsData.Reservations().ToList()[0]);
}

[Fact]
public async Task ListOrdersAndMenuItems_ShouldReturnCorrectData()
{
var pairs = await _genericController.ListOrdersAndMenuItems(1).ToListAsync();
pairs.Count.Should().Be(1);

var (order, menuItems) = pairs[0];
order.Should().BeEquivalentTo(ModelsData.Orders().ToList()[0]);

var menuItemList = await menuItems.ToListAsync();
menuItemList.Count.Should().Be(1);
menuItemList[0].Should().BeEquivalentTo(ModelsData.MenuItems().ToList()[0]);
}

[Fact]
public async Task ListOrderedMenuItems_ShouldReturnCorrectData()
{
var menuItems = await _genericController.ListOrderedMenuItems(1).ToListAsync();
menuItems.Count.Should().Be(1);
menuItems[0].Should().BeEquivalentTo(ModelsData.MenuItems().ToList()[0]);
}

[Fact]
public async Task CalculateAverageOrderAmount_ShouldReturnCorrectData()
{
var average = await _genericController.CalculateAverageOrderAmount(1);
average.Should().BeApproximately(45.9, 1e-6);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.4" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.5.3"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3"/>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public async IAsyncEnumerable<Reservation> GetReservationsByCustomer(int custome

await foreach (var order in orders.AsAsyncEnumerable())
{
var menuItems = context.OrderItems
var menuItems = await context.OrderItems
.Where(oi => oi.OrderId == order.OrderId)
.Select(od => od.MenuItem)
.AsAsyncEnumerable();
.Distinct()
.ToListAsync();

yield return (order, menuItems);
yield return (order, menuItems.ToAsyncEnumerable());
}
}

Expand Down

0 comments on commit 3909b50

Please sign in to comment.