summaryrefslogtreecommitdiffstats
path: root/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'src/test')
-rw-r--r--src/test/kotlin/org/cepis/LookupTest.kt61
-rw-r--r--src/test/kotlin/org/cepis/ServerTest.kt13
2 files changed, 74 insertions, 0 deletions
diff --git a/src/test/kotlin/org/cepis/LookupTest.kt b/src/test/kotlin/org/cepis/LookupTest.kt
index c266025..e106f70 100644
--- a/src/test/kotlin/org/cepis/LookupTest.kt
+++ b/src/test/kotlin/org/cepis/LookupTest.kt
@@ -79,6 +79,67 @@ internal class LookupTest : ServerTest() {
}
@Test
+ fun `not found names the municipality the cep would belong to`() {
+ server.respond("/25845000", Reply(404, Fixtures.NOT_FOUND_WITH_FAIXA_JSON))
+ val error = assertFailsWith<CepNotFoundException> { client.lookup("25845-000") }
+ val faixa = assertNotNull(error.faixa)
+
+ assertEquals("Areal", faixa.localidade)
+ assertEquals("RJ", faixa.uf)
+ assertEquals("Rio de Janeiro", faixa.estado)
+ assertEquals("Sudeste", faixa.regiao)
+ assertEquals("3300225", faixa.ibge)
+ assertEquals("25845001", faixa.cepIni)
+ assertEquals("25849899", faixa.cepFim)
+ assertEquals(118, faixa.ceps)
+ assertTrue(error.message!!.contains("Areal"))
+ }
+
+ @Test
+ fun `not found without a faixa is still a clean not found`() {
+ server.respond("/99999999", Reply(404, Fixtures.NOT_FOUND_JSON))
+ val error = assertFailsWith<CepNotFoundException> { client.lookup("99999999") }
+
+ assertNull(error.faixa)
+ assertTrue(error.message!!.contains("99999999"))
+ }
+
+ @Test
+ fun `an unusable faixa does not cost the not found`() {
+ val bodies =
+ listOf(
+ """{"erro":"cep_nao_encontrado","faixa":null}""",
+ """{"erro":"cep_nao_encontrado","faixa":{}}""",
+ """{"erro":"cep_nao_encontrado","faixa":"Areal"}""",
+ """{"erro":"cep_nao_encontrado","faixa":{"uf":"RJ"}}""",
+ """{"erro":"cep_nao_encontrado","faixa":""",
+ "<html>not found</html>",
+ "",
+ )
+ for (body in bodies) {
+ server.respond("/99999999", Reply(404, body))
+ val error = assertFailsWith<CepNotFoundException>(body) { client.lookup("99999999") }
+
+ assertNull(error.faixa, body)
+ }
+ }
+
+ @Test
+ fun `a faixa missing a field keeps the rest`() {
+ val body =
+ """{"erro":"cep_nao_encontrado","faixa":{"localidade":"Areal","uf":"RJ"}}"""
+ server.respond("/25845000", Reply(404, body))
+ val faixa =
+ assertNotNull(
+ assertFailsWith<CepNotFoundException> { client.lookup("25845000") }.faixa,
+ )
+
+ assertEquals("Areal", faixa.localidade)
+ assertEquals("", faixa.cepIni)
+ assertEquals(0, faixa.ceps)
+ }
+
+ @Test
fun `not found is not retried`() {
server.respond("/99999999", Reply(404, """{"erro":"x"}"""))
assertFailsWith<CepNotFoundException> { client.lookup("99999999") }
diff --git a/src/test/kotlin/org/cepis/ServerTest.kt b/src/test/kotlin/org/cepis/ServerTest.kt
index b22975a..8175190 100644
--- a/src/test/kotlin/org/cepis/ServerTest.kt
+++ b/src/test/kotlin/org/cepis/ServerTest.kt
@@ -40,6 +40,19 @@ internal object Fixtures {
"ddd":"11","siafi":"7107"}
"""
+ /** A 404 body carrying the municipality the CEP would have belonged to. */
+ const val NOT_FOUND_WITH_FAIXA_JSON = """
+ {"erro":"cep_nao_encontrado","mensagem":"CEP não existe na base do DNE",
+ "faixa":{"localidade":"Areal","uf":"RJ","estado":"Rio de Janeiro",
+ "regiao":"Sudeste","ibge":"3300225","cep_ini":"25845001",
+ "cep_fim":"25849899","ceps":118}}
+ """
+
+ /** A 404 for a number in a band no municipality owns: `faixa` is absent. */
+ const val NOT_FOUND_JSON = """
+ {"erro":"cep_nao_encontrado","mensagem":"CEP não existe na base do DNE"}
+ """
+
const val CSV_HEADER =
"cep,logradouro,complemento,unidade,bairro,localidade,uf,estado," +
"regiao,ibge,gia,ddd,siafi"