using System.Text.Json; namespace Cepis.Tests; /// The data objects on their own, with no server involved. public sealed class ModelTests { [Fact] public void EveryFieldDefaultsToTheEmptyString() { var address = new Address(); Assert.Equal("", address.Cep); Assert.Equal("", address.Logradouro); Assert.Equal("", address.Siafi); Assert.Null(address.Geo); } [Fact] public void FormattedCepAddsTheMask() => Assert.Equal("01001-000", new Address { Cep = "01001000" }.FormattedCep); [Fact] public void FormattedCepLeavesAnythingElseAlone() => Assert.Equal("123", new Address { Cep = "123" }.FormattedCep); [Fact] public void TwoAddressesForTheSamePlaceAreEqual() { var one = new Address { Cep = "01001000", Localidade = "São Paulo" }; var other = new Address { Cep = "01001000", Localidade = "São Paulo" }; Assert.Equal(one, other); Assert.Equal(one.GetHashCode(), other.GetHashCode()); } [Fact] public void EqualityIgnoresFieldsThisLibraryDoesNotModel() { var plain = new Address { Cep = "01001000" }; var enriched = plain with { Extra = new Dictionary(StringComparer.Ordinal) { ["cep_pai"] = JsonDocument.Parse("\"01000000\"").RootElement, }, }; Assert.Equal(plain, enriched); Assert.DoesNotContain("cep_pai", enriched.ToString(), StringComparison.Ordinal); } [Fact] public void TheCoordinateIsPreciseOnlyForAStreet() { Assert.True(new Geo { Precisao = "logradouro" }.IsPrecise); Assert.False(new Geo { Precisao = "localidade" }.IsPrecise); Assert.False(new Geo().IsPrecise); } [Fact] public void ARangeDefaultsToTheEmptyStringToo() { var faixa = new Faixa(); Assert.Equal("", faixa.Localidade); Assert.Equal("", faixa.CepIni); Assert.Equal("", faixa.CepFim); Assert.Equal(0, faixa.Ceps); } [Fact] public void ANotFoundCarriesNoRangeUnlessTheApiNamedOne() { Assert.Null(new CepNotFoundException("nope").Faixa); Assert.Equal( "Areal", new CepNotFoundException("nope", new Faixa { Localidade = "Areal" }).Faixa?.Localidade); } [Fact] public void HealthIsOkOnlyForTheDocumentedStatus() { Assert.True(new Health { Status = "ok" }.Ok); Assert.False(new Health { Status = "degradado" }.Ok); Assert.False(new Health().Ok); } }