Skip to content

Commit

Permalink
Merge pull request #332 from inbo/remove-indirect-dependencies
Browse files Browse the repository at this point in the history
Remove indirect dependencies
  • Loading branch information
PietrH authored Oct 22, 2024
2 parents afdf082 + 121d097 commit 85bf463
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 131 deletions.
4 changes: 0 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ Depends:
Imports:
askpass,
assertthat,
DBI,
dplyr,
etnservice,
glue,
httr,
jsonlite,
lifecycle,
lubridate,
methods,
odbc,
readr,
rlang,
stringr,
Expand Down
81 changes: 0 additions & 81 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -47,46 +34,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
Expand Down Expand Up @@ -385,31 +332,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)
}
20 changes: 0 additions & 20 deletions tests/testthat/test-check_date_time.R

This file was deleted.

26 changes: 0 additions & 26 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,21 +9,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"
)
})

0 comments on commit 85bf463

Please sign in to comment.