Skip to content

Commit

Permalink
Skip tests, rendering if offline (#39)
Browse files Browse the repository at this point in the history
* Skip tests, rendering if offline

* Fix check
  • Loading branch information
mikemahoney218 authored Feb 12, 2024
1 parent 25e2dd2 commit faf89e3
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
6 changes: 1 addition & 5 deletions tests/testthat/_snaps/spectral_indices.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# no cache, update false

Code
spectral_indices(update_cache = FALSE)
Condition
Warning:
No cache file present and `download_indices` set to `FALSE`.
i Returning (likely outdated) package data instead.
suppressWarnings(spectral_indices(update_cache = FALSE))
Output
# A tibble: 231 x 9
application_domain bands contributor date_of_addition formula long_name
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-calculate_indices.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_that("Index calculation is stable", {
expect_no_error(
out <- calculate_indices(
system.file("rasters/example_sentinel1.tif", package = "rsi"),
filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)"),
suppressWarnings(filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)")),
index_out,
names_suffix = "sentinel1"
)
Expand All @@ -27,7 +27,7 @@ test_that("Index calculations fail when missing a column", {
expect_error(
calculate_indices(
system.file("rasters/example_sentinel1.tif", package = "rsi"),
filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)")["formula"],
suppressWarnings(filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)"))["formula"],
index_out
),
class = "rsi_missing_column"
Expand All @@ -40,7 +40,7 @@ test_that("Index calculations fail when missing bands", {
expect_error(
calculate_indices(
system.file("rasters/example_sentinel1.tif", package = "rsi"),
filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Landsat-OLI"),
suppressWarnings(filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Landsat-OLI")),
index_out
),
class = "rsi_missing_indices"
Expand All @@ -49,7 +49,7 @@ test_that("Index calculations fail when missing bands", {

test_that("Index calculations stop obvious security issues", {
skip_on_cran()
example_indices <- filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)")[1, ]
example_indices <- suppressWarnings(filter_platforms(spectral_indices(download_indices = FALSE, update_cache = FALSE), platforms = "Sentinel-1 (Dual Polarisation VV-VH)"))[1, ]
example_indices$formula <- 'system("echo something bad")'
expect_error(calculate_indices(
system.file("rasters/example_sentinel1.tif", package = "rsi"),
Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test-filters.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ test_that("filter_platforms() works", {
test_plats <- c("Landsat-OLI", "Sentinel-2")

expect_no_error(
both_plats <- filter_platforms(idxs, test_plats)
# no internet and no cache file == warning
both_plats <- suppressWarnings(filter_platforms(idxs, test_plats), classes = "rsi_failed_download")
)

expect_true(
Expand All @@ -18,7 +19,7 @@ test_that("filter_platforms() works", {
)

expect_no_error(
either_plat <- filter_platforms(idxs, test_plats, operand = "any")
either_plat <- suppressWarnings(filter_platforms(idxs, test_plats, operand = "any"), classes = "rsi_failed_download")
)

expect_true(
Expand All @@ -38,7 +39,7 @@ test_that("filter_bands() works", {
test_bands <- c("R", "N")

expect_no_error(
both_bands <- filter_bands(idxs, test_bands)
both_bands <- suppressWarnings(filter_bands(idxs, test_bands), classes = "rsi_failed_download")
)

expect_true(
Expand All @@ -48,7 +49,7 @@ test_that("filter_bands() works", {
)

expect_no_error(
either_band <- filter_bands(idxs, test_bands, operand = "any")
either_band <- suppressWarnings(filter_bands(idxs, test_bands, operand = "any"), classes = "rsi_failed_download")
)

expect_true(
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-get_stac_data.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_that("get_landsat_imagery() is stable", {
skip_on_cran()
skip_if_offline()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100)
Expand All @@ -23,6 +24,7 @@ test_that("get_landsat_imagery() is stable", {

test_that("get_sentinel1_imagery() is stable", {
skip_on_cran()
skip_if_offline()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100)
Expand All @@ -46,6 +48,7 @@ test_that("get_sentinel1_imagery() is stable", {

test_that("get_sentinel2_imagery() is stable", {
skip_on_cran()
skip_if_offline()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100)
Expand All @@ -70,6 +73,7 @@ test_that("get_sentinel2_imagery() is stable", {

test_that("get_dem() is stable", {
skip_on_cran()
skip_if_offline()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100)
Expand All @@ -91,6 +95,7 @@ test_that("get_dem() is stable", {

test_that("non-default mappings work", {
skip_on_cran()
skip_if_offline()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100)
Expand All @@ -116,6 +121,7 @@ test_that("non-default mappings work", {
test_that("can download RTC products", {
skip_if(Sys.getenv("rsi_pc_key") == "", "Environment variable `rsi_pc_key` not set")
skip_on_cran()
skip_if_offline()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100)
Expand All @@ -140,6 +146,7 @@ test_that("can download RTC products", {

test_that("hidden arguments work", {
skip_on_cran()
skip_if_offline()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100)
Expand All @@ -165,6 +172,7 @@ test_that("hidden arguments work", {

test_that("simple merge method works", {
skip_on_cran()
skip_if_offline()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100)
Expand All @@ -187,6 +195,7 @@ test_that("simple merge method works", {

test_that("warning (but not error) fires if `mask_band` is not NULL with NULL `mask_function`", {
skip_on_cran()
skip_if_offline()
aoi <- sf::st_point(c(-74.912131, 44.080410))
aoi <- sf::st_set_crs(sf::st_sfc(aoi), 4326)
aoi <- sf::st_buffer(sf::st_transform(aoi, 3857), 100)
Expand All @@ -205,6 +214,7 @@ test_that("warning (but not error) fires if `mask_band` is not NULL with NULL `m

test_that("get_*_data works with mapply() (#17)", {
skip_on_cran()
skip_if_offline()
san_antonio = sf::st_point(c(-98.491142, 29.424349))
san_antonio = sf::st_sfc(san_antonio, crs = "EPSG:4326")
san_antonio = sf::st_buffer(sf::st_transform(san_antonio, "EPSG:3081"), 100)
Expand Down
23 changes: 22 additions & 1 deletion tests/testthat/test-spectral_indices.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test_that("spectral_indices() works", {

# Second, if we're online, we should be able to download the current indices:
skip_if_offline()
skip_on_cran()
expect_no_warning(spectral_indices(update_cache = FALSE, download_indices = TRUE))

# Third, if we're online and can write to the cache folder,
Expand Down Expand Up @@ -48,15 +49,18 @@ test_that("spectral_indices_url() respects environment variables", {

test_that("no cache", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
if (file.exists(indices_path)) file.remove(indices_path)
expect_no_warning(spectral_indices())
})

test_that("no cache, update false", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
if (file.exists(indices_path)) file.remove(indices_path)
expect_snapshot(spectral_indices(update_cache = FALSE))
expect_snapshot(suppressWarnings(spectral_indices(update_cache = FALSE)))
})

test_that("no cache, download false", {
Expand All @@ -73,69 +77,80 @@ test_that("no cache, download and update false", {

test_that("no cache, update true", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
if (file.exists(indices_path)) file.remove(indices_path)
expect_no_warning(spectral_indices(update_cache = TRUE))
})

test_that("no cache, download true", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
if (file.exists(indices_path)) file.remove(indices_path)
expect_no_warning(spectral_indices(download_indices = TRUE))
})

test_that("no cache, download and update true", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
if (file.exists(indices_path)) file.remove(indices_path)
expect_no_warning(spectral_indices(download_indices = TRUE, update_cache = TRUE))
})

test_that("new cache", {
skip_if_offline()
skip_on_cran()
invisible(spectral_indices(update_cache = TRUE))
expect_no_warning(spectral_indices())
})

test_that("new cache, update false", {
skip_if_offline()
skip_on_cran()
invisible(spectral_indices(update_cache = TRUE))
expect_no_warning(spectral_indices(update_cache = FALSE))
})

test_that("new cache, download false", {
skip_if_offline()
skip_on_cran()
invisible(spectral_indices(update_cache = TRUE))
expect_no_warning(spectral_indices(download_indices = FALSE))
})

test_that("new cache, download and update false", {
skip_if_offline()
skip_on_cran()
invisible(spectral_indices(update_cache = TRUE))
expect_no_warning(spectral_indices(download_indices = FALSE, update_cache = FALSE))
})

test_that("new cache, update true", {
skip_if_offline()
skip_on_cran()
invisible(spectral_indices(update_cache = TRUE))
expect_no_warning(spectral_indices(update_cache = TRUE))
})

test_that("new cache, download true", {
skip_if_offline()
skip_on_cran()
invisible(spectral_indices(update_cache = TRUE))
expect_no_warning(spectral_indices(download_indices = TRUE))
})

test_that("new cache, download and update true", {
skip_if_offline()
skip_on_cran()
invisible(spectral_indices(update_cache = TRUE))
expect_no_warning(spectral_indices(download_indices = TRUE, update_cache = TRUE))
})

test_that("old cache", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
invisible(spectral_indices(update_cache = TRUE))
Sys.setFileTime(indices_path, "1970-01-01")
Expand All @@ -144,6 +159,7 @@ test_that("old cache", {

test_that("old cache, update false", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
invisible(spectral_indices(update_cache = TRUE))
Sys.setFileTime(indices_path, "1970-01-01")
Expand All @@ -152,6 +168,7 @@ test_that("old cache, update false", {

test_that("old cache, download false", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
invisible(spectral_indices(update_cache = TRUE))
Sys.setFileTime(indices_path, "1970-01-01")
Expand All @@ -160,6 +177,7 @@ test_that("old cache, download false", {

test_that("old cache, download and update false", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
invisible(spectral_indices(update_cache = TRUE))
Sys.setFileTime(indices_path, "1970-01-01")
Expand All @@ -168,6 +186,7 @@ test_that("old cache, download and update false", {

test_that("old cache, update true", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
invisible(spectral_indices(update_cache = TRUE))
Sys.setFileTime(indices_path, "1970-01-01")
Expand All @@ -176,6 +195,7 @@ test_that("old cache, update true", {

test_that("old cache, download true", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
invisible(spectral_indices(update_cache = TRUE))
Sys.setFileTime(indices_path, "1970-01-01")
Expand All @@ -184,6 +204,7 @@ test_that("old cache, download true", {

test_that("old cache, download and update true", {
skip_if_offline()
skip_on_cran()
indices_path <- file.path(tools::R_user_dir("rsi"), "indices.rda")
invisible(spectral_indices(update_cache = TRUE))
Sys.setFileTime(indices_path, "1970-01-01")
Expand Down
2 changes: 1 addition & 1 deletion vignettes/rsi.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ vignette: >
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
eval = rlang::is_installed("curl") && !is.null(curl::nslookup("r-project.org", error = FALSE))
eval = rlang::is_installed("curl") && !is.null(curl::nslookup("r-project.org", error = FALSE)) && !(!interactive() && !isTRUE(as.logical(Sys.getenv("NOT_CRAN", "false"))))
)
```

Expand Down

0 comments on commit faf89e3

Please sign in to comment.