From 912c7c1e4bfabe74a9edfa8f3f75cf3c43cff48c Mon Sep 17 00:00:00 2001 From: Anthony Sena Date: Wed, 10 Jan 2024 12:24:05 -0500 Subject: [PATCH] Add informative error message when connectionDetailsReference not set - fixes #100 --- R/Settings.R | 4 ++++ tests/testthat/test-Settings.R | 10 +++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/R/Settings.R b/R/Settings.R index 9fb31f6..0713b36 100644 --- a/R/Settings.R +++ b/R/Settings.R @@ -269,6 +269,10 @@ retrieveConnectionDetails <- function(connectionDetailsReference, keyringName = checkmate::assertLogical(x = (is.null(keyringName) || keyringName %in% keyringList$keyring), add = errorMessages) checkmate::reportAssertions(collection = errorMessages) + if (!connectionDetailsReference %in% keyring::key_list(keyring = keyringName)$service) { + stop("Connection details with connectionDetailsReference = \"", connectionDetailsReference, "\" were not found in your keyring. Please check that you have used the Strategus storeConnectionDetails function to save your connection details with this connectionDetailsReference name.") + } + # If the keyring is locked, unlock it, set the value and then re-lock it keyringLocked <- unlockKeyring(keyringName = keyringName) diff --git a/tests/testthat/test-Settings.R b/tests/testthat/test-Settings.R index be23755..d5f8362 100644 --- a/tests/testthat/test-Settings.R +++ b/tests/testthat/test-Settings.R @@ -91,6 +91,14 @@ test_that("Store and retrieve connection details", { connFromKeyring <- DatabaseConnector::connect( connectionDetailsFromKeyring ) - DatabaseConnector::disconnect(connFromKeyring) + expect_silent(DatabaseConnector::disconnect(connFromKeyring)) } }) + +test_that("Retrieve connection details that do not exists throws informative error", { + # Setup keyring for the test + Sys.setenv("STRATEGUS_KEYRING_PASSWORD" = keyringPassword) + createKeyringForUnitTest(selectedKeyring = keyringName, selectedKeyringPassword = keyringPassword) + on.exit(deleteKeyringForUnitTest()) + expect_error(retrieveConnectionDetails(connectionDetailsReference = "does-not-exist", keyringName = keyringName)) +})