diff --git a/NEWS.md b/NEWS.md index fc0e9e2..8f7a6f0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # RPresto 1.4.7.9000 +* Replaced deprecated `with_mock()` usage in unit testing with + `with_mocked_bindings()`. (#292) + # RPresto 1.4.7 * Fixed Trino support quirks (#254) and expanded unit tests to Trino diff --git a/R/PrestoQuery.R b/R/PrestoQuery.R index 5da72c0..1253090 100644 --- a/R/PrestoQuery.R +++ b/R/PrestoQuery.R @@ -49,6 +49,19 @@ wait <- function() { return(content) } +# Wrapper of httr::POST() and httr::GET() so that we can mock the POST +# and GET responses within RPresto in unit tests +# See https://testthat.r-lib.org/reference/local_mocked_bindings.html#namespaced-calls +httr_POST <- function(...) { + httr::POST(...) +} +httr_GET <- function(...) { + httr::GET(...) +} +httr_handle_reset <- function(...) { + httr::handle_reset(...) +} + #' Class to encapsulate a Presto query #' #' This reference class (so that the object can be passed by reference and @@ -207,7 +220,7 @@ PrestoQuery <- setRefClass("PrestoQuery", headers <- .request_headers(.conn) while (status == 503L || (retries > 0 && status >= 400L)) { wait() - post.response <- httr::POST( + post.response <- httr_POST( url, body = enc2utf8(.statement), config = headers @@ -224,7 +237,7 @@ PrestoQuery <- setRefClass("PrestoQuery", headers <- .request_headers(.conn) get.response <- tryCatch( { - response <- httr::GET(.next.uri, config = headers) + response <- httr_GET(.next.uri, config = headers) if (httr::status_code(response) >= 400L) { # stop_for_status also fails for 300 <= status < 400 # so we need the if condition @@ -244,7 +257,7 @@ PrestoQuery <- setRefClass("PrestoQuery", '", retrying [', 4 - num.retry, "/3]\n" ) wait() - httr::handle_reset(.next.uri) + httr_handle_reset(.next.uri) return(get(num.retry - 1)) } ) diff --git a/R/dbClearResult.R b/R/dbClearResult.R index cf16c20..d12f501 100644 --- a/R/dbClearResult.R +++ b/R/dbClearResult.R @@ -7,6 +7,10 @@ #' @include PrestoResult.R NULL +httr_DELETE <- function(...) { + httr::DELETE(...) +} + #' @rdname PrestoResult-class #' @importMethodsFrom DBI dbClearResult #' @export @@ -32,7 +36,7 @@ setMethod( res@connection@host, ":", res@connection@port, "/v1/query/", res@query$id() ) - delete.result <- httr::DELETE(delete.uri, config = headers) + delete.result <- httr_DELETE(delete.uri, config = headers) s <- httr::status_code(delete.result) if (s >= 200 && s < 300) { res@query$state("__KILLED") diff --git a/tests/testthat/helper-mock_connection.R b/tests/testthat/helper-mock_connection.R index ed9d3d3..a10db53 100644 --- a/tests/testthat/helper-mock_connection.R +++ b/tests/testthat/helper-mock_connection.R @@ -5,16 +5,7 @@ # LICENSE file in the root directory of this source tree. setup_mock_connection <- function() { - with_mock( - `httr::POST` = mock_httr_replies( - mock_httr_response( - "http://localhost:8000/v1/statement", - status_code = 200, - state = "FINISHED", - request_body = "SELECT current_timezone() AS tz", - data = data.frame(tz = Sys.timezone(), stringsAsFactors = FALSE) - ) - ), + with_mocked_bindings( { mock.conn <- dbConnect( RPresto::Presto(), @@ -27,16 +18,8 @@ setup_mock_connection <- function() { user = Sys.getenv("USER") ) return(mock.conn) - } - ) -} - -setup_mock_dplyr_connection <- function() { - if (!requireNamespace("dplyr", quietly = TRUE)) { - skip("Skipping dplyr tests because we can't load dplyr") - } - with_mock( - `httr::POST` = mock_httr_replies( + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -44,7 +27,15 @@ setup_mock_dplyr_connection <- function() { request_body = "SELECT current_timezone() AS tz", data = data.frame(tz = Sys.timezone(), stringsAsFactors = FALSE) ) - ), + ) + ) +} + +setup_mock_dplyr_connection <- function() { + if (!requireNamespace("dplyr", quietly = TRUE)) { + skip("Skipping dplyr tests because we can't load dplyr") + } + with_mocked_bindings( { db <- src_presto( schema = "test", @@ -58,6 +49,15 @@ setup_mock_dplyr_connection <- function() { ) return(list(db = db, iris_table_name = "iris_table")) - } + }, + httr_POST = mock_httr_replies( + mock_httr_response( + "http://localhost:8000/v1/statement", + status_code = 200, + state = "FINISHED", + request_body = "SELECT current_timezone() AS tz", + data = data.frame(tz = Sys.timezone(), stringsAsFactors = FALSE) + ) + ) ) } diff --git a/tests/testthat/test-PrestoQuery.R b/tests/testthat/test-PrestoQuery.R index d13a697..fba33f6 100644 --- a/tests/testthat/test-PrestoQuery.R +++ b/tests/testthat/test-PrestoQuery.R @@ -25,14 +25,7 @@ test_that("PrestoQuery methods work correctly", { ), class = "response" ) - with_mock( - `httr::POST` = mock_httr_replies( - list( - url = "http://localhost:8000/v1/statement", - response = post.response, - request_body = "SELECT 1" - ) - ), + with_mocked_bindings( { query <- PrestoQuery(conn, "SELECT 1") res <- query$execute() @@ -49,7 +42,14 @@ test_that("PrestoQuery methods work correctly", { expect_false(query$postDataFetched(FALSE)) query$state("__TEST") expect_equal(query$state(), "__TEST") - } + }, + httr_POST = mock_httr_replies( + list( + url = "http://localhost:8000/v1/statement", + response = post.response, + request_body = "SELECT 1" + ) + ) ) }) @@ -62,8 +62,7 @@ test_that("PrestoQuery methods work correctly with POST data", { data = data.frame(x = 1), request_body = "SELECT 1" ) - with_mock( - `httr::POST` = mock_httr_replies(mock_response), + with_mocked_bindings( { query <- PrestoQuery(conn, "SELECT 1") res <- query$execute() @@ -78,6 +77,7 @@ test_that("PrestoQuery methods work correctly with POST data", { expect_true(query$postDataFetched(TRUE)) expect_true(query$postDataFetched()) expect_true(query$hasCompleted()) - } + }, + httr_POST = mock_httr_replies(mock_response) ) }) diff --git a/tests/testthat/test-dbClearResult.R b/tests/testthat/test-dbClearResult.R index f43f8a3..9ef265d 100644 --- a/tests/testthat/test-dbClearResult.R +++ b/tests/testthat/test-dbClearResult.R @@ -14,8 +14,19 @@ test_that("dbClearResult works with live database", { test_that("dbClearResult works with mock", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + result <- dbSendQuery(conn, "SELECT 1") + expect_true(dbClearResult(result), label = "regular query") + expect_true(dbClearResult(result), label = "idempotency") + + result <- dbSendQuery(conn, "SELECT 2") + expect_false(dbClearResult(result), label = "DELETE fails") + + result <- dbSendQuery(conn, "SELECT 3") + expect_true(dbClearResult(result), label = "complete query") + }, + httr_POST = mock_httr_replies( mock_httr_response( url = "http://localhost:8000/v1/statement", status_code = 200, @@ -40,7 +51,7 @@ test_that("dbClearResult works with mock", { query_id = "query_3" ) ), - `httr::DELETE` = mock_httr_replies( + httr_DELETE = mock_httr_replies( mock_httr_response( url = "http://localhost:8000/v1/query/query_1", status_code = 200, @@ -51,17 +62,6 @@ test_that("dbClearResult works with mock", { status_code = 500, state = "" ) - ), - { - result <- dbSendQuery(conn, "SELECT 1") - expect_true(dbClearResult(result), label = "regular query") - expect_true(dbClearResult(result), label = "idempotency") - - result <- dbSendQuery(conn, "SELECT 2") - expect_false(dbClearResult(result), label = "DELETE fails") - - result <- dbSendQuery(conn, "SELECT 3") - expect_true(dbClearResult(result), label = "complete query") - } + ) ) }) diff --git a/tests/testthat/test-dbConnect.R b/tests/testthat/test-dbConnect.R index 694a3b1..a9da85b 100644 --- a/tests/testthat/test-dbConnect.R +++ b/tests/testthat/test-dbConnect.R @@ -7,16 +7,7 @@ context("dbConnect") test_that("dbConnect constructs PrestoConnection correctly", { - with_mock( - `httr::POST` = mock_httr_replies( - mock_httr_response( - "http://localhost:8000/v1/statement", - status_code = 200, - state = "FINISHED", - request_body = "SELECT current_timezone() AS tz", - data = data.frame(tz = Sys.timezone(), stringsAsFactors = FALSE) - ) - ), + with_mocked_bindings( { expect_error(dbConnect(RPresto::Presto()), label = "not enough arguments") @@ -141,6 +132,15 @@ test_that("dbConnect constructs PrestoConnection correctly", { ), "should be one of" ) - } + }, + httr_POST = mock_httr_replies( + mock_httr_response( + "http://localhost:8000/v1/statement", + status_code = 200, + state = "FINISHED", + request_body = "SELECT current_timezone() AS tz", + data = data.frame(tz = Sys.timezone(), stringsAsFactors = FALSE) + ) + ) ) }) diff --git a/tests/testthat/test-dbFetch.R b/tests/testthat/test-dbFetch.R index d69342e..b046e86 100644 --- a/tests/testthat/test-dbFetch.R +++ b/tests/testthat/test-dbFetch.R @@ -31,8 +31,54 @@ test_that("dbFetch works with live database", { test_that("dbFetch works with mock", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + result <- dbSendQuery(conn, "SELECT n FROM (VALUES (1), (2)) AS t (n)") + expect_error( + dbFetch(result, 1), + ".*fetching custom number of rows.*is not supported.*" + ) + expect_equal(dbFetch(result), tibble::tibble(n = 1L)) + expect_true(dbClearResult(result)) + expect_error( + dbFetch(result), + ".*Result object is not valid.*" + ) + + result <- dbSendQuery(conn, "SELECT n FROM (VALUES (1), (2)) AS t (n)") + expect_equal(dbGetRowCount(result), 0) + expect_equal(dbFetch(result), tibble::tibble(n = 1L)) + expect_equal(dbGetRowCount(result), 1) + expect_equal(dbFetch(result), tibble::tibble(n = 2L)) + expect_equal(dbGetRowCount(result), 2) + expect_true(dbHasCompleted(result)) + + result <- dbSendQuery(conn, "SELECT n FROM (VALUES (1), (2)) AS t (n)") + expect_equal(dbFetch(result, -1), tibble::tibble(n = c(1L, 2L))) + expect_equal(dbGetRowCount(result), 2) + + result <- dbSendQuery(conn, "SELECT 2") + expect_error( + dbFetch(result), + paste0( + "Cannot fetch .*, error: ", + "There was a problem with the request and we have exhausted ", + "our retry limit" + ) + ) + + result <- dbSendQuery(conn, "SELECT 3") + expect_error( + dbFetch(result), + ".*Query .*localhost.8000.query.3.1 failed.*" + ) + + result <- dbSendQuery(conn, "SELECT 4 LIMIT 0") + rv <- dbFetch(result, -1) + ev <- tibble::tibble("_col0" = numeric(0)) + expect_equal_data_frame(rv, ev) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -64,7 +110,7 @@ test_that("dbFetch works with mock", { next_uri = "http://localhost:8000/query_4/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -97,65 +143,38 @@ test_that("dbFetch works with mock", { state = "FINISHED" ) ), - `httr::DELETE` = mock_httr_replies( + httr_DELETE = mock_httr_replies( mock_httr_response( url = "http://localhost:8000/v1/query/query_1", status_code = 200, state = "" ) ), - `httr::handle_reset` = function(...) { + httr_handle_reset = function(...) { return() - }, + } + ) + with_mocked_bindings( { - result <- dbSendQuery(conn, "SELECT n FROM (VALUES (1), (2)) AS t (n)") - expect_error( - dbFetch(result, 1), - ".*fetching custom number of rows.*is not supported.*" - ) - expect_equal(dbFetch(result), tibble::tibble(n = 1L)) - expect_true(dbClearResult(result)) - expect_error( - dbFetch(result), - ".*Result object is not valid.*" - ) - - result <- dbSendQuery(conn, "SELECT n FROM (VALUES (1), (2)) AS t (n)") - expect_equal(dbGetRowCount(result), 0) - expect_equal(dbFetch(result), tibble::tibble(n = 1L)) - expect_equal(dbGetRowCount(result), 1) - expect_equal(dbFetch(result), tibble::tibble(n = 2L)) - expect_equal(dbGetRowCount(result), 2) - expect_true(dbHasCompleted(result)) - - result <- dbSendQuery(conn, "SELECT n FROM (VALUES (1), (2)) AS t (n)") - expect_equal(dbFetch(result, -1), tibble::tibble(n = c(1L, 2L))) - expect_equal(dbGetRowCount(result), 2) - - result <- dbSendQuery(conn, "SELECT 2") + result <- dbSendQuery(conn, "SELECT 3") expect_error( - dbFetch(result), + suppressMessages(dbFetch(result)), paste0( "Cannot fetch .*, error: ", - "There was a problem with the request and we have exhausted ", - "our retry limit" + "There was a problem with the request and we have exhausted our ", + "retry limit" ) ) - result <- dbSendQuery(conn, "SELECT 3") - expect_error( - dbFetch(result), - ".*Query .*localhost.8000.query.3.1 failed.*" + assign("request.count", 0, envir = environment(httr_GET)) + result <- dbSendQuery(conn, "SELECT 4") + expect_message( + v <- dbFetch(result), + "First request is an error" ) - - result <- dbSendQuery(conn, "SELECT 4 LIMIT 0") - rv <- dbFetch(result, -1) - ev <- tibble::tibble("_col0" = numeric(0)) - expect_equal_data_frame(rv, ev) - } - ) - with_mock( - `httr::POST` = mock_httr_replies( + expect_equal(v, tibble::tibble(n = 4)) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -171,7 +190,7 @@ test_that("dbFetch works with mock", { next_uri = "http://localhost:8000/query_4/1" ) ), - `httr::GET` = function(url, ...) { + httr_GET = function(url, ...) { if (url == "http://localhost:8000/query_3/1") { stop("Error") } @@ -188,27 +207,8 @@ test_that("dbFetch works with mock", { ) )(url, ...)) }, - `httr::handle_reset` = function(...) { + httr_handle_reset = function(...) { return() - }, - { - result <- dbSendQuery(conn, "SELECT 3") - expect_error( - suppressMessages(dbFetch(result)), - paste0( - "Cannot fetch .*, error: ", - "There was a problem with the request and we have exhausted our ", - "retry limit" - ) - ) - - assign("request.count", 0, envir = environment(httr::GET)) - result <- dbSendQuery(conn, "SELECT 4") - expect_message( - v <- dbFetch(result), - "First request is an error" - ) - expect_equal(v, tibble::tibble(n = 4)) } ) }) @@ -216,8 +216,8 @@ test_that("dbFetch works with mock", { with_locale(test.locale(), test_that)("dbFetch rbind works correctly", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -226,7 +226,7 @@ with_locale(test.locale(), test_that)("dbFetch rbind works correctly", { next_uri = "http://localhost:8000/query_1/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -272,8 +272,44 @@ with_locale(test.locale(), test_that)("dbFetch rbind works with zero row chunks" -match(c("raw", "list_unnamed", "list_named"), colnames(full.data)), drop = FALSE ] - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + result <- dbSendQuery( + conn, + "SELECT integer, double, logical FROM all_types" + ) + + expect_equal_data_frame( + dbFetch(result, -1), + data, + label = "multiple columns" + ) + + result <- dbSendQuery(conn, "SELECT double FROM all_types") + + expect_equal_data_frame( + dbFetch(result, -1), + data[, "double", drop = FALSE], + label = "single column" + ) + + result <- dbSendQuery(conn, "SELECT * FROM all_types") + full.data$POSIXct <- + lubridate::with_tz(full.data$POSIXct, test.timezone()) + expect_equal_data_frame( + dbFetch(result, -1), + full.data, + label = "zero chunk first" + ) + + result <- dbSendQuery(conn, "SELECT * FROM all_types_with_queue") + expect_equal_data_frame( + dbFetch(result, -1), + full.data, + label = "with empty chunks" + ) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -303,7 +339,7 @@ with_locale(test.locale(), test_that)("dbFetch rbind works with zero row chunks" next_uri = "http://localhost:8000/query_4/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -408,42 +444,6 @@ with_locale(test.locale(), test_that)("dbFetch rbind works with zero row chunks" status_code = 200, state = "FINISHED" ) - ), - { - result <- dbSendQuery( - conn, - "SELECT integer, double, logical FROM all_types" - ) - - expect_equal_data_frame( - dbFetch(result, -1), - data, - label = "multiple columns" - ) - - result <- dbSendQuery(conn, "SELECT double FROM all_types") - - expect_equal_data_frame( - dbFetch(result, -1), - data[, "double", drop = FALSE], - label = "single column" - ) - - result <- dbSendQuery(conn, "SELECT * FROM all_types") - full.data$POSIXct <- - lubridate::with_tz(full.data$POSIXct, test.timezone()) - expect_equal_data_frame( - dbFetch(result, -1), - full.data, - label = "zero chunk first" - ) - - result <- dbSendQuery(conn, "SELECT * FROM all_types_with_queue") - expect_equal_data_frame( - dbFetch(result, -1), - full.data, - label = "with empty chunks" - ) - } + ) ) }) diff --git a/tests/testthat/test-dbGetInfo.R b/tests/testthat/test-dbGetInfo.R index b325e8a..1a8de4f 100644 --- a/tests/testthat/test-dbGetInfo.R +++ b/tests/testthat/test-dbGetInfo.R @@ -44,32 +44,7 @@ test_that("dbGetInfo works with mock", { schema = "test" ) ) - with_mock( - `httr::POST` = mock_httr_replies( - mock_httr_response( - "http://localhost:8000/v1/statement", - status_code = 200, - state = "QUEUED", - request_body = "SELECT n FROM two_rows", - next_uri = "http://localhost:8000/query_1/1", - query_id = "query_1" - ) - ), - `httr::GET` = mock_httr_replies( - mock_httr_response( - "http://localhost:8000/query_1/1", - status_code = 200, - data = data.frame(n = 1, stringsAsFactors = FALSE), - state = "FINISHED", - next_uri = "http://localhost:8000/query_1/2" - ), - mock_httr_response( - "http://localhost:8000/query_1/2", - status_code = 200, - data = data.frame(n = 2, stringsAsFactors = FALSE), - state = "FINISHED" - ) - ), + with_mocked_bindings( { result <- dbSendQuery(conn, "SELECT n FROM two_rows") expect_equal( @@ -104,6 +79,31 @@ test_that("dbGetInfo works with mock", { stats = list(state = "FINISHED") ) ) - } + }, + httr_POST = mock_httr_replies( + mock_httr_response( + "http://localhost:8000/v1/statement", + status_code = 200, + state = "QUEUED", + request_body = "SELECT n FROM two_rows", + next_uri = "http://localhost:8000/query_1/1", + query_id = "query_1" + ) + ), + httr_GET = mock_httr_replies( + mock_httr_response( + "http://localhost:8000/query_1/1", + status_code = 200, + data = data.frame(n = 1, stringsAsFactors = FALSE), + state = "FINISHED", + next_uri = "http://localhost:8000/query_1/2" + ), + mock_httr_response( + "http://localhost:8000/query_1/2", + status_code = 200, + data = data.frame(n = 2, stringsAsFactors = FALSE), + state = "FINISHED" + ) + ) ) }) diff --git a/tests/testthat/test-dbGetQuery.R b/tests/testthat/test-dbGetQuery.R index b85ec62..c9f1d2d 100644 --- a/tests/testthat/test-dbGetQuery.R +++ b/tests/testthat/test-dbGetQuery.R @@ -38,8 +38,18 @@ with_locale(test.locale(), test_that)("dbGetQuery works with live database", { with_locale(test.locale(), test_that)("dbGetQuery works with mock", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + expect_equal_data_frame( + dbGetQuery(conn, "SELECT n FROM two_rows"), + tibble::tibble(n = c(1, 2)) + ) + expect_equal_data_frame( + dbGetQuery(conn, "SELECT t FROM encoding_test"), + tibble::tibble(t = c("çğıöşü", "ÇĞİÖŞÜ")) + ) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -55,7 +65,7 @@ with_locale(test.locale(), test_that)("dbGetQuery works with mock", { next_uri = "http://localhost:8000/query_2/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -82,25 +92,26 @@ with_locale(test.locale(), test_that)("dbGetQuery works with mock", { data = data.frame(t = "ÇĞİÖŞÜ"), state = "FINISHED" ) - ), - { - expect_equal_data_frame( - dbGetQuery(conn, "SELECT n FROM two_rows"), - tibble::tibble(n = c(1, 2)) - ) - expect_equal_data_frame( - dbGetQuery(conn, "SELECT t FROM encoding_test"), - tibble::tibble(t = c("çğıöşü", "ÇĞİÖŞÜ")) - ) - } + ) ) }) test_that("dbGetQuery works with data in POST response", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + expect_equal_data_frame( + dbGetQuery(conn, "SELECT 1 AS x"), + tibble::tibble(x = 1) + ) + + expect_equal_data_frame( + dbGetQuery(conn, "SELECT n FROM two_rows"), + tibble::tibble(n = c(3, 4)) + ) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -117,33 +128,28 @@ test_that("dbGetQuery works with data in POST response", { next_uri = "http://localhost:8000/query_1/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, data = data.frame(n = 4, stringsAsFactors = FALSE), state = "FINISHED" ) - ), - { - expect_equal_data_frame( - dbGetQuery(conn, "SELECT 1 AS x"), - tibble::tibble(x = 1) - ) - - expect_equal_data_frame( - dbGetQuery(conn, "SELECT n FROM two_rows"), - tibble::tibble(n = c(3, 4)) - ) - } + ) ) }) test_that("Inconsistent data in chunks fail", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + expect_error( + dbGetQuery(conn, "SELECT a, b FROM broken_chunks"), + "Chunk column names are different across chunks" + ) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -153,7 +159,7 @@ test_that("Inconsistent data in chunks fail", { query_id = "query_1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -181,18 +187,12 @@ test_that("Inconsistent data in chunks fail", { data = data.frame(a = 3, b = FALSE) ) ), - `httr::DELETE` = mock_httr_replies( + httr_DELETE = mock_httr_replies( mock_httr_response( url = "http://localhost:8000/v1/query/query_1", status_code = 200, state = "" ) - ), - { - expect_error( - dbGetQuery(conn, "SELECT a, b FROM broken_chunks"), - "Chunk column names are different across chunks" - ) - } + ) ) }) diff --git a/tests/testthat/test-dbGetRowCount.R b/tests/testthat/test-dbGetRowCount.R index 36e8986..6bce4c1 100644 --- a/tests/testthat/test-dbGetRowCount.R +++ b/tests/testthat/test-dbGetRowCount.R @@ -22,8 +22,18 @@ test_that("dbGetRowCount works with live database", { test_that("dbGetRowCount works with mock", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + result <- dbSendQuery(conn, "SELECT n FROM two_rows") + expect_equal(dbGetRowCount(result), 0) + expect_equal(dbFetch(result), tibble::tibble(n = 1)) + expect_equal(dbGetRowCount(result), 1) + expect_equal(dbFetch(result), tibble::tibble(n = 2)) + expect_equal(dbGetRowCount(result), 2) + expect_equal(dbFetch(result), tibble::tibble()) + expect_equal(dbGetRowCount(result), 2) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -32,7 +42,7 @@ test_that("dbGetRowCount works with mock", { next_uri = "http://localhost:8000/query_1/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -46,16 +56,6 @@ test_that("dbGetRowCount works with mock", { data = data.frame(n = 2, stringsAsFactors = FALSE), state = "FINISHED" ) - ), - { - result <- dbSendQuery(conn, "SELECT n FROM two_rows") - expect_equal(dbGetRowCount(result), 0) - expect_equal(dbFetch(result), tibble::tibble(n = 1)) - expect_equal(dbGetRowCount(result), 1) - expect_equal(dbFetch(result), tibble::tibble(n = 2)) - expect_equal(dbGetRowCount(result), 2) - expect_equal(dbFetch(result), tibble::tibble()) - expect_equal(dbGetRowCount(result), 2) - } + ) ) }) diff --git a/tests/testthat/test-dbGetStatement.R b/tests/testthat/test-dbGetStatement.R index 1ff45c9..307e8ba 100644 --- a/tests/testthat/test-dbGetStatement.R +++ b/tests/testthat/test-dbGetStatement.R @@ -17,8 +17,16 @@ test_that("dbGetStatement works with live database", { test_that("dbGetStatement works with mock", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + result <- dbSendQuery(conn, "SELECT n FROM two_rows") + expect_equal(dbGetStatement(result), "SELECT n FROM two_rows") + expect_equal(dbFetch(result, -1), tibble::tibble(n = c(1, 2))) + expect_equal(dbGetStatement(result), "SELECT n FROM two_rows") + expect_equal(dbFetch(result), tibble::tibble()) + expect_equal(dbGetStatement(result), "SELECT n FROM two_rows") + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -27,7 +35,7 @@ test_that("dbGetStatement works with mock", { next_uri = "http://localhost:8000/query_1/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -41,14 +49,6 @@ test_that("dbGetStatement works with mock", { data = data.frame(n = 2, stringsAsFactors = FALSE), state = "FINISHED" ) - ), - { - result <- dbSendQuery(conn, "SELECT n FROM two_rows") - expect_equal(dbGetStatement(result), "SELECT n FROM two_rows") - expect_equal(dbFetch(result, -1), tibble::tibble(n = c(1, 2))) - expect_equal(dbGetStatement(result), "SELECT n FROM two_rows") - expect_equal(dbFetch(result), tibble::tibble()) - expect_equal(dbGetStatement(result), "SELECT n FROM two_rows") - } + ) ) }) diff --git a/tests/testthat/test-dbHasCompleted.R b/tests/testthat/test-dbHasCompleted.R index 5922e51..07d0a2d 100644 --- a/tests/testthat/test-dbHasCompleted.R +++ b/tests/testthat/test-dbHasCompleted.R @@ -17,8 +17,8 @@ test_that("dbHasCompleted works with live database", { test_that("dbHasCompleted works with mock", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -27,7 +27,7 @@ test_that("dbHasCompleted works with mock", { next_uri = "http://localhost:8000/query_1/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, diff --git a/tests/testthat/test-dbIsValid.R b/tests/testthat/test-dbIsValid.R index 22b5817..88d391d 100644 --- a/tests/testthat/test-dbIsValid.R +++ b/tests/testthat/test-dbIsValid.R @@ -44,8 +44,18 @@ test_that("dbIsValid works with live database", { test_that("dbIsValid works with mock - successful queries", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + result <- dbSendQuery(conn, "SELECT n FROM two_rows") + expect_true(dbIsValid(result)) + expect_equal(dbFetch(result), tibble::tibble(n = 1)) + expect_true(dbIsValid(result)) + expect_equal(dbFetch(result), tibble::tibble(n = 2)) + expect_true(dbIsValid(result)) + expect_equal(dbFetch(result), tibble::tibble()) + expect_true(dbIsValid(result)) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -54,7 +64,7 @@ test_that("dbIsValid works with mock - successful queries", { next_uri = "http://localhost:8000/query_1/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -68,24 +78,52 @@ test_that("dbIsValid works with mock - successful queries", { data = data.frame(n = 2, stringsAsFactors = FALSE), state = "FINISHED" ) - ), + ) + ) +}) + +test_that("dbIsValid works with mock - retries and failures", { + conn <- setup_mock_connection() + with_mocked_bindings( { - result <- dbSendQuery(conn, "SELECT n FROM two_rows") + result <- dbSendQuery(conn, "SELECT 1") expect_true(dbIsValid(result)) - expect_equal(dbFetch(result), tibble::tibble(n = 1)) + expect_error( + suppressMessages(dbFetch(result)), + "There was a problem with the request" + ) + expect_false(dbIsValid(result)) + + assign("request.count", 0, envir = environment(httr_GET)) + result <- dbSendQuery(conn, "SELECT 2 AS n") expect_true(dbIsValid(result)) - expect_equal(dbFetch(result), tibble::tibble(n = 2)) + expect_message( + v <- dbFetch(result), + "First request is an error" + ) + expect_equal(v, tibble::tibble(n = 2)) expect_true(dbIsValid(result)) - expect_equal(dbFetch(result), tibble::tibble()) + expect_true(dbHasCompleted(result)) + + assign("request.count", 0, envir = environment(httr_GET)) + result <- dbSendQuery(conn, 'SELECT "text" AS z') expect_true(dbIsValid(result)) - } - ) -}) + expect_message( + v <- dbFetch(result), + "GET call failed with error: .*\\((HTTP )*404\\).*, retrying.*" + ) + expect_equal(v, tibble::tibble(z = "text")) + expect_true(dbIsValid(result)) + expect_true(dbHasCompleted(result)) -test_that("dbIsValid works with mock - retries and failures", { - conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + result <- dbSendQuery(conn, "SELECT x FROM respond_with_404") + expect_true(dbIsValid(result)) + expect_error( + dbFetch(result), + "There was a problem with the request" + ) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -115,7 +153,7 @@ test_that("dbIsValid works with mock - retries and failures", { next_uri = "http://localhost:8000/query_4/1" ) ), - `httr::GET` = function(url, ...) { + httr_GET = function(url, ...) { if (url == "http://localhost:8000/query_1/1") { stop("Error") } @@ -156,54 +194,32 @@ test_that("dbIsValid works with mock - retries and failures", { } stop("Unhandled url in httr::GET mock: ", url) }, - `httr::handle_reset` = function(...) { + httr_handle_reset = function(...) { return() - }, + } + ) +}) + +test_that("dbIsValid works with mock - dbClearResult", { + conn <- setup_mock_connection() + with_mocked_bindings( { result <- dbSendQuery(conn, "SELECT 1") expect_true(dbIsValid(result)) - expect_error( - suppressMessages(dbFetch(result)), - "There was a problem with the request" - ) + expect_true(dbClearResult(result)) + expect_false(dbIsValid(result)) + expect_true(dbClearResult(result)) expect_false(dbIsValid(result)) - assign("request.count", 0, envir = environment(httr::GET)) - result <- dbSendQuery(conn, "SELECT 2 AS n") - expect_true(dbIsValid(result)) - expect_message( - v <- dbFetch(result), - "First request is an error" - ) - expect_equal(v, tibble::tibble(n = 2)) - expect_true(dbIsValid(result)) - expect_true(dbHasCompleted(result)) - - assign("request.count", 0, envir = environment(httr::GET)) - result <- dbSendQuery(conn, 'SELECT "text" AS z') - expect_true(dbIsValid(result)) - expect_message( - v <- dbFetch(result), - "GET call failed with error: .*\\((HTTP )*404\\).*, retrying.*" - ) - expect_equal(v, tibble::tibble(z = "text")) + result <- dbSendQuery(conn, "SELECT 2") + expect_false(dbClearResult(result), label = "DELETE fails") expect_true(dbIsValid(result)) - expect_true(dbHasCompleted(result)) - result <- dbSendQuery(conn, "SELECT x FROM respond_with_404") + result <- dbSendQuery(conn, "SELECT 3") + expect_true(dbClearResult(result), label = "complete query") expect_true(dbIsValid(result)) - expect_error( - dbFetch(result), - "There was a problem with the request" - ) - } - ) -}) - -test_that("dbIsValid works with mock - dbClearResult", { - conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + }, + httr_POST = mock_httr_replies( mock_httr_response( url = "http://localhost:8000/v1/statement", status_code = 200, @@ -228,7 +244,7 @@ test_that("dbIsValid works with mock - dbClearResult", { query_id = "query_3" ) ), - `httr::DELETE` = mock_httr_replies( + httr_DELETE = mock_httr_replies( mock_httr_response( url = "http://localhost:8000/v1/query/query_1", status_code = 200, @@ -239,22 +255,6 @@ test_that("dbIsValid works with mock - dbClearResult", { status_code = 500, state = "" ) - ), - { - result <- dbSendQuery(conn, "SELECT 1") - expect_true(dbIsValid(result)) - expect_true(dbClearResult(result)) - expect_false(dbIsValid(result)) - expect_true(dbClearResult(result)) - expect_false(dbIsValid(result)) - - result <- dbSendQuery(conn, "SELECT 2") - expect_false(dbClearResult(result), label = "DELETE fails") - expect_true(dbIsValid(result)) - - result <- dbSendQuery(conn, "SELECT 3") - expect_true(dbClearResult(result), label = "complete query") - expect_true(dbIsValid(result)) - } + ) ) }) diff --git a/tests/testthat/test-dbListFields.R b/tests/testthat/test-dbListFields.R index d75d307..587b47f 100644 --- a/tests/testthat/test-dbListFields.R +++ b/tests/testthat/test-dbListFields.R @@ -44,8 +44,15 @@ test_that("dbListFields works with identifier", { test_that("dbListFields works with mock - PrestoConnection", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + expect_equal(dbListFields(conn, "two_columns"), c("column1", "column2")) + expect_error( + dbListFields(conn, "__non_existent_table__"), + "Query .* failed" + ) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -62,7 +69,7 @@ test_that("dbListFields works with mock - PrestoConnection", { query_id = "query_2" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -77,27 +84,45 @@ test_that("dbListFields works with mock - PrestoConnection", { state = "FAILED", ) ), - `httr::DELETE` = mock_httr_replies( + httr_DELETE = mock_httr_replies( mock_httr_response( url = "http://localhost:8000/v1/query/query_2", status_code = 200, state = "" ) - ), - { - expect_equal(dbListFields(conn, "two_columns"), c("column1", "column2")) - expect_error( - dbListFields(conn, "__non_existent_table__"), - "Query .* failed" - ) - } + ) ) }) test_that("dbListFields works with mock - PrestoResult", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + result <- dbSendQuery(conn, "SELECT * FROM two_columns") + expect_true(dbIsValid(result)) + expect_equal(dbListFields(result), c("column1", "column2")) + + result <- dbSendQuery(conn, "SELECT * FROM __non_existent_table__") + expect_true(dbIsValid(result)) + expect_error( + dbListFields(result), + "Query.*failed: Table __non_existent_table__ does not exist" + ) + + result <- dbSendQuery(conn, "SELECT * FROM empty_table") + expect_equal(dbListFields(result), character(0)) + + result <- dbSendQuery(conn, "SELECT * FROM empty_table") + expect_true(dbClearResult(result)) + expect_error( + dbListFields(result), + "The result object is not valid" + ) + + result <- dbSendQuery(conn, "SELECT * FROM three_columns") + expect_equal(dbListFields(result), c("a", "b", "c")) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -150,7 +175,7 @@ test_that("dbListFields works with mock - PrestoResult", { query_id = "query_6" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -199,7 +224,7 @@ test_that("dbListFields works with mock - PrestoResult", { state = "FINISHED", ) ), - `httr::DELETE` = mock_httr_replies( + httr_DELETE = mock_httr_replies( mock_httr_response( url = "http://localhost:8000/v1/query_3", status_code = 200, @@ -215,39 +240,29 @@ test_that("dbListFields works with mock - PrestoResult", { status_code = 200, state = "" ) - ), + ) + ) +}) + +test_that("dbListFields works with mock - PrestoResult - POST data", { + conn <- setup_mock_connection() + with_mocked_bindings( { result <- dbSendQuery(conn, "SELECT * FROM two_columns") expect_true(dbIsValid(result)) expect_equal(dbListFields(result), c("column1", "column2")) + expect_equal(dbFetch(result), tibble::tibble(column1 = 3, column2 = 4)) + expect_true(dbHasCompleted(result)) - result <- dbSendQuery(conn, "SELECT * FROM __non_existent_table__") + result <- dbSendQuery(conn, "SELECT * FROM two_rows") expect_true(dbIsValid(result)) - expect_error( - dbListFields(result), - "Query.*failed: Table __non_existent_table__ does not exist" - ) - - result <- dbSendQuery(conn, "SELECT * FROM empty_table") - expect_equal(dbListFields(result), character(0)) - - result <- dbSendQuery(conn, "SELECT * FROM empty_table") - expect_true(dbClearResult(result)) - expect_error( - dbListFields(result), - "The result object is not valid" - ) - - result <- dbSendQuery(conn, "SELECT * FROM three_columns") - expect_equal(dbListFields(result), c("a", "b", "c")) - } - ) -}) - -test_that("dbListFields works with mock - PrestoResult - POST data", { - conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + expect_equal(dbListFields(result), c("column1", "column2")) + expect_equal(dbFetch(result), tibble::tibble(column1 = 5, column2 = 6)) + expect_false(dbHasCompleted(result)) + expect_equal(dbFetch(result), tibble::tibble(column1 = 7, column2 = 8)) + expect_true(dbHasCompleted(result)) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -264,36 +279,39 @@ test_that("dbListFields works with mock - PrestoResult - POST data", { next_uri = "http://localhost:8000/query_2/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_2/1", status_code = 200, data = data.frame(column1 = 7, column2 = 8), state = "FINISHED", ) - ), + ) + ) +}) + +test_that("dbListFields works with mock - PrestoResult - POST columns", { + conn <- setup_mock_connection() + with_mocked_bindings( { result <- dbSendQuery(conn, "SELECT * FROM two_columns") expect_true(dbIsValid(result)) expect_equal(dbListFields(result), c("column1", "column2")) - expect_equal(dbFetch(result), tibble::tibble(column1 = 3, column2 = 4)) + expect_equal(dbFetch(result), tibble::tibble(column1 = 9, column2 = 10)) expect_true(dbHasCompleted(result)) - result <- dbSendQuery(conn, "SELECT * FROM two_rows") + result <- dbSendQuery(conn, "SELECT * FROM other_two_columns") expect_true(dbIsValid(result)) - expect_equal(dbListFields(result), c("column1", "column2")) - expect_equal(dbFetch(result), tibble::tibble(column1 = 5, column2 = 6)) + expect_equal(dbListFields(result), c("column3", "column4")) + expect_equal( + dbFetch(result), + tibble::tibble(column3 = 1, column4 = 2)[FALSE, , drop = FALSE] + ) expect_false(dbHasCompleted(result)) - expect_equal(dbFetch(result), tibble::tibble(column1 = 7, column2 = 8)) + expect_equal(dbFetch(result), tibble::tibble(column3 = 13, column4 = 14)) expect_true(dbHasCompleted(result)) - } - ) -}) - -test_that("dbListFields works with mock - PrestoResult - POST columns", { - conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + }, + httr_POST = mock_httr_replies( list( url = "http://localhost:8000/v1/statement", response = structure( @@ -345,7 +363,7 @@ test_that("dbListFields works with mock - PrestoResult - POST columns", { request_body = "SELECT \\* FROM other_two_columns" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -382,24 +400,6 @@ test_that("dbListFields works with mock - PrestoResult - POST columns", { data = data.frame(column3 = 13, column4 = 14), state = "FINISHED", ) - ), - { - result <- dbSendQuery(conn, "SELECT * FROM two_columns") - expect_true(dbIsValid(result)) - expect_equal(dbListFields(result), c("column1", "column2")) - expect_equal(dbFetch(result), tibble::tibble(column1 = 9, column2 = 10)) - expect_true(dbHasCompleted(result)) - - result <- dbSendQuery(conn, "SELECT * FROM other_two_columns") - expect_true(dbIsValid(result)) - expect_equal(dbListFields(result), c("column3", "column4")) - expect_equal( - dbFetch(result), - tibble::tibble(column3 = 1, column4 = 2)[FALSE, , drop = FALSE] - ) - expect_false(dbHasCompleted(result)) - expect_equal(dbFetch(result), tibble::tibble(column3 = 13, column4 = 14)) - expect_true(dbHasCompleted(result)) - } + ) ) }) diff --git a/tests/testthat/test-dbListTables.R b/tests/testthat/test-dbListTables.R index e45aadd..6285d91 100644 --- a/tests/testthat/test-dbListTables.R +++ b/tests/testthat/test-dbListTables.R @@ -20,8 +20,12 @@ test_that("dbListTables works with live database", { test_that("dbListTables works with mock", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + expect_equal(dbListTables(conn), letters) + expect_equal(dbListTables(conn, pattern = "_no_table_"), character(0)) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -37,7 +41,7 @@ test_that("dbListTables works with mock", { next_uri = "http://localhost:8000/query_2/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -50,10 +54,6 @@ test_that("dbListTables works with mock", { data = data.frame(Table = "a", stringsAsFactors = FALSE)[FALSE, ], state = "FINISHED" ) - ), - { - expect_equal(dbListTables(conn), letters) - expect_equal(dbListTables(conn, pattern = "_no_table_"), character(0)) - } + ) ) }) diff --git a/tests/testthat/test-dbSendQuery.R b/tests/testthat/test-dbSendQuery.R index 853fb35..33b14e5 100644 --- a/tests/testthat/test-dbSendQuery.R +++ b/tests/testthat/test-dbSendQuery.R @@ -52,8 +52,20 @@ test_that("dbSendQuery works with live database", { test_that("dbSendQuery works with mock - status code 404", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = function(url, body, ...) { + with_mocked_bindings( + { + assign("request.count", 0, envir = environment(httr_POST)) + result <- dbSendQuery(conn, "SELECT 1 AS n") + expect_true(dbIsValid(result)) + + broken_conn <- conn + broken_conn@host <- "http://broken_host" + expect_error( + dbSendQuery(broken_conn, "SELECT 1 AS n"), + "Received error response \\(HTTP 404\\).*" + ) + }, + httr_POST = function(url, body, ...) { response_404 <- mock_httr_response( url, status_code = 404, @@ -76,26 +88,19 @@ test_that("dbSendQuery works with mock - status code 404", { request_body = "SELECT 1 AS n", next_uri = "http://localhost:8000/query_1/1" )[["response"]]) - }, - { - assign("request.count", 0, envir = environment(httr::POST)) - result <- dbSendQuery(conn, "SELECT 1 AS n") - expect_true(dbIsValid(result)) - - broken_conn <- conn - broken_conn@host <- "http://broken_host" - expect_error( - dbSendQuery(broken_conn, "SELECT 1 AS n"), - "Received error response \\(HTTP 404\\).*" - ) } ) }) test_that("dbSendQuery works with mock - status code 503", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = function(url, body, ...) { + with_mocked_bindings( + { + assign("request.count", 0, envir = environment(httr_POST)) + result <- dbSendQuery(conn, "SELECT 1 AS n") + expect_true(dbIsValid(result)) + }, + httr_POST = function(url, body, ...) { if (request.count == 0) { request.count <<- 1 return(mock_httr_replies( @@ -116,19 +121,17 @@ test_that("dbSendQuery works with mock - status code 503", { next_uri = "http://localhost:8000/query_1/1" ) )(url, body = body, ...)) - }, - { - assign("request.count", 0, envir = environment(httr::POST)) - result <- dbSendQuery(conn, "SELECT 1 AS n") - expect_true(dbIsValid(result)) } ) }) test_that("dbSendQuery works with mock - status code 400", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + expect_error(dbSendQuery(conn, "SELECT 1"), '"Servers down"') + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 400, @@ -136,93 +139,72 @@ test_that("dbSendQuery works with mock - status code 400", { extra_content = list(error = list(message = "Servers down")), state = "" ) - ), - { - expect_error(dbSendQuery(conn, "SELECT 1"), '"Servers down"') - } + ) ) }) test_that("dbSendQuery works with mock - status code 200, FAILED", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + expect_error(dbSendQuery(conn, "SELECT 1"), "Query .* failed:") + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, request_body = "SELECT 1", state = "FAILED" ) - ), - { - expect_error(dbSendQuery(conn, "SELECT 1"), "Query .* failed:") - } + ) ) }) test_that("dbSendQuery works with mock - status code 500", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + expect_error( + dbSendQuery(conn, "SELECT 1"), + "Received error response \\(HTTP 500\\):.*" + ) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 500, request_body = "SELECT 1", state = "" ) - ), - { - expect_error( - dbSendQuery(conn, "SELECT 1"), - "Received error response \\(HTTP 500\\):.*" - ) - } + ) ) }) test_that("dbSendQuery works with mock - regular", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( - mock_httr_response( - "http://localhost:8000/v1/statement", - status_code = 200, - request_body = "SELECT 1", - state = "RUNNING", - next_uri = "http://localhost:8000/query_1/1" - ) - ), + with_mocked_bindings( { result <- dbSendQuery(conn, "SELECT 1") expect_is(result, "PrestoResult") expect_equal(result@statement, "SELECT 1") expect_equal(result@query$fetchedRowCount(), 0) expect_false(result@query$hasCompleted()) - } - ) -}) - -test_that("dbSendQuery works with mock - POST data", { - conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, - request_body = "SELECT 1 AS x", + request_body = "SELECT 1", state = "RUNNING", - data = data.frame(x = 1), next_uri = "http://localhost:8000/query_1/1" ) - ), - `httr::GET` = mock_httr_replies( - mock_httr_response( - "http://localhost:8000/query_1/1", - status_code = 200, - state = "FINISHED", - data = data.frame() - ) - ), + ) + ) +}) + +test_that("dbSendQuery works with mock - POST data", { + conn <- setup_mock_connection() + with_mocked_bindings( { result <- dbSendQuery(conn, "SELECT 1 AS x") expect_is(result, "PrestoResult") @@ -242,6 +224,24 @@ test_that("dbSendQuery works with mock - POST data", { expect_true(result@query$hasCompleted()) expect_equal(result@query$fetchedRowCount(), 1) expect_true(dbHasCompleted(result)) - } + }, + httr_POST = mock_httr_replies( + mock_httr_response( + "http://localhost:8000/v1/statement", + status_code = 200, + request_body = "SELECT 1 AS x", + state = "RUNNING", + data = data.frame(x = 1), + next_uri = "http://localhost:8000/query_1/1" + ) + ), + httr_GET = mock_httr_replies( + mock_httr_response( + "http://localhost:8000/query_1/1", + status_code = 200, + state = "FINISHED", + data = data.frame() + ) + ) ) }) diff --git a/tests/testthat/test-db_explain.R b/tests/testthat/test-db_explain.R index 4d1fb59..ab4a69d 100644 --- a/tests/testthat/test-db_explain.R +++ b/tests/testthat/test-db_explain.R @@ -20,8 +20,12 @@ test_that("db_explain works with live database", { test_that("db_explain works with mock", { s <- setup_mock_dplyr_connection()[["db"]] - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + result <- dplyr::db_explain(s[["con"]], dplyr::sql("SHOW TABLES")) + expect_equal(result, "explanation") + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -30,17 +34,13 @@ test_that("db_explain works with mock", { next_uri = "http://localhost:8000/query_1/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, data = data.frame(e = "explanation", stringsAsFactors = FALSE), state = "FINISHED" ) - ), - { - result <- dplyr::db_explain(s[["con"]], dplyr::sql("SHOW TABLES")) - expect_equal(result, "explanation") - } + ) ) }) diff --git a/tests/testthat/test-db_query_fields.R b/tests/testthat/test-db_query_fields.R index 6f2c6bc..59f13ec 100644 --- a/tests/testthat/test-db_query_fields.R +++ b/tests/testthat/test-db_query_fields.R @@ -37,8 +37,41 @@ test_that("db_query_fields works with live database", { test_that("db_query_fields works with mock", { s <- setup_mock_dplyr_connection()[["db"]] - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + fields <- dplyr::db_query_fields( + s[["con"]], + dplyr::sql_subquery( + s[["con"]], + dplyr::sql("SELECT 1 AS a, 't' AS b"), + name = "a" + ) + ) + expect_equal(fields, c("a", "b")) + + expect_equal( + dplyr::db_query_fields( + s[["con"]], + dplyr::ident("two_columns") + ), + c("a", "b") + ) + + expect_equal( + dplyr::db_query_fields( + s[["con"]], + dplyr::ident("empty_table") + ), + character(0) + ) + expect_error( + dplyr::db_query_fields( + s[["con"]], + dplyr::ident("__non_existent_table__") + ) + ) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -197,7 +230,7 @@ test_that("db_query_fields works with mock", { next_uri = "http://localhost:8000/query_4/1", ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -224,45 +257,12 @@ test_that("db_query_fields works with mock", { state = "FINISHED", ) ), - `httr::DELETE` = function(url, body, ...) { + httr_DELETE = function(url, body, ...) { mock_httr_response( url = "http://localhost:8000/v1/query/query_1", status_code = 200, state = "" )[["response"]] - }, - { - fields <- dplyr::db_query_fields( - s[["con"]], - dplyr::sql_subquery( - s[["con"]], - dplyr::sql("SELECT 1 AS a, 't' AS b"), - name = "a" - ) - ) - expect_equal(fields, c("a", "b")) - - expect_equal( - dplyr::db_query_fields( - s[["con"]], - dplyr::ident("two_columns") - ), - c("a", "b") - ) - - expect_equal( - dplyr::db_query_fields( - s[["con"]], - dplyr::ident("empty_table") - ), - character(0) - ) - expect_error( - dplyr::db_query_fields( - s[["con"]], - dplyr::ident("__non_existent_table__") - ) - ) } ) }) diff --git a/tests/testthat/test-fetch.R b/tests/testthat/test-fetch.R index fe96320..d74e591 100644 --- a/tests/testthat/test-fetch.R +++ b/tests/testthat/test-fetch.R @@ -24,8 +24,21 @@ test_that("fetch works with live database", { test_that("fetch works with mock", { conn <- setup_mock_connection() - with_mock( - `httr::POST` = mock_httr_replies( + with_mocked_bindings( + { + result <- dbSendQuery(conn, "SELECT 1 AS n") + expect_error( + fetch(result, 1), + ".*fetching custom number of rows.*is not supported.*" + ) + expect_equal(fetch(result), tibble::tibble(n = 1)) + expect_equal(fetch(result, -1), tibble::tibble(n = 2)) + expect_true(dbHasCompleted(result)) + + result <- dbSendQuery(conn, "SELECT 1 AS n") + expect_equal(fetch(result, -1), tibble::tibble(n = c(1, 2))) + }, + httr_POST = mock_httr_replies( mock_httr_response( "http://localhost:8000/v1/statement", status_code = 200, @@ -34,7 +47,7 @@ test_that("fetch works with mock", { next_uri = "http://localhost:8000/query_1/1" ) ), - `httr::GET` = mock_httr_replies( + httr_GET = mock_httr_replies( mock_httr_response( "http://localhost:8000/query_1/1", status_code = 200, @@ -48,19 +61,6 @@ test_that("fetch works with mock", { data = data.frame(n = 2), state = "FINISHED" ) - ), - { - result <- dbSendQuery(conn, "SELECT 1 AS n") - expect_error( - fetch(result, 1), - ".*fetching custom number of rows.*is not supported.*" - ) - expect_equal(fetch(result), tibble::tibble(n = 1)) - expect_equal(fetch(result, -1), tibble::tibble(n = 2)) - expect_true(dbHasCompleted(result)) - - result <- dbSendQuery(conn, "SELECT 1 AS n") - expect_equal(fetch(result, -1), tibble::tibble(n = c(1, 2))) - } + ) ) })