Skip to content

Commit c73ae62

Browse files
authored
Merge pull request #131 from rOpenSpain/mock-httr2
Use `testthat::local_mocked_bindings()` on API error testing.
2 parents aac62b8 + f7d81fe commit c73ae62

33 files changed

+342
-237
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"better-yaml.flowCollectionPadding": false,
1818
"better-yaml.lineWidth": 80,
1919
"positron.r.pipe": "|>",
20+
"positron.r.useNativePipe": true,
2021
"yaml.completion": true,
2122
"yaml.format.printWidth": 80,
2223
"yaml.validate": true

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# mapSpain (development version)
22

3+
- Use `testthat::local_mocked_bindings()` on API error testing.
4+
35
# mapSpain 1.0.0
46

57
This major release introduces a full overhaul of the codebase and test suite.

R/esp-check-access.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ esp_check_access <- function() {
4242
#' @noRd
4343
skip_if_siane_offline <- function() {
4444
# nocov start
45-
test_offline <- getOption("mapspain_test_404", FALSE)
45+
test_offline <- is_404()
4646
if (test_offline) {
4747
return(invisible(TRUE))
4848
}
@@ -65,7 +65,7 @@ skip_if_siane_offline <- function() {
6565
#' @noRd
6666
skip_if_gisco_offline <- function() {
6767
# nocov start
68-
test_offline <- getOption("gisco_test_404", FALSE)
68+
test_offline <- is_404()
6969
if (test_offline) {
7070
return(invisible(TRUE))
7171
}

R/esp-get-munic.R

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ esp_get_munic <- function(
104104
cache_dir <- create_cache_dir(cache_dir)
105105

106106
if (as.character(year) >= "2011") {
107-
data_sf <- giscoR::gisco_get_lau(
107+
data_sf <- giscor_get_lau(
108108
year = year,
109109
epsg = gisco_epsg,
110110
cache = cache, # Deprecated...
@@ -218,3 +218,10 @@ esp_get_munic <- function(
218218
data_sf <- sanitize_sf(data_sf)
219219
data_sf
220220
}
221+
222+
223+
#' Wrap for mocking
224+
#' @noRd
225+
giscor_get_lau <- function(...) {
226+
giscoR::gisco_get_lau(...)
227+
}

R/esp-get-nuts.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ esp_get_nuts <- function(
156156
"Use {.arg update_cache = TRUE} to re-load from file"
157157
)
158158
} else {
159-
data_sf <- giscoR::gisco_get_nuts(
159+
data_sf <- giscor_get_nuts(
160160
year = year,
161161
epsg = gisco_epsg,
162162
cache = cache,
@@ -210,3 +210,9 @@ esp_get_nuts <- function(
210210
data_sf <- sanitize_sf(data_sf)
211211
data_sf
212212
}
213+
214+
#' Wrapper for mock
215+
#' @noRd
216+
giscor_get_nuts <- function(...) {
217+
giscoR::gisco_get_nuts(...)
218+
}

R/utils-url.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ download_url <- function(
6868
req <- httr2::req_progress(req)
6969
}
7070

71-
test_off <- getOption("mapspain_test_offline", FALSE)
72-
73-
if (any(!httr2::is_online(), test_off)) {
71+
if (!is_online_fun()) {
7472
cli::cli_alert_danger("Offline")
7573
cli::cli_alert("Returning {.val NULL}")
7674
return(NULL)
@@ -92,7 +90,7 @@ download_url <- function(
9290
}
9391

9492
# Testing
95-
test_offline <- getOption("mapspain_test_404", FALSE)
93+
test_offline <- is_404()
9694
if (test_offline) {
9795
# Modify to redirect to fake url
9896
req <- httr2::req_url(
@@ -157,3 +155,15 @@ for_import_jsonlite <- function() {
157155
local <- NULL
158156
invisible(local)
159157
}
158+
159+
#' Wrapper is_online for testing
160+
#' @noRd
161+
is_online_fun <- function(...) {
162+
httr2::is_online()
163+
}
164+
165+
#' Wrapper is_404 for testing
166+
#' @noRd
167+
is_404 <- function(...) {
168+
FALSE
169+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ load it, which speeds up the process.
230230
## Citation
231231

232232
<p>
233+
233234
Hernangómez D (2026). <em>mapSpain: Administrative Boundaries of
234235
Spain</em>.
235236
<a href="https://doi.org/10.5281/zenodo.5366622">doi:10.5281/zenodo.5366622</a>,

codemeta.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
"applicationCategory": "cartography",
297297
"isPartOf": "https://ropenspain.es/",
298298
"keywords": ["ropenspain", "tiles", "r", "maps", "spatial", "rstats", "r-package", "municipalities", "spain", "gisco", "provinces", "ign", "administrative-boundaries", "ccaa", "static-tiles", "cran", "ggplot2", "gis"],
299-
"fileSize": "1813.341KB",
299+
"fileSize": "1811.861KB",
300300
"citation": [
301301
{
302302
"@type": "SoftwareSourceCode",

tests/testthat/test-esp-get-capimun.R

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,36 @@ test_that("Test offline", {
22
skip_on_cran()
33
skip_if_siane_offline()
44

5-
options(mapspain_test_offline = TRUE)
5+
local_mocked_bindings(is_online_fun = function(...) {
6+
FALSE
7+
})
68
expect_message(
79
n <- esp_get_capimun(update_cache = TRUE),
810
"Offline"
911
)
1012
expect_null(n)
1113

12-
options(mapspain_test_offline = FALSE)
14+
local_mocked_bindings(is_online_fun = function(...) {
15+
httr2::is_online()
16+
})
1317
})
1418

1519
test_that("Test 404", {
1620
skip_on_cran()
1721
skip_if_siane_offline()
1822

19-
options(mapspain_test_404 = TRUE)
23+
local_mocked_bindings(is_404 = function(...) {
24+
TRUE
25+
})
2026
expect_message(
2127
n <- esp_get_capimun(update_cache = TRUE),
2228
"Error"
2329
)
2430
expect_null(n)
2531

26-
options(mapspain_test_404 = FALSE)
32+
local_mocked_bindings(is_404 = function(...) {
33+
FALSE
34+
})
2735
})
2836

2937

tests/testthat/test-esp-get-ccaa-siane.R

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,37 @@ test_that("Test offline", {
33
skip_if_siane_offline()
44
skip_if_gisco_offline()
55

6-
options(gisco_test_offline = TRUE)
7-
options(mapspain_test_offline = TRUE)
6+
local_mocked_bindings(is_online_fun = function(...) {
7+
FALSE
8+
})
89
expect_message(
910
n <- esp_get_ccaa_siane(update_cache = TRUE, verbose = FALSE),
1011
"Offline"
1112
)
1213
expect_null(n)
1314

14-
options(mapspain_test_offline = FALSE)
15-
options(gisco_test_offline = FALSE)
15+
local_mocked_bindings(is_online_fun = function(...) {
16+
httr2::is_online()
17+
})
1618
})
1719

1820
test_that("Test 404", {
1921
skip_on_cran()
2022
skip_if_siane_offline()
2123
skip_if_gisco_offline()
2224

23-
options(gisco_test_404 = TRUE)
24-
options(mapspain_test_404 = TRUE)
25+
local_mocked_bindings(is_404 = function(...) {
26+
TRUE
27+
})
2528
expect_message(
2629
n <- esp_get_ccaa_siane(update_cache = TRUE),
2730
"Error"
2831
)
2932
expect_null(n)
3033

31-
options(mapspain_test_404 = FALSE)
32-
options(gisco_test_404 = FALSE)
34+
local_mocked_bindings(is_404 = function(...) {
35+
FALSE
36+
})
3337
})
3438

3539
test_that("Cache vs non-cached", {

0 commit comments

Comments
 (0)