From 3ca4e11f525e862a0a38fb4f935ed497aab06bbf Mon Sep 17 00:00:00 2001 From: Fonti Kar Date: Fri, 1 Mar 2024 15:04:13 +1100 Subject: [PATCH] Testing spatial functions in global.R (#90) * Code to generate link in processing #87 * Renames Establishment means earlier in workflow to remove processing in server.R * Moved renaming of voucher location to repository to earlier workflow and out of server. Removed creation of URL from server and directly using link field. * Suppressed warnings from APCalign #87 * Removed URL creaton in downloadhandler * Small test for load_places * Specifying dependencies and simple test for circle * Updated snapshot * Tests for spatial functions in infinitylists * Test and code to fail gracefully #91 --- R/global.R | 8 +++++ tests/testthat/test-galah_download.R | 7 ++++ tests/testthat/test-spatial-global.R | 49 ++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tests/testthat/test-spatial-global.R diff --git a/R/global.R b/R/global.R index 4b1698f..4b51a73 100644 --- a/R/global.R +++ b/R/global.R @@ -180,6 +180,14 @@ check_and_download_update <- function() { "infinitylists", "/releases/latest" ) + + # Simulate network down + network_down <- as.logical(Sys.getenv("NETWORK_UP", unset = TRUE)) + + if(httr::http_error(url)| network_down){ + message("No internet connection or data source down, try again later") + return(NULL) + } response <- httr::GET(url) release_data <- jsonlite::fromJSON(httr::content(response, "text")) diff --git a/tests/testthat/test-galah_download.R b/tests/testthat/test-galah_download.R index 639b2fe..6853580 100644 --- a/tests/testthat/test-galah_download.R +++ b/tests/testthat/test-galah_download.R @@ -10,3 +10,10 @@ test_that("Download exceutes", { expect_named(odonata) expect_s3_class(odonata, "tbl_df") }) + + +test_that("Fails gracefully", { + # in my tests + Sys.setenv("NETWORK_UP" = TRUE) + expect_message(check_and_download_update()) +}) \ No newline at end of file diff --git a/tests/testthat/test-spatial-global.R b/tests/testthat/test-spatial-global.R new file mode 100644 index 0000000..b9f4901 --- /dev/null +++ b/tests/testthat/test-spatial-global.R @@ -0,0 +1,49 @@ +test_that("Loading a place successfully", { + places <- list.files(system.file(package = "infinitylists", "extdata", "places")) + paths <- file.path(system.file(package = "infinitylists", "extdata", "places", places)) + + kmls <- purrr::map(paths, + ~load_place(.x) + ) + + expect_visible(purrr::map(paths, + ~load_place(.x)) + ) + + expect_type(kmls[[1]], "list") + + expect_visible(places) + expect_type(places, "character") +}) + + +test_that("Circle or buffer is added", { + # malabar + radii <- seq(10,100, by = 10) + + expect_visible(purrr::map(radii, + ~create_circle_polygon(lat = 33.9679, long = 151.2511, .x) + )) + + with_circles <- purrr::map(radii, + ~create_circle_polygon(lat = 33.9679, long = 151.2511, .x) + ) + expect_type(with_circles[[1]], "list") + + expect_error(add_buffer(with_circles, 5)) +}) + +test_that("Intersections are working", { + occ <- dplyr::tibble(lat = 33.9679, long = 151.2511) + occ_sf <- sf::st_as_sf(occ, coords = c("long", "lat"), crs = 4326) + + expect_true(points_in_target(occ_sf, create_circle_polygon(lat = 33.9679, long = 151.2511, 10))) + + expect_true(points_in_buffer(occ_sf, create_circle_polygon(lat = 33.9679, long = 151.2511, 10), buffer_size = 10)) +}) + + + + + +