library(cepis) source(file.path("helper", "utils.R")) rows <- c( paste0( "01001000,Pra\u00e7a da S\u00e9,lado \u00edmpar,,S\u00e9,S\u00e3o Paulo,", "SP,S\u00e3o Paulo,Sudeste,3550308,1004,11,7107" ), "99999999,,,,,,,,,,,,", paste0( "20040020,Avenida Rio Branco,,,Centro,Rio de Janeiro,RJ,Rio de Janeiro,", "Sudeste,3304557,,21,6001" ) ) with_server( list(route("/csv?", response(200L, csv(rows)))), function(client, server) { result <- cep_lookup_many(c("01001000", "99999999", "20040020"), client) check("the batch is as long as the input", length(result) == 3L) check("the first CEP resolved", identical(result[[1L]]$localidade, "S\u00e3o Paulo")) check("a missing CEP is NULL at its position", is.null(result[[2L]])) check("the third CEP kept its position", identical(result[[3L]]$uf, "RJ")) check("a batch address carries no coordinate", is.null(result[[3L]]$geo)) check("one request served the whole batch", calls(server, "/csv?") == 1L) } ) quoted <- paste0( '01001000,"Rua Um, Dois",,,Centro,S\u00e3o Paulo,SP,S\u00e3o Paulo,', "Sudeste,1,,11,1" ) with_server( list(route("/csv?", response(200L, csv(quoted)))), function(client, server) { result <- cep_lookup_many("01001000", client) check( "a quoted field containing a comma is parsed as one value", identical(result[[1L]]$logradouro, "Rua Um, Dois") ) } ) with_server( list(route("/csv?", response(200L, csv(character(0L))))), function(client, server) { check("an empty input returns an empty list", identical(cep_lookup_many(character(0L), client), list())) check("and makes no request at all", calls(server) == 0L) } ) many <- sprintf("%08d", seq_len(501L) - 1L) chunk_row <- function(cep) paste0(cep, ",,,,,X,SP,S\u00e3o Paulo,Sudeste,1,,11,1") with_server( list(route( "/csv?", response(200L, csv(vapply(many[1:500], chunk_row, character(1L)))), response(200L, csv(vapply(many[501], chunk_row, character(1L)))) )), function(client, server) { result <- cep_lookup_many(many, client) check("501 CEPs are split into two requests", calls(server, "/csv?") == 2L) check("the result still has one entry per input", length(result) == 501L) check("the last entry belongs to the last CEP", identical(result[[501L]]$cep, "00000500")) } ) with_server( list(route("/csv?", response(200L, csv("01001000,,,,,S\u00e3o Paulo,SP,S\u00e3o Paulo,Sudeste,1,,11,1")))), function(client, server) { result <- cep_lookup_many(c("01001000", "20040020"), client) check("a truncated response is padded, never shifted", length(result) == 2L) check("the missing row became NULL", is.null(result[[2L]])) check("the row that arrived kept its position", identical(result[[1L]]$cep, "01001000")) } )