From cb78ed59962ddb460120d0c02b72411b565e37bf Mon Sep 17 00:00:00 2001 From: HomoCodens <32675029+HomoCodens@users.noreply.github.com> Date: Wed, 7 Oct 2020 13:20:50 +0200 Subject: [PATCH 1/4] dontrun examples for unexported date helpers --- R/utils.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/utils.R b/R/utils.R index fefbc09f..46a62225 100644 --- a/R/utils.R +++ b/R/utils.R @@ -11,7 +11,7 @@ #' @param as.string logical If as.string is TRUE the string representation of the #' Date is returned, otherwise a Date object. #' @examples -#' index_to_date(2020.25) +#' \dontrun{index_to_date(2020.25)} index_to_date <- function (x, as.string = FALSE) { if(inherits(x, "Date")) { @@ -45,7 +45,7 @@ index_to_date <- function (x, as.string = FALSE) #' #' @return The numeric representation of the date that can be used with ts #' @examples -#' date_to_index("2020-07-01") +#' \dontrun{date_to_index("2020-07-01")} date_to_index <- function(x) { x <- as.character(x) components <- as.numeric(unlist(strsplit(x, "-"))) From 841f7cc904bf7a4cba4759388d74915abf5ba8d9 Mon Sep 17 00:00:00 2001 From: HomoCodens <32675029+HomoCodens@users.noreply.github.com> Date: Wed, 7 Oct 2020 13:40:59 +0200 Subject: [PATCH 2/4] fix s3 method consistency --- R/metadata.R | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/R/metadata.R b/R/metadata.R index 08a0812f..92eb31dd 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -7,7 +7,7 @@ create_meta <- function(...) { } #' @export -create_meta.list <- function(metadata) { +create_meta.list <- function(metadata, ...) { if(is.null(names(metadata)) || any(nchar(names(metadata)) == 0)) { stop("All fields of metadata objects must be named!") } @@ -50,7 +50,7 @@ as.tsmeta <- function(meta, ...) { } #' @export -as.tsmeta.data.table <- function(meta) { +as.tsmeta.data.table <- function(meta, ...) { if(nrow(meta) > 0) { out <- apply(meta[, -"ts_key", with = FALSE], 1, as.list) names(out) <- meta$ts_key @@ -63,7 +63,7 @@ as.tsmeta.data.table <- function(meta) { } #' @export -as.tsmeta.list <- function(meta, check_depth = TRUE) { +as.tsmeta.list <- function(meta, check_depth = TRUE, ...) { if(check_depth && !has_depth_2(meta) && length(meta) > 0) { stop("A meta list must have exactly depth 2!") } @@ -76,13 +76,15 @@ as.tsmeta.list <- function(meta, check_depth = TRUE) { } #' @export -as.tsmeta.data.frame <- function(meta) { +as.tsmeta.data.frame <- function(meta, ...) { as.tsmeta(as.data.table(meta)) } #' @export -as.tsmeta.tsmeta <- identity +as.tsmeta.tsmeta <- function(meta, ...) { + x +} # printers ---------------------------------------------------------------- From 83355b480a34862ed3bc91093c25ac69d4d04daf Mon Sep 17 00:00:00 2001 From: HomoCodens <32675029+HomoCodens@users.noreply.github.com> Date: Wed, 7 Oct 2020 14:00:26 +0200 Subject: [PATCH 3/4] fix as.tsmeta.tsmets --- R/metadata.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/metadata.R b/R/metadata.R index 92eb31dd..150ad178 100644 --- a/R/metadata.R +++ b/R/metadata.R @@ -83,7 +83,7 @@ as.tsmeta.data.frame <- function(meta, ...) { #' @export as.tsmeta.tsmeta <- function(meta, ...) { - x + meta } From 3b8aa06a2b44b5e95e23bfe60f677ed0cfc8d897 Mon Sep 17 00:00:00 2001 From: HomoCodens <32675029+HomoCodens@users.noreply.github.com> Date: Wed, 7 Oct 2020 14:30:44 +0200 Subject: [PATCH 4/4] fix mocking of unexported functions --- tests/testthat/test_db_create_connection.R | 6 ++++-- tests/testthat/test_db_with_helpers.R | 16 ++++++++-------- tests/testthat/test_json_to_ts.R | 2 +- tests/testthat/test_metadata.R | 2 +- tests/testthat/test_read_metadata.R | 8 ++++---- tests/testthat/test_release_calendar.R | 4 ++-- tests/testthat/test_store_metadata.R | 16 ++++++++-------- 7 files changed, 28 insertions(+), 26 deletions(-) diff --git a/tests/testthat/test_db_create_connection.R b/tests/testthat/test_db_create_connection.R index caba7b45..40bdb928 100644 --- a/tests/testthat/test_db_create_connection.R +++ b/tests/testthat/test_db_create_connection.R @@ -94,6 +94,8 @@ test_that("password from env, missing", { }) test_that("asking for password", { + skip_if_not(require(rstudioapi) && rstudioapi::isAvailable()) + fake_dbConnect = mock() with_mock( @@ -122,7 +124,7 @@ test_that("getting password from file", { Sys.info = mock(list(user = "bobby")), file.exists = mock(TRUE), dbConnect = fake_dbConnect, - readPasswordFile = fake_readlines, + "timeseriesdb:::readPasswordFile" = fake_readlines, { db_connection_create("mydb", passwd = "my/password/file", @@ -146,7 +148,7 @@ test_that("password from file, line no too big", { with_mock( file.exists = mock(TRUE), - readPasswordFile = fake_readlines, + "timeseriesdb:::readPasswordFile" = fake_readlines, { expect_error( db_connection_create("mydb", diff --git a/tests/testthat/test_db_with_helpers.R b/tests/testthat/test_db_with_helpers.R index 7f59aac2..55449d2a 100644 --- a/tests/testthat/test_db_with_helpers.R +++ b/tests/testthat/test_db_with_helpers.R @@ -6,7 +6,7 @@ test_that("It creates a temp table and hands it over to admin", { with_mock( dbWriteTable = fake_dbWriteTable, - db_grant_to_admin = fake_db_grant_to_admin, + "timeseriesdb:::db_grant_to_admin" = fake_db_grant_to_admin, dbRemoveTable = mock(), { db_with_temp_table("con", "temp", "content", "field.types", {1+1}, "schema") @@ -37,7 +37,7 @@ test_that("it evaluates the code", { fake_code = mock() with_mock( dbWriteTable = mock(), - db_grant_to_admin = mock(), + "timeseriesdb:::db_grant_to_admin" = mock(), dbRemoveTable = mock(), { db_with_temp_table("con", "temp", "content", "field.types", fake_code(), "schema") @@ -51,7 +51,7 @@ test_that("it removes the table even in error case", { with_mock( dbWriteTable = mock(), - db_grant_to_admin = mock(), + "timeseriesdb:::db_grant_to_admin" = mock(), dbRemoveTable = fake_remove, { capture_error(db_with_temp_table("con", "temp", "content", "field.types", stop("oh noes"), "schema")) @@ -67,7 +67,7 @@ context("db_with_tmp_read") test_that("It warns when using regex with multiple ts_keys", { with_mock( dbWriteTable = mock(), - db_grant_to_admin = mock(), + "timeseriesdb:::db_grant_to_admin" = mock(), dbRemoveTable = mock(), dbExecute = mock(), dbQuoteIdentifier = mock(), @@ -85,7 +85,7 @@ test_that("It only uses the first element of ts_keys when regex == TRUE", { db_quote_literal_mock = mock() with_mock( dbWriteTable = mock(), - db_grant_to_admin = mock(), + "timeseriesdb:::db_grant_to_admin" = mock(), dbRemoveTable = mock(), dbExecute = mock(), dbQuoteIdentifier = mock(), @@ -103,7 +103,7 @@ test_that("It evaluates the code", { with_mock( dbWriteTable = mock(), - db_grant_to_admin = mock(), + "timeseriesdb:::db_grant_to_admin" = mock(), dbRemoveTable = mock(), dbExecute = mock(), dbQuoteIdentifier = mock(), @@ -121,7 +121,7 @@ test_that("It removes the temp table in error case", { with_mock( dbWriteTable = mock(), - db_grant_to_admin = mock(), + "timeseriesdb:::db_grant_to_admin" = mock(), dbRemoveTable = fake_dbRemoveTable, dbExecute = mock(), dbQuoteIdentifier = mock(), @@ -144,7 +144,7 @@ test_with_fresh_db(con_admin, "temp_ts_read gets cleaned up in failure case", { boom_bot <- mock(stop("Kablammy!")) with_mock( - get_tsl_from_res = boom_bot, + "timeseriesdb:::get_tsl_from_res" = boom_bot, { e <- capture_error(db_ts_read(con_admin, "rts1", schema = "tsdb_test")) diff --git a/tests/testthat/test_json_to_ts.R b/tests/testthat/test_json_to_ts.R index 662d51a6..01a7097c 100644 --- a/tests/testthat/test_json_to_ts.R +++ b/tests/testthat/test_json_to_ts.R @@ -28,7 +28,7 @@ mock_date_to_index <- mock(2019) test_that("it converts a regular json to ts", { # Is this too pedantic? with_mock( - date_to_index = mock_date_to_index, + "timeseriesdb:::date_to_index" = mock_date_to_index, { x <- json_to_ts(regular_json) expect_is(x, "ts") diff --git a/tests/testthat/test_metadata.R b/tests/testthat/test_metadata.R index a7d77c89..ab5076d9 100644 --- a/tests/testthat/test_metadata.R +++ b/tests/testthat/test_metadata.R @@ -147,7 +147,7 @@ test_that("as.tsmeta.data.table skips depth check", { fake_as.tsmeta.list <- mock() with_mock( - as.tsmeta.list = fake_as.tsmeta.list, + "timeseriesdb:::as.tsmeta.list" = fake_as.tsmeta.list, { as.tsmeta(as.data.table(meta_df())) } diff --git a/tests/testthat/test_read_metadata.R b/tests/testthat/test_read_metadata.R index b7c4f4d2..a6bfc9bd 100644 --- a/tests/testthat/test_read_metadata.R +++ b/tests/testthat/test_read_metadata.R @@ -14,8 +14,8 @@ test_that("is passes correct args to db_call_function unlocalized", { fake_db_with_tmp_read <- function(con, keys, regex, code, schema){force(code)} with_mock( - db_call_function = fake_db_call_function, - db_with_tmp_read = fake_db_with_tmp_read, + "timeseriesdb:::db_call_function" = fake_db_call_function, + "timeseriesdb:::db_with_tmp_read" = fake_db_with_tmp_read, { db_metadata_read("con", "vts1", valid_on = "2020-01-01", schema = "schema") @@ -38,8 +38,8 @@ test_that("is passes correct args to db_call_function localized", { fake_db_with_tmp_read <- function(con, keys, regex, code, schema){force(code)} with_mock( - db_with_tmp_read = fake_db_with_tmp_read, - db_call_function = fake_db_call_function, + "timeseriesdb:::db_with_tmp_read" = fake_db_with_tmp_read, + "timeseriesdb:::db_call_function" = fake_db_call_function, { db_metadata_read("con", "vts1", valid_on = "2020-01-01", schema = "schema", locale = "de") diff --git a/tests/testthat/test_release_calendar.R b/tests/testthat/test_release_calendar.R index f4407460..b656b829 100644 --- a/tests/testthat/test_release_calendar.R +++ b/tests/testthat/test_release_calendar.R @@ -20,9 +20,9 @@ test_that("defaults", { schema){force(code)} with_mock( - db_call_function = fake_db_call_function, + "timeseriesdb:::db_call_function" = fake_db_call_function, dbWriteTable = mock(), - db_with_temp_table = fake_db_with_tmp_table, + "timeseriesdb:::db_with_temp_table" = fake_db_with_tmp_table, { db_release_create("con", "a_release", diff --git a/tests/testthat/test_store_metadata.R b/tests/testthat/test_store_metadata.R index c4d4af58..0562bdae 100644 --- a/tests/testthat/test_store_metadata.R +++ b/tests/testthat/test_store_metadata.R @@ -49,11 +49,11 @@ test_that("is passes correct args to db_call_function unlocalized", { fake_db_call_function = mock() with_mock( - db_with_temp_table = function(con, name, content, field.types, code, schema){eval(code)}, - toJSON = mock("json"), - fromJSON = mock(list(status = "ok")), + "timeseriesdb:::db_with_temp_table" = function(con, name, content, field.types, code, schema){eval(code)}, + "jsonlite::toJSON" = mock("json"), + "jsonlite::fromJSON" = mock(list(status = "ok")), dbExecute = mock(), - db_call_function = fake_db_call_function, + "timeseriesdb:::db_call_function" = fake_db_call_function, { db_metadata_store("con", as.tsmeta.list( @@ -334,11 +334,11 @@ test_that("is passes correct args to db_call_function localized", { fake_db_call_function = mock() with_mock( - db_with_temp_table = function(con, name, content, field.types, code, schema){eval(code)}, - toJSON = mock("json"), - fromJSON = mock(list(status = "ok")), + "timeseriesdb:::db_with_temp_table" = function(con, name, content, field.types, code, schema){eval(code)}, + "jsonlite::toJSON" = mock("json"), + "jsonlite::fromJSON" = mock(list(status = "ok")), dbExecute = mock(), - db_call_function = fake_db_call_function, + "timeseriesdb:::db_call_function" = fake_db_call_function, { db_metadata_store("con", as.tsmeta.list(