From 2ffa58841f411783c463bbd6a6f1af37412dafe6 Mon Sep 17 00:00:00 2001 From: Karim-Mane Date: Fri, 27 Oct 2023 10:54:53 +0000 Subject: [PATCH] check args default values and consistency --- R/check_dhis2_attributes-helpers.R | 56 +++++----- R/check_dhis2_attributes.R | 40 +++---- R/read_from_dhis2-helpers.R | 47 ++++++-- R/read_from_dhis2.R | 26 ++--- R/read_from_fingertips-helpers.R | 69 +++++++++--- R/read_from_fingertips.R | 17 ++- R/read_from_redcap-helpers.R | 72 ++++++++---- R/read_from_redcap.R | 10 +- R/read_servers.R | 19 ++-- R/read_servers_helpers.R | 103 ++++++++++++++---- R/readepi.R | 8 +- R/readepi_helpers.R | 9 +- R/show_tables.R | 7 +- man/get_dhis2_attributes.Rd | 6 +- man/readepi.Rd | 2 +- man/show_tables.Rd | 8 +- man/visualise_table.Rd | 7 +- .../test-check_dhis2_attributes-helpers.R | 72 ++++++------ tests/testthat/test-check_dhis2_attributes.R | 58 +++++----- tests/testthat/test-read_from_dhis2.R | 30 ++--- tests/testthat/test-read_from_dhis2_helpers.R | 65 +++++------ .../test-read_from_fingertips-helpers.R | 6 +- .../testthat/test-read_from_redcap-helpers.R | 91 ++++++++-------- tests/testthat/test-read_servers.R | 4 +- tests/testthat/test-show_tables.R | 12 +- 25 files changed, 525 insertions(+), 319 deletions(-) diff --git a/R/check_dhis2_attributes-helpers.R b/R/check_dhis2_attributes-helpers.R index 2c8697e7..e78cf46a 100644 --- a/R/check_dhis2_attributes-helpers.R +++ b/R/check_dhis2_attributes-helpers.R @@ -1,7 +1,7 @@ #' Make an API request to the target DHIS2 system #' #' @param base_url the base URL of the DHIS2 server -#' @param username the user name +#' @param user_name the user name #' @param password the user's password #' @param which the target DHIS2 attribute name. Possible values are: #' \enumerate{ @@ -16,11 +16,13 @@ #' interest. #' @keywords internal #' @noRd -make_api_request <- function(base_url, username, password, - which = "dataElements") { +dhis2_make_api_request <- function(base_url = "https://play.dhis2.org/dev", + user_name = "admin", + password = "district", + which = "dataElements") { checkmate::assert_character(base_url, len = 1L, any.missing = FALSE, null.ok = FALSE, pattern = "https://") - checkmate::assert_character(username, + checkmate::assert_character(user_name, len = 1L, any.missing = FALSE, null.ok = FALSE) checkmate::assert_character(password, @@ -38,14 +40,14 @@ make_api_request <- function(base_url, username, password, which, "?fields=id,name,shortName&paging=false" ) - response <- httr::GET(url, httr::authenticate(username, password)) + response <- httr::GET(url, httr::authenticate(user_name, password)) response } #' Get the relevant dataset #' #' @param base_url the web address of the server the user wishes to log in to -#' @param username the user name +#' @param user_name the user name #' @param password the user's password #' @param attribute_id a comma-separated list of DHIS2 attribute ids. The ids #' could be those of a dataSet or an organisationUnit. @@ -58,25 +60,25 @@ make_api_request <- function(base_url, username, password, #' @examples #' \dontrun{ #' result <- dhis2_get_relevant_attributes( -#' attribute_id = "pBOMPrpg1QX,BfMAe6Itzgt", #' base_url = "https://play.dhis2.org/dev/", -#' username = "admin", +#' user_name = "admin", #' password = "district", +#' attribute_id = "pBOMPrpg1QX,BfMAe6Itzgt", #' which = "dataSets" #' ) #' } #' @keywords internal #' @noRd -dhis2_get_relevant_attributes <- function(attribute_id = NULL, - base_url = +dhis2_get_relevant_attributes <- function(base_url = "https://play.dhis2.org/dev/", - username = "admin", - password = "district", - which = "dataSets") { + user_name = "admin", + password = "district", + attribute_id = NULL, + which = "dataSets") { checkmate::assert_character(base_url, len = 1L, any.missing = FALSE, null.ok = FALSE) - checkmate::assert_character(username, + checkmate::assert_character(user_name, len = 1L, any.missing = FALSE, null.ok = FALSE) checkmate::assert_character(password, @@ -93,14 +95,14 @@ dhis2_get_relevant_attributes <- function(attribute_id = NULL, if (is.character(attribute_id)) { attribute_id <- unlist(strsplit(attribute_id, ",", fixed = TRUE)) } - response <- make_api_request(base_url, username, password, which) + response <- dhis2_make_api_request(base_url, user_name, password, which) content <- httr::content(response, as = "parsed") attributes <- do.call(rbind.data.frame, content[[which]]) if (which != "dataElements") { idx <- which(attribute_id %in% attributes[["id"]]) if (length(idx) == 0L) { stop("Provided attribute ids not found!\n - Use readepi:::make_api_request() function to view the list of + Use readepi:::dhis2_make_api_request() function to view the list of available attributes") } if (length(idx) < length(attribute_id)) { @@ -109,7 +111,7 @@ dhis2_get_relevant_attributes <- function(attribute_id = NULL, glue::glue_collapse(attribute_id[-idx], sep = ", ") ) } - attribute_id <- paste(attribute_id[idx], collapse = ",") + attribute_id <- glue::glue_collapse(attribute_id[idx], sep = ",") } res <- switch( @@ -128,7 +130,7 @@ dhis2_get_relevant_attributes <- function(attribute_id = NULL, #' Get the target DHIS2 attribute identifiers and names #' #' @param base_url the base URL of the DHIS2 server -#' @param username the user name +#' @param user_name the user name #' @param password the user's password #' @param which the target DHIS2 attribute name. #' @@ -140,19 +142,19 @@ dhis2_get_relevant_attributes <- function(attribute_id = NULL, #' \dontrun{ #' datasets <- get_dhis2_attributes( #' base_url = "https://play.dhis2.org/dev/", -#' username = "admin", +#' user_name = "admin", #' password = "district", #' which = "dataSets" #' ) #' } -get_dhis2_attributes <- function(base_url = "https://play.dhis2.org/dev/", - username = "admin", - password = "district", - which = "dataSets") { +get_dhis2_attributes <- function(base_url = "https://play.dhis2.org/dev/", + user_name = "admin", + password = "district", + which = "dataSets") { checkmate::assert_character(base_url, len = 1L, any.missing = FALSE, - null.ok = FALSE, pattern = "https://") - checkmate::assert_character(username, + null.ok = FALSE) + checkmate::assert_character(user_name, len = 1L, any.missing = FALSE, null.ok = FALSE) checkmate::assert_character(password, @@ -163,8 +165,8 @@ get_dhis2_attributes <- function(base_url = "https://play.dhis2.org/dev/", null.ok = FALSE) checkmate::check_choice(which, c("dataSets", "organisationUnits", "dataElementGroups", "dataElements")) - response <- make_api_request(base_url, username, password, which) - content <- httr::content(response, as = "parsed") + response <- dhis2_make_api_request(base_url, user_name, password, which) + content <- httr::content(response, as = "parsed") attributes <- do.call(rbind.data.frame, content[[which]]) attributes } diff --git a/R/check_dhis2_attributes.R b/R/check_dhis2_attributes.R index 4e8fd9e3..60938531 100644 --- a/R/check_dhis2_attributes.R +++ b/R/check_dhis2_attributes.R @@ -1,8 +1,8 @@ #' Check the DHIS2 attributes for data import #' -#' @param username the user name -#' @param password the user's password #' @param base_url the web address of the server the user wishes to log in to +#' @param user_name the user name +#' @param password the user's password #' @param dataset the dataSets identifiers #' @param organisation_unit the organisationUnits identifiers #' @param data_element_group the dataElementGroups identifiers @@ -12,30 +12,30 @@ #' @examples #' \dontrun{ #' attributes <- dhis2_check_attributes( -#' username = "admin", -#' password = "district", -#' base_url = "https://play.dhis2.org/dev/", -#' dataset = "pBOMPrpg1QX", -#' organisation_unit = "DiszpKrYNg8", -#' data_element_group = NULL +#' base_url = "https://play.dhis2.org/dev/", +#' user_name = "admin", +#' password = "district", +#' dataset = "pBOMPrpg1QX", +#' organisation_unit = "DiszpKrYNg8", +#' data_element_group = NULL #' ) #' } #' #' @keywords internal #' @noRd -dhis2_check_attributes <- function(username, - password, - base_url, - dataset, +dhis2_check_attributes <- function(base_url = "https://play.dhis2.org/dev/", + user_name = "admin", + password = "district", + dataset = "pBOMPrpg1QX", organisation_unit = NULL, data_element_group = NULL) { # get the relevant dataset if (!is.null(dataset)) { tmp_res <- dhis2_get_relevant_attributes( - attribute_id = dataset, base_url = base_url, - username = username, + user_name = user_name, password = password, + attribute_id = dataset, which = "dataSets" ) dataset <- tmp_res[["dataset"]] @@ -47,10 +47,10 @@ dhis2_check_attributes <- function(username, # get the relevant organisation units if (!is.null(organisation_unit)) { tmp_res <- dhis2_get_relevant_attributes( - attribute_id = organisation_unit, base_url = base_url, - username = username, + user_name = user_name, password = password, + attribute_id = organisation_unit, which = "organisationUnits" ) organisation_unit <- tmp_res[["organisation_unit"]] @@ -62,10 +62,10 @@ dhis2_check_attributes <- function(username, # get the relevant data element groups if (!is.null(data_element_group)) { tmp_res <- dhis2_get_relevant_attributes( - attribute_id = data_element_group, base_url = base_url, - username = username, + user_name = user_name, password = password, + attribute_id = data_element_group, which = "dataElementGroups" ) data_element_group <- tmp_res[["data_element_group"]] @@ -76,10 +76,10 @@ dhis2_check_attributes <- function(username, # get the data element data_elements <- dhis2_get_relevant_attributes( - attribute_id = NULL, base_url = base_url, - username = username, + user_name = user_name, password = password, + attribute_id = NULL, which = "dataElements" ) diff --git a/R/read_from_dhis2-helpers.R b/R/read_from_dhis2-helpers.R index 3af65d63..dec98239 100644 --- a/R/read_from_dhis2-helpers.R +++ b/R/read_from_dhis2-helpers.R @@ -4,14 +4,20 @@ #' specifying that the user was successfully connected. Otherwise, it will throw #' an error message. #' -#' @param username the user name -#' @param password the user's password #' @param base_url the base URL of the DHIS2 server +#' @param user_name the user name +#' @param password the user's password +#' +#' @return a message if the dhis2_login was successful, +#' throws an error otherwise. #' #' @keywords internal #' @noRd -login <- function(username, password, base_url) { - checkmate::assert_character(username, +dhis2_login <- function(base_url = file.path("https:/", "play.dhis2.org", + "dev"), + user_name = "admin", + password = "district") { + checkmate::assert_character(user_name, len = 1L, any.missing = FALSE, null.ok = FALSE) checkmate::assert_character(password, @@ -20,8 +26,8 @@ login <- function(username, password, base_url) { checkmate::assert_character(base_url, len = 1L, any.missing = FALSE, null.ok = FALSE) - url <- file.path(base_url, "api", "me") - resp <- httr::GET(url, httr::authenticate(username, password)) + url <- file.path(base_url, "api", "me") + resp <- httr::GET(url, httr::authenticate(user_name, password)) httr::stop_for_status(resp) message("\nLogged in successfully!") } @@ -52,7 +58,18 @@ login <- function(username, password, base_url) { #' } #' @keywords internal #' @noRd -dhis2_subset_fields <- function(data, fields) { +dhis2_subset_fields <- function( + data = readepi( + credentials_file = system.file("extdata", "test.ini", + package = "readepi"), + data_source = "https://play.dhis2.org/dev", + dataset = "pBOMPrpg1QX,BfMAe6Itzgt", + organisation_unit = "DiszpKrYNg8", + data_element_group = NULL, + start_date = "2014", + end_date = "2023" + )[["data"]], + fields = c("dataElement", "period", "value")) { checkmate::assert_data_frame(data, min.rows = 1L, null.ok = FALSE, min.cols = 1L) @@ -107,7 +124,19 @@ dhis2_subset_fields <- function(data, fields) { #' } #' @keywords internal #' @noRd -dhis2_subset_records <- function(data, records, id_col_name) { +dhis2_subset_records <- function( + data = readepi(credentials_file = system.file("extdata", + "test.ini", + package = "readepi"), + data_source = "https://play.dhis2.org/dev", + + dataset = "pBOMPrpg1QX,BfMAe6Itzgt", + organisation_unit = "DiszpKrYNg8", + data_element_group = NULL, + start_date = "2014", + end_date = "2023")[["data"]], + records = c("FTRrcoaog83", "eY5ehpbEsB7", "Ix2HsbDMLea"), + id_col_name = "dataElement") { checkmate::assert_data_frame(data, min.rows = 1L, null.ok = FALSE, min.cols = 1L) @@ -141,7 +170,7 @@ dhis2_subset_records <- function(data, records, id_col_name) { #' @return an object of type `list` with the values for the DHIS2 attributes #' @keywords internal #' @noRd -get_attributes_from_user <- function(args_list) { +dhis2_get_attributes_from_user <- function(args_list) { dataset <- organisation_unit <- data_element_group <- start_date <- end_date <- NULL if ("dataset" %in% names(args_list)) { diff --git a/R/read_from_dhis2.R b/R/read_from_dhis2.R index dc7117fa..f467e4a8 100644 --- a/R/read_from_dhis2.R +++ b/R/read_from_dhis2.R @@ -36,17 +36,17 @@ #' id_col_name = "dataElement" #' ) #' } -read_from_dhis2 <- function(base_url, - user_name, - password, - dataset, - organisation_unit, - data_element_group, - start_date, - end_date, - records = NULL, - fields = NULL, - id_col_name = "dataElement") { +read_from_dhis2 <- function(base_url = "https://play.dhis2.org/dev", + user_name = "admin", + password = "district", + dataset = "pBOMPrpg1QX", + organisation_unit = "DiszpKrYNg8", + data_element_group = "oDkJh5Ddh7d", + start_date = "2014", + end_date = "2023", + records = NULL, + fields = NULL, + id_col_name = "dataElement") { checkmate::assert_character(base_url, len = 1L, null.ok = FALSE, any.missing = FALSE) @@ -73,13 +73,13 @@ read_from_dhis2 <- function(base_url, null.ok = TRUE, unique = TRUE) # checking user credentials - login(user_name, password, base_url) + dhis2_login(base_url, user_name, password) # checking the attribute details attributes <- dhis2_check_attributes( + base_url, user_name, password, - base_url, dataset, organisation_unit, data_element_group diff --git a/R/read_from_fingertips-helpers.R b/R/read_from_fingertips-helpers.R index 7db6cc8b..bcce35e1 100644 --- a/R/read_from_fingertips-helpers.R +++ b/R/read_from_fingertips-helpers.R @@ -43,7 +43,14 @@ get_fingertips_metadata <- function() { #' @keywords internal #' @noRd #' -get_ind_id_from_ind_name <- function(metadata, indicator_name) { +get_ind_id_from_ind_name <- function(metadata = + list(indicator_profile_domain = + fingertipsR::indicators(), + indicator_ids_names = + fingertipsR::indicators_unique(), + area_type = + fingertipsR::area_types()), + indicator_name = "Pupil absence") { checkmate::assert_list(metadata, len = 3L, null.ok = FALSE, any.missing = FALSE) @@ -103,7 +110,14 @@ get_ind_id_from_ind_name <- function(metadata, indicator_name) { #' @keywords internal #' @noRd #' -get_ind_id_from_domain_id <- function(metadata, domain_id, +get_ind_id_from_domain_id <- function(metadata = + list(indicator_profile_domain = + fingertipsR::indicators(), + indicator_ids_names = + fingertipsR::indicators_unique(), + area_type = + fingertipsR::area_types()), + domain_id = 1000041L, indicator_name = NULL) { checkmate::assert_list(metadata, len = 3L, null.ok = FALSE, @@ -171,7 +185,14 @@ get_ind_id_from_domain_id <- function(metadata, domain_id, #' @keywords internal #' @noRd #' -get_ind_id_from_domain_name <- function(metadata, domain_name, +get_ind_id_from_domain_name <- function(metadata = + list(indicator_profile_domain = + fingertipsR::indicators(), + indicator_ids_names = + fingertipsR::indicators_unique(), # nolint: line_length_linter + area_type = + fingertipsR::area_types()), + domain_name = "B. Wider determinants of health", # nolint: line_length_linter indicator_name = NULL) { checkmate::assert_list(metadata, len = 3L, null.ok = FALSE, @@ -235,18 +256,26 @@ get_ind_id_from_domain_name <- function(metadata, domain_name, #' @examples #' \dontrun{ #' res <- get_profile_name( -#' profile_id = 19, -#' profile_name = "Public Health Outcomes Framework", #' metadata = list( #' indicator_profile_domain = fingertipsR::indicators(), #' indicator_ids_names = fingertipsR::indicators_unique(), #' area_type = fingertipsR::area_types() #' ) +#' profile_id = 19, +#' profile_name = "Public Health Outcomes Framework", #' ) #' } #' @keywords internal #' @noRd -get_profile_name <- function(profile_id, profile_name, metadata) { +get_profile_name <- function(metadata = + list(indicator_profile_domain = + fingertipsR::indicators(), + indicator_ids_names = + fingertipsR::indicators_unique(), + area_type = + fingertipsR::area_types()), + profile_id = 19L, + profile_name = "Public Health Outcomes Framework") { # nolint: line_length_linter checkmate::assert_list(metadata, len = 3L, null.ok = FALSE, any.missing = FALSE) @@ -303,11 +332,18 @@ get_profile_name <- function(profile_id, profile_name, metadata) { #' } #' @keywords internal #' @noRd -get_ind_id_from_profile <- function(metadata, domain_id = NULL, - domain_name = NULL, +get_ind_id_from_profile <- function(metadata = + list(indicator_profile_domain = + fingertipsR::indicators(), + indicator_ids_names = + fingertipsR::indicators_unique(), + area_type = + fingertipsR::area_types()), + domain_id = NULL, + domain_name = NULL, indicator_name = NULL, - profile_name = NULL, - profile_id = NULL) { + profile_name = NULL, + profile_id = 19L) { checkmate::assert_list(metadata, len = 3L, null.ok = FALSE, any.missing = FALSE) @@ -323,7 +359,7 @@ get_ind_id_from_profile <- function(metadata, domain_id = NULL, checkmate::assert_vector(profile_id, min.len = 0L, null.ok = TRUE, any.missing = FALSE) - tmp_res <- get_profile_name(profile_id, profile_name, metadata) + tmp_res <- get_profile_name(metadata, profile_id, profile_name) profile_name <- tmp_res[["profile_name"]] idx <- tmp_res[["profile_index"]] @@ -373,7 +409,11 @@ get_ind_id_from_profile <- function(metadata, domain_id = NULL, #' } #' @keywords internal #' @noRd -fingertips_subset_rows <- function(data, records, id_col_name) { +fingertips_subset_rows <- function(data = readepi(profile_id = 19L, + area_type_id = 202L)[["data"]], # nolint: line_length_linter + records = c("E92000001", "E12000002", + "E12000009"), + id_col_name = "AreaCode") { checkmate::assert_data_frame(data, null.ok = FALSE, min.rows = 1L, min.cols = 1L) @@ -418,7 +458,10 @@ fingertips_subset_rows <- function(data, records, id_col_name) { #' } #' @keywords internal #' @noRd -fingertips_subset_columns <- function(data, fields) { +fingertips_subset_columns <- function(data = readepi(profile_id = 19L, + area_type_id = 202L)[["data"]], # nolint: line_length_linter + fields = c("IndicatorID", "AreaCode", + "Age", "Value")) { checkmate::assert_data_frame(data, min.cols = 1L, min.rows = 1L, null.ok = FALSE) diff --git a/R/read_from_fingertips.R b/R/read_from_fingertips.R index f6f3d277..8c007cdf 100644 --- a/R/read_from_fingertips.R +++ b/R/read_from_fingertips.R @@ -26,13 +26,18 @@ #' } #' @keywords internal #' @noRd -read_from_fingertips <- function(indicator_id = NULL, indicator_name = NULL, - area_type_id = NULL, +read_from_fingertips <- function(indicator_id = 90362L, + indicator_name = NULL, + area_type_id = 202L, parent_area_type_id = NULL, - profile_id = NULL, profile_name = NULL, - domain_id = NULL, domain_name = NULL, - fields = NULL, records = NULL, - id_position = NULL, id_col_name = NULL) { + profile_id = NULL, + profile_name = NULL, + domain_id = NULL, + domain_name = NULL, + fields = NULL, + records = NULL, + id_position = NULL, + id_col_name = NULL) { checkmate::assert_vector(indicator_id, any.missing = FALSE, min.len = 0L, null.ok = TRUE, unique = TRUE) diff --git a/R/read_from_redcap-helpers.R b/R/read_from_redcap-helpers.R index c8a5e05c..921144e1 100644 --- a/R/read_from_redcap-helpers.R +++ b/R/read_from_redcap-helpers.R @@ -5,11 +5,11 @@ #' project, around the function that read specific records/fields or both at #' the same time #' -#' @param records a vector or a comma-separated string of subset of subject IDs -#' @param fields a vector or a comma-separated string of column names #' @param uri the URI of the server #' @param token the user-specific string that serves as the password for a #' project +#' @param records a vector or a comma-separated string of subset of subject IDs +#' @param fields a vector or a comma-separated string of column names #' @param id_position the column position of the variable that unique identifies #' the subjects #' @param id_col_name the column name with the subject IDs @@ -23,26 +23,31 @@ #' token = "9A81268476645C4E5F03428B8AC3AA7B", #' records = c("1", "3", "5"), #' fields = c("record_id", "name_first", "age", "bmi"), -#' id_col_name = NULL, #' id_position = 1 +#' id_col_name = NULL #' ) #' } #' @keywords internal #' @noRd -import_redcap_data <- function(records, fields, uri, token, - id_position, id_col_name) { +import_redcap_data <- function(uri = "https://bbmc.ouhsc.edu/redcap/api/", # nolint: line_length_linter + token = "9A81268476645C4E5F03428B8AC3AA7B", + records = c("1", "3", "5"), + fields = c("record_id", "name_first", + "age", "bmi"), + id_position = 1L, + id_col_name = NULL) { if (is.null(records) && is.null(fields)) { res <- redcap_read_data(uri, token, id_position) } else if (!is.null(records) && !is.null(fields)) { res <- redcap_read_rows_columns( - fields, uri, token, id_position, - id_col_name, records + uri, token, fields, records, id_position, + id_col_name ) } else if (!is.null(fields) && is.null(records)) { - res <- redcap_read_fields(fields, uri, token, id_position) + res <- redcap_read_fields(uri, token, fields, id_position) } else if (!is.null(records) && is.null(fields)) { res <- redcap_read_records( - records, uri, token, + uri, token, records, id_position, id_col_name ) } @@ -67,7 +72,9 @@ import_redcap_data <- function(records, fields, uri, token, #' data and its associated metadata. #' @keywords internal #' @noRd -redcap_read_data <- function(uri, token, id_position) { +redcap_read_data <- function(uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + id_position = 1L) { redcap_data <- REDCapR::redcap_read( redcap_uri = uri, token = token, @@ -90,22 +97,27 @@ redcap_read_data <- function(uri, token, id_position) { #' Subset records and columns from a REDCap project #' -#' @param fields a vector or a comma-separated string of column names #' @param uri the URI of the REDCap project #' @param token the user-specific string that serves as the password for a #' project +#' @param fields a vector or a comma-separated string of column names +#' @param records a vector or a comma-separated string of subset of subject IDs #' @param id_position the column position of the variable that unique identifies #' the subjects #' @param id_col_name the column name with the subject IDs -#' @param records a vector or a comma-separated string of subset of subject IDs #' #' @return a `list` of 2 elements of type `data.frame` that contain the project #' data with only the records and fields of interest and #' its associated metadata. #' @keywords internal #' @noRd -redcap_read_rows_columns <- function(fields, uri, token, id_position, - id_col_name, records) { +redcap_read_rows_columns <- function(uri = "https://bbmc.ouhsc.edu/redcap/api/", # nolint: line_length_linter + token = "9A81268476645C4E5F03428B8AC3AA7B", # nolint: line_length_linter + fields = c("record_id", "name_first", + "age", "bmi"), + records = c("1", "3", "5"), + id_position = 1L, + id_col_name = NULL) { fields <- glue::glue_collapse(fields, sep = ", ") redcap_data <- REDCapR::redcap_read( redcap_uri = uri, @@ -147,10 +159,10 @@ redcap_read_rows_columns <- function(fields, uri, token, id_position, #' Subset fields from a REDCap project #' -#' @param fields a vector or a comma-separated string of column names #' @param uri the URI of the REDCap project #' @param token the user-specific string that serves as the password for a #' project +#' @param fields a vector or a comma-separated string of column names #' @param id_position the column position of the variable that unique identifies #' the subjects #' @@ -158,7 +170,11 @@ redcap_read_rows_columns <- function(fields, uri, token, id_position, #' data with the fields of interest and its associated metadata. #' @keywords internal #' @noRd -redcap_read_fields <- function(fields, uri, token, id_position) { +redcap_read_fields <- function(uri = "https://bbmc.ouhsc.edu/redcap/api/", # nolint: line_length_linter + token = "9A81268476645C4E5F03428B8AC3AA7B", + fields = c("record_id", "name_first", + "age", "bmi"), + id_position = 1L) { fields <- glue::glue_collapse(fields, sep = ", ") redcap_data <- REDCapR::redcap_read( redcap_uri = uri, token = token, @@ -179,10 +195,10 @@ redcap_read_fields <- function(fields, uri, token, id_position) { #' Subset records from a REDCap project #' -#' @param records a vector or a comma-separated string of subset of subject IDs #' @param uri the URI of the REDCap project #' @param token the user-specific string that serves as the password for a #' project +#' @param records a vector or a comma-separated string of subset of subject IDs #' @param id_position the column position of the variable that unique identifies #' the subjects #' @param id_col_name the column name with the subject IDs @@ -191,7 +207,11 @@ redcap_read_fields <- function(fields, uri, token, id_position) { #' data with the records of interest and its associated metadata. #' @keywords internal #' @noRd -redcap_read_records <- function(records, uri, token, id_position, id_col_name) { +redcap_read_records <- function(uri = "https://bbmc.ouhsc.edu/redcap/api/", # nolint: line_length_linter + token = "9A81268476645C4E5F03428B8AC3AA7B", # nolint: line_length_linter + records = c("1", "2", "3"), + id_position = 1L, + id_col_name = NULL) { records <- glue::glue_collapse(records, sep = ", ") redcap_data <- REDCapR::redcap_read( redcap_uri = uri, @@ -256,7 +276,21 @@ redcap_read_records <- function(records, uri, token, id_position, id_col_name) { #' } #' @keywords internal #' @noRd -redcap_get_results <- function(redcap_data, metadata) { +redcap_get_results <- function( + redcap_data = REDCapR::redcap_read( + redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + records = c("1", "3", "5"), + fields = c("record_id", "name_first", "age", "bmi"), + verbose = FALSE, + id_position = 1L + ), + metadata = REDCapR::redcap_metadata_read( + redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + fields = NULL, + verbose = FALSE + )) { checkmate::assert_list(redcap_data, null.ok = FALSE, min.len = 2L, diff --git a/R/read_from_redcap.R b/R/read_from_redcap.R index 27127f87..08160f91 100644 --- a/R/read_from_redcap.R +++ b/R/read_from_redcap.R @@ -26,8 +26,12 @@ #' frame of the dataset of interest and its associated metadata. #' @keywords internal #' @noRd -read_from_redcap <- function(uri, token, id_position = NULL, id_col_name = NULL, - records = NULL, fields = NULL) { +read_from_redcap <- function(uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + id_position = 1L, + id_col_name = NULL, + records = NULL, + fields = NULL) { # check input variables checkmate::assert_character(token, n.chars = 32L, len = 1L, null.ok = FALSE, @@ -43,7 +47,7 @@ read_from_redcap <- function(uri, token, id_position = NULL, id_col_name = NULL, # importing data and the metadata into R tmp_res <- import_redcap_data( - records, fields, uri, token, + uri, token, records, fields, id_position, id_col_name ) redcap_data <- tmp_res[["redcap_data"]] diff --git a/R/read_servers.R b/R/read_servers.R index 27278243..26eb59d3 100644 --- a/R/read_servers.R +++ b/R/read_servers.R @@ -39,21 +39,26 @@ #' host = "mysql-rfam-public.ebi.ac.uk", #' port = 4497, #' database_name = "Rfam", -#' src = "author", #' driver_name = "", +#' src = "author", #' dbms = "MySQL" #' ) #' } #' @importFrom magrittr %>% #' @keywords internal #' @noRd -sql_server_read_data <- function(user = "rfamro", password = "", +sql_server_read_data <- function(user = "rfamro", + password = "", host = "mysql-rfam-public.ebi.ac.uk", port = 1433L, - database_name = "Rfam", driver_name = "", - src = NULL, records = NULL, - fields = NULL, id_position = NULL, - id_col_name = NULL, dbms = "MySQL") { + database_name = "Rfam", + driver_name = "", + src = NULL, + records = NULL, + fields = NULL, + id_position = NULL, + id_col_name = NULL, + dbms = "MySQL") { # check the input arguments checkmate::assert_number(port, lower = 1L) checkmate::assert_character(user, @@ -108,7 +113,7 @@ sql_server_read_data <- function(user = "rfamro", password = "", # fetch data using SQL query if (length(src) > 0L) { from_query <- fetch_data_from_query( - src, dbms, tables, + source, dbms, tables, driver_name, host, database_name, user, password, port ) diff --git a/R/read_servers_helpers.R b/R/read_servers_helpers.R index d0ed6e89..215f410a 100644 --- a/R/read_servers_helpers.R +++ b/R/read_servers_helpers.R @@ -24,8 +24,13 @@ #' } #' @keywords internal #' @noRd -connect_to_server <- function(dbms, driver_name, host, database_name, - user, password, port) { +connect_to_server <- function(dbms = "MySQL", + driver_name = "", + host = "mysql-rfam-public.ebi.ac.uk", + database_name = "Rfam", + user = "rfamro", + password = "", + port = 4497L) { con <- switch( dbms, "SQLServer" = pool::dbPool(odbc::odbc(), # nolint: keyword_quote_linter @@ -69,7 +74,8 @@ connect_to_server <- function(dbms, driver_name, host, database_name, #' } #' @keywords internal #' @noRd -identify_table_name <- function(query, tables) { +identify_table_name <- function(query = "select * from author", + tables = c("family_author", "author", "test")) { checkmate::assert_character(query, any.missing = FALSE, len = 1L, null.ok = FALSE) @@ -115,9 +121,15 @@ identify_table_name <- function(query, tables) { #' } #' @keywords internal #' @noRd -fetch_data_from_query <- function(source, dbms, tables, - driver_name, host, database_name, - user, password, port) { +fetch_data_from_query <- function(source = "select author_id, name, last_name from author", # nolint: line_length_linter + tables = c("family_author", "author"), + dbms = "MySQL", + driver_name = "", + host = "mysql-rfam-public.ebi.ac.uk", + database_name = "Rfam", + user = "rfamro", + password = "", + port = 4497L) { checkmate::assert_vector(tables, any.missing = FALSE, min.len = 1L, null.ok = FALSE, unique = TRUE) @@ -177,10 +189,18 @@ fetch_data_from_query <- function(source, dbms, tables, #' } #' @keywords internal #' @noRd -sql_select_data <- function(table_names, dbms, id_col_name, - fields, records, id_position, - driver_name, host, database_name, - user, password, port) { +sql_select_data <- function(table_names = "author", + id_col_name = "author_id", + fields = c("author_id", "name"), + records = NULL, + id_position = NULL, + dbms = "MySQL", + driver_name = "", + host = "mysql-rfam-public.ebi.ac.uk", + database_name = "Rfam", + user = "rfamro", + password = "", + port = 4497L) { checkmate::assert_vector(table_names, any.missing = FALSE, min.len = 1L, null.ok = FALSE, unique = FALSE) @@ -250,7 +270,9 @@ sql_select_data <- function(table_names, dbms, id_col_name, #' corresponding to the ID column name and position #' @keywords internal #' @noRd -get_id_column_name <- function(id_col_name, j, id_position) { +get_id_column_name <- function(id_col_name = c("author_id", "rfam_acc"), + j = 1L, + id_position = c(1L, 1L)) { checkmate::assert_numeric(j, lower = 1L, any.missing = FALSE, len = 1L, null.ok = FALSE) @@ -302,8 +324,14 @@ get_id_column_name <- function(id_col_name, j, id_position) { #' } #' @keywords internal #' @noRd -sql_select_entire_dataset <- function(table, dbms, driver_name, host, - database_name, user, password, port) { +sql_select_entire_dataset <- function(table = "author", + dbms = "MySQL", + driver_name = "", + host = "mysql-rfam-public.ebi.ac.uk", + database_name = "Rfam", + user = "rfamro", + password = "", + port = 4497L) { checkmate::assert_character(table, any.missing = FALSE, len = 1L, null.ok = FALSE) @@ -359,9 +387,19 @@ sql_select_entire_dataset <- function(table, dbms, driver_name, host, #' } #' @keywords internal #' @noRd -sql_select_records_and_fields <- function(table, record, id_column_name, field, - id_pos, dbms, driver_name, host, - database_name, user, password, port) { +sql_select_records_and_fields <- function(table = "author", + record = c("1", "20", "50"), + id_column_name = "author_id", + field = c("author_id", + "last_name"), + id_pos = NULL, + dbms = "MySQL", + driver_name = "", + host = "mysql-rfam-public.ebi.ac.uk", + database_name = "Rfam", + user = "rfamro", + password = "", + port = 4497L) { checkmate::assert_character(id_column_name, any.missing = FALSE, null.ok = TRUE, unique = TRUE) @@ -416,7 +454,12 @@ sql_select_records_and_fields <- function(table, record, id_column_name, field, #' } #' @export #' -visualise_table <- function(data_source, credentials_file, from, driver_name) { +visualise_table <- function(data_source = "mysql-rfam-public.ebi.ac.uk", + credentials_file = system.file("extdata", + "test.ini", + package = "readepi"), + from = "author", + driver_name = "") { checkmate::assert_character(from, any.missing = FALSE, len = 1L, null.ok = FALSE) @@ -476,9 +519,17 @@ visualise_table <- function(data_source, credentials_file, from, driver_name) { #' } #' @keywords internal #' @noRd -sql_select_records_only <- function(table, record, id_column_name, id_pos, - dbms, driver_name, host, database_name, - user, password, port) { +sql_select_records_only <- function(table = "author", + record = c("1", "20", "50"), + id_column_name = NULL, + id_pos = 1L, + dbms = "MySQL", + driver_name = "", + host = "mysql-rfam-public.ebi.ac.uk", + database_name = "Rfam", + user = "rfamro", + password = "", + port = 4497L) { checkmate::assert_vector(id_pos, any.missing = FALSE, min.len = 0L, null.ok = TRUE, unique = FALSE) @@ -549,8 +600,16 @@ sql_select_records_only <- function(table, record, id_column_name, id_pos, #' } #' @keywords internal #' @noRd -sql_select_fields_only <- function(table, field, dbms, driver_name, host, - database_name, user, password, port) { +sql_select_fields_only <- function(table = "author", + field = c("author_id", "name", + "last_name"), + dbms = "MySQL", + driver_name = "", + host = "mysql-rfam-public.ebi.ac.uk", + database_name = "Rfam", + user = "rfamro", + password = "", + port = 4497L) { checkmate::assert_character(table, any.missing = FALSE, len = 1L, null.ok = FALSE) diff --git a/R/readepi.R b/R/readepi.R index 7ed72f9e..4d59c2ea 100644 --- a/R/readepi.R +++ b/R/readepi.R @@ -28,9 +28,9 @@ #' } #' @returns a `list` of 1 or several object of type `data frame`. #' @export -readepi <- function(data_source = NULL, - records = NULL, - fields = NULL, +readepi <- function(data_source = "mysql-rfam-public.ebi.ac.uk", + records = NULL, + fields = NULL, id_position = NULL, id_col_name = NULL, ...) { @@ -119,7 +119,7 @@ readepi <- function(data_source = NULL, dbms = credentials[["dbms"]] ) } else if (credentials[["dbms"]] %in% c("dhis2", "DHIS2")) { - tmp_attributes <- get_attributes_from_user(args_list) + tmp_attributes <- dhis2_get_attributes_from_user(args_list) res <- read_from_dhis2( base_url = credentials[["host"]], user_name = credentials[["user"]], diff --git a/R/readepi_helpers.R b/R/readepi_helpers.R index 235662fd..b285e2d9 100644 --- a/R/readepi_helpers.R +++ b/R/readepi_helpers.R @@ -72,7 +72,14 @@ read_credentials <- function( #' } #' @keywords internal #' @noRd -get_read_fingertips_args <- function(args_list) { +get_read_fingertips_args <- function(args_list = list(indicator_id = NULL, + indicator_name = NULL, + area_type_id = NULL, + profile_id = NULL, + profile_name = NULL, + domain_id = NULL, + domain_name = NULL, + parent_area_type_id = NULL)) { # nolint: line_length_linter checkmate::assert_list(args_list) if ("indicator_id" %in% names(args_list)) { indicator_id <- args_list[["indicator_id"]] diff --git a/R/show_tables.R b/R/show_tables.R index b7735958..8160f9e1 100644 --- a/R/show_tables.R +++ b/R/show_tables.R @@ -9,9 +9,9 @@ #' @examples #' \dontrun{ #' show_tables( +#' data_source = "mysql-rfam-public.ebi.ac.uk", #' credentials_file = system.file("extdata", "test.ini", #' package = "readepi"), -#' data_source = "mysql-rfam-public.ebi.ac.uk", #' driver_name = "" #' ) #' } @@ -19,7 +19,10 @@ #' in the specified database #' @export #' -show_tables <- function(data_source, credentials_file, driver_name) { +show_tables <- function(data_source = "mysql-rfam-public.ebi.ac.uk", + credentials_file = system.file("extdata", "test.ini", + package = "readepi"), + driver_name = "") { # checking input parameters checkmate::assert_character(credentials_file, null.ok = TRUE, len = 1L) checkmate::assert_file_exists(credentials_file) diff --git a/man/get_dhis2_attributes.Rd b/man/get_dhis2_attributes.Rd index 3f03746c..3c7cd03a 100644 --- a/man/get_dhis2_attributes.Rd +++ b/man/get_dhis2_attributes.Rd @@ -6,7 +6,7 @@ \usage{ get_dhis2_attributes( base_url = "https://play.dhis2.org/dev/", - username = "admin", + user_name = "admin", password = "district", which = "dataSets" ) @@ -14,7 +14,7 @@ get_dhis2_attributes( \arguments{ \item{base_url}{the base URL of the DHIS2 server} -\item{username}{the user name} +\item{user_name}{the user name} \item{password}{the user's password} @@ -31,7 +31,7 @@ Get the target DHIS2 attribute identifiers and names \dontrun{ datasets <- get_dhis2_attributes( base_url = "https://play.dhis2.org/dev/", - username = "admin", + user_name = "admin", password = "district", which = "dataSets" ) diff --git a/man/readepi.Rd b/man/readepi.Rd index 33c07ca7..6b039944 100644 --- a/man/readepi.Rd +++ b/man/readepi.Rd @@ -5,7 +5,7 @@ \title{Import data from different data_sources into R} \usage{ readepi( - data_source = NULL, + data_source = "mysql-rfam-public.ebi.ac.uk", records = NULL, fields = NULL, id_position = NULL, diff --git a/man/show_tables.Rd b/man/show_tables.Rd index 64e4f6aa..b2b397e1 100644 --- a/man/show_tables.Rd +++ b/man/show_tables.Rd @@ -4,7 +4,11 @@ \alias{show_tables} \title{Display the list of tables in a database} \usage{ -show_tables(data_source, credentials_file, driver_name) +show_tables( + data_source = "mysql-rfam-public.ebi.ac.uk", + credentials_file = system.file("extdata", "test.ini", package = "readepi"), + driver_name = "" +) } \arguments{ \item{data_source}{the URL of the HIS of interest} @@ -26,9 +30,9 @@ Display the list of tables in a database \examples{ \dontrun{ show_tables( + data_source = "mysql-rfam-public.ebi.ac.uk", credentials_file = system.file("extdata", "test.ini", package = "readepi"), - data_source = "mysql-rfam-public.ebi.ac.uk", driver_name = "" ) } diff --git a/man/visualise_table.Rd b/man/visualise_table.Rd index a25b9de7..c35537a0 100644 --- a/man/visualise_table.Rd +++ b/man/visualise_table.Rd @@ -4,7 +4,12 @@ \alias{visualise_table} \title{Visualize the first 5 rows of the data from a table} \usage{ -visualise_table(data_source, credentials_file, from, driver_name) +visualise_table( + data_source = "mysql-rfam-public.ebi.ac.uk", + credentials_file = system.file("extdata", "test.ini", package = "readepi"), + from = "author", + driver_name = "" +) } \arguments{ \item{data_source}{the the URL of the HIS} diff --git a/tests/testthat/test-check_dhis2_attributes-helpers.R b/tests/testthat/test-check_dhis2_attributes-helpers.R index c507403a..92df6096 100644 --- a/tests/testthat/test-check_dhis2_attributes-helpers.R +++ b/tests/testthat/test-check_dhis2_attributes-helpers.R @@ -2,11 +2,11 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() test_that("the API request is working fine", { - response <- make_api_request( - base_url = file.path("https:/", "play.dhis2.org", "dev"), - username = "admin", - password = "district", - which = "dataElements" + response <- dhis2_make_api_request( + base_url = file.path("https:/", "play.dhis2.org", "dev"), + user_name = "admin", + password = "district", + which = "dataElements" ) expect_type(response, "list") expect_length(response, 8L) @@ -18,7 +18,7 @@ httptest::with_mock_api({ result <- dhis2_get_relevant_attributes( attribute_id = "pBOMPrpg1QX,BfMAe6Itzgt", base_url = file.path("https:/", "play.dhis2.org", "dev"), - username = "admin", + user_name = "admin", password = "district", which = "dataSets" ) @@ -31,10 +31,10 @@ httptest::with_mock_api({ dataElements", { result <- dhis2_get_relevant_attributes( attribute_id = "FTRrcoaog83", - base_url = file.path("https:/", "play.dhis2.org", "dev"), - username = "admin", - password = "district", - which = "dataElements" + base_url = file.path("https:/", "play.dhis2.org", "dev"), + user_name = "admin", + password = "district", + which = "dataElements" ) expect_s3_class(result, "data.frame") }) @@ -43,10 +43,10 @@ httptest::with_mock_api({ organisationUnits", { result <- dhis2_get_relevant_attributes( attribute_id = "Rp268JB6Ne4", - base_url = file.path("https:/", "play.dhis2.org", "dev"), - username = "admin", - password = "district", - which = "organisationUnits" + base_url = file.path("https:/", "play.dhis2.org", "dev"), + user_name = "admin", + password = "district", + which = "organisationUnits" ) expect_type(result, "list") expect_identical(result[["organisation_unit"]], "Rp268JB6Ne4") @@ -57,10 +57,10 @@ httptest::with_mock_api({ dataElementGroups", { result <- dhis2_get_relevant_attributes( attribute_id = "oDkJh5Ddh7d", - base_url = file.path("https:/", "play.dhis2.org", "dev"), - username = "admin", - password = "district", - which = "dataElementGroups" + base_url = file.path("https:/", "play.dhis2.org", "dev"), + user_name = "admin", + password = "district", + which = "dataElementGroups" ) expect_type(result, "list") expect_identical(result[["data_element_group"]], "oDkJh5Ddh7d") @@ -69,10 +69,10 @@ httptest::with_mock_api({ test_that("get_dhis2_attributes works as expected", { attributes <- get_dhis2_attributes( - base_url = file.path("https:/", "play.dhis2.org", "dev"), - username = "admin", - password = "district", - which = "dataSets" + base_url = file.path("https:/", "play.dhis2.org", "dev"), + user_name = "admin", + password = "district", + which = "dataSets" ) expect_s3_class(attributes, "data.frame") expect_identical(ncol(attributes), 3L) @@ -84,9 +84,9 @@ test_that("the API request fails as expected", { testthat::skip_on_cran() testthat::skip_if_offline() expect_error( - make_api_request( + dhis2_make_api_request( base_url = file.path("test", "play.dhis2.org", "dev"), - username = "admin", + user_name = "admin", password = "district", which = "dataElements" ), @@ -98,9 +98,9 @@ test_that("the API request fails with an incorrect attribute", { testthat::skip_on_cran() testthat::skip_if_offline() expect_error( - make_api_request( + dhis2_make_api_request( base_url = file.path("test", "play.dhis2.org", "dev"), - username = "admin", + user_name = "admin", password = "district", which = "test" ), @@ -115,10 +115,10 @@ test_that("dhis2_get_relevant_attributes fails as expected", { expect_error( dhis2_get_relevant_attributes( attribute_id = "pBOMPrpg1QX,BfMAe6Itzgt", - base_url = file.path("test", "play.dhis2.org", "dev"), - username = "admin", - password = "district", - which = "dataSets" + base_url = file.path("test", "play.dhis2.org", "dev"), + user_name = "admin", + password = "district", + which = "dataSets" ), regexp = cat("The 'base_url' should start with 'https://'") ) @@ -126,10 +126,10 @@ test_that("dhis2_get_relevant_attributes fails as expected", { expect_error( dhis2_get_relevant_attributes( attribute_id = "pBOMPrpg1QX,BfMAe6Itzgt", - base_url = file.path("https:/", "play.dhis2.org", "dev"), - username = "admin", - password = "district", - which = "test" + base_url = file.path("https:/", "play.dhis2.org", "dev"), + user_name = "admin", + password = "district", + which = "test" ), regexp = cat("The expected values for the 'which' argument are: 'dataSets, 'organisationUnits', 'dataElementGroups', 'dataElements'") @@ -139,7 +139,7 @@ test_that("dhis2_get_relevant_attributes fails as expected", { dhis2_get_relevant_attributes( attribute_id = "test", base_url = file.path("https:/", "play.dhis2.org", "dev"), - username = "admin", + user_name = "admin", password = "district", which = "dataSets" ), @@ -150,7 +150,7 @@ test_that("dhis2_get_relevant_attributes fails as expected", { dhis2_get_relevant_attributes( attribute_id = "pBOMPrpg1QX, test", base_url = file.path("https:/", "play.dhis2.org", "dev"), - username = "admin", + user_name = "admin", password = "district", which = "dataSets" ), diff --git a/tests/testthat/test-check_dhis2_attributes.R b/tests/testthat/test-check_dhis2_attributes.R index fc39b515..51eb8ec4 100644 --- a/tests/testthat/test-check_dhis2_attributes.R +++ b/tests/testthat/test-check_dhis2_attributes.R @@ -4,11 +4,12 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() attributes <- dhis2_check_attributes( - username = "admin", - password = "district", - base_url = file.path("https:/", "play.dhis2.org", "dev"), - dataset = "pBOMPrpg1QX", - organisation_unit = "DiszpKrYNg8", + user_name = "admin", + password = "district", + base_url = file.path("https:/", "play.dhis2.org", + "dev"), + dataset = "pBOMPrpg1QX", + organisation_unit = "DiszpKrYNg8", data_element_group = "oDkJh5Ddh7d" ) expect_type(attributes, "list") @@ -41,11 +42,12 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() attributes <- dhis2_check_attributes( - username = "admin", - password = "district", - base_url = file.path("https:/", "play.dhis2.org", "dev"), - dataset = "pBOMPrpg1QX", - organisation_unit = NULL, + user_name = "admin", + password = "district", + base_url = file.path("https:/", "play.dhis2.org", + "dev"), + dataset = "pBOMPrpg1QX", + organisation_unit = NULL, data_element_group = NULL ) expect_type(attributes, "list") @@ -73,11 +75,11 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() attributes <- dhis2_check_attributes( - username = "admin", - password = "district", + user_name = "admin", + password = "district", base_url = file.path("https:/", "play.dhis2.org", "dev"), # nolint: line_length_linter - dataset = NULL, - organisation_unit = "DiszpKrYNg8", # nolint: line_length_linter + dataset = NULL, + organisation_unit = "DiszpKrYNg8", # nolint: line_length_linter data_element_group = NULL) expect_type(attributes, "list") expect_length(attributes, 7L) @@ -104,11 +106,11 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() attributes <- dhis2_check_attributes( - username = "admin", - password = "district", + user_name = "admin", + password = "district", base_url = file.path("https:/", "play.dhis2.org", "dev"), # nolint: line_length_linter - dataset = NULL, - organisation_unit = NULL, + dataset = NULL, + organisation_unit = NULL, data_element_group = "oDkJh5Ddh7d") # nolint: line_length_linter expect_type(attributes, "list") expect_length(attributes, 7L) @@ -137,11 +139,11 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() attributes <- dhis2_check_attributes( - username = "admin", - password = "district", + user_name = "admin", + password = "district", base_url = file.path("https:/", "play.dhis2.org", "dev"), # nolint: line_length_linter - dataset = NULL, - organisation_unit = NULL, + dataset = NULL, + organisation_unit = NULL, data_element_group = NULL) expect_type(attributes, "list") expect_length(attributes, 7L) @@ -166,11 +168,11 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() attributes <- dhis2_check_attributes( - username = "admin", - password = "district", + user_name = "admin", + password = "district", base_url = file.path("https:/", "play.dhis2.org", "dev"), # nolint: line_length_linter - dataset = "pBOMPrpg1QX", - organisation_unit = "DiszpKrYNg8", # nolint: line_length_linter + dataset = "pBOMPrpg1QX", + organisation_unit = "DiszpKrYNg8", # nolint: line_length_linter data_element_group = NULL) expect_type(attributes, "list") expect_length(attributes, 7L) @@ -199,7 +201,7 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() attributes <- dhis2_check_attributes( - username = "admin", + user_name = "admin", password = "district", base_url = file.path("https:/", "play.dhis2.org", "dev"), # nolint: line_length_linter dataset = "pBOMPrpg1QX", @@ -234,7 +236,7 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() attributes <- dhis2_check_attributes( - username = "admin", + user_name = "admin", password = "district", base_url = file.path("https:/", "play.dhis2.org", "dev"), # nolint: line_length_linter dataset = NULL, diff --git a/tests/testthat/test-read_from_dhis2.R b/tests/testthat/test-read_from_dhis2.R index c4edceeb..1238a7da 100644 --- a/tests/testthat/test-read_from_dhis2.R +++ b/tests/testthat/test-read_from_dhis2.R @@ -25,11 +25,11 @@ test_that("read_from_dhis2 works as expected when subsetting on columns and testthat::skip_on_cran() testthat::skip_if_offline() data <- read_from_dhis2( - base_url = file.path("https:/", "play.dhis2.org", "dev"), - user_name = "admin", - password = "district", - dataset = "pBOMPrpg1QX", - organisation_unit = "DiszpKrYNg8", + base_url = file.path("https:/", "play.dhis2.org", "dev"), # nolint: line_length_linter + user_name = "admin", + password = "district", + dataset = "pBOMPrpg1QX", + organisation_unit = "DiszpKrYNg8", data_element_group = "oDkJh5Ddh7d", start_date = "2014", end_date = "2023", @@ -48,17 +48,17 @@ test_that("read_from_dhis2 fails with a wrong URL", { testthat::skip_if_offline() expect_error( read_from_dhis2( - base_url = NULL, - user_name = "admin", - password = "district", - dataset = "pBOMPrpg1QX", - organisation_unit = "DiszpKrYNg8", + base_url = NULL, + user_name = "admin", + password = "district", + dataset = "pBOMPrpg1QX", + organisation_unit = "DiszpKrYNg8", data_element_group = "oDkJh5Ddh7d", - start_date = "2014", - end_date = "2023", - records = c("FTRrcoaog83", "eY5ehpbEsB7", "Ix2HsbDMLea"), - fields = c("dataElement", "period", "value"), - id_col_name = "dataElement" + start_date = "2014", + end_date = "2023", + records = c("FTRrcoaog83", "eY5ehpbEsB7", "Ix2HsbDMLea"), + fields = c("dataElement", "period", "value"), + id_col_name = "dataElement" ), regexp = cat("Assertion on',base_url,'failed: Must be provided.") ) diff --git a/tests/testthat/test-read_from_dhis2_helpers.R b/tests/testthat/test-read_from_dhis2_helpers.R index 696396fc..c5bb3be7 100644 --- a/tests/testthat/test-read_from_dhis2_helpers.R +++ b/tests/testthat/test-read_from_dhis2_helpers.R @@ -1,26 +1,27 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() - test_that("login works as expected", { + testthat::skip_if_not_installed("httptest") + test_that("dhis2_login works as expected", { expect_message( - login( - username = "admin", - password = "district", - base_url = file.path("https:/", "play.dhis2.org", "dev") + dhis2_login( + base_url = file.path("https:/", "play.dhis2.org", "dev"), + user_name = "admin", + password = "district" ), "Logged in successfully!" ) }) }) -test_that("login fails as expected", { +test_that("dhis2_login fails as expected", { testthat::skip_on_cran() testthat::skip_if_offline() expect_error( - login( - username = "admin", - password = "district", - base_url = file.path("test", "play.dhis2.org", "dev") + dhis2_login( + base_url = file.path("test", "play.dhis2.org", "dev"), + user_name = "admin", + password = "district" ), regexp = cat("The base URL should start with 'https://'") ) @@ -30,14 +31,14 @@ test_that("dhis2_subset_fields works as expected", { testthat::skip_on_cran() testthat::skip_if_offline() data <- readepi( - credentials_file = system.file("extdata", "test.ini", - package = "readepi"), - data_source = "https://play.dhis2.org/dev", - dataset = "pBOMPrpg1QX,BfMAe6Itzgt", - organisation_unit = "DiszpKrYNg8", + credentials_file = system.file("extdata", "test.ini", + package = "readepi"), + data_source = "https://play.dhis2.org/dev", + dataset = "pBOMPrpg1QX,BfMAe6Itzgt", + organisation_unit = "DiszpKrYNg8", data_element_group = NULL, - start_date = "2014", - end_date = "2023" + start_date = "2014", + end_date = "2023" )[["data"]] results <- dhis2_subset_fields(data = data, fields = c("dataElement", "period", "value")) @@ -59,23 +60,23 @@ test_that("dhis2_subset_fields fails as expected", { end_date = "2023" )[["data"]] expect_error( - dhis2_subset_fields(data = data, fields = c(1, 2, 3)), # nolint - regexp = cat("The value for the 'fields' argument should be a vector of - character.")) + dhis2_subset_fields(data = data, fields = c(1L, 2L, 3L)), + regexp = cat("The value for the 'fields' argument should be a + vector of character.")) }) test_that("dhis2_subset_records works as expected", { testthat::skip_on_cran() testthat::skip_if_offline() data <- readepi( - credentials_file = system.file("extdata", "test.ini", - package = "readepi"), - data_source = "https://play.dhis2.org/dev", - dataset = "pBOMPrpg1QX,BfMAe6Itzgt", - organisation_unit = "DiszpKrYNg8", + credentials_file = system.file("extdata", "test.ini", + package = "readepi"), + data_source = "https://play.dhis2.org/dev", + dataset = "pBOMPrpg1QX,BfMAe6Itzgt", + organisation_unit = "DiszpKrYNg8", data_element_group = NULL, - start_date = "2014", - end_date = "2023" + start_date = "2014", + end_date = "2023" )[["data"]] result <- dhis2_subset_records( data = data, @@ -115,14 +116,14 @@ test_that("dhis2_subset_fields sends a warning when the provided field is not testthat::skip_on_cran() testthat::skip_if_offline() data <- readepi( - credentials_file = system.file("extdata", "test.ini", - package = "readepi"), - data_source = "https://play.dhis2.org/dev", + credentials_file = system.file("extdata", "test.ini", + package = "readepi"), + data_source = "https://play.dhis2.org/dev", dataset = "pBOMPrpg1QX,BfMAe6Itzgt", organisation_unit = "DiszpKrYNg8", data_element_group = NULL, - start_date = "2014", - end_date = "2023" + start_date = "2014", + end_date = "2023" )[["data"]] expect_warning( diff --git a/tests/testthat/test-read_from_fingertips-helpers.R b/tests/testthat/test-read_from_fingertips-helpers.R index fa2595b4..0d3153c8 100644 --- a/tests/testthat/test-read_from_fingertips-helpers.R +++ b/tests/testthat/test-read_from_fingertips-helpers.R @@ -464,13 +464,13 @@ httptest::with_mock_api({ testthat::skip_on_cran() testthat::skip_if_offline() res <- get_profile_name( - profile_id = 19L, - profile_name = "Public Health Outcomes Framework", metadata = list( indicator_profile_domain = fingertipsR::indicators(), indicator_ids_names = fingertipsR::indicators_unique(), area_type = fingertipsR::area_types() - ) + ), + profile_id = 19L, + profile_name = "Public Health Outcomes Framework" ) expect_type(res, "list") expect_length(res, 2L) diff --git a/tests/testthat/test-read_from_redcap-helpers.R b/tests/testthat/test-read_from_redcap-helpers.R index a1cf8b94..7de679fe 100644 --- a/tests/testthat/test-read_from_redcap-helpers.R +++ b/tests/testthat/test-read_from_redcap-helpers.R @@ -3,12 +3,12 @@ httptest::with_mock_api({ testthat::skip_if_offline() test_that("import_redcap_data works as expected", { result <- import_redcap_data( - uri = file.path("https:/", "bbmc.ouhsc.edu", "redcap", "api", ""), - token = "9A81268476645C4E5F03428B8AC3AA7B", - records = c("1", "3", "5"), - fields = c("record_id", "name_first", "age", "bmi"), - id_col_name = NULL, - id_position = 1L + uri = file.path("https:/", "bbmc.ouhsc.edu", "redcap", "api", ""), + token = "9A81268476645C4E5F03428B8AC3AA7B", + records = c("1", "3", "5"), + fields = c("record_id", "name_first", "age", "bmi"), + id_position = 1L, + id_col_name = NULL ) expect_type(result, "list") expect_length(result, 2L) @@ -22,17 +22,18 @@ httptest::with_mock_api({ test_that("redcap_get_results works as expected", { result <- redcap_get_results( redcap_data = REDCapR::redcap_read( - redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", - records = c("1", "3", "5"), - fields = c("record_id", "name_first", "age", "bmi"), verbose = FALSE, + redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + records = c("1", "3", "5"), + fields = c("record_id", "name_first", "age", "bmi"), + verbose = FALSE, id_position = 1L ), metadata = REDCapR::redcap_metadata_read( redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", - fields = NULL, - verbose = FALSE + token = "9A81268476645C4E5F03428B8AC3AA7B", + fields = NULL, + verbose = FALSE ) ) expect_type(result, "list") @@ -48,9 +49,9 @@ httptest::with_mock_api({ redcap_data = NULL, metadata = REDCapR::redcap_metadata_read( redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", - fields = NULL, - verbose = FALSE + token = "9A81268476645C4E5F03428B8AC3AA7B", + fields = NULL, + verbose = FALSE ) ), regexp = cat("Assertion on',redcap_data,'failed: Must be provided") @@ -61,9 +62,9 @@ httptest::with_mock_api({ redcap_data = NA, metadata = REDCapR::redcap_metadata_read( redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", - fields = NULL, - verbose = FALSE + token = "9A81268476645C4E5F03428B8AC3AA7B", + fields = NULL, + verbose = FALSE ) ), regexp = cat("Assertion on',redcap_data,'failed: Missing value not allowed @@ -75,10 +76,11 @@ httptest::with_mock_api({ expect_error( redcap_get_results( redcap_data = REDCapR::redcap_read( - redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", - records = c("1", "3", "5"), - fields = c("record_id", "name_first", "age", "bmi"), verbose = FALSE, + redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + records = c("1", "3", "5"), + fields = c("record_id", "name_first", "age", "bmi"), + verbose = FALSE, id_position = 1L ), metadata = NULL @@ -89,10 +91,11 @@ httptest::with_mock_api({ expect_error( redcap_get_results( redcap_data = REDCapR::redcap_read( - redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", - records = c("1", "3", "5"), - fields = c("record_id", "name_first", "age", "bmi"), verbose = FALSE, + redcap_uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + records = c("1", "3", "5"), + fields = c("record_id", "name_first", "age", "bmi"), + verbose = FALSE, id_position = 1L ), metadata = NA @@ -104,8 +107,8 @@ httptest::with_mock_api({ test_that("redcap_read_data works as expected", { result <- redcap_read_data( - uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", + uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", id_position = 1L ) expect_type(result, "list") @@ -119,12 +122,12 @@ httptest::with_mock_api({ test_that("redcap_read_rows_columns works as expected", { result <- redcap_read_rows_columns( - uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", + uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + fields = c("record_id", "name_first", "age", "bmi"), + records = c("1", "3", "5"), id_position = 1L, - id_col_name = NULL, - fields = c("record_id", "name_first", "age", "bmi"), - records = c("1", "3", "5") + id_col_name = NULL ) expect_type(result, "list") expect_length(result, 2L) @@ -137,10 +140,10 @@ httptest::with_mock_api({ test_that("redcap_read_fields works as expected", { result <- redcap_read_fields( - uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", - id_position = 1L, - fields = c("record_id", "name_first", "age", "bmi") + uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + fields = c("record_id", "name_first", "age", "bmi"), + id_position = 1L ) expect_type(result, "list") expect_length(result, 2L) @@ -153,10 +156,10 @@ httptest::with_mock_api({ test_that("redcap_read_records works as expected", { result <- redcap_read_records( - uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", + uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + records = c("1", "2", "3"), id_position = 1L, - records = c("1", "2", "3"), id_col_name = NULL ) expect_type(result, "list") @@ -171,10 +174,10 @@ httptest::with_mock_api({ test_that("redcap_read_records fails as expected", { expect_error( redcap_read_records( - uri = "https://bbmc.ouhsc.edu/redcap/api/", - token = "9A81268476645C4E5F03428B8AC3AA7B", + uri = "https://bbmc.ouhsc.edu/redcap/api/", + token = "9A81268476645C4E5F03428B8AC3AA7B", + records = c("1", "2", "3"), id_position = NULL, - records = c("1", "2", "3"), id_col_name = "Karim" ), regexp = cat("'Karim' is an invalid column name.") diff --git a/tests/testthat/test-read_servers.R b/tests/testthat/test-read_servers.R index d22ff469..6620fc24 100644 --- a/tests/testthat/test-read_servers.R +++ b/tests/testthat/test-read_servers.R @@ -7,8 +7,8 @@ test_that("sql_server_read_data works as expected", { host = "mysql-rfam-public.ebi.ac.uk", port = 4497L, database_name = "Rfam", - src = "author", driver_name = "", + src = "author", dbms = "MySQL" ) expect_type(result, "list") @@ -19,8 +19,8 @@ test_that("sql_server_read_data works as expected", { host = "mysql-rfam-public.ebi.ac.uk", port = 4497L, database_name = "Rfam", - src = "select * from author", driver_name = "", + src = "select * from author", dbms = "MySQL" ) expect_type(result, "list") diff --git a/tests/testthat/test-show_tables.R b/tests/testthat/test-show_tables.R index 270d9d56..b0a46e05 100644 --- a/tests/testthat/test-show_tables.R +++ b/tests/testthat/test-show_tables.R @@ -14,12 +14,12 @@ test_that("show_tables fails as expected", { testthat::skip_if_offline() expect_error( show_tables( - data_source = "mysql-rfam-public.ebi.ac.uk", + data_source = "mysql-rfam-public.ebi.ac.uk", credentials_file = c(system.file("extdata", "test.ini", package = "readepi"), system.file("extdata", "test.ini", package = "readepi")), - driver_name = "" + driver_name = "" ), regexp = cat("Assertion on',credentials_file,'failed: Must be of type 'character' of length 1.") @@ -37,13 +37,13 @@ test_that("show_tables fails as expected", { expect_error( show_tables( - data_source = c( + data_source = c( "mysql-rfam-public.ebi.ac.uk", "mysql-rfam-public.ebi.ac.uk" ), credentials_file = system.file("extdata", "test.ini", package = "readepi"), - driver_name = "" + driver_name = "" ), regexp = cat("Assertion on',data_source,'failed: Must be of type 'character' of length 1.") @@ -51,10 +51,10 @@ test_that("show_tables fails as expected", { expect_error( show_tables( - data_source = NULL, + data_source = NULL, credentials_file = system.file("extdata", "test.ini", package = "readepi"), - driver_name = "" + driver_name = "" ), regexp = cat("Assertion on',data_source,'failed: Must be specified.") )