Skip to content

Commit 8c4e805

Browse files
Merge pull request #65 from Teal-Insights/64-resolve-cran-test-failures
64 resolve cran test failures
2 parents 22877ee + eb02e00 commit 8c4e805

File tree

4 files changed

+67
-22
lines changed

4 files changed

+67
-22
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ inst/doc
88
.env
99
docs
1010
/.quarto/
11+
.specstory

DESCRIPTION

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Imports:
2626
dplyr (>= 1.0.0),
2727
purrr (>= 1.0.0),
2828
tidyr (>= 1.0.0),
29-
rlang (>= 1.0.0)
29+
rlang (>= 1.0.0),
30+
mime
3031
Suggests:
3132
curl,
3233
jsonlite,

R/ids_bulk.R

+12-1
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,19 @@ get_response_headers <- function(file_url) {
8585
#' @noRd
8686
#'
8787
download_bulk_file <- function(file_url, file_path, timeout, warn_size, quiet) {
88-
88+
# Error on response mime type mismatch (esp. HTML instead of Excel)
8989
response_headers <- get_response_headers(file_url)
90+
mime_type <- mime::guess_type(file_path)
91+
92+
if (response_headers$`content-type` != mime_type) {
93+
cli::cli_abort(
94+
paste0(
95+
"Request returned an invalid file type. ",
96+
"Please check the URL and try again."
97+
)
98+
)
99+
}
100+
90101
size_mb <- as.numeric(response_headers$`content-length`) / 1024^2
91102
formatted_size <- format(round(size_mb, 1), nsmall = 1) # nolint
92103

tests/testthat/test-ids_bulk.R

+52-20
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ test_that("ids_bulk handles custom file paths", {
77
skip_if_not_installed("jsonlite")
88
skip_if_not_installed("readxl")
99

10-
test_url <- ids_bulk_files()$file_url[1]
10+
test_url <- paste0(
11+
"https://datacatalogfiles.worldbank.org/ddh-published/0038015/DR0092201/",
12+
"A_D.xlsx?versionId=2024-12-04T18:30:10.8890786Z"
13+
)
1114
temp_path <- tempfile(fileext = ".xlsx")
1215

1316
local_mocked_bindings(
@@ -22,7 +25,10 @@ test_that("ids_bulk handles custom file paths", {
2225
tibble::tibble()
2326
},
2427
get_response_headers = function(...) {
25-
list(`content-length` = 1000)
28+
list(
29+
`content-type` = mime::guess_type(temp_path),
30+
`content-length` = 1000
31+
)
2632
}
2733
)
2834

@@ -40,9 +46,17 @@ test_that("ids_bulk handles custom file paths", {
4046
})
4147

4248
test_that("ids_bulk fails gracefully with invalid URL", {
49+
local_mocked_bindings(
50+
get_response_headers = function(file_url) {
51+
list(
52+
`content-type` = "text/html; charset=utf-8"
53+
)
54+
}
55+
)
56+
4357
expect_error(
4458
ids_bulk("https://invalid-url.com/file.xlsx"),
45-
"cannot open URL|download failed|Could not resolve host|SSL certificate problem" # nolint
59+
"Request returned an invalid file type. Please check the URL and try again."
4660
)
4761
})
4862

@@ -63,7 +77,10 @@ test_that("ids_bulk handles message parameter correctly", {
6377
skip_if_not_installed("jsonlite")
6478
skip_if_not_installed("readxl")
6579

66-
test_url <- ids_bulk_files()$file_url[1]
80+
test_url <- paste0(
81+
"https://datacatalogfiles.worldbank.org/ddh-published/0038015/DR0092201/",
82+
"A_D.xlsx?versionId=2024-12-04T18:30:10.8890786Z"
83+
)
6784

6885
mock_data <- tibble::tibble(
6986
"Country Code" = "ABC",
@@ -123,7 +140,12 @@ test_that("ids_bulk handles timeout parameter correctly", {
123140
}
124141
stop("Unexpected timeout value", call. = FALSE)
125142
},
126-
get_response_headers = function(...) list("content-length" = "1000")
143+
get_response_headers = function(...) {
144+
list(
145+
`content-type` = mime::guess_type("file.xlsx"),
146+
`content-length` = "1000"
147+
)
148+
}
127149
)
128150

129151
expect_error(
@@ -138,31 +160,27 @@ test_that("ids_bulk handles warn_size parameter", {
138160
skip_if_not_installed("jsonlite")
139161
skip_if_not_installed("readxl")
140162

141-
test_url <- ids_bulk_files()$file_url[1]
142-
143-
local_mocked_bindings(
144-
download_file = function(...) TRUE
145-
)
146-
147-
local_mocked_bindings(
148-
validate_file = function(...) TRUE
163+
test_url <- paste0(
164+
"https://datacatalogfiles.worldbank.org/ddh-published/0038015/DR0092201/",
165+
"A_D.xlsx?versionId=2024-12-04T18:30:10.8890786Z"
149166
)
150167

151168
local_mocked_bindings(
169+
download_file = function(...) TRUE,
170+
validate_file = function(...) TRUE,
152171
check_interactive = function() FALSE
153172
)
154173

155174
expect_warning(
156175
download_bulk_file(
157-
test_url, tempfile(), 60, warn_size = TRUE, quiet = TRUE
176+
test_url, tempfile(fileext = ".xlsx"), 60, warn_size = TRUE, quiet = TRUE
158177
),
159-
"may take several minutes to download",
160-
fixed = FALSE
178+
"may take several minutes to download"
161179
)
162180

163181
expect_no_warning(
164182
download_bulk_file(
165-
test_url, tempfile(), 60, warn_size = FALSE, quiet = TRUE
183+
test_url, tempfile(fileext = ".xlsx"), 60, warn_size = FALSE, quiet = TRUE
166184
)
167185
)
168186
})
@@ -186,7 +204,10 @@ test_that("download_bulk_file downloads files correctly", {
186204
skip_if_offline()
187205
skip_on_cran()
188206

189-
test_url <- ids_bulk_files()$file_url[1]
207+
test_url <- paste0(
208+
"https://datacatalogfiles.worldbank.org/ddh-published/0038015/DR0092201/",
209+
"A_D.xlsx?versionId=2024-12-04T18:30:10.8890786Z"
210+
)
190211
test_path <- tempfile(fileext = ".xlsx")
191212

192213
local_mocked_bindings(
@@ -325,15 +346,26 @@ test_that("warn_size warning is triggered & user prompt is handled correctly", {
325346
skip_if_not_installed("jsonlite")
326347
skip_if_not_installed("readxl")
327348

349+
test_url <- paste0(
350+
"https://datacatalogfiles.worldbank.org/ddh-published/0038015/DR0092201/",
351+
"A_D.xlsx?versionId=2024-12-04T18:30:10.8890786Z"
352+
)
353+
temp_file <- tempfile(fileext = ".xlsx")
354+
328355
with_mocked_bindings(
329-
get_response_headers = function(...) list(`content-length` = 150 * 1024^2),
356+
get_response_headers = function(...) {
357+
list(
358+
`content-type` = mime::guess_type(temp_file),
359+
`content-length` = 150 * 1024^2
360+
)
361+
},
330362
check_interactive = function(...) TRUE,
331363
prompt_user = function(...) "n",
332364
{
333365
expect_error(
334366
expect_warning(
335367
download_bulk_file(
336-
test_url, tempfile(fileext = ".xlsx"),
368+
test_url, temp_file,
337369
timeout = 30, warn_size = TRUE, quiet = TRUE
338370
),
339371
regexp = "may take several minutes to download."

0 commit comments

Comments
 (0)