Skip to content

Commit

Permalink
improved stability when server is down
Browse files Browse the repository at this point in the history
  • Loading branch information
equitable-equations committed Mar 31, 2024
1 parent e0f499d commit 4eb15de
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 29 deletions.
4 changes: 2 additions & 2 deletions R/download_assessment_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ download_assessment_list <- function(database_id, ...) {
inventories_summary <- index_fqa_assessments(database_id)

if (nrow(inventories_summary) == 0){
return(list())
return(invisible(list()))
}

inventories_requested <- inventories_summary |>
Expand Down Expand Up @@ -78,7 +78,7 @@ download_assessment_list <- function(database_id, ...) {
}

if (length(results) == 0){
warning("No matches found. Empty list returned.", call. = FALSE)
message("No matches found. Empty list returned.")
}

results
Expand Down
23 changes: 14 additions & 9 deletions tests/testthat/test-download_assessment_list.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
test_that("download_assessment_list works", {

skip_if_offline()

expect_equal(class(suppressMessages(download_assessment_list(-2))), "list")

expect_warning(download_assessment_list(1, id == "hi"), "No matches found. Empty list returned.")
two_assessments <- download_assessment_list(1, id == 8 | id == 12)
expect_equal(class(two_assessments), "list")
expect_equal(length(two_assessments), 2)
expect_equal(class(two_assessments[[1]]), c("tbl_df",
"tbl",
"data.frame"))
skip_if_offline()

if(length(suppressMessages(index_fqa_assessments(1))) != 0){
expect_message(download_assessment_list(1, id == "hi"), "No matches found. Empty list returned.")
two_assessments <- download_assessment_list(1, id == 8 | id == 12)
expect_equal(class(two_assessments), "list")
expect_equal(length(two_assessments), 2)
expect_equal(class(two_assessments[[1]]), c("tbl_df",
"tbl",
"data.frame"))
expect_equal(ncol(two_assessments[[1]]), 9)
} else {
# if server doesn't respond
expect_message(download_assessment_list(1))
}
})
18 changes: 11 additions & 7 deletions tests/testthat/test-download_database_internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ test_that("download_database_internal works", {
expect_equal(nrow(null_output), 0)
expect_equal(memoise::has_cache(download_database_internal)(-40000), TRUE)

skip_if_offline()

test_db <- suppressMessages(download_database_internal(1))
expect_equal(ncol(test_db), 9)
expect_equal(test_db$V1[1], "Chicago Region")
expect_equal(class(test_db), c("tbl_df",
"tbl",
"data.frame"))

if (nrow(test_db) != 0) {
expect_equal(ncol(test_db), 9)
expect_equal(test_db$V1[1], "Chicago Region")
expect_equal(class(test_db), c("tbl_df",
"tbl",
"data.frame"))
} else {
# for when server doesn't respond
expect_message(download_database_internal(1))
}
})
10 changes: 7 additions & 3 deletions tests/testthat/test-is_assessment.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ test_that("is_assessment works", {
expect_false(is_assessment("hi"))
expect_false(is_assessment(data.frame()))

skip_if_offline()
test_raw <- suppressMessages(download_assessment(25002))

test_raw <- download_assessment(25002)
expect_true(is_assessment(test_raw))
if (nrow(test_raw) != 0) {
expect_true(is_assessment(test_raw))
} else {
# for when server doesn't respond
expect_false(is_assessment(test_raw))
}
})
10 changes: 7 additions & 3 deletions tests/testthat/test-is_inventory.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ test_that("is_inventory works", {
expect_false(is_inventory(NULL))
expect_false(is_inventory(test_assessment))

skip_if_offline()
edison <- suppressMessages(download_assessment(25002))

edison <- download_assessment(25002)
expect_true(is_inventory(assessment_inventory(edison)))
if (nrow(edison) != 0) {
expect_true(is_inventory(assessment_inventory(edison)))
} else {
# for when server doesn't respond
expect_false(is_inventory(assessment_inventory(edison)))
}

})
1 change: 1 addition & 0 deletions tests/testthat/test-is_inventory_list.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
test_that("is_inventory_list works", {

test_list <- list(test_assessment, test_assessment)
test_inv_list <- assessment_list_inventory(test_list)
bad_list <- list(faithful, faithful)
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-is_transect.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ test_that("is_transect works", {
expect_false(is_transect("hi"))
expect_false(is_transect(data.frame()))

skip_if_offline()

test_raw <- suppressMessages(download_transect(4492))

expect_true(is_transect(test_raw))
Expand Down
3 changes: 0 additions & 3 deletions tests/testthat/test-is_transect_list.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
test_that("is_transect_list works", {

skip_if_offline()

test_raw <- suppressMessages(download_transect(4492))
test_transect_list <- list(test_raw, test_raw)
test_inv_list <- suppressMessages(transect_list_inventory(test_transect_list))
bad_list <- list(faithful, faithful)

expect_false(is_transect_list(random_unassigned_variable_5000))
Expand Down

0 comments on commit 4eb15de

Please sign in to comment.