Skip to content

Commit

Permalink
add testthat skippers
Browse files Browse the repository at this point in the history
that react to the "ONLY_READ_TESTS" environmental variable

ref #330
  • Loading branch information
wibeasley committed Nov 2, 2021
1 parent 6b4bbef commit 8409a3a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 3 deletions.
7 changes: 7 additions & 0 deletions R/skippers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
skip_if_onlyread <- function() {
if (identical(Sys.getenv("ONLY_READ_TESTS"), "")) {
return(invisible(TRUE))
}

testthat::skip("Skipping test if only server reads/exports are used. (And server writes/imports are skipped.)" )
}
12 changes: 12 additions & 0 deletions tests/testthat/test-skippers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

test_that("skip_if_onlyread set", {
# Test that a skip happens
withr::local_envvar(ONLY_READ_TESTS = "yes")
expect_condition(skip_if_onlyread(), class = "skip")
})

test_that("skip_if_onlyread not set", {
# Test that a skip doesn't happen
withr::local_envvar(ONLY_READ_TESTS = "")
expect_condition(skip_if_onlyread(), NA, class = "skip")
})
12 changes: 11 additions & 1 deletion tests/testthat/test-write-batch.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ update_expectation <- FALSE

test_that("Smoke Test", {
testthat::skip_on_cran()
skip_if_onlyread()

start_clean_result <- REDCapR:::clean_start_simple(batch=TRUE)
project <- start_clean_result$redcap_project
})

