Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests #4

Merged
merged 3 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ else

protected override async Task OnInitializedAsync()
{
deals = await HttpDealService.GetDealsAsync(false);
deals = await HttpDealService.GetDealsAsync();
}
}
73 changes: 73 additions & 0 deletions StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using StpFoodBlazor.Models;
using System;

namespace StpFoodBlazor.Helpers {
public class DealFilter {
public DealEvent[]? Deals { get; set; }
public string? Day { get; set; }
public string? Name { get; set; }
public bool? Alcohol { get; set; }
public bool? HappyHour { get; set; }

public DealEvent[] filter() {
if (Deals == null) {
return new DealEvent[0];
}

DealEvent[] filteredDeals = Deals;

if (!string.IsNullOrWhiteSpace(Day)) {
filteredDeals = filterByDay(filteredDeals, Day);
}

if (!string.IsNullOrWhiteSpace(Name)) {
filteredDeals = filterByName(filteredDeals, Name);
}

if (Alcohol != null) {
filteredDeals = filterByAlcohol(filteredDeals, (bool)Alcohol);
}

// if(HappyHour != null) {
// filteredDeals = filterByHappyHour(filteredDeals, (bool)HappyHour);
// }
// if (Name != null) {
// filteredDeals = filterByName(filteredDeals);
// }
// if (Alcohol != null) {
// filteredDeals = filterByAlcohol(filteredDeals);
// }
// if (HappyHour != null) {
// filteredDeals = filterByHappyHour(filteredDeals);
// }
return filteredDeals;
}

private static DealEvent[] filterByDay(DealEvent[] deals, String day) {
return deals.Where(deal => deal.Day == day).ToArray();
}

private static DealEvent[] filterByName(DealEvent[] deals, String name) {
return deals.Where(deal => deal.Name == name).ToArray();
}

private static DealEvent[] filterByAlcohol(DealEvent[] deals, Boolean alcohol) {
if (alcohol) {
return deals.Where(deal => !string.IsNullOrWhiteSpace(deal.Alcohol)).ToArray();
}

return deals.Where(deal => string.IsNullOrWhiteSpace(deal.Alcohol)).ToArray();
}

// Need to convert column header with space to model attribute without one
// private static DealEvent[] filterByHappyHour(DealEvent[] deals, Boolean happyHour) {
// if (happyHour) {
// return deals.Where(deal =>
// deal.HappyHour != null && deal.HappyHour.Trim() != "").ToArray();
// }

// return deals.Where(deal =>
// deal.HappyHour == null || deal.HappyHour.Trim() == "").ToArray();
// }
}
}
23 changes: 12 additions & 11 deletions StpFoodBlazor/StpFoodBlazor/Models/Deal.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
namespace StpFoodBlazor.Models
{
public class DealEvent
{
public string? Deal { get; set; }
public string? Day { get; set; }
public string? Name { get; set; }
public string? Alcohol { get; set; }

}
}
namespace StpFoodBlazor.Models
{
public class DealEvent
{
public string? Deal { get; set; }
public string? Day { get; set; }
public string? Name { get; set; }
public string? Alcohol { get; set; }
public string? HappyHour { get; set; }

}
}
6 changes: 3 additions & 3 deletions StpFoodBlazor/StpFoodBlazor/Services/HttpDealService.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using StpFoodBlazor.Models;
using System.Net.Http.Json;

