Skip to content

Commit

Permalink
Adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
Clifftech123 committed May 22, 2024
1 parent 1176983 commit 6d05549
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 18 deletions.
4 changes: 2 additions & 2 deletions sample/CountryData.Sample.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ static void GetPhoneCodeByCountryShortCode(string shortCode)
/// <param name="phoneCode">The phone code.</param>
static void GetCountryByPhoneCode(string phoneCode)
{
var country = _helper.GetCountryByPhoneCode(phoneCode);
var country = _helper.GetCountriesByPhoneCode(phoneCode);
Console.WriteLine($"Country for phone code {phoneCode}:");
Console.WriteLine(country.CountryName);
Console.WriteLine(country.FirstOrDefault());
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0-android;net6.0-ios</TargetFrameworks>
<TargetFrameworks>net8.0-android;net8.0-ios</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net6.0-tizen</TargetFrameworks> -->
<OutputType>Exe</OutputType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public IActionResult GetPhoneCodeByCountryShortCode(string shortCode)
[HttpGet("phoneCode/{phoneCode}")]
public IActionResult GetCountryByPhoneCode(string phoneCode)
{
var country = _helper.GetCountryByPhoneCode(phoneCode);
var country = _helper.GetCountriesByPhoneCode(phoneCode);
if (country == null)
{
return NotFound();
Expand Down
24 changes: 13 additions & 11 deletions src/CountryData.Standard/CountryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CountryHelper
private const string strFileName = "CountryData.Standard.data.json";

public CountryHelper()
{
{
var json = GetJsonData(strFileName);
_Countries = JsonConvert.DeserializeObject<List<Country>>(json);
foreach (var country in _Countries)
Expand All @@ -22,11 +22,11 @@ public CountryHelper()
}


/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
/// <summary>
///
/// </summary>
/// <param name="path"></param>
/// <returns></returns>

private string GetJsonData(string path)
{
Expand All @@ -35,7 +35,7 @@ private string GetJsonData(string path)
using (Stream stream = assembly.GetManifestResourceStream(path))
{
var reader = new StreamReader(stream);
json= reader.ReadToEnd();
json = reader.ReadToEnd();

}
return json;
Expand Down Expand Up @@ -72,15 +72,15 @@ public string GetCountryEmojiFlag(string shortCode)
return string.Concat(shortCode.ToUpper().Select(x => char.ConvertFromUtf32(x + 0x1F1A5)));
}

/// <summary>
/// <summary>
/// Selects Regions in a Particular Country
/// </summary>
/// <param name="ShortCode"></param>
/// <returns>List<Regions> a list of regions</returns>
public List<Regions> GetRegionByCountryCode(string ShortCode)
{
return _Countries.Where(x => x.CountryShortCode == ShortCode)
.Select(r=>r.Regions).FirstOrDefault()
.Select(r => r.Regions).FirstOrDefault()
.ToList();
}

Expand Down Expand Up @@ -111,12 +111,14 @@ public string GetPhoneCodeByCountryShortCode(string shortCode)
/// </summary>
/// <param name="phoneCode"></param>
/// <returns>Country</returns>
public Country GetCountryByPhoneCode(string phoneCode)
public IEnumerable<Country> GetCountriesByPhoneCode(string phoneCode)
{
return _Countries.SingleOrDefault(c => c.PhoneCode == phoneCode);
var countries = _Countries.Where(c => c.PhoneCode == phoneCode);
return countries;
}




}
}
73 changes: 70 additions & 3 deletions test/CountryData.UnitTests/CountryHelperTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using CountryData.Standard;
using FluentAssertions;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace CountryData.UnitTests;

public class CountryHelperTests
{
//Arrange
private readonly CountryHelper _countryHelper = new();
private readonly CountryHelper _countryHelper = new();

[Fact]
public void GetCountryData_ShouldReturnListOfCountryData()
Expand All @@ -21,10 +22,16 @@ public void GetCountryData_ShouldReturnListOfCountryData()
countryData.Should().NotBeNullOrEmpty();
}


/// <summary>
/// tesing
/// </summary>
/// <param name="shortCode"></param>

[Theory]
[InlineData("GH")]
[InlineData("US")]
public void GetCountryByCode_WithCorrectCode_ShouldReturnCountry(string shortCode)
public void GetCountryByCode_WithCorrectCode_ShouldReturnCountry(string shortCode)
{
//Act
var country = _countryHelper.GetCountryByCode(shortCode);
Expand All @@ -34,11 +41,18 @@ public void GetCountryByCode_WithCorrectCode_ShouldReturnCountry(string shortCod
country.CountryShortCode.Should().Be(shortCode);
}


/// <summary>
/// Tests the GetCountryFlagByCode method in the CountryHelper class.
/// This test verifies if the method correctly returns the emoji flag associated with a given country's short code.
/// </summary>
/// <param name="shortCode">The short code of the country for which the emoji flag is to be retrieved.</param>

[Theory]
[InlineData("GH")]
[InlineData("CM")]
[InlineData("US")]
public void GetCountryFlagByCode_WithCorrectCode_ShouldReturnEmojiFlag(string shortCode)
public void GetCountryFlagByCode_WithCorrectCode_ShouldReturnEmojiFlag(string shortCode)
{
//Act
var countryFlag = _countryHelper.GetCountryEmojiFlag(shortCode);
Expand All @@ -53,6 +67,12 @@ public void GetCountryFlagByCode_WithCorrectCode_ShouldReturnEmojiFlag(string sh
countryFlag.Should().Be("🇺🇸");
}

/// <summary>
/// Tests the GetCountryByCode method in the CountryHelper class with incorrect short codes.
/// This test checks if the method correctly returns null when an incorrect short code is provided.
/// </summary>
/// <param name="shortCode">The incorrect short code of the country to be retrieved.</param>

[Theory]
[InlineData("GHA")]
[InlineData("G")]
Expand All @@ -66,4 +86,51 @@ public void GetCountryByCode_WithInCorrectCode_ShouldReturnNull(string shortCode
country.Should().BeNull();
}


/// <summary>
/// Tests the GetPhoneCodeByCountryShortCode method in the CountryHelper class.
/// This test verifies if the method correctly returns the phone code associated with a given country's short code.
/// </summary>
/// <param name="shortCode">The short code of the country for which the phone code is to be retrieved.</param>
/// <param name="expectedPhoneCode">The expected phone code of the country associated with the given short code.</param>

[Theory]
[InlineData("GH", "+233")]
[InlineData("US", "+1")]
public void GetPhoneCodeByCountryShortCode_WithCorrectShortCode_ShouldReturnPhoneCode(string shortCode, string expectedPhoneCode)
{
//Act
var phoneCode = _countryHelper.GetPhoneCodeByCountryShortCode(shortCode);

//Assert
phoneCode.Should().NotBeNull();
phoneCode.Should().Be(expectedPhoneCode);
}


/// <summary>
/// Tests the GetCountryByPhoneCode method in the CountryHelper class.
/// This test checks if the method correctly returns the country associated with a given phone code.
/// </summary>
/// <param name="phoneCode">The phone code of the country to be retrieved.</param>
/// <param name="expectedShortCode">The expected short code of the country associated with the phone code.</param>

[Theory]
[InlineData("+233", "GH")]
[InlineData("+1", "US")]
public void GetCountriesByPhoneCode_WithCorrectPhoneCode_ShouldReturnCountries(string phoneCode, string expectedShortCode)
{
//Act
var countries = _countryHelper.GetCountriesByPhoneCode(phoneCode);

//Assert
countries.Should().NotBeNullOrEmpty();
countries.Any(c => c.CountryShortCode == expectedShortCode).Should().BeTrue();
}






}

0 comments on commit 6d05549

Please sign in to comment.