test_that("default", {
testthat::skip_on_cran()
skip_if_onlyread()

path_expected <- "test-data/specific-redcapr/write-batch/default.R"
start_clean_result <- REDCapR:::clean_start_simple(batch=TRUE)
project <- start_clean_result$redcap_project
Expand Down Expand Up @@ -37,7 +41,9 @@ test_that("default", {
})

test_that("update-one-field", {
testthat::skip_on_cran()
testthat::skip_on_cran
skip_if_onlyread()

path_expected <- "test-data/specific-redcapr/write-batch/update-one-field.R"
start_clean_result <- REDCapR:::clean_start_simple(batch=TRUE)
project <- start_clean_result$redcap_project
Expand Down Expand Up @@ -77,6 +83,8 @@ test_that("update-one-field", {

test_that("update-two-fields", {
testthat::skip_on_cran()
skip_if_onlyread()

path_expected <- "test-data/specific-redcapr/write-batch/update-two-fields.R"
start_clean_result <- REDCapR:::clean_start_simple(batch=TRUE)
project <- start_clean_result$redcap_project
Expand Down Expand Up @@ -116,6 +124,8 @@ test_that("update-two-fields", {
})
test_that("Write Batch -Bad URI", {
testthat::skip_on_cran()
skip_if_onlyread()

bad_uri <- "google.com"
start_clean_result <- REDCapR:::clean_start_simple(batch=TRUE)
project <- start_clean_result$redcap_project
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-write-dag.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ update_expectation <- FALSE

test_that("Smoke Test", {
testthat::skip_on_cran()
skip_if_onlyread()

start_clean_result <- REDCapR:::clean_start_dag_write(batch=FALSE)
project <- start_clean_result$redcap_project
})

test_that("default", {
testthat::skip_on_cran()
skip_if_onlyread()

path_expected_before <- "test-data/specific-redcapr/write-dag/before.R"
path_expected_after <- "test-data/specific-redcapr/write-dag/after.R"
start_clean_result <- REDCapR:::clean_start_dag_write(batch=FALSE)
Expand Down Expand Up @@ -55,6 +59,7 @@ test_that("default", {

test_that("reassign subject to a different dag", {
testthat::skip_on_cran()
skip_if_onlyread()

# Step 1: Initialize the project
start_clean_result <- REDCapR:::clean_start_dag_write(batch=FALSE)
Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-write-error.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ update_expectation <- FALSE

test_that("One Shot: writing with read-only privileges", {
testthat::skip_on_cran()
skip_if_onlyread()

credential <- retrieve_credential_testing(project_id = 153L) # read-only
# expect_message(
# returned_object1 <- redcap_read_oneshot(redcap_uri=credential$redcap_uri, token=credential$token, raw_or_label="raw")
Expand Down Expand Up @@ -35,6 +37,8 @@ test_that("One Shot: writing with read-only privileges", {

test_that("Single Batch: writing with read-only privileges", {
testthat::skip_on_cran()
skip_if_onlyread()

credential <- retrieve_credential_testing(project_id = 153L) # read-only

expected_message <- "The REDCapR write/import operation was not successful. The error message was:\nERROR: You do not have API Import/Update privileges"
Expand All @@ -54,6 +58,8 @@ test_that("Single Batch: writing with read-only privileges", {

test_that("Many Batches: writing with read-only privileges", {
testthat::skip_on_cran()
skip_if_onlyread()

credential <- retrieve_credential_testing(project_id = 153L) # read-only

expected_message <- "The REDCapR write/import operation was not successful. The error message was:\nERROR: You do not have API Import/Update privileges"
Expand All @@ -72,6 +78,8 @@ test_that("Many Batches: writing with read-only privileges", {

test_that("Single Batch: writing with read-only privileges --contiue on error", {
testthat::skip_on_cran()
skip_if_onlyread()

credential <- retrieve_credential_testing(project_id = 153L) # read-only

expected_message <- "The REDCapR write/import operation was not successful. The error message was:\nERROR: You do not have API Import/Update privileges"
Expand All @@ -94,6 +102,8 @@ test_that("Single Batch: writing with read-only privileges --contiue on error",

test_that("Many Batches: writing with read-only privileges --contiue on error", {
testthat::skip_on_cran()
skip_if_onlyread()

credential <- retrieve_credential_testing(project_id = 153L) # read-only

expected_message <- "The REDCapR write/import operation was not successful. The error message was:\nERROR: You do not have API Import/Update privileges"
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-write-oneshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ update_expectation <- FALSE

test_that("Smoke Test", {
testthat::skip_on_cran()
skip_if_onlyread()

start_clean_result <- REDCapR:::clean_start_simple(batch=FALSE)
project <- start_clean_result$redcap_project
})

test_that("default", {
testthat::skip_on_cran()
skip_if_onlyread()

path_expected <- "test-data/specific-redcapr/write-oneshot/default.R"
start_clean_result <- REDCapR:::clean_start_simple(batch=FALSE)
project <- start_clean_result$redcap_project
Expand Down Expand Up @@ -37,6 +41,8 @@ test_that("default", {

test_that("one-field", {
testthat::skip_on_cran()
skip_if_onlyread()

path_expected <- "test-data/specific-redcapr/write-oneshot/one-field.R"
expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."

Expand Down Expand Up @@ -77,6 +83,8 @@ test_that("one-field", {

test_that("two-fields", {
testthat::skip_on_cran()
skip_if_onlyread()

path_expected <- "test-data/specific-redcapr/write-oneshot/two-fields.R"
start_clean_result <- REDCapR:::clean_start_simple(batch=FALSE)
project <- start_clean_result$redcap_project
Expand Down Expand Up @@ -117,6 +125,8 @@ test_that("two-fields", {

test_that("overwrite-false", {
testthat::skip_on_cran()
skip_if_onlyread()

path_expected <- "test-data/specific-redcapr/write-oneshot/default.R"
expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."

Expand Down Expand Up @@ -158,6 +168,8 @@ test_that("overwrite-false", {

test_that("overwrite-true", {
testthat::skip_on_cran()
skip_if_onlyread()

path_expected <- "test-data/specific-redcapr/write-oneshot/overwrite-true.R"
expected_outcome_message <- "\\d+ records and \\d+ columns were read from REDCap in \\d+(\\.\\d+\\W|\\W)seconds\\."

Expand Down
5 changes: 3 additions & 2 deletions utility/refresh.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ devtools::build_vignettes()

checks_to_exclude <- c(
"covr",
"cyclocomp",
# "cyclocomp"#,
"lintr_line_length_linter"
)
gp <-
Expand All @@ -33,9 +33,10 @@ pkgdown::build_site()
devtools::run_examples(); #dev.off() #This overwrites the NAMESPACE file too
# devtools::run_examples(, "redcap_read.Rd")
test_results_checked <- devtools::test()
test_results_checked <- devtools::test(filter = "write-error")
test_results_checked <- devtools::test(filter = "column")
test_results_checked <- devtools::test(filter = "validate.*$")
withr::local_envvar(ONLYREADTESTS = "true")
test_results_checked <- devtools::test(filter = "write-batch")

# testthat::test_dir("./tests/")
test_results_not_checked <- testthat::test_dir("./tests/manual/")
Expand Down

0 comments on commit 8409a3a

Please sign in to comment.