diff --git a/.github/workflows/autoMerge.yml b/.github/workflows/autoMerge.yml deleted file mode 100644 index 570bb8a..0000000 --- a/.github/workflows/autoMerge.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Auto merge approved pull requests -on: - pull_request: - types: - - labeled - - unlabeled - - synchronize - - opened - - edited - - ready_for_review - - reopened - - unlocked - pull_request_review: - types: - - submitted - check_suite: - types: - - completed - status: {} -jobs: - autoMerge: - runs-on: ubuntu-latest - steps: - - name: autoMerge - uses: "pascalgn/automerge-action@f81beb99aef41bb55ad072857d43073fba833a98" - env: - GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" diff --git a/README.md b/README.md index bff0034..49fa21f 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,9 @@ A .NET client wrapper for both .NET Core & .NET Framework projects of [Via CEP A ## CI/CD -| Branch | Build status | Last commit | Tests | -|--------|--------------|-------------|-------| -| Master | [![Build status](https://ci.appveyor.com/api/projects/status/9jnsy1e08jhyxl7j/branch/master?svg=true)](https://ci.appveyor.com/project/guibranco/ViaCEP) | [![GitHub last commit](https://img.shields.io/github/last-commit/guibranco/ViaCEP/master)](https://github.com/guibranco/ViaCEP) | ![AppVeyor tests (branch)](https://img.shields.io/appveyor/tests/guibranco/ViaCEP/master?compact_message) | -| Develop | [![Build status](https://ci.appveyor.com/api/projects/status/9jnsy1e08jhyxl7j/branch/develop?svg=true)](https://ci.appveyor.com/project/guibranco/ViaCEP/branch/develop) | [![GitHub last commit](https://img.shields.io/github/last-commit/guibranco/ViaCEP/develop)](https://github.com/guibranco/ViaCEP) | ![AppVeyor tests (branch)](https://img.shields.io/appveyor/tests/guibranco/ViaCEP/develop?compact_message) | +| Build status | Last commit | Tests | +|--------------|-------------|-------| +| [![Build status](https://ci.appveyor.com/api/projects/status/9jnsy1e08jhyxl7j/branch/master?svg=true)](https://ci.appveyor.com/project/guibranco/ViaCEP) | [![GitHub last commit](https://img.shields.io/github/last-commit/guibranco/ViaCEP/master)](https://github.com/guibranco/ViaCEP) | ![AppVeyor tests (branch)](https://img.shields.io/appveyor/tests/guibranco/ViaCEP/master?compact_message) | ## Code Quality diff --git a/Src/ViaCEP/IViaCepClient.cs b/Src/ViaCEP/IViaCepClient.cs index a4da4dd..355f07a 100644 --- a/Src/ViaCEP/IViaCepClient.cs +++ b/Src/ViaCEP/IViaCepClient.cs @@ -1,9 +1,9 @@ -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace ViaCep +namespace ViaCep { + using System.Collections.Generic; + using System.Threading; + using System.Threading.Tasks; + /// /// The ViaCEP client interface. /// @@ -17,7 +17,7 @@ public interface IViaCepClient ViaCepResult Search(string zipCode); /// - /// Searches the specified state initials. + /// Searches the specified address by state initials (UF), city and address name. /// /// The state initials. /// The city. @@ -26,7 +26,7 @@ public interface IViaCepClient IEnumerable Search(string stateInitials, string city, string address); /// - /// Searches the asynchronous. + /// Searches the specified zip code asynchronous. /// /// The zip code. /// The cancellation token. @@ -34,7 +34,7 @@ public interface IViaCepClient Task SearchAsync(string zipCode, CancellationToken cancellationToken); /// - /// Searches the asynchronous. + /// Searches the specified address by state initials (UF), city and address name asynchronous. /// /// The state initials. /// The city. diff --git a/Src/ViaCep/IViaCepClient.cs b/Src/ViaCep/IViaCepClient.cs new file mode 100644 index 0000000..1f9627b --- /dev/null +++ b/Src/ViaCep/IViaCepClient.cs @@ -0,0 +1,46 @@ +namespace ViaCep +{ + using System.Collections.Generic; + using System.Threading; + using System.Threading.Tasks; + + /// + /// The ViaCEP client interface. + /// + public interface IViaCepClient + { + /// + /// Searches the specified zip code. + /// + /// The zip code. + /// + ViaCepResult Search(string zipCode); + + /// + /// Searches the specified address by state initials (UF), city and address name. + /// + /// The state initials. + /// The city. + /// The address. + /// + IEnumerable Search(string stateInitials, string city, string address); + + /// + /// Searches the specified zip code asynchronous. + /// + /// The zip code. + /// The cancellation token. + /// + Task SearchAsync(string zipCode, CancellationToken cancellationToken); + + /// + /// Searches the specified address by state initials (UF), city and address name asynchronous. + /// + /// The state initials. + /// The city. + /// The address. + /// The cancellation token. + /// + Task> SearchAsync(string stateInitials, string city, string address, CancellationToken cancellationToken); + } +} diff --git a/Src/ViaCep/VIaCEPResult.cs b/Src/ViaCep/VIaCEPResult.cs deleted file mode 100644 index 3c20f56..0000000 --- a/Src/ViaCep/VIaCEPResult.cs +++ /dev/null @@ -1,91 +0,0 @@ -namespace ViaCep -{ - using Newtonsoft.Json; - - /// - /// The Via CEP result class. - /// - public sealed class ViaCepResult - { - /// - /// Gets or sets the zip code. - /// - /// - /// The zip code. - /// - [JsonProperty("cep")] - public string ZipCode { get; set; } - - /// - /// Gets or sets the street. - /// - /// - /// The street. - /// - [JsonProperty("logradouro")] - public string Street { get; set; } - - /// - /// Gets or sets the complement. - /// - /// - /// The complement. - /// - [JsonProperty("complemento")] - public string Complement { get; set; } - - /// - /// Gets or sets the neighborhood. - /// - /// - /// The neighborhood. - /// - [JsonProperty("bairro")] - public string Neighborhood { get; set; } - - /// - /// Gets or sets the city. - /// - /// - /// The city. - /// - [JsonProperty("localidade")] - public string City { get; set; } - - /// - /// Gets or sets the state initials. - /// - /// - /// The state initials. - /// - [JsonProperty("uf")] - public string StateInitials { get; set; } - - /// - /// Gets or sets the unit. - /// - /// - /// The unit. - /// - [JsonProperty("unidade")] - public string Unit { get; set; } - - /// - /// Gets or sets the ibge code. - /// - /// - /// The ibge code. - /// - [JsonProperty("ibge")] - public int IBGECode { get; set; } - - /// - /// Gets or sets the gia code. - /// - /// - /// The gia code. - /// - [JsonProperty("gia")] - public int? GIACode { get; set; } - } -} diff --git a/Src/ViaCEP/VIaCEPResult.cs b/Src/ViaCep/VIaCepResult.cs similarity index 100% rename from Src/ViaCEP/VIaCEPResult.cs rename to Src/ViaCep/VIaCepResult.cs diff --git a/Src/ViaCEP/ViaCEP.csproj b/Src/ViaCep/ViaCep.csproj similarity index 90% rename from Src/ViaCEP/ViaCEP.csproj rename to Src/ViaCep/ViaCep.csproj index 87b59a3..9bc8823 100644 --- a/Src/ViaCEP/ViaCEP.csproj +++ b/Src/ViaCep/ViaCep.csproj @@ -1,30 +1,30 @@ - - - - A251D320-410E-48F1-889D-9F59EB8CFEB9 - netstandard2.0;netstandard2.1 - true - Guilherme Branco Stracini - © 2020 Guilherme Branco Stracini. All rights reserved. - The ViaCEP WebService client for .NET projects (both .NET Core and .NET Framework) - https://guibranco.github.io/ViaCEP/ - https://github.com/guibranco/ViaCEP/ - GIT - cep zipcode postalcode postal zip address location api webservice brasil brazil correios ibge gia mf fazenda - https://raw.githubusercontent.com/guibranco/ViaCEP/master/logo.png?v=1 - logo.png - 1.0.0 - 1.0.0.0 - LICENSE - - - - - - - - - - - - + + + + A251D320-410E-48F1-889D-9F59EB8CFEB9 + netstandard2.0;netstandard2.1 + true + Guilherme Branco Stracini + © 2020 Guilherme Branco Stracini. All rights reserved. + The ViaCep API/WebService client for .NET projects (both .NET Core and .NET Framework) + https://guibranco.github.io/ViaCEP/ + https://github.com/guibranco/ViaCEP/ + GIT + cep zipcode postalcode postal zip address location api webservice brasil brazil correios ibge gia mf fazenda + https://raw.githubusercontent.com/guibranco/ViaCEP/master/logo.png?v=1 + logo.png + 1.0.0 + 1.0.0.0 + LICENSE + + + + + + + + + + + + diff --git a/Src/ViaCEP/ViaCEPClient.cs b/Src/ViaCep/ViaCepClient.cs similarity index 93% rename from Src/ViaCEP/ViaCEPClient.cs rename to Src/ViaCep/ViaCepClient.cs index 82c6016..a812f0d 100644 --- a/Src/ViaCEP/ViaCEPClient.cs +++ b/Src/ViaCep/ViaCepClient.cs @@ -1,105 +1,105 @@ -namespace ViaCep -{ - using System; - using System.Collections.Generic; - using System.Net.Http; - using System.Threading; - using System.Threading.Tasks; - - /// - /// The Via CEP client class. - /// - public class ViaCepClient : IViaCepClient - { - #region Private fields - - /// - /// The base URL - /// - private const string BaseUrl = "https://viacep.com.br"; - - /// - /// The HTTP client - /// - private readonly HttpClient _httpClient; - - #endregion - - #region ~Ctors - - /// - /// Initializes a new instance of the class. - /// - public ViaCepClient() - { - _httpClient = HttpClientFactory.Create(); - _httpClient.BaseAddress = new Uri(BaseUrl); - } - - /// - /// Initializes a new instance of the class. - /// - /// The HTTP client. - public ViaCepClient(HttpClient httpClient) - { - _httpClient = httpClient; - } - - #endregion - - #region Public methods - - /// - /// Searches the specified zip code. - /// - /// The zip code. - /// - public ViaCepResult Search(string zipCode) - { - return SearchAsync(zipCode, CancellationToken.None).Result; - } - - /// - /// Searches the specified state initials. - /// - /// The state initials. - /// The city. - /// The address. - /// - public IEnumerable Search(string stateInitials, string city, string address) - { - return SearchAsync(stateInitials, city, address, CancellationToken.None).Result; - } - - /// - /// Searches the asynchronous. - /// - /// The zip code. - /// The token. - /// - public async Task SearchAsync(string zipCode, CancellationToken cancellationToken) - { - var response = await _httpClient.GetAsync($"/ws/{zipCode}/json", cancellationToken).ConfigureAwait(false); - response.EnsureSuccessStatusCode(); - return await response.Content.ReadAsAsync(cancellationToken).ConfigureAwait(false); - } - - /// - /// Searches the asynchronous. - /// - /// The state initials. - /// The city. - /// The address. - /// The token. - /// - public async Task> SearchAsync( - string stateInitials, string city, string address, CancellationToken cancellationToken) - { - var response = await _httpClient.GetAsync($"/ws/{stateInitials}/{city}/{address}/json", cancellationToken).ConfigureAwait(false); - response.EnsureSuccessStatusCode(); - return await response.Content.ReadAsAsync>(cancellationToken).ConfigureAwait(false); - } - - #endregion - } -} +namespace ViaCep +{ + using System; + using System.Collections.Generic; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; + + /// + /// The Via CEP client class. + /// + public class ViaCepClient : IViaCepClient + { + #region Private fields + + /// + /// The base URL + /// + private const string _baseUrl = "https://viacep.com.br"; + + /// + /// The HTTP client + /// + private readonly HttpClient _httpClient; + + #endregion + + #region ~Ctors + + /// + /// Initializes a new instance of the class. + /// + public ViaCepClient() + { + _httpClient = HttpClientFactory.Create(); + _httpClient.BaseAddress = new Uri(_baseUrl); + } + + /// + /// Initializes a new instance of the class. + /// + /// The HTTP client. + public ViaCepClient(HttpClient httpClient) + { + _httpClient = httpClient; + } + + #endregion + + #region Public methods + + /// + /// Searches the specified zip code. + /// + /// The zip code. + /// + public ViaCepResult Search(string zipCode) + { + return SearchAsync(zipCode, CancellationToken.None).Result; + } + + /// + /// Searches the specified state initials. + /// + /// The state initials. + /// The city. + /// The address. + /// + public IEnumerable Search(string stateInitials, string city, string address) + { + return SearchAsync(stateInitials, city, address, CancellationToken.None).Result; + } + + /// + /// Searches the asynchronous. + /// + /// The zip code. + /// The token. + /// + public async Task SearchAsync(string zipCode, CancellationToken cancellationToken) + { + var response = await _httpClient.GetAsync($"/ws/{zipCode}/json", cancellationToken).ConfigureAwait(false); + response.EnsureSuccessStatusCode(); + return await response.Content.ReadAsAsync(cancellationToken).ConfigureAwait(false); + } + + /// + /// Searches the asynchronous. + /// + /// The state initials. + /// The city. + /// The address. + /// The token. + /// + public async Task> SearchAsync( + string stateInitials, string city, string address, CancellationToken cancellationToken) + { + var response = await _httpClient.GetAsync($"/ws/{stateInitials}/{city}/{address}/json", cancellationToken).ConfigureAwait(false); + response.EnsureSuccessStatusCode(); + return await response.Content.ReadAsAsync>(cancellationToken).ConfigureAwait(false); + } + + #endregion + } +} diff --git a/Tests/ViaCEP.Tests/CoverageTests.cs b/Tests/ViaCEP.Tests/CoverageTests.cs index 68589d4..3a2820f 100644 --- a/Tests/ViaCEP.Tests/CoverageTests.cs +++ b/Tests/ViaCEP.Tests/CoverageTests.cs @@ -1,13 +1,13 @@ -using System; -using System.Linq; -using System.Net.Http; -using RichardSzalay.MockHttp; -using Xunit; - -namespace ViaCep.Tests +namespace ViaCep.Tests { + using System; + using System.Linq; + using System.Net.Http; + using RichardSzalay.MockHttp; + using Xunit; + /// - /// + /// The coverage tests class. /// public class CoverageTests { diff --git a/Tests/ViaCEP.Tests/ResultsFixture.cs b/Tests/ViaCEP.Tests/ResultsFixture.cs index 21386b0..b1af504 100644 --- a/Tests/ViaCEP.Tests/ResultsFixture.cs +++ b/Tests/ViaCEP.Tests/ResultsFixture.cs @@ -1,20 +1,7 @@ -// *********************************************************************** -// Assembly : ViaCep.Tests -// Author : Guilherme Branco Stracini -// Created : 10-23-2020 -// -// Last Modified By : Guilherme Branco Stracini -// Last Modified On : 10-23-2020 -// *********************************************************************** -// -// Copyright (c) Guilherme Branco Stracini ME. All rights reserved. -// -// -// *********************************************************************** -using System.Collections.Generic; - -namespace ViaCep.Tests +namespace ViaCep.Tests { + using System.Collections.Generic; + /// /// Class ResultsFixture. /// diff --git a/Tests/ViaCEP.Tests/ZipCodeTests.cs b/Tests/ViaCEP.Tests/ZipCodeTests.cs index 942104a..2d96cd9 100644 --- a/Tests/ViaCEP.Tests/ZipCodeTests.cs +++ b/Tests/ViaCEP.Tests/ZipCodeTests.cs @@ -1,10 +1,9 @@ -using Moq; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - namespace ViaCep.Tests { + using Moq; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; using Xunit; /// diff --git a/Tests/ViaCep.Tests/AddressTests.cs b/Tests/ViaCep.Tests/AddressTests.cs new file mode 100644 index 0000000..71e2c5b --- /dev/null +++ b/Tests/ViaCep.Tests/AddressTests.cs @@ -0,0 +1,57 @@ +namespace ViaCep.Tests +{ + using System; + using System.Linq; + using Xunit; + using Moq; + using System.Threading; + using System.Threading.Tasks; + + /// + /// The address tests class. + /// + public class AddressTests + { + /// + /// Validates the search by full address. + /// + [Fact] + public void ValidateSearchByFullAddress() + { + var fixtureResults = ResultsFixture.GetSampleResults(); + var clientMock = new Mock(); + clientMock + .Setup(c => c.Search(It.IsAny(), It.IsAny(), It.IsAny())) + .Returns(fixtureResults); + + var results = clientMock.Object.Search(fixtureResults.First().StateInitials, fixtureResults.First().City, fixtureResults.First().Street); + Assert.NotNull(results); + + var list = results.ToList(); + Assert.True(list.Any()); + Assert.Contains(list, r => r.ZipCode.Equals("12345-678", StringComparison.InvariantCultureIgnoreCase)); + Assert.Contains(list, r => r.ZipCode.Equals("98765-432", StringComparison.InvariantCultureIgnoreCase)); + } + + /// + /// Validates the search by full address asynchronous. + /// + [Fact] + public async Task ValidateSearchByFullAddressAsync() + { + var fixtureResults = ResultsFixture.GetSampleResults(); + var clientMock = new Mock(); + clientMock + .Setup(c => c.SearchAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + .ReturnsAsync(fixtureResults); + + var results = await clientMock.Object.SearchAsync(fixtureResults.First().StateInitials, fixtureResults.First().City, fixtureResults.First().Street, CancellationToken.None); + Assert.NotNull(results); + + var list = results.ToList(); + Assert.True(list.Any()); + Assert.Contains(list, r => r.ZipCode.Equals("12345-678", StringComparison.InvariantCultureIgnoreCase)); + Assert.Contains(list, r => r.ZipCode.Equals("98765-432", StringComparison.InvariantCultureIgnoreCase)); + } + } +} \ No newline at end of file diff --git a/Tests/ViaCep.Tests/CoverageTests.cs b/Tests/ViaCep.Tests/CoverageTests.cs new file mode 100644 index 0000000..4555e11 --- /dev/null +++ b/Tests/ViaCep.Tests/CoverageTests.cs @@ -0,0 +1,86 @@ +namespace ViaCep.Tests +{ + using System; + using System.Linq; + using System.Net.Http; + using RichardSzalay.MockHttp; + using Xunit; + + /// + /// The coverage tests class. + /// + public class CoverageTests + { + /// + /// The base URL + /// + private readonly Uri _baseUrl = new Uri("https://viacep.com.br/"); + + /// + /// Validates the create new instance. + /// + [Fact] + public void ValidateCreateNewInstance() + { + var client = new ViaCepClient(); + Assert.NotNull(client); + } + + /// + /// Validates the search by zip code coverage. + /// + [Fact] + public void ValidateSearchByZipCodeCoverage() + { + const string resultData = "{'cep': '01001-000','logradouro': 'Praça da Sé','complemento': 'lado ímpar','bairro': 'Sé','localidade': 'São Paulo','uf': 'SP','unidade': '','ibge': '3550308','gia': '1004'}"; + var httpClientMock = new MockHttpMessageHandler(); + httpClientMock.When("https://viacep.com.br/ws/*/json") + .Respond("application/json", resultData); + + var httpClient = new HttpClient(httpClientMock) {BaseAddress = _baseUrl}; + var client = new ViaCepClient(httpClient); + + var result = client.Search("01001000"); + Assert.NotNull(result); + Assert.Equal("01001-000", result.ZipCode); + Assert.Equal("Praça da Sé", result.Street); + Assert.Equal("lado ímpar", result.Complement); + Assert.Equal("Sé", result.Neighborhood); + Assert.Equal("São Paulo", result.City); + Assert.Equal("SP", result.StateInitials); + Assert.Equal(string.Empty, result.Unit); + Assert.Equal(3550308, result.IBGECode); + Assert.True(result.GIACode.HasValue); + Assert.Equal(1004, result.GIACode); + } + + /// + /// Validates the search by address coverage. + /// + [Fact] + public void ValidateSearchByAddressCoverage() + { + const string resultData = + "[ { 'cep': '91790-072', 'logradouro': 'Rua Domingos José Poli', 'complemento': '', 'bairro': 'Restinga', 'localidade': 'Porto Alegre', 'uf': 'RS', 'unidade': '', 'ibge': '4314902', 'gia': '' }, { 'cep': '91910-420', 'logradouro': 'Rua José Domingos Varella', 'complemento': '', 'bairro': 'Cavalhada', 'localidade': 'Porto Alegre', 'uf': 'RS', 'unidade': '', 'ibge': '4314902', 'gia': '' }, { 'cep': '90420-200', 'logradouro': 'Rua Domingos José de Almeida', 'complemento': '', 'bairro': 'Rio Branco', 'localidade': 'Porto Alegre', 'uf': 'RS', 'unidade': '', 'ibge': '4314902', 'gia': '' } ]"; + var httpClientMock = new MockHttpMessageHandler(); + httpClientMock.When("https://viacep.com.br/ws/*/*/*/json") + .Respond("application/json", resultData); + + var httpClient = new HttpClient(httpClientMock) { BaseAddress = _baseUrl }; + var client = new ViaCepClient(httpClient); + + var results = client.Search("RS", "Porto Alegre", "Domingos Jose"); + Assert.NotNull(results); + + var list = results.ToList(); + Assert.Equal(3, list.Count); + Assert.All(list, result => Assert.False(result.GIACode.HasValue)); + Assert.All(list, result => Assert.Empty(result.Complement)); + Assert.All(list, result => Assert.Equal("Porto Alegre", result.City)); + Assert.All(list, result => Assert.Equal("RS", result.StateInitials)); + Assert.All(list, result => Assert.Equal(4314902, result.IBGECode)); + + } + + } +} diff --git a/Tests/ViaCep.Tests/ResultsFixture.cs b/Tests/ViaCep.Tests/ResultsFixture.cs new file mode 100644 index 0000000..a3a7ec1 --- /dev/null +++ b/Tests/ViaCep.Tests/ResultsFixture.cs @@ -0,0 +1,55 @@ +namespace ViaCep.Tests +{ + using System.Collections.Generic; + + /// + /// Class ResultsFixture. + /// + internal static class ResultsFixture + { + /// + /// Gets the sample results. + /// + /// ICollection<ViaCepResult>. + public static ICollection GetSampleResults() => + new List + { + new ViaCepResult + { + Unit = "Any", + City = "São Paulo", + Complement = "", + GIACode = 1, + IBGECode = 1, + Neighborhood = "Centro", + StateInitials = "SP", + Street = "Rua Direita", + ZipCode = "12345-678" + }, + new ViaCepResult + { + Unit = "", + City = "São Paulo", + Complement = "", + GIACode = null, + IBGECode = 1, + Neighborhood = "Centro", + StateInitials = "SP", + Street = "Rua Direita", + ZipCode = "45632-870" + }, + new ViaCepResult + { + Unit = "", + City = "São Paulo", + Complement = "", + GIACode = 12, + IBGECode = 123, + Neighborhood = "Centro", + StateInitials = "SP", + Street = "Rua Direita", + ZipCode = "98765-432" + } + }; + } +} \ No newline at end of file diff --git a/Tests/ViaCEP.Tests/ViaCEP.Tests.csproj b/Tests/ViaCep.Tests/ViaCep.Tests.csproj similarity index 94% rename from Tests/ViaCEP.Tests/ViaCEP.Tests.csproj rename to Tests/ViaCep.Tests/ViaCep.Tests.csproj index 3ce9b5d..1b75eb7 100644 --- a/Tests/ViaCEP.Tests/ViaCEP.Tests.csproj +++ b/Tests/ViaCep.Tests/ViaCep.Tests.csproj @@ -12,17 +12,18 @@ all - + + - - all - runtime; build; native; contentfiles; analyzers; buildtransitive + + all + runtime; build; native; contentfiles; analyzers; buildtransitive - - + + diff --git a/Tests/ViaCep.Tests/ZipCodeTests.cs b/Tests/ViaCep.Tests/ZipCodeTests.cs new file mode 100644 index 0000000..e62d03a --- /dev/null +++ b/Tests/ViaCep.Tests/ZipCodeTests.cs @@ -0,0 +1,113 @@ +namespace ViaCep.Tests +{ + using Moq; + using System.Linq; + using System.Threading; + using System.Threading.Tasks; + using Xunit; + + /// + /// The zip code tests class. + /// + public class ZipCodeTests + { + /// + /// Validates the search by zip code. + /// + [Fact] + public void ValidateSearchByZipCode() + { + var fixtureResults = ResultsFixture.GetSampleResults(); + var clientMock = new Mock(); + clientMock + .Setup(c => c.Search(It.IsAny())) + .Returns(fixtureResults.First(r => r.ZipCode.Equals("12345-678"))); + + var result = clientMock.Object.Search("12345678"); + Assert.NotNull(result); + Assert.Equal("Any", result.Unit); + Assert.Equal("Rua Direita", result.Street); + Assert.Equal(string.Empty, result.Complement); + Assert.Equal(1, result.GIACode); + Assert.Equal(1, result.IBGECode); + Assert.Equal("Centro", result.Neighborhood); + Assert.Equal("São Paulo", result.City); + Assert.Equal("SP", result.StateInitials); + } + + /// + /// Validates the search by zip code asynchronous. + /// + /// + [Fact] + public async Task ValidateSearchByZipCodeAsync() + { + var fixtureResults = ResultsFixture.GetSampleResults(); + var clientMock = new Mock(); + clientMock + .Setup(c => c.SearchAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(fixtureResults.First(r => r.ZipCode.Equals("12345-678"))); + + var result = await clientMock.Object.SearchAsync("12345678", CancellationToken.None); + Assert.NotNull(result); + Assert.Equal("Any", result.Unit); + Assert.Equal("Rua Direita", result.Street); + Assert.Equal(string.Empty, result.Complement); + Assert.Equal(1, result.GIACode); + Assert.Equal(1, result.IBGECode); + Assert.Equal("Centro", result.Neighborhood); + Assert.Equal("São Paulo", result.City); + Assert.Equal("SP", result.StateInitials); + } + + + /// + /// Validates if the search by zip code don't throw a exception if the address doesn't have a gia code + /// + /// + [Fact] + public void ValidateSearchByZipCodeWithoutGiaCode() + { + var fixtureResults = ResultsFixture.GetSampleResults(); + var clientMock = new Mock(); + clientMock + .Setup(c => c.Search(It.IsAny())) + .Returns(fixtureResults.First(r => !r.GIACode.HasValue)); + + var result = clientMock.Object.Search("12345678"); + Assert.NotNull(result); + Assert.Equal("", result.Unit); + Assert.Equal("Rua Direita", result.Street); + Assert.Equal(string.Empty, result.Complement); + Assert.Null(result.GIACode); + Assert.Equal(1, result.IBGECode); + Assert.Equal("Centro", result.Neighborhood); + Assert.Equal("São Paulo", result.City); + Assert.Equal("SP", result.StateInitials); + } + + /// + /// Validates the search by zip code without gia code asynchronous. + /// + [Fact] + public async Task ValidateSearchByZipCodeWithoutGiaCodeAsync() + { + var fixtureResults = ResultsFixture.GetSampleResults(); + var clientMock = new Mock(); + clientMock + .Setup(c => c.SearchAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(fixtureResults.First(r => !r.GIACode.HasValue)); + + var result = await clientMock.Object.SearchAsync("12345678", CancellationToken.None); + Assert.NotNull(result); + Assert.Equal("", result.Unit); + Assert.Equal("Rua Direita", result.Street); + Assert.Equal(string.Empty, result.Complement); + Assert.Null(result.GIACode); + Assert.Equal(1, result.IBGECode); + Assert.Equal("Centro", result.Neighborhood); + Assert.Equal("São Paulo", result.City); + Assert.Equal("SP", result.StateInitials); + } + } +} \ No newline at end of file diff --git a/ViaCep.sln.DotSettings b/ViaCep.sln.DotSettings index fa45c58..397d6e6 100644 --- a/ViaCep.sln.DotSettings +++ b/ViaCep.sln.DotSettings @@ -1,8 +1,12 @@  GIA IBGE + True True + True True True + True + True True True diff --git a/appveyor.yml b/appveyor.yml index e2e9b38..7b08897 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 3.2.{build} +version: 4.1.{build} skip_tags: true image: Visual Studio 2019 configuration: Release @@ -24,9 +24,9 @@ dotnet_csproj: informational_version: '{version}' init: -- set PATH=C:\Program Files\Java\jdk15\bin;%PATH% +- SET JAVA_HOME=C:\Program Files\Java\jdk15 +- SET PATH=%JAVA_HOME%\bin;%PATH% - set JAVA_HOME_11_X64=C:\Program Files\Java\jdk15 -- set JAVA_HOME=C:\Program Files\Java\jdk15 before_build: - ps: $env:SONAR_PROJECT = "$env:APPVEYOR_REPO_NAME" -replace "/","_" @@ -34,7 +34,7 @@ before_build: - cmd: nuget restore - cmd: choco install opencover.portable - cmd: choco install codecov -- cmd: curl -L https://github.com/codacy/codacy-coverage-reporter/releases/download/12.2.0/codacy-coverage-reporter-assembly.jar > ./codacy-test-reporter.jar +- cmd: curl -L https://github.com/codacy/codacy-coverage-reporter/releases/download/13.11.17/codacy-coverage-reporter-assembly.jar > ./codacy-test-reporter.jar - cmd: dotnet tool install --global dotnet-sonarscanner build: @@ -44,16 +44,30 @@ build: verbosity: normal build_script: -- ps: 'if (-Not $env:APPVEYOR_PULL_REQUEST_NUMBER -And $env:APPVEYOR_REPO_BRANCH -eq "master") { & - dotnet sonarscanner begin - /k:"$env:SONAR_PROJECT" - /o:"$env:SONAR_ORGANIZATION" - /d:sonar.host.url="https://sonarcloud.io" - /d:sonar.login="$env:SONAR_TOKEN" - /v:"$env:APPVEYOR_BUILD_NUMBER" - /d:sonar.exclusions="**/bin/**/*,**/obj/**/*" - /d:sonar.coverage.exclusions="**/$env:SOLUTION_NAME.Tests/**,**/*Tests.cs" - /d:sonar.cs.opencover.reportsPaths="$env:CD\Tests\$env:SOLUTION_NAME.Tests\coverage.opencover.xml" }' +- ps: 'if (-Not $env:APPVEYOR_PULL_REQUEST_NUMBER) { & + dotnet sonarscanner begin + /k:"$env:SONAR_PROJECT" + /o:"$env:SONAR_ORGANIZATION" + /d:sonar.branch.name="$env:APPVEYOR_REPO_BRANCH" + /d:sonar.host.url="https://sonarcloud.io" + /d:sonar.login="$env:SONAR_TOKEN" + /v:"$env:APPVEYOR_BUILD_NUMBER" + /d:sonar.exclusions="**/bin/**/*,**/obj/**/*" + /d:sonar.coverage.exclusions="**/$env:SOLUTION_NAME.Tests/**,**/*Tests.cs" + /d:sonar.cs.opencover.reportsPaths="$env:CD\Tests\$env:SOLUTION_NAME.Tests\coverage.opencover.xml" + }' +- ps: 'if ($env:APPVEYOR_PULL_REQUEST_NUMBER) { & + dotnet sonarscanner begin + /k:"$env:SONAR_PROJECT" + /o:"$env:SONAR_ORGANIZATION" + /d:sonar.pullrequest.key="$env:APPVEYOR_PULL_REQUEST_NUMBER" + /d:sonar.host.url="https://sonarcloud.io" + /d:sonar.login="$env:SONAR_TOKEN" + /v:"$env:APPVEYOR_BUILD_NUMBER" + /d:sonar.exclusions="**/bin/**/*,**/obj/**/*" + /d:sonar.coverage.exclusions="**/$env:SOLUTION_NAME.Tests/**,**/*Tests.cs" + /d:sonar.cs.opencover.reportsPaths="$env:CD\Tests\$env:SOLUTION_NAME.Tests\coverage.opencover.xml" + }' - dotnet build %SOLUTION_NAME%.sln - dotnet test .\Tests\%SOLUTION_NAME%.Tests\%SOLUTION_NAME%.Tests.csproj /p:CollectCoverage=true @@ -65,9 +79,7 @@ build_script: -l CSharp -t %CODACY_PROJECT_TOKEN% -r "%CD%\Tests\%SOLUTION_NAME%.Tests\coverage.opencover.xml" -- ps: 'if (-Not $env:APPVEYOR_PULL_REQUEST_NUMBER -And $env:APPVEYOR_REPO_BRANCH -eq "master") { & - dotnet sonarscanner end - /d:sonar.login="$env:SONAR_TOKEN" }' +- dotnet sonarscanner end /d:sonar.login="%SONAR_TOKEN%" after_build: - xcopy %CD%\Src\%SOLUTION_NAME%\bin\Release\netstandard2.0\*.* %CD%\Build\ @@ -82,8 +94,10 @@ after_build: artifacts: - path: $(SOLUTION_NAME).%APPVEYOR_BUILD_VERSION%.zip name: ZipFile + - path: $(SOLUTION_NAME).%APPVEYOR_BUILD_VERSION%.nupkg name: Package + - path: $(SOLUTION_NAME).%APPVEYOR_BUILD_VERSION%.Coverage.zip name: Coverage @@ -93,6 +107,7 @@ deploy: on: branch: master artifact: Package + - provider: GitHub on: branch: master