Skip to content

Commit

Permalink
fix #5 add tests mocking requests, add suggest pkgs jsonlite and webm…
Browse files Browse the repository at this point in the history
…ockr
  • Loading branch information
sckott committed Jan 26, 2024
1 parent 1c069c8 commit afa036c
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 2 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ Imports:
glue,
httr
Suggests:
jsonlite,
knitr,
rmarkdown,
testthat (>= 3.0.0),
webmockr,
withr
Config/testthat/edition: 3
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions tests/testthat/helper-proofr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
library(webmockr)
59 changes: 59 additions & 0 deletions tests/testthat/helper-stubs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
response_status_running <- list(
canJobStart = FALSE,
jobStatus = "RUNNING",
cromwellUrl = "https://some.url",
jobInfo = list(
WORKFLOWLOGDIR = "/some/path",
SCRATCHDIR = "/some/path",
SLURM_JOB_ID = "someid",
CROMWELL_DIR = "/some/path",
SERVERLOGDIR = "/some/path",
SINGULARITY_CACHEDIR = "/some/path",
SERVERTIME = "1-0",
USE_AWS = FALSE,
SLURM_JOB_ACCOUNT = "/some/path"
)
)

response_status_not_running <- list(
canJobStart = TRUE,
jobStatus = NULL,
cromwellUrl = NULL,
jobInfo = list(
WORKFLOWLOGDIR = NULL,
SCRATCHDIR = NULL,
SLURM_JOB_ID = NULL,
CROMWELL_DIR = NULL,
SERVERLOGDIR = NULL,
SINGULARITY_CACHEDIR = NULL,
SERVERTIME = NULL,
USE_AWS = NULL,
SLURM_JOB_ACCOUNT = NULL
)
)

response_start_success <- list(
job_id = "12345678",
info = "Job submitted successfully. Some message"
)

response_start_conflict <- list(
message = "Job is already running"
)

response_cancel_success <- list(
message = "Proof/cromwell job for user xxxxx has been cancelled"
)

response_cancel_conflict <- list(
message = "Job is not running, nothing to delete"
)

response_authenticate_success <- list(
username = "jane",
token = "somerandomstring",
server_url = NULL,
proxy_url = NULL,
job_id = NULL,
token_creation_date = NULL
)
41 changes: 41 additions & 0 deletions tests/testthat/test-proof_authenticate.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test_that("proof_authenticate - error behavior", {
# errors if no username supplied
expect_error(proof_authenticate(),
'"username" is missing')

# errors if no password supplied
expect_error(proof_authenticate(username = "apple"),
'"password" is missing')

# username should be character
expect_error(proof_authenticate(5),
'username must be of class character')

# password should be character
expect_error(proof_authenticate(username = "apple", password = 5),
'password must be of class character')
})

test_that("proof_start - success", {
stub_registry_clear()
stub_request("post", make_url("authenticate")) %>%
to_return(
body = jsonlite::toJSON(
response_authenticate_success, auto_unbox = TRUE),
status = 200L,
headers = list('Content-type' = "application/json")
)
stub_registry()

enable(quiet = TRUE)

auth_res <- proof_authenticate("jane", "faketoken")

expect_type(auth_res, "character")
expect_equal(auth_res, "somerandomstring")


stub_registry_clear()
disable(quiet = TRUE)
Sys.unsetenv("PROOF_TOKEN")
})
43 changes: 43 additions & 0 deletions tests/testthat/test-proof_cancel.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
test_that("proof_cancel - success", {
stub_registry_clear()
stub_request("delete", make_url("cromwell-server")) %>%
to_return(
body = jsonlite::toJSON(response_cancel_success, auto_unbox = TRUE),
status = 200L,
headers = list('Content-type' = "application/json")
)
stub_registry()

enable(quiet = TRUE)

withr::with_envvar(c("PROOF_TOKEN" = "notarealtoken"), {
cancel_res <- proof_cancel()
})

expect_type(cancel_res, "list")
expect_type(cancel_res$message, "character")
expect_match(cancel_res$message, "cancelled")


stub_registry_clear()
disable(quiet = TRUE)
})

