diff --git a/Tests/ViaCep.IntegrationTests/AddressTests.cs b/Tests/ViaCep.IntegrationTests/AddressTests.cs new file mode 100644 index 0000000..e690168 --- /dev/null +++ b/Tests/ViaCep.IntegrationTests/AddressTests.cs @@ -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(() => + Client.SearchAsync(validState, validCity, validAddress, cancellationTokenSource.Token)); + } + } +} diff --git a/Tests/ViaCep.IntegrationTests/IntegrationTest.cs b/Tests/ViaCep.IntegrationTests/IntegrationTest.cs new file mode 100644 index 0000000..131a8a6 --- /dev/null +++ b/Tests/ViaCep.IntegrationTests/IntegrationTest.cs @@ -0,0 +1,7 @@ +namespace ViaCep.IntegrationTests +{ + public class IntegrationTest + { + protected readonly ViaCepClient Client = new ViaCepClient(); + } +} diff --git a/Tests/ViaCep.IntegrationTests/ViaCep.IntegrationTests.csproj b/Tests/ViaCep.IntegrationTests/ViaCep.IntegrationTests.csproj new file mode 100644 index 0000000..6234cc8 --- /dev/null +++ b/Tests/ViaCep.IntegrationTests/ViaCep.IntegrationTests.csproj @@ -0,0 +1,28 @@ + + + + net7.0 + enable + enable + + false + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + diff --git a/Tests/ViaCep.IntegrationTests/ZipCodeTests.cs b/Tests/ViaCep.IntegrationTests/ZipCodeTests.cs new file mode 100644 index 0000000..a0a37df --- /dev/null +++ b/Tests/ViaCep.IntegrationTests/ZipCodeTests.cs @@ -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(() => Client.SearchAsync(invalidZipCode, default)); + } + + [Fact] + public async Task Search_WithCanceledToken_ThrowsTaskCanceledException() + { + //Arrange + var cancellationTokenSource = new CancellationTokenSource(); + cancellationTokenSource.Cancel(); + + //Act and Assert + await Assert.ThrowsAsync(() => + Client.SearchAsync("01001-000", cancellationTokenSource.Token)); + } + } +} diff --git a/ViaCep.sln b/ViaCep.sln index a750d93..d5a39d2 100644 --- a/ViaCep.sln +++ b/ViaCep.sln @@ -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 @@ -31,6 +33,10 @@ 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 @@ -38,6 +44,7 @@ Global 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}