summaryrefslogtreecommitdiffstats
path: root/tests/Cepis.Tests/ModelTests.cs
blob: 7d9d0d6ae2064f2b4f3657ac3172ba61c4a0139b (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
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 HealthIsOkOnlyForTheDocumentedStatus()
    {
        Assert.True(new Health { Status = "ok" }.Ok);
        Assert.False(new Health { Status = "degradado" }.Ok);
        Assert.False(new Health().Ok);
    }
}