Skip to content

Commit

Permalink
Merge pull request #72 from Riju-bak/add-integration-test
Browse files Browse the repository at this point in the history
Added Integration Tests
  • Loading branch information
guibranco committed Jul 28, 2023
2 parents 18dd050 + 24d545a commit 7a56cc7
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Tests/ViaCep.IntegrationTests/AddressTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Xunit;

namespace ViaCep.IntegrationTests
{
public class AddressTests : IntegrationTest
{
[Fact]
public async Task Search_WithValidAddress_ReturnsExpectedResult()
{
//Arrange
var validState = "SP";
var validCity = "São Paulo";
var validAddress = "Rua Direita";
var validZipCodeOne = "01002-902"; //Corresponds to the above city and state
var validZipCodeTwo = "01002-000"; //Another valid zip code for the same city and state

//Act
var results = (await Client.SearchAsync(validState, validCity, validAddress, default)).ToList();

//Assert
Assert.NotNull(results);
Assert.Contains(results, r => r.ZipCode == validZipCodeOne);
Assert.Contains(results, r => r.ZipCode == validZipCodeTwo);
}

[Fact]
public async Task Search_WithNonexistentAddress_ReturnsEmptyResults()
{
//Arrange
var nonExistentAddress = "Non-existent Street";
var nonExistentCity = "Non-existent City";

//Act
var results = await Client.SearchAsync("XX", nonExistentCity, nonExistentAddress, default);

//Assert
Assert.Empty(results);
}

[Fact]
public async Task Search_WithCanceledToken_ThrowsTaskCanceledException()
{
//Arrange
var validState = "SP";
var validCity = "São Paulo";
var validAddress = "Rua Direita";
var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Cancel();

//Act and Assert
await Assert.ThrowsAsync<TaskCanceledException>(() =>
Client.SearchAsync(validState, validCity, validAddress, cancellationTokenSource.Token));
}
}
}
7 changes: 7 additions & 0 deletions Tests/ViaCep.IntegrationTests/IntegrationTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ViaCep.IntegrationTests
{
public class IntegrationTest
{
protected readonly ViaCepClient Client = new ViaCepClient();
}
}
28 changes: 28 additions & 0 deletions Tests/ViaCep.IntegrationTests/ViaCep.IntegrationTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Src\ViaCep\ViaCep.csproj" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions Tests/ViaCep.IntegrationTests/ZipCodeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Xunit;

namespace ViaCep.IntegrationTests
{
public class ZipCodeTests : IntegrationTest
{
[Fact]
public async Task Search_WithValidZipCode_ReturnsExpectedResult()
{
//Arrange
var validZipCode = "01001-000";

//Act
var result = await Client.SearchAsync(validZipCode, default);

//Assert
Assert.NotNull(result);
Assert.Equal(validZipCode, result.ZipCode);
Assert.NotNull(result.City);
Assert.NotNull(result.StateInitials);
}

[Fact]
public async Task Search_WithInvalidZipCode_ThrowsHttpRequestException()
{
//Arrange
var invalidZipCode = "invalid";

//Act and Assert
await Assert.ThrowsAsync<HttpRequestException>(() => Client.SearchAsync(invalidZipCode, default));
}

[Fact]
public async Task Search_WithCanceledToken_ThrowsTaskCanceledException()
{
//Arrange
var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Cancel();

//Act and Assert
await Assert.ThrowsAsync<TaskCanceledException>(() =>
Client.SearchAsync("01001-000", cancellationTokenSource.Token));
}
}
}
7 changes: 7 additions & 0 deletions ViaCep.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ViaCep.IntegrationTests", "Tests\ViaCep.IntegrationTests\ViaCep.IntegrationTests.csproj", "{E57D93CD-0D0B-414B-96F6-6B9FCC661893}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -31,13 +33,18 @@ Global
{D9FD0AA7-A4C5-43A2-820F-827BD503B52B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9FD0AA7-A4C5-43A2-820F-827BD503B52B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D9FD0AA7-A4C5-43A2-820F-827BD503B52B}.Release|Any CPU.Build.0 = Release|Any CPU
{E57D93CD-0D0B-414B-96F6-6B9FCC661893}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E57D93CD-0D0B-414B-96F6-6B9FCC661893}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E57D93CD-0D0B-414B-96F6-6B9FCC661893}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E57D93CD-0D0B-414B-96F6-6B9FCC661893}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{A251D320-410E-48F1-889D-9F59EB8CFEB9} = {A8E8D875-2058-4E15-B179-37FD1D408F66}
{D9FD0AA7-A4C5-43A2-820F-827BD503B52B} = {474F8215-8B66-4E36-927C-E078635C7996}
{E57D93CD-0D0B-414B-96F6-6B9FCC661893} = {474F8215-8B66-4E36-927C-E078635C7996}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AC1987AC-4E78-4CCD-84BC-543F3D7426B4}
Expand Down

0 comments on commit 7a56cc7

Please sign in to comment.