Skip to content

Commit

Permalink
Adjust messages and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Jul 3, 2023
1 parent ad7c8c9 commit 756cf9f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Suggests:
VignetteBuilder:
knitr
Config/Needs/website: ggplot2, tmap, styler, sessioninfo,
ropengov/rogtemplate
ropengov/rogtemplate, ragg
Config/testthat/edition: 3
Config/testthat/parallel: false
Encoding: UTF-8
Expand Down
22 changes: 15 additions & 7 deletions R/get_eurostat_geospatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,33 @@ get_eurostat_geospatial <- function(output_class = "sf",
# Check if package "giscoR" is installed
# nocov start
if (!requireNamespace("giscoR")) {
message("'giscoR' package is required for geospatial functionalities")
return(invisible())
}

if (!requireNamespace("sf")) {
message("'sf' package is required for geospatial functionalities")
return(invisible())
}
# nocov end

message("Extracting data using giscoR package")
message(paste0(
"Extracting data using giscoR package, please report issues",
" on https://github.com/rOpenGov/giscoR/issues"
))

# Manage cache: Priority is eurostat cache (if set)
# If not make use of giscoR default options
detect_eurostat_cache <- eur_helper_detect_cache_dir()
if (!is.null(cache_dir)) {
# Already set by the user, no need message
cache_dir <- eur_helper_cachedir(cache_dir)
} else if (identical(detect_eurostat_cache, file.path(tempdir(), "eurostat"))) {
} else if (identical(
detect_eurostat_cache,
file.path(tempdir(), "eurostat")
)) {
# eurostat not set, using default giscoR cache management
message("Cache management as per giscoR. see 'giscoR::gisco_get_nuts()")
message("Cache management as per giscoR. see 'giscoR::gisco_get_nuts()'")
} else {
cache_dir <- eur_helper_cachedir(cache_dir)
}
Expand All @@ -241,20 +252,17 @@ get_eurostat_geospatial <- function(output_class = "sf",

# Just to capture potential NULL outputs from giscoR - this happen
# on some errors
# nocov start
if (is.null(shp)) {
return(NULL)
}
# nocov end

# Post-data treatments

# Adding a `geo` column for easier joins with dplyr
shp$geo <- shp$NUTS_ID
# to df
if (output_class == "df") {
# Convert yl df
sf_col <- attr(shp, "sf_column")
# Remove geometry
shp <- sf::st_drop_geometry(shp)
}

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/_snaps/03_get_eurostat_geospatial.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
Code
a <- get_eurostat_geospatial(nuts_id = "LU", nuts_level = 0)
Message <simpleMessage>
Extracting data using giscoR package
Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues

---

Code
a <- get_eurostat_geospatial(nuts_id = "LU", nuts_level = 0, cache_dir = another_temp)
Message <simpleMessage>
Extracting data using giscoR package
Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues

---

Code
a <- get_eurostat_geospatial(nuts_id = "LU", nuts_level = 0)
Message <simpleMessage>
Extracting data using giscoR package
Cache management as per giscoR. see 'giscoR::gisco_get_nuts()
Extracting data using giscoR package, please report issues on https://github.com/rOpenGov/giscoR/issues
Cache management as per giscoR. see 'giscoR::gisco_get_nuts()'

20 changes: 20 additions & 0 deletions tests/testthat/test_03_get_eurostat_geospatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,23 @@ test_that("get_eurostat_geospatial cache_dir", {
suppressMessages(set_eurostat_cache_dir(init_cache))
expect_identical(init_cache, eur_helper_detect_cache_dir())
})


test_that("giscoR returns NULL", {
skip_if_not_installed(pkg = "giscoR")
skip_on_cran()
skip_if_offline()
skip_if(!giscoR::gisco_check_access(), "No access to GISCO")
skip_if(packageVersion("giscoR") < "0.3.5", "Use latest giscoR release")

options(giscoR_test_offline = TRUE)
expect_message(
n <- get_eurostat_geospatial(
country = "AT", nuts_level = "0",
update_cache = TRUE
),
"not reachable"
)
expect_null(n)
options(giscoR_test_offline = FALSE)
})

0 comments on commit 756cf9f

Please sign in to comment.