summaryrefslogtreecommitdiffstats
path: root/tests/helper/utils.R
blob: 94a15fffb911b7906d0332b896fb76642aca8862 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
SERVER_SCRIPT <- normalizePath(file.path("helper", "server.R"), mustWork = TRUE)

SE_ADDRESS <- list(
  cep = "01001000",
  logradouro = "Pra\u00e7a da S\u00e9",
  complemento = "lado \u00edmpar",
  unidade = "",
  bairro = "S\u00e9",
  localidade = "S\u00e3o Paulo",
  uf = "SP",
  estado = "S\u00e3o Paulo",
  regiao = "Sudeste",
  ibge = "3550308",
  gia = "1004",
  ddd = "11",
  siafi = "7107"
)

AREAL_FAIXA <- list(
  localidade = "Areal",
  uf = "RJ",
  estado = "Rio de Janeiro",
  regiao = "Sudeste",
  ibge = "3300225",
  cep_ini = "25845001",
  cep_fim = "25849899",
  ceps = 118L
)

CSV_HEADER <- paste0(
  "cep,logradouro,complemento,unidade,bairro,localidade,uf,estado,regiao,",
  "ibge,gia,ddd,siafi"
)

json <- function(x) {
  as.character(jsonlite::toJSON(x, auto_unbox = TRUE))
}

csv <- function(rows) {
  paste0(CSV_HEADER, "\n", paste(rows, collapse = "\n"), "\n")
}

response <- function(status, body, headers = list()) {
  list(status = status, body = body, headers = headers)
}

route <- function(match, ...) {
  list(match = match, responses = list(...))
}

start_server <- function(routes) {
  directory <- tempfile("cepis-fake-server-")
  dir.create(directory)
  routes_path <- file.path(directory, "routes.rds")
  saveRDS(routes, routes_path)
  logfile <- file.path(directory, "requests.log")
  file.create(logfile)
  readyfile <- file.path(directory, "ready")

  system2(
    file.path(R.home("bin"), "Rscript"),
    shQuote(c(SERVER_SCRIPT, routes_path, logfile, readyfile)),
    wait = FALSE,
    stdout = file.path(directory, "stdout"),
    stderr = file.path(directory, "stderr")
  )

  deadline <- Sys.time() + 30
  while (!file.exists(readyfile) && Sys.time() < deadline) {
    Sys.sleep(0.02)
  }
  if (!file.exists(readyfile)) {
    stop(
      "the fake server did not start: ",
      paste(readLines(file.path(directory, "stderr"), warn = FALSE),
            collapse = " ")
    )
  }

  port <- readLines(readyfile, warn = FALSE)[[1L]]
  list(
    url = paste0("http://127.0.0.1:", port),
    directory = directory,
    logfile = logfile
  )
}

stop_server <- function(server) {
  try(
    curl::curl_fetch_memory(paste0(server$url, "/__shutdown__")),
    silent = TRUE
  )
  unlink(server$directory, recursive = TRUE)
}

calls <- function(server, prefix = "") {
  lines <- readLines(server$logfile, warn = FALSE)
  sum(startsWith(lines, prefix))
}

with_server <- function(routes, body) {
  server <- start_server(routes)
  on.exit(stop_server(server), add = TRUE)
  client <- cepis_client(
    base_url = server$url,
    timeout = 5,
    backoff_base = 0.001,
    backoff_max = 0.01
  )
  body(client, server)
}

caught <- function(expr) {
  tryCatch(
    {
      force(expr)
      NULL
    },
    error = function(condition) condition
  )
}

check <- function(label, condition) {
  if (!isTRUE(condition)) {
    stop("FAIL: ", label, call. = FALSE)
  }
  cat("ok - ", label, "\n", sep = "")
  invisible(TRUE)
}

check_condition <- function(label, expr, class) {
  condition <- caught(expr)
  check(
    label,
    inherits(condition, class) && inherits(condition, "cepis_error")
  )
  invisible(condition)
}