test_that("proof_cancel - not running, can not cancel", {
stub_request("delete", make_url("cromwell-server")) %>%
to_return(
body = jsonlite::toJSON(response_cancel_conflict, auto_unbox = TRUE),
status = 409L,
headers = list('Content-type' = "application/json")
)

enable(quiet = TRUE)

withr::with_envvar(c("PROOF_TOKEN" = "notarealtoken"), {
expect_error(proof_cancel(), "Job is not running, nothing to delete")
})


stub_registry_clear()
disable(quiet = TRUE)
})
4 changes: 2 additions & 2 deletions tests/testthat/test-proof_header.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test_that("proof_header", {
expect_match(proof_header("adf")$headers[[1]], "adf")

# If PROOF_TOKEN env var set, fxn can find it
withr::with_envvar(c("PROOF_TOKEN" = "world"), {
expect_match(proof_header()$headers[[1]], "world")
withr::with_envvar(c("PROOF_TOKEN" = "notarealtoken"), {
expect_match(proof_header()$headers[[1]], "notarealtoken")
})
})
41 changes: 41 additions & 0 deletions tests/testthat/test-proof_start.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
test_that("proof_start - success", {
stub_request("post", make_url("cromwell-server")) %>%
to_return(
body = jsonlite::toJSON(response_start_success, auto_unbox = TRUE),
status = 200L,
headers = list('Content-type' = "application/json")
)

enable(quiet = TRUE)

withr::with_envvar(c("PROOF_TOKEN" = "notarealtoken"), {
start_res <- proof_start()
})

expect_type(start_res, "list")
expect_type(start_res$job_id, "character")
expect_type(start_res$info, "character")


stub_registry_clear()
disable(quiet = TRUE)
})

test_that("proof_start - already running", {
stub_request("post", make_url("cromwell-server")) %>%
to_return(
body = jsonlite::toJSON(response_start_conflict, auto_unbox = TRUE),
status = 409L,
headers = list('Content-type' = "application/json")
)

enable(quiet = TRUE)

withr::with_envvar(c("PROOF_TOKEN" = "notarealtoken"), {
expect_error(proof_start(), "Job is already running")
})


stub_registry_clear()
disable(quiet = TRUE)
})
55 changes: 55 additions & 0 deletions tests/testthat/test-proof_status.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
test_that("proof_status - server IS running", {
stub_request("get", make_url("cromwell-server")) %>%
to_return(
body = jsonlite::toJSON(
response_status_running, auto_unbox = TRUE, null = "null"),
status = 200L,
headers = list('Content-type' = "application/json")
)

enable(quiet = TRUE)

withr::with_envvar(c("PROOF_TOKEN" = "notarealtoken"), {
status_res <- proof_status()
})

expect_type(status_res, "list")
expect_type(status_res$canJobStart, "logical")
expect_type(status_res$jobStatus, "character")
expect_type(status_res$cromwellUrl, "character")
expect_type(status_res$jobInfo, "list")
for (item in status_res$jobInfo) {
expect_true(inherits(item, c("character", "logical")))
}


stub_registry_clear()
disable(quiet = TRUE)
})

test_that("proof_status - server IS NOT running", {
stub_request("get", make_url("cromwell-server")) %>%
to_return(
body = jsonlite::toJSON(
response_status_not_running, auto_unbox = TRUE, null = "null"),
status = 200L,
headers = list('Content-type' = "application/json")
)

enable(quiet = TRUE)

withr::with_envvar(c("PROOF_TOKEN" = "notarealtoken"), {
status_res <- proof_status()
})

expect_type(status_res, "list")
expect_type(status_res$canJobStart, "logical")
expect_null(status_res$jobStatus)
expect_null(status_res$cromwellUrl)
expect_type(status_res$jobInfo, "list")
for (item in status_res$jobInfo) expect_null(item)


stub_registry_clear()
disable(quiet = TRUE)
})

0 comments on commit afa036c

Please sign in to comment.