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
|
defmodule CepisTest do
use ExUnit.Case, async: true
doctest Cepis
doctest Cepis.Client
describe "normalize_cep/1" do
test "accepts the mask and the bare digits" do
for value <- ["01001000", "01001-000", " 01001-000 ", "01001.000"] do
assert Cepis.normalize_cep(value) == {:ok, "01001000"}
end
end
test "rejects anything that is not 8 digits" do
for value <- ["", "123", "0100100a", "010010000", "abcdefgh"] do
assert {:error, %Cepis.InvalidCEPError{}} = Cepis.normalize_cep(value)
end
end
test "rejects a value that is not a string" do
assert {:error, %Cepis.InvalidCEPError{}} = Cepis.normalize_cep(1_001_000)
end
test "the bang variant raises" do
assert_raise Cepis.InvalidCEPError, fn -> Cepis.normalize_cep!("123") end
end
end
describe "Cepis.Client.new/1" do
test "strips a trailing slash so separators cannot double up" do
assert Cepis.Client.new(base_url: "http://x/").base_url == "http://x"
assert Cepis.Client.new(base_url: "http://x//").base_url == "http://x"
end
test "rejects an impossible configuration" do
assert_raise ArgumentError, fn -> Cepis.Client.new(max_attempts: 0) end
assert_raise ArgumentError, fn -> Cepis.Client.new(timeout: 0) end
end
test "rejects an unknown option" do
assert_raise KeyError, fn -> Cepis.Client.new(retries: 3) end
end
test "the default user agent identifies the library" do
assert Cepis.Client.new([]).user_agent =~ "cepis-elixir/"
end
test "an already built client passes through untouched" do
client = Cepis.Client.new(base_url: "http://x")
assert Cepis.Client.new(client) == client
end
end
describe "Cepis.Error" do
test "splits retriable transport failures from definitive answers" do
assert Cepis.Error.retriable?(%Cepis.ServerError{status: 500})
assert Cepis.Error.retriable?(%Cepis.RateLimitError{})
assert Cepis.Error.retriable?(%Cepis.NetworkError{})
refute Cepis.Error.retriable?(%Cepis.NotFoundError{})
refute Cepis.Error.retriable?(%Cepis.InvalidCEPError{})
end
test "recognises its own errors" do
assert Cepis.Error.error?(%Cepis.NotFoundError{})
refute Cepis.Error.error?(%RuntimeError{})
refute Cepis.Error.error?(:oops)
end
end
describe "Cepis.Address.formatted_cep/1" do
test "adds the mask" do
assert Cepis.Address.formatted_cep(%Cepis.Address{cep: "01001000"}) == "01001-000"
end
test "leaves an unexpected value alone" do
assert Cepis.Address.formatted_cep(%Cepis.Address{cep: ""}) == ""
end
end
end
|