Skip to content

Commit

Permalink
Upgrade packages, add UT for service and create charts.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi-Aghaei committed Jul 23, 2024
1 parent 0848d77 commit e127f97
Show file tree
Hide file tree
Showing 8 changed files with 4,543 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Pandemic.Tracker.Web\Pandemic.Tracker.Web.csproj" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
Expand Down
84 changes: 84 additions & 0 deletions Pandemic.Tracker.Web.Unit.Tests/Services/CountryServiceTests.cs
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));
}
}
}
29 changes: 29 additions & 0 deletions Pandemic.Tracker.Web/Components/Pages/CountryChart.razor
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();
}
}
24 changes: 3 additions & 21 deletions Pandemic.Tracker.Web/Components/Pages/Home.razor
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.
1 change: 1 addition & 0 deletions Pandemic.Tracker.Web/Components/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
@using Pandemic.Tracker.Web.Components
@using Pandemic.Tracker.Web.Models
@using Pandemic.Tracker.Web.Services
@using ApexCharts
Loading

0 comments on commit e127f97

Please sign in to comment.