From 0bad7dd0bb08ff6c1f408cf01c6abb6a6f8b59de Mon Sep 17 00:00:00 2001 From: Heather Savoy Date: Mon, 6 Mar 2023 16:15:26 -0600 Subject: [PATCH] pass along errors from GeoCDL and start testing #22 --- DESCRIPTION | 4 +++- R/upload_geometry.R | 4 ++-- R/utils.R | 6 +++--- R/view_metadata.R | 9 +++++++-- tests/testthat.R | 12 ++++++++++++ tests/testthat/test-upload_geometry.R | 4 ++++ tests/testthat/test-utils.R | 6 ++++++ tests/testthat/test-view_metadata.R | 3 +++ 8 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 tests/testthat.R create mode 100644 tests/testthat/test-upload_geometry.R create mode 100644 tests/testthat/test-utils.R create mode 100644 tests/testthat/test-view_metadata.R diff --git a/DESCRIPTION b/DESCRIPTION index 52616fa..622d497 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,5 +19,7 @@ Imports: RoxygenNote: 7.2.0 Suggests: knitr, - rmarkdown + rmarkdown, + testthat (>= 3.0.0) VignetteBuilder: knitr +Config/testthat/edition: 3 diff --git a/R/upload_geometry.R b/R/upload_geometry.R index ec33b0d..24e8f73 100644 --- a/R/upload_geometry.R +++ b/R/upload_geometry.R @@ -136,8 +136,8 @@ upload_geometry <- function(geom, tmp_dir = tempdir()) { # Check for error status code if(httr::http_error(guid_r)){ stop(paste('GeoCDL returned an error:', - httr::http_status(guid_r)$message, - httr::content(guid_r))) + httr::http_status(guid_r)$message, '\n', + httr::content(guid_r)$detail)) } # Extract geometry upload ID diff --git a/R/utils.R b/R/utils.R index 7c6906b..09fee2f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -248,14 +248,14 @@ submit_subset_query <- function(query_str, dsn, req_name){ subset_response <- httr::GET(query_str[q], httr::write_disk(subset_zip, overwrite=TRUE)) + # Check for bad request / errors if(httr::http_error(subset_response)){ stop(paste('GeoCDL returned an error:', - httr::http_status(subset_response)$message, - httr::content(subset_response))) + httr::http_status(subset_response)$message, '\n', + httr::content(subset_response)$detail)) } - # Unzip results to temporary file utils::unzip(subset_zip, exdir = subset_dir) diff --git a/R/view_metadata.R b/R/view_metadata.R index 5800867..ef2bee4 100644 --- a/R/view_metadata.R +++ b/R/view_metadata.R @@ -23,11 +23,16 @@ view_metadata <- function(ds) { # Get response from REST API md_response <- httr::GET(md_query) - # Check for bad request / errors - # Convert from JSON and return list md_parsed <- httr::content(md_response) + # Check for bad request / errors + if(httr::http_error(md_response)){ + stop(paste('GeoCDL returned an error:', + httr::http_status(md_response)$message, '\n', + md_parsed$detail)) + } + # Format list into text cat(paste0('Name: ', md_parsed$name, '\n', 'ID: ', md_parsed$id, '\n', diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000..7f4350a --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,12 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/tests.html +# * https://testthat.r-lib.org/reference/test_package.html#special-files + +library(testthat) +library(rgeocdl) + +test_check("rgeocdl") diff --git a/tests/testthat/test-upload_geometry.R b/tests/testthat/test-upload_geometry.R new file mode 100644 index 0000000..5e1787c --- /dev/null +++ b/tests/testthat/test-upload_geometry.R @@ -0,0 +1,4 @@ +test_that("unsupported geometries return GeoCDL message", { + expect_condition(upload_geometry('test'), + 'No uploaded polygon data found for GUID ') +}) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R new file mode 100644 index 0000000..9e66768 --- /dev/null +++ b/tests/testthat/test-utils.R @@ -0,0 +1,6 @@ +test_that("invalid datasets return GeoCDL message", { + expect_condition(submit_subset_query('10.1.1.80:8000/subset_polygon?datasets=PRISM', + dsn = '.', + req_name = ''), + 'Incorrect dataset specification') +}) diff --git a/tests/testthat/test-view_metadata.R b/tests/testthat/test-view_metadata.R new file mode 100644 index 0000000..d5534be --- /dev/null +++ b/tests/testthat/test-view_metadata.R @@ -0,0 +1,3 @@ +test_that("invalid datasets return GeoCDL message", { + expect_condition(view_metadata('PRIS'),'Invalid dataset ID') +})