-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade packages, add UT for service and create charts.
- Loading branch information
1 parent
0848d77
commit e127f97
Showing
8 changed files
with
4,543 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
Pandemic.Tracker.Web.Unit.Tests/Services/CountryServiceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// --------------------------------------------------------------- | ||
// Copyright (c) Coalition of the Good-Hearted Engineers | ||
// FREE TO USE TO CONNECT THE WORLD | ||
// --------------------------------------------------------------- | ||
using System.Net; | ||
using System.Net.Http.Json; | ||
using FluentAssertions; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using NSubstitute; | ||
using Pandemic.Tracker.Web.Models; | ||
using Pandemic.Tracker.Web.Services; | ||
|
||
namespace Pandemic.Tracker.Web.Unit.Tests.Services; | ||
public sealed class CountryServiceTests | ||
{ | ||
[Fact] | ||
public void GetCountriesAsync_ShouldReturnCSountries_WhenApiCallIsSuccessful() | ||
{ | ||
// Arrange | ||
var httpClient = new HttpClient(new MockHttpMessageHandler()); | ||
var configuration = Substitute.For<IConfiguration>(); | ||
var logger = Substitute.For<ILogger<CountryService>>(); | ||
|
||
configuration["APIUrl"].Returns("https://example.com/"); | ||
|
||
var service = new CountryService(httpClient, configuration, logger); | ||
|
||
// Act | ||
var countries = service.GetCountries(); | ||
|
||
// Assert | ||
countries.Should().NotBeNullOrEmpty(); | ||
countries.Should().ContainSingle(c => c.Name == "Country1"); | ||
} | ||
|
||
[Fact] | ||
public void GetCountriesAsync_ShouldReturnEmptyArray_WhenApiCallFails() | ||
{ | ||
// Arrange | ||
var httpClient = new HttpClient(new MockHttpMessageHandler(HttpStatusCode.InternalServerError)); | ||
var configuration = Substitute.For<IConfiguration>(); | ||
var logger = Substitute.For<ILogger<CountryService>>(); | ||
|
||
configuration["APIUrl"].Returns("https://example.com/"); | ||
|
||
var service = new CountryService(httpClient, configuration, logger); | ||
|
||
// Act | ||
var countries = service.GetCountries(); | ||
|
||
// Assert | ||
countries.Should().BeEmpty(); | ||
logger.Received().LogError(Arg.Any<HttpRequestException>(), "An error occurred while fetching countries"); | ||
} | ||
|
||
private class MockHttpMessageHandler : HttpMessageHandler | ||
{ | ||
private readonly HttpStatusCode _statusCode; | ||
|
||
public MockHttpMessageHandler(HttpStatusCode statusCode = HttpStatusCode.OK) | ||
{ | ||
_statusCode = statusCode; | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) | ||
{ | ||
if (_statusCode == HttpStatusCode.OK) | ||
{ | ||
var countries = new[] | ||
{ | ||
new Country { Name = "Country1" } | ||
}; | ||
|
||
return Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK) | ||
{ | ||
Content = JsonContent.Create(countries) | ||
}); | ||
} | ||
|
||
return Task.FromResult(new HttpResponseMessage(_statusCode)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
@page "/country-chart" | ||
@rendermode InteractiveServer | ||
@inject CountryService CountryService | ||
|
||
<ApexChart TItem="Country" Title="Country Data"> | ||
|
||
<ApexPointSeries TItem="Country" | ||
Items="Countries" | ||
Name="Cases" | ||
SeriesType="SeriesType.Bar" | ||
XValue="e => e.Name" | ||
YValue="e=> (decimal)e.Cases" /> | ||
|
||
<ApexPointSeries TItem="Country" | ||
Items="Countries" | ||
Name="Deaths" | ||
SeriesType="SeriesType.Bar" | ||
XValue="e => e.Name" | ||
YValue="e=> (decimal)e.Deaths" /> | ||
</ApexChart> | ||
|
||
@code { | ||
public List<Country>Countries { get; set; } = []; | ||
|
||
protected override void OnInitialized() | ||
{ | ||
Countries = CountryService.GetCountriesStatic().Take(10).ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,7 @@ | ||
@inject CountryService CountryService; | ||
@attribute [StreamRendering] | ||
@page "/" | ||
@page "/" | ||
|
||
<PageTitle>Home</PageTitle> | ||
|
||
@if(Countries.Length == 0) | ||
{ | ||
<p><em>Loading...</em></p> | ||
} | ||
@foreach (var country in Countries) | ||
{ | ||
<p>@country.Name</p> | ||
<p>@country.Cases</p> | ||
} | ||
<h1>Hello everyone!</h1> | ||
|
||
|
||
@code { | ||
public Country[] Countries { get; set; } = []; | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
Countries = await CountryService.GetCountriesAsync(); | ||
} | ||
} | ||
Welcome to Pandemic Tracker. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.