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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
library(cepis)
source(file.path("helper", "utils.R"))
with_server(
list(route("/01001000", response(200L, json(SE_ADDRESS)))),
function(client, server) {
address <- cep_lookup("01001-000", client)
check("lookup reads the address", identical(address$localidade, "S\u00e3o Paulo"))
check("lookup reads the state", identical(address$uf, "SP"))
check("lookup keeps the CEP unmasked", identical(address$cep, "01001000"))
check("formatted_cep masks the address", identical(formatted_cep(address), "01001-000"))
check("an address without geo has geo NULL", is.null(address$geo))
check("the address is a cepis_address", inherits(address, "cepis_address"))
}
)
precise <- c(SE_ADDRESS, list(geo = list(
lat = -23.5, lon = -46.6, raio_m = 120L, precisao = "logradouro"
)))
with_server(
list(route("/01310100", response(200L, json(precise)))),
function(client, server) {
address <- cep_lookup("01310100", client)
check("the coordinate is read", isTRUE(all.equal(address$geo$lat, -23.5)))
check("raio_m is an integer", identical(address$geo$raio_m, 120L))
check("a street coordinate is precise", isTRUE(address$geo$is_precise))
}
)
municipality <- c(SE_ADDRESS, list(geo = list(
lat = -23.5, lon = -46.6, raio_m = 22000L, precisao = "localidade"
)))
with_server(
list(route("/01001000", response(200L, json(municipality)))),
function(client, server) {
address <- cep_lookup("01001000", client)
check(
"a municipality coordinate is not precise",
isFALSE(address$geo$is_precise)
)
}
)
broken <- c(SE_ADDRESS, list(geo = list(lat = "not a number")))
with_server(
list(route("/01001000", response(200L, json(broken)))),
function(client, server) {
address <- cep_lookup("01001000", client)
check("a malformed geo is dropped", is.null(address$geo))
check(
"a malformed geo does not lose the address",
identical(address$localidade, "S\u00e3o Paulo")
)
}
)
grown <- c(SE_ADDRESS, list(campo_do_futuro = "value"))
with_server(
list(route("/01001000", response(200L, json(grown)))),
function(client, server) {
address <- cep_lookup("01001000", client)
check(
"an unknown field is kept in raw",
identical(address$raw$campo_do_futuro, "value")
)
}
)
with_server(
list(route(
"/99999999",
response(404L, json(list(erro = "cep_nao_encontrado", mensagem = "gone")))
)),
function(client, server) {
condition <- check_condition(
"a missing CEP signals cepis_not_found",
cep_lookup("99999999", client),
"cepis_not_found"
)
check("the condition carries the CEP", identical(condition$cep, "99999999"))
check("404 is not retried, it is a definitive answer", calls(server, "/99999999") == 1L)
}
)
not_found_body <- function(faixa = NULL) {
body <- list(
erro = "cep_nao_encontrado",
mensagem = "CEP n\u00e3o existe na base do DNE"
)
if (!is.null(faixa)) {
body$faixa <- faixa
}
json(body)
}
# 25845000 is the DNE's own first number for Areal, and one of the 957 that
# were never issued: the guess the faixa exists to answer.
with_server(
list(route("/25845000", response(404L, not_found_body(AREAL_FAIXA)))),
function(client, server) {
condition <- check_condition(
"a CEP inside a municipality's range is still not found",
cep_lookup("25845000", client),
"cepis_not_found"
)
faixa <- condition$faixa
check("the condition carries the faixa", inherits(faixa, "cepis_faixa"))
check("the faixa names the municipality", identical(faixa$localidade, "Areal"))
check("the faixa carries the state", identical(faixa$uf, "RJ"))
check("the faixa carries the IBGE code", identical(faixa$ibge, "3300225"))
check("the faixa counts the CEPs that exist", identical(faixa$ceps, 118L))
check(
"the bounds are the CEPs that exist, unmasked",
identical(c(faixa$cep_ini, faixa$cep_fim), c("25845001", "25849899"))
)
check(
"the message names the municipality",
grepl("Areal/RJ", conditionMessage(condition), fixed = TRUE)
)
check("the faixa prints", length(capture.output(print(faixa))) == 2L)
check("a 404 with a faixa is not retried either", calls(server, "/25845000") == 1L)
}
)
with_server(
list(route("/99999999", response(404L, not_found_body()))),
function(client, server) {
condition <- check_condition(
"a CEP in a band nobody owns is not found too",
cep_lookup("99999999", client),
"cepis_not_found"
)
check("an omitted faixa is NULL", is.null(condition$faixa))
check("the CEP is still carried", identical(condition$cep, "99999999"))
check(
"the message claims no range",
!grepl("belongs to", conditionMessage(condition), fixed = TRUE)
)
}
)
# A faixa is decoration on an error. None of these may cost the caller the
# error itself, which is the only thing it asked for.
unusable <- list(
"a null faixa" = "{\"erro\":\"x\",\"faixa\":null}",
"an empty faixa" = "{\"erro\":\"x\",\"faixa\":{}}",
"a faixa that is an array" = "{\"erro\":\"x\",\"faixa\":[]}",
"a faixa that is a string" = "{\"erro\":\"x\",\"faixa\":\"Areal\"}",
"a faixa with no municipality" = "{\"erro\":\"x\",\"faixa\":{\"uf\":\"RJ\"}}",
"a faixa with an empty municipality" =
"{\"erro\":\"x\",\"faixa\":{\"localidade\":\"\",\"uf\":\"RJ\"}}",
"a truncated body" = "{\"erro\":\"cep_nao",
"a body that is not JSON at all" = "<html>captive portal</html>",
"an empty body" = ""
)
for (label in names(unusable)) {
with_server(
list(route("/99999999", response(404L, unusable[[label]]))),
function(client, server) {
condition <- check_condition(
paste0(label, " is still a clean cepis_not_found"),
cep_lookup("99999999", client),
"cepis_not_found"
)
check(paste0(label, " yields no faixa"), is.null(condition$faixa))
}
)
}
odd_count <- c(AREAL_FAIXA[names(AREAL_FAIXA) != "ceps"], list(ceps = "many"))
with_server(
list(route("/25845000", response(404L, not_found_body(odd_count)))),
function(client, server) {
condition <- caught(cep_lookup("25845000", client))
check(
"an unreadable count does not lose the municipality",
identical(condition$faixa$localidade, "Areal")
)
check("an unreadable count reads as zero", identical(condition$faixa$ceps, 0L))
}
)
with_server(
list(route("/01001000", response(200L, json(SE_ADDRESS)))),
function(client, server) {
check_condition(
"a malformed CEP never leaves the machine",
cep_lookup("123", client),
"cepis_invalid_cep"
)
check("no request was made for it", calls(server) == 0L)
}
)
with_server(
list(route(
"/00000000",
response(400L, json(list(erro = "cep_invalido", mensagem = "rejected by the API")))
)),
function(client, server) {
condition <- check_condition(
"a 400 becomes cepis_invalid_cep",
cep_lookup("00000000", client),
"cepis_invalid_cep"
)
check(
"it carries the message the API sent",
grepl("rejected by the API", conditionMessage(condition), fixed = TRUE)
)
}
)
with_server(
list(route("/01001000", response(200L, "<html>captive portal</html>"))),
function(client, server) {
check_condition(
"a body that is not JSON signals cepis_invalid_response",
cep_lookup("01001000", client),
"cepis_invalid_response"
)
}
)
|