summaryrefslogtreecommitdiffstats
path: root/tests/test-lookup.R
blob: 0223f4dd5a43682f89b73a186bd0b8a665488274 (plain)
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
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)
  }
)

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"
    )
  }
)