diff --git a/DESCRIPTION b/DESCRIPTION index a73fe22..8158b40 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -26,5 +26,6 @@ Imports: lifecycle, janitor Suggests: - testthat (>= 3.0.0) + testthat (>= 3.0.0), + withr Config/testthat/edition: 3 diff --git a/SQLRtools.Rproj b/SQLRtools.Rproj index 21a4da0..3cec3bb 100644 --- a/SQLRtools.Rproj +++ b/SQLRtools.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: e2646519-d824-45f7-8bd6-885d2b38f559 RestoreWorkspace: Default SaveWorkspace: Default diff --git a/tests/testthat/test-get_env_var.R b/tests/testthat/test-get_env_var.R new file mode 100644 index 0000000..3196eb5 --- /dev/null +++ b/tests/testthat/test-get_env_var.R @@ -0,0 +1,50 @@ +test_that("returns an expected password as if it were from keyring", { + local_mocked_bindings( + get_env_var = function(var_name) { + service = "GLOBAL-SETTING" + username = "keyring_words" + return(list(var_name = service, username = username)) + } + ) + + # Call the function + result <- SQLRtools::get_env_var(var_name = "GLOBAL-SETTING") + + # Check the result + expect_equal(result$username, "keyring_words") + expect_equal(result$var_name, "GLOBAL-SETTING") + +}) + +test_that("returns message when not keyring is not set up", { + + testthat::expect_error( + get_env_var(var_name = "Something"), + "variable Something not found using keyring or Sys.getenv()" + ) + +}) + + +test_that("warning if string from Sys.getenv but username is not rstudio-connect", { + + withr::local_envvar(c("USERNAME" = "something-connect")) + withr::local_envvar(c("GLOBAL_SETTING" = "system_words")) + + expect_warning( + SQLRtools::get_env_var(var_name = "GLOBAL_SETTING"), + "found in Renviron file but not windows credentials" + ) +}) + +test_that("returns string when username is rstudio-connect", { + + withr::local_envvar(c("USERNAME" = "rstudio-connect")) + withr::local_envvar(c("GLOBAL_SETTING" = "system_words")) + + # Check the result + expect_equal(SQLRtools::get_env_var(var_name = "GLOBAL_SETTING"), "system_words") + +}) + + diff --git a/tests/testthat/test-sql_server.R b/tests/testthat/test-sql_server.R new file mode 100644 index 0000000..322d90b --- /dev/null +++ b/tests/testthat/test-sql_server.R @@ -0,0 +1,97 @@ +test_that("expect R6 class object", { + local_mocked_bindings( + sql_server = function(driver, + server, + database, + port, + uid, + pwd) { + driver = "MySQL ODBC 8.0 Unicode Driver" + server = "HOST_NAME" + database = "MYSQL_DB" + port = "MYSQL_PORT" + uid = "MYSQL_USER" + pwd = "MYSQL_PASSWORD" + return(R6Class("Test", + public = list(driver = driver, + server = server, + database = database, + port = port, + uid = uid, + pwd = pwd) + )) + }) + + mysql_serv_mock <- sql_server(driver = "MySQL ODBC 8.0 Unicode Driver", + server = "HOST_NAME", + database = "MYSQL_DB", + port = "MYSQL_PORT", + uid = "MYSQL_USER", + pwd = "MYSQL_PASSWORD") + + mysql_serv_NULL <- sql_server(driver = NULL, + server = NULL, + database = NULL, + port = NULL, + uid = NULL, + pwd = NULL) + + expect_true(is.R6Class(mysql_serv_mock)) + expect_true(is.R6Class(mysql_serv_NULL)) + expect_error(mysql_serv_mock$new(), NA) + expect_error(mysql_serv_NULL$new(), NA) +}) + + +test_that("expect message when drop_table used as no table exists", { + local_mocked_bindings( + sql_server = function(driver, + server, + database, + port, + uid, + pwd, + table_name) { + driver = "driver" + server = "HOST_NAME" + database = "MYSQL_DB" + port = "MYSQL_PORT" + uid = "MYSQL_USER" + pwd = "MYSQL_PASSWORD" + table_name = NULL + return(R6Class("Test", + public = list(driver = driver, + server = server, + database = database, + port = port, + uid = uid, + pwd = pwd, + table_name = table_name) + )) + }) + + # create vector + test_table_name <- "SQLRtools_test_table" + + # create dummy data using dittodb + test_data <- data.frame(Int_field = 1:200, + char_field_1 = stri_rand_strings(200, sample(5:11, 5, replace = TRUE), '[a-zA-Z]'), + char_field_2 = stri_rand_strings(200, sample(5:11, 5, replace = TRUE), '[a-zA-Z]'), + date_field = sample(seq(as.Date('2018/01/01'), as.Date('2024/01/01'), by = "day"), 200), + date_time_field = sample(seq(as_datetime('2018-01-01 00:00:00'), + as_datetime('2024-01-01 00:00:00'), + by = "min"), 200)) + + mysql_serv_mock <- sql_server(driver = "driver", + server = "HOST_NAME", + database = "MYSQL_DB", + port = "MYSQL_PORT", + uid = "MYSQL_USER", + pwd = "MYSQL_PASSWORD", + table_name = NULL) + + expect_message(mysql_serv$drop_table(test_table_name), "table SQLRtools_test_table doesn't exist") + +}) + +