From 82ab0ea6e98358b62420b5659ae12a0b7f8703b2 Mon Sep 17 00:00:00 2001 From: eebbesen Date: Mon, 28 Oct 2024 20:04:35 -0500 Subject: [PATCH 1/3] refactor services --- .../StpFoodBlazor/Services/HttpDealService.cs | 5 +++-- .../StpFoodBlazor/Services/IDealService.cs | 14 +++++++------- .../StpFoodBlazorTest/Services/TestDealService.cs | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/StpFoodBlazor/StpFoodBlazor/Services/HttpDealService.cs b/StpFoodBlazor/StpFoodBlazor/Services/HttpDealService.cs index 7366080..d51b86e 100644 --- a/StpFoodBlazor/StpFoodBlazor/Services/HttpDealService.cs +++ b/StpFoodBlazor/StpFoodBlazor/Services/HttpDealService.cs @@ -5,9 +5,10 @@ namespace StpFoodBlazor.Services { public class HttpDealService(HttpClient httpClient) : IDealService { - public Task GetDealsAsync(bool includeAlcohol) + public async Task GetDealsAsync() { - return httpClient.GetFromJsonAsync(GetUrl()); + var result = await httpClient.GetFromJsonAsync(GetUrl()); + return result ?? Array.Empty(); } private static String GetUrl() diff --git a/StpFoodBlazor/StpFoodBlazor/Services/IDealService.cs b/StpFoodBlazor/StpFoodBlazor/Services/IDealService.cs index ac339db..65b1d86 100644 --- a/StpFoodBlazor/StpFoodBlazor/Services/IDealService.cs +++ b/StpFoodBlazor/StpFoodBlazor/Services/IDealService.cs @@ -1,7 +1,7 @@ -namespace StpFoodBlazor.Services -{ - public interface IDealService - { - public Task GetDealsAsync(bool includeAlcohol); - } -} +namespace StpFoodBlazor.Services +{ + public interface IDealService + { + public Task GetDealsAsync(); + } +} diff --git a/StpFoodBlazor/StpFoodBlazorTest/Services/TestDealService.cs b/StpFoodBlazor/StpFoodBlazorTest/Services/TestDealService.cs index b070cc0..0ad61d8 100644 --- a/StpFoodBlazor/StpFoodBlazorTest/Services/TestDealService.cs +++ b/StpFoodBlazor/StpFoodBlazorTest/Services/TestDealService.cs @@ -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 GetDealsAsync(bool includeAlcohol) + public async Task GetDealsAsync() { if (File.Exists(DEAL_FIXTURES_PATH)) { From 30aafc23480524ff83a6a7b019807bc9c9e0c32d Mon Sep 17 00:00:00 2001 From: eebbesen Date: Mon, 28 Oct 2024 21:45:41 -0500 Subject: [PATCH 2/3] more tests --- .../Components/Pages/Deals.razor | 2 +- .../StpFoodBlazor/Helpers/DealFilter.cs | 72 ++++++++++ StpFoodBlazor/StpFoodBlazor/Models/Deal.cs | 23 +-- .../StpFoodBlazor/Services/HttpDealService.cs | 1 - .../Helpers/DealFilterTest.cs | 132 ++++++++++++++++++ 5 files changed, 217 insertions(+), 13 deletions(-) create mode 100644 StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs create mode 100644 StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs diff --git a/StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor b/StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor index e0e93cd..409fade 100644 --- a/StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor +++ b/StpFoodBlazor/StpFoodBlazor/Components/Pages/Deals.razor @@ -44,6 +44,6 @@ else protected override async Task OnInitializedAsync() { - deals = await HttpDealService.GetDealsAsync(false); + deals = await HttpDealService.GetDealsAsync(); } } diff --git a/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs b/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs new file mode 100644 index 0000000..8470008 --- /dev/null +++ b/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs @@ -0,0 +1,72 @@ +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 (Day != null) { + filteredDeals = filterByDay(filteredDeals, Day); + } + + if (Name != null) { + 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(); + // } + } +} diff --git a/StpFoodBlazor/StpFoodBlazor/Models/Deal.cs b/StpFoodBlazor/StpFoodBlazor/Models/Deal.cs index 19c8f2b..dab7bf9 100644 --- a/StpFoodBlazor/StpFoodBlazor/Models/Deal.cs +++ b/StpFoodBlazor/StpFoodBlazor/Models/Deal.cs @@ -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; } + + } +} diff --git a/StpFoodBlazor/StpFoodBlazor/Services/HttpDealService.cs b/StpFoodBlazor/StpFoodBlazor/Services/HttpDealService.cs index d51b86e..c2aef3b 100644 --- a/StpFoodBlazor/StpFoodBlazor/Services/HttpDealService.cs +++ b/StpFoodBlazor/StpFoodBlazor/Services/HttpDealService.cs @@ -1,5 +1,4 @@ using StpFoodBlazor.Models; -using System.Net.Http.Json; namespace StpFoodBlazor.Services { diff --git a/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs b/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs new file mode 100644 index 0000000..0e0167e --- /dev/null +++ b/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs @@ -0,0 +1,132 @@ +using StpFoodBlazor.Helpers; +using StpFoodBlazor.Models; +using StpFoodBlazor.Services; +using System.Threading.Tasks; +using System; +using System.Runtime.ConstrainedExecution; +using Bunit.Extensions; + +namespace StpFoodBlazorTest.Helpers { + + public class DealFilterTest { + [Fact] + public void ShouldHandleEmptyDeals() { + DealFilter filter = new DealFilter(); + + DealEvent[] filteredDeals = filter.filter(); + + Assert.Empty(filteredDeals); + } + + [Fact] + public async Task ShouldReturnInputWhenNoFilterConditions() { + DealEvent[] deals = await getDeals(); + int dealsLength = deals.Length; + DealFilter filter = new DealFilter(); + filter.Deals = deals; + + DealEvent[] filteredDeals = filter.filter(); + + Assert.Equal(324, dealsLength); + Assert.Equal(dealsLength, filteredDeals.Length); + } + + [Fact] + public async Task ShouldReturnFilteredByDay() { + String day = "Monday"; + DealEvent[] deals = await getDeals(); + DealFilter filter = new DealFilter(); + 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 async Task ShouldReturnFilteredByName() { + String name = "Pino's Pizza"; + DealEvent[] deals = await getDeals(); + DealFilter filter = new DealFilter(); + 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 async Task ShouldReturnFilteredByNameAndDay() { + String name = "Pino's Pizza"; + String day = "Tuesday"; + DealEvent[] deals = await getDeals(); + DealFilter filter = new DealFilter(); + 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 async Task ShouldReturnFilteredByAlcoholTrue() { + DealEvent[] deals = await getDeals(); + DealFilter filter = new DealFilter(); + 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 async Task ShouldReturnFilteredByAlcoholFalse() { + DealEvent[] deals = await getDeals(); + DealFilter filter = new DealFilter(); + 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 getDeals() { + TestDealService dealService = new TestDealService(); + return await dealService.GetDealsAsync(); + } + + } + +} From e998e4c11e40ac6f0027957f960f6f5848d0a506 Mon Sep 17 00:00:00 2001 From: eebbesen Date: Mon, 28 Oct 2024 21:59:06 -0500 Subject: [PATCH 3/3] refactor --- .../StpFoodBlazor/Helpers/DealFilter.cs | 5 +-- .../Helpers/DealFilterTest.cs | 35 +++++++------------ 2 files changed, 16 insertions(+), 24 deletions(-) diff --git a/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs b/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs index 8470008..18979a3 100644 --- a/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs +++ b/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs @@ -15,11 +15,12 @@ public DealEvent[] filter() { } DealEvent[] filteredDeals = Deals; - if (Day != null) { + + if (!string.IsNullOrWhiteSpace(Day)) { filteredDeals = filterByDay(filteredDeals, Day); } - if (Name != null) { + if (!string.IsNullOrWhiteSpace(Name)) { filteredDeals = filterByName(filteredDeals, Name); } diff --git a/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs b/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs index 0e0167e..3f827a5 100644 --- a/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs +++ b/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs @@ -3,26 +3,27 @@ using StpFoodBlazor.Services; using System.Threading.Tasks; using System; -using System.Runtime.ConstrainedExecution; -using Bunit.Extensions; 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() { - DealFilter filter = new DealFilter(); - DealEvent[] filteredDeals = filter.filter(); Assert.Empty(filteredDeals); } [Fact] - public async Task ShouldReturnInputWhenNoFilterConditions() { - DealEvent[] deals = await getDeals(); + public void ShouldReturnInputWhenNoFilterConditions() { int dealsLength = deals.Length; - DealFilter filter = new DealFilter(); filter.Deals = deals; DealEvent[] filteredDeals = filter.filter(); @@ -32,10 +33,8 @@ public async Task ShouldReturnInputWhenNoFilterConditions() { } [Fact] - public async Task ShouldReturnFilteredByDay() { + public void ShouldReturnFilteredByDay() { String day = "Monday"; - DealEvent[] deals = await getDeals(); - DealFilter filter = new DealFilter(); filter.Deals = deals; filter.Day = day; @@ -46,10 +45,8 @@ public async Task ShouldReturnFilteredByDay() { } [Fact] - public async Task ShouldReturnFilteredByName() { + public void ShouldReturnFilteredByName() { String name = "Pino's Pizza"; - DealEvent[] deals = await getDeals(); - DealFilter filter = new DealFilter(); filter.Deals = deals; filter.Name = name; @@ -60,11 +57,9 @@ public async Task ShouldReturnFilteredByName() { } [Fact] - public async Task ShouldReturnFilteredByNameAndDay() { + public void ShouldReturnFilteredByNameAndDay() { String name = "Pino's Pizza"; String day = "Tuesday"; - DealEvent[] deals = await getDeals(); - DealFilter filter = new DealFilter(); filter.Deals = deals; filter.Name = name; filter.Day = day; @@ -79,9 +74,7 @@ public async Task ShouldReturnFilteredByNameAndDay() { } [Fact] - public async Task ShouldReturnFilteredByAlcoholTrue() { - DealEvent[] deals = await getDeals(); - DealFilter filter = new DealFilter(); + public void ShouldReturnFilteredByAlcoholTrue() { filter.Deals = deals; filter.Alcohol = true; @@ -94,9 +87,7 @@ public async Task ShouldReturnFilteredByAlcoholTrue() { } [Fact] - public async Task ShouldReturnFilteredByAlcoholFalse() { - DealEvent[] deals = await getDeals(); - DealFilter filter = new DealFilter(); + public void ShouldReturnFilteredByAlcoholFalse() { filter.Deals = deals; filter.Alcohol = false;