summaryrefslogtreecommitdiffstats
path: root/tests/Cepis.Tests/ModelTests.cs
blob: f53ecb73dfc945bae99f9b0f05b24995df5f9045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Text.Json;

namespace Cepis.Tests;

/// <summary>The data objects on their own, with no server involved.</summary>
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<string, JsonElement>(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);
    }
}