diff options
Diffstat (limited to 'test/cepis_test.exs')
| -rw-r--r-- | test/cepis_test.exs | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/test/cepis_test.exs b/test/cepis_test.exs new file mode 100644 index 0000000..a6bedb1 --- /dev/null +++ b/test/cepis_test.exs @@ -0,0 +1,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 |