From 47bf414a64d002d60eb98ed83f27ece77859d367 Mon Sep 17 00:00:00 2001 From: PietrH Date: Tue, 22 Oct 2024 10:02:35 +0200 Subject: [PATCH 1/5] remove unused helper -> moved to etnservice --- R/utils.R | 40 --------------------------- tests/testthat/test-check_date_time.R | 20 -------------- 2 files changed, 60 deletions(-) delete mode 100644 tests/testthat/test-check_date_time.R diff --git a/R/utils.R b/R/utils.R index aaa79571..9cb2dbb3 100644 --- a/R/utils.R +++ b/R/utils.R @@ -47,46 +47,6 @@ check_value <- function(x, y, name = "value", lowercase = FALSE) { return(x) } -#' Check if the string input can be converted to a date -#' -#' Returns `FALSE`` or the cleaned character version of the date -#' (acknowledgments to micstr/isdate.R). -#' -#' @param date_time Character. A character representation of a date. -#' @param date_name Character. Informative description to user about type of -#' date. -#' @return `FALSE` | character -#' @family helper functions -#' @noRd -#' @examples -#' \dontrun{ -#' check_date_time("1985-11-21") -#' check_date_time("1985-11") -#' check_date_time("1985") -#' check_date_time("1985-04-31") # invalid date -#' check_date_time("01-03-1973") # invalid format -#' } -check_date_time <- function(date_time, date_name = "start_date") { - parsed <- tryCatch( - lubridate::parse_date_time(date_time, orders = c("ymd", "ym", "y")), - warning = function(warning) { - if (grepl("No formats found", warning$message)) { - stop(glue::glue( - "The given {date_name}, {date_time}, is not in a valid ", - "date format. Use a yyyy-mm-dd format or shorter, ", - "e.g. 2012-11-21, 2012-11 or 2012." - )) - } else { - stop(glue::glue( - "The given {date_name}, {date_time} can not be interpreted ", - "as a valid date." - )) - } - } - ) - as.character(parsed) -} - #' Get the credentials from environment variables, or set them manually #' #' By default, it's not necessary to set any values in this function as it's diff --git a/tests/testthat/test-check_date_time.R b/tests/testthat/test-check_date_time.R deleted file mode 100644 index d7e5c778..00000000 --- a/tests/testthat/test-check_date_time.R +++ /dev/null @@ -1,20 +0,0 @@ -test_that("Test input", { - expect_true(check_date_time("1985-11-21") == "1985-11-21") - expect_true(check_date_time("1985-11") == "1985-11-01") - expect_true(check_date_time("1985") == "1985-01-01") - expect_error( - check_date_time("1985-04-31"), - paste( - "The given start_date, 1985-04-31 can not be interpreted", - "as a valid date." - ) - ) - expect_error( - check_date_time("01-03-1973"), - paste( - "The given start_date, 01-03-1973, is not in a valid date", - "format. Use a yyyy-mm-dd format or shorter,", - "e.g. 2012-11-21, 2012-11 or 2012." - ) - ) -}) From 96805a1e94d8107ae0fa3fe95fa0d01b78019b0e Mon Sep 17 00:00:00 2001 From: PietrH Date: Tue, 22 Oct 2024 10:04:23 +0200 Subject: [PATCH 2/5] local database connection is made via etnservice --- R/utils.R | 28 ---------------------------- tests/testthat/test-utils.R | 18 ------------------ 2 files changed, 46 deletions(-) diff --git a/R/utils.R b/R/utils.R index 9cb2dbb3..9622a823 100644 --- a/R/utils.R +++ b/R/utils.R @@ -345,31 +345,3 @@ conduct_parent_to_helpers <- function(api, return(out) } - -#' Create a local connection to the ETN database -#' -#' Connect to the ETN database using username and password. -#' -#' @param username Character. Username to use for the connection. -#' @param password Character. Password to use for the connection. -#' -#' @return ODBC connection to ETN database. -#' @noRd -create_connection <- function(credentials = get_credentials()) { - # Check the input arguments - assertthat::assert_that( - assertthat::is.string(credentials$username) - ) - assertthat::assert_that( - assertthat::is.string(credentials$password) - ) - - # Connect to the ETN database - con <- DBI::dbConnect( - odbc::odbc(), - "ETN", - uid = tolower(credentials$username), - pwd = credentials$password - ) - return(con) -} diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 81a8c97f..7999dcbf 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -17,21 +17,3 @@ test_that("deprecate_warn_connection() returns warning when connection is provid fixed = TRUE ) }) - -# create_connection() ----------------------------------------------------- -test_that("create_connection() can create a connection with the database", { - con <- create_connection() - expect_true(check_connection(con)) - expect_true(isClass(con, "PostgreSQL")) -}) - -test_that("create_connection() returns error on non character arguments", { - expect_error( - create_connection(list(username = 1, password = "password")), - regexp = "username is not a string" - ) - expect_error( - create_connection(credentials = list(username = "username", password = 1)), - regexp = "password is not a string" - ) -}) From 21c93fe6b055dca498f74a5ac48c2219791a65b6 Mon Sep 17 00:00:00 2001 From: PietrH Date: Tue, 22 Oct 2024 10:12:23 +0200 Subject: [PATCH 3/5] remove unused helper -> moved to etnservice --- R/utils.R | 13 ------------- tests/testthat/test-utils.R | 8 -------- 2 files changed, 21 deletions(-) diff --git a/R/utils.R b/R/utils.R index 9622a823..2e1782f8 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,18 +1,5 @@ # HELPER FUNCTIONS -#' Check the validity of the database connection -#' -#' @param connection A connection to the ETN database. Defaults to `con`. -#' @family helper functions -#' @noRd -check_connection <- function(connection) { - assertthat::assert_that( - methods::is(connection, "PostgreSQL"), - msg = "Not a connection object to database." - ) - assertthat::assert_that(connection@info$dbname == "ETN") -} - #' Check input value against valid values #' #' @param x Value(s) to test. diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 7999dcbf..881dc13c 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -1,11 +1,3 @@ -# check_connection() ------------------------------------------------------ -test_that("check_connection() returns error when connection is not valid", { - not_a_connection <- "This is not a valid connection object" - expect_error(check_connection(not_a_connection), - regexp = "Not a connection object to database.", - fixed = TRUE) -}) - # deprecate_warn_connection() --------------------------------------------- test_that("deprecate_warn_connection() returns warning when connection is provided", { # because this helper looks at the environment two levels up, it's not very From 49a2104cebfdf5fab6da674d751c0189bdd3b3f2 Mon Sep 17 00:00:00 2001 From: PietrH Date: Tue, 22 Oct 2024 10:14:48 +0200 Subject: [PATCH 4/5] Add utils for calling from etnservice namespace --- DESCRIPTION | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 564fa0c8..0e58d727 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,20 +23,15 @@ Depends: Imports: askpass, assertthat, - DBI, dplyr, etnservice, glue, httr, - jsonlite, lifecycle, - lubridate, methods, - odbc, readr, rlang, - stringr, - utils + stringr Suggests: formattable, leaflet, From 121d0977ebea863ffcc47747dd1b5ac5b86bdd1a Mon Sep 17 00:00:00 2001 From: PietrH Date: Tue, 22 Oct 2024 10:15:13 +0200 Subject: [PATCH 5/5] add utils to call from etnservice namespace --- DESCRIPTION | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 0e58d727..9076992d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,7 +31,8 @@ Imports: methods, readr, rlang, - stringr + stringr, + utils Suggests: formattable, leaflet,