namespace StpFoodBlazor.Services
{
public class HttpDealService(HttpClient httpClient) : IDealService
{
public Task<DealEvent[]> GetDealsAsync(bool includeAlcohol)
public async Task<DealEvent[]> GetDealsAsync()
{
return httpClient.GetFromJsonAsync<DealEvent[]>(GetUrl());
var result = await httpClient.GetFromJsonAsync<DealEvent[]>(GetUrl());
return result ?? Array.Empty<DealEvent>();
}

private static String GetUrl()
Expand Down
14 changes: 7 additions & 7 deletions StpFoodBlazor/StpFoodBlazor/Services/IDealService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace StpFoodBlazor.Services
{
public interface IDealService
{
public Task<Models.DealEvent[]> GetDealsAsync(bool includeAlcohol);
}
}
namespace StpFoodBlazor.Services
{
public interface IDealService
{
public Task<Models.DealEvent[]> GetDealsAsync();
}
}
123 changes: 123 additions & 0 deletions StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using StpFoodBlazor.Helpers;
using StpFoodBlazor.Models;
using StpFoodBlazor.Services;
using System.Threading.Tasks;
using System;

namespace StpFoodBlazorTest.Helpers {

public class DealFilterTest {
private readonly DealEvent[] deals;
private readonly DealFilter filter;
public DealFilterTest() {
deals = getDeals().Result;
filter = new DealFilter();
}

[Fact]
public void ShouldHandleEmptyDeals() {
DealEvent[] filteredDeals = filter.filter();

Assert.Empty(filteredDeals);
}

[Fact]
public void ShouldReturnInputWhenNoFilterConditions() {
int dealsLength = deals.Length;
filter.Deals = deals;

DealEvent[] filteredDeals = filter.filter();

Assert.Equal(324, dealsLength);
Assert.Equal(dealsLength, filteredDeals.Length);
}

[Fact]
public void ShouldReturnFilteredByDay() {
String day = "Monday";
filter.Deals = deals;
filter.Day = day;

DealEvent[] filteredDeals = filter.filter();

Assert.Equal(45, filteredDeals.Length);
Array.ForEach(filteredDeals, deal => Assert.Equal(day, deal.Day));
}

[Fact]
public void ShouldReturnFilteredByName() {
String name = "Pino's Pizza";
filter.Deals = deals;
filter.Name = name;

DealEvent[] filteredDeals = filter.filter();

Assert.Equal(5, filteredDeals.Length);
Array.ForEach(filteredDeals, deal => Assert.Equal(name, deal.Name));
}

[Fact]
public void ShouldReturnFilteredByNameAndDay() {
String name = "Pino's Pizza";
String day = "Tuesday";
filter.Deals = deals;
filter.Name = name;
filter.Day = day;

DealEvent[] filteredDeals = filter.filter();

Assert.Single(filteredDeals);
Array.ForEach(filteredDeals, deal => {
Assert.Equal(name, deal.Name);
Assert.Equal(day, deal.Day);
});
}

[Fact]
public void ShouldReturnFilteredByAlcoholTrue() {
filter.Deals = deals;
filter.Alcohol = true;

DealEvent[] filteredDeals = filter.filter();

Assert.Equal(109, filteredDeals.Length);
Array.ForEach(filteredDeals, deal => {
Assert.False(string.IsNullOrEmpty(deal.Alcohol));
});
}

[Fact]
public void ShouldReturnFilteredByAlcoholFalse() {
filter.Deals = deals;
filter.Alcohol = false;

DealEvent[] filteredDeals = filter.filter();

Assert.Equal(215, filteredDeals.Length);
Array.ForEach(filteredDeals, deal => {
Assert.True(string.IsNullOrEmpty(deal.Alcohol));
});
}

// Need to convert column header with space to model attribute without one
// [Fact]
// public async Task ShouldReturnFilteredByHappyHourTrue() {
// DealEvent[] deals = await getDeals();
// DealFilter filter = new DealFilter();
// filter.Deals = deals;
// filter.HappyHour = true;

// DealEvent[] filteredDeals = filter.filter();

// Assert.Equal(5, filteredDeals.Length);
// Array.ForEach(filteredDeals, deal => Assert.NotNull(deal.HappyHour));
// }

private static async Task<DealEvent[]> getDeals() {
TestDealService dealService = new TestDealService();
return await dealService.GetDealsAsync();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace StpFoodBlazor.Services
public class TestDealService : IDealService
{
private static readonly string DEAL_FIXTURES_PATH = Path.Combine(Directory.GetCurrentDirectory(), "fixtures", "deals.json");
public async Task<DealEvent[]> GetDealsAsync(bool includeAlcohol)
public async Task<DealEvent[]> GetDealsAsync()
{
if (File.Exists(DEAL_FIXTURES_PATH))
{
Expand Down