diff options
Diffstat (limited to 'test/cepis_test.rb')
| -rw-r--r-- | test/cepis_test.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/test/cepis_test.rb b/test/cepis_test.rb index 06a6bf6..6fd3353 100644 --- a/test/cepis_test.rb +++ b/test/cepis_test.rb @@ -91,6 +91,67 @@ class LookupTest < ServerTest assert_includes error.message, "99999999" end + def test_not_found_carries_the_band + body = { "erro" => "cep_nao_encontrado", "mensagem" => "CEP não existe na base do DNE", + "faixa" => AREAL_FAIXA } + routes.respond("/25845000", [404, JSON.generate(body), {}]) + error = assert_raises(Cepis::CEPNotFound) { @client.lookup("25845000") } + + assert_equal "Areal", error.faixa.localidade + assert_equal "RJ", error.faixa.uf + assert_equal "3300225", error.faixa.ibge + assert_equal 118, error.faixa.ceps + # The band starts above the CEP that just failed: cep_ini is the smallest + # CEP that exists, not the DNE's published start, which is 25845000. + assert_equal "25845001", error.faixa.cep_ini + assert_equal "25849899", error.faixa.cep_fim + end + + def test_the_band_names_the_municipality_in_the_message + body = { "erro" => "cep_nao_encontrado", "faixa" => AREAL_FAIXA } + routes.respond("/25845000", [404, JSON.generate(body), {}]) + error = assert_raises(Cepis::CEPNotFound) { @client.lookup("25845000") } + + assert_includes error.message, "25845000" + assert_includes error.message, "Areal/RJ" + end + + def test_band_is_absent_when_no_municipality_owns_the_numbers + # The API omits `faixa` in that case — it never sends null. + routes.respond( + "/99999999", + [404, JSON.generate({ "erro" => "cep_nao_encontrado", "mensagem" => "gone" }), {}] + ) + error = assert_raises(Cepis::CEPNotFound) { @client.lookup("99999999") } + + assert_nil error.faixa + end + + def test_malformed_band_does_not_break_the_not_found + ["Areal", {}, { "uf" => "RJ" }, { "localidade" => "" }, [], 7].each do |faixa| + body = { "erro" => "cep_nao_encontrado", "faixa" => faixa } + routes.respond("/99999999", [404, JSON.generate(body), {}]) + error = assert_raises(Cepis::CEPNotFound) { @client.lookup("99999999") } + + assert_nil error.faixa, "expected #{faixa.inspect} to be ignored" + assert_includes error.message, "99999999" + end + end + + def test_unreadable_404_body_is_still_a_clean_not_found + routes.respond("/99999999", [404, "<html>captive portal</html>", {}]) + error = assert_raises(Cepis::CEPNotFound) { @client.lookup("99999999") } + + assert_nil error.faixa + end + + def test_band_never_turns_a_not_found_into_an_address + body = { "erro" => "cep_nao_encontrado", "faixa" => AREAL_FAIXA } + routes.respond("/25845000", [404, JSON.generate(body), {}]) + + assert_raises(Cepis::CEPNotFound) { @client.lookup("25845000") } + end + def test_not_found_is_not_retried routes.respond("/99999999", [404, JSON.generate({ "erro" => "x" }), {}]) assert_raises(Cepis::CEPNotFound) { @client.lookup("99999999") } |