summaryrefslogtreecommitdiffstats
path: root/test/cepis/lookup_test.exs
diff options
context:
space:
mode:
Diffstat (limited to 'test/cepis/lookup_test.exs')
-rw-r--r--test/cepis/lookup_test.exs74
1 files changed, 74 insertions, 0 deletions
diff --git a/test/cepis/lookup_test.exs b/test/cepis/lookup_test.exs
index b68ecbe..39d9d31 100644
--- a/test/cepis/lookup_test.exs
+++ b/test/cepis/lookup_test.exs
@@ -87,6 +87,80 @@ defmodule Cepis.LookupTest do
assert Exception.message(error) =~ "99999999"
end
+ test "a not-found names the municipality the CEP would belong to", %{
+ server: server,
+ client: client
+ } do
+ body = not_found_fixture(%{"faixa" => faixa_fixture()})
+ FakeServer.respond(server, "/25845000", {404, json(body), []})
+
+ assert {:error, %Cepis.NotFoundError{cep: "25845000", faixa: faixa} = error} =
+ Cepis.lookup("25845-000", client)
+
+ assert faixa.localidade == "Areal"
+ assert faixa.uf == "RJ"
+ assert faixa.estado == "Rio de Janeiro"
+ assert faixa.regiao == "Sudeste"
+ assert faixa.ibge == "3300225"
+ assert faixa.cep_ini == "25845001"
+ assert faixa.cep_fim == "25849899"
+ assert faixa.ceps == 118
+
+ assert Exception.message(error) =~ "Areal/RJ"
+ assert Exception.message(error) =~ "25845000 does not exist"
+ end
+
+ test "a not-found in a band no municipality owns has no faixa", %{
+ server: server,
+ client: client
+ } do
+ FakeServer.respond(server, "/99999999", {404, json(not_found_fixture()), []})
+
+ assert {:error, %Cepis.NotFoundError{faixa: nil} = error} = Cepis.lookup("99999999", client)
+ assert Exception.message(error) == "CEP 99999999 does not exist in the DNE database"
+ end
+
+ test "a malformed faixa does not become a parse failure", %{server: server, client: client} do
+ bodies = [
+ not_found_fixture(%{"faixa" => %{}}),
+ not_found_fixture(%{"faixa" => %{"uf" => "RJ"}}),
+ not_found_fixture(%{"faixa" => %{"localidade" => ""}}),
+ not_found_fixture(%{"faixa" => "Areal"}),
+ # the API omits the field rather than nulling it, but a null must not
+ # cost the caller the error either
+ not_found_fixture(%{"faixa" => :null})
+ ]
+
+ for {body, index} <- Enum.with_index(bodies) do
+ path = "/2584500#{index}"
+ FakeServer.respond(server, path, {404, json(body), []})
+
+ assert {:error, %Cepis.NotFoundError{faixa: nil}} =
+ Cepis.lookup(String.trim_leading(path, "/"), client)
+ end
+ end
+
+ test "a 404 body that is not JSON is still a clean not-found", %{
+ server: server,
+ client: client
+ } do
+ FakeServer.respond(server, "/99999999", {404, "<html>gateway</html>", []})
+
+ assert {:error, %Cepis.NotFoundError{cep: "99999999", faixa: nil}} =
+ Cepis.lookup("99999999", client)
+ end
+
+ test "a faixa keeps whatever the API sends, even partially", %{server: server, client: client} do
+ body = not_found_fixture(%{"faixa" => %{"localidade" => "Areal", "ceps" => "118"}})
+ FakeServer.respond(server, "/25845000", {404, json(body), []})
+
+ assert {:error, %Cepis.NotFoundError{faixa: faixa}} = Cepis.lookup("25845000", client)
+ assert faixa.localidade == "Areal"
+ assert faixa.ceps == 118
+ assert faixa.cep_ini == ""
+ assert Cepis.Faixa.density(faixa) == 0.0
+ end
+
test "a 404 is never retried", %{server: server, client: client} do
FakeServer.respond(server, "/99999999", {404, json(%{"erro" => "cep_nao_encontrado"}), []})