Skip to content
This repository has been archived by the owner on Oct 28, 2019. It is now read-only.

Commit

Permalink
Use mockery::stub() in tests #128
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Oct 21, 2017
1 parent 8dff736 commit 0a1bf08
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Expand Up @@ -33,5 +33,6 @@ Suggests:
rmarkdown,
lme4,
gbm,
MASS
MASS,
mockery
RoxygenNote: 5.0.1
2 changes: 1 addition & 1 deletion tests/testthat/test-1-workspace.R
Expand Up @@ -44,7 +44,7 @@ test_that("workspace() throws helpful 401 error with invalid id", {
.expect_error_in <- function(object, msgs){
if(missing(object) || is.null(object)) return(FALSE)
ptn <- sprintf("[%s]", paste(sprintf("(%s)", msgs), collapse = "|"))
grepl(ptn, object)
expect_true(grepl(ptn, object))
}

m <- .catchError(workspace(id = "x", auth = "y", .validate = TRUE))
Expand Down
11 changes: 10 additions & 1 deletion tests/testthat/test-2-datasets-upload-download-delete.R
Expand Up @@ -50,8 +50,17 @@ test_that("Can delete dataset from workspace", {
z <- delete.datasets(ws, timestamped_name)
expect_true(timestamped_name %in% z$Name && z$Deleted[z$Name == timestamped_name])
# Force refresh - sometime this fails in non-interactive
Sys.sleep(1); refresh(ws, what = "datasets")
max_wait <- 15
wait_period <- 3
i <- 0
ds <- datasets(ws, filter = "my")
while(i < max_wait && nrow(ds) > 0 && timestamped_name %in% ds$Name) {
Sys.sleep(wait_period)
i <- i + wait_period
refresh(ws, what = "datasets")
ds <- datasets(ws, filter = "my")
}
if(nrow(ds) > 0 || timestamped_name %in% ds$Name) skip("skip waiting for delete")
expect_true(nrow(ds) == 0 || !timestamped_name %in% ds$Name)
})

Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-2-download-each-dataset-type.R
Expand Up @@ -26,6 +26,7 @@ test_that("setup global variables", {
Zip <<- oneOfEach[oneOfEach$DataTypeId %in% c("Zip"), ]
oneOfEach <<- oneOfEach[!oneOfEach$DataTypeId %in% c("Zip"), ]
# oneOfEach$DataTypeId
expect_is(oneOfEach, "Datasets")
})

if(exists("oneOfEach")){
Expand Down
23 changes: 10 additions & 13 deletions tests/testthat/test-5-try_fetch.R
@@ -1,22 +1,19 @@
if(interactive()) library(testthat)

context("try_fetch")
library(mockery)

test_that("try_fetch() gives exponential retry messages",{
set.seed(1)
with_mock(
curl_fetch_memory = function(...){
retry_on = c(400, 401, 440, 503, 504, 509)
status_code <- if(runif(1) > 0.26) sample(retry_on, 1) else 200
list(status_code = status_code, contents = NA)
}, {
msg <- "Request failed with status 509. Waiting 0.0 seconds before retry\n"
expect_message(
try_fetch(delay = 0.1, no_message_threshold = 0),
msg
)
},
.env = "curl"
mockery::stub(try_fetch, "curl_fetch_memory", function(...){
retry_on = c(400, 401, 440, 503, 504, 509)
status_code <- if(runif(1) > 0.26) sample(retry_on, 1) else 200
list(status_code = status_code, contents = NA)
})
msg <- "Request failed with status 509. Waiting 0.0 seconds before retry\n"
expect_message(
try_fetch(delay = 0.1, no_message_threshold = 0),
msg
)

})

0 comments on commit 0a1bf08

Please sign in to comment.