From cecc13a8e944ef7ecb6258c11be3db498efc2c4b Mon Sep 17 00:00:00 2001 From: Hanneke Leegwater Date: Fri, 20 Dec 2024 11:43:56 +0100 Subject: [PATCH 1/5] Not running the API functions during testing --- .github/workflows/R-CMD-check.yaml | 2 +- R/api.R | 4 ++-- man/get_api_key.Rd | 2 +- man/get_api_url.Rd | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 0309311..0967791 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -2,7 +2,7 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: [main, master, dev] + branches: [main, master, dev, fix_problems_CI] pull_request: name: R-CMD-check.yaml diff --git a/R/api.R b/R/api.R index 8e6b411..c594c79 100644 --- a/R/api.R +++ b/R/api.R @@ -5,7 +5,7 @@ #' See [set_api_url()] on how to set it. #' #' @examples -#' \donttest{ +#' \dontrun{ #' get_api_url() #' } #' @@ -50,7 +50,7 @@ set_api_url <- function(rspace_api_url) { #' See [set_api_key()] on how to set it. #' #' @examples -#' \donttest{ +#' \dontrun{ #' get_api_key() #' } #' diff --git a/man/get_api_key.Rd b/man/get_api_key.Rd index bc56b42..4773091 100644 --- a/man/get_api_key.Rd +++ b/man/get_api_key.Rd @@ -11,7 +11,7 @@ Gets the RSpace API key from the \code{RSPACE_API_KEY} environment variable. See \code{\link[=set_api_key]{set_api_key()}} on how to set it. } \examples{ -\donttest{ +\dontrun{ get_api_key() } diff --git a/man/get_api_url.Rd b/man/get_api_url.Rd index 9f6e7d1..80460b4 100644 --- a/man/get_api_url.Rd +++ b/man/get_api_url.Rd @@ -11,7 +11,7 @@ Gets the RSpace API URL from the \code{RSPACE_API_URL} environment variable. See \code{\link[=set_api_url]{set_api_url()}} on how to set it. } \examples{ -\donttest{ +\dontrun{ get_api_url() } From 87bd128c5e2b76659e722cc3140dbe229db727f8 Mon Sep 17 00:00:00 2001 From: Hanneke Leegwater Date: Fri, 20 Dec 2024 11:54:32 +0100 Subject: [PATCH 2/5] Do not run example for api_status during package build. --- R/api.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/R/api.R b/R/api.R index c594c79..8a10b41 100644 --- a/R/api.R +++ b/R/api.R @@ -109,7 +109,9 @@ request <- function() { #' @param api_key RSpace API key #' #' @examples +#' \dontrun{ #' api_status() +#' } #' #' @export api_status <- function(api_key = get_api_key()) { From 356b282f46d38a6a0f2e20bca150a091ed6dc94d Mon Sep 17 00:00:00 2001 From: Hanneke Leegwater Date: Fri, 20 Dec 2024 12:53:45 +0100 Subject: [PATCH 3/5] Apply style suggestions from the lintr and styler packages --- R/documents.R | 30 ++++---- R/fields.R | 15 ++-- R/files.R | 6 +- R/mdtodoc.R | 127 +++++++++++++++++--------------- R/utils.R | 4 +- man/api_status.Rd | 2 + man/doc_to_form_id.Rd | 2 +- man/document_search.Rd | 3 +- tests/testthat/helper.R | 68 +++++++++-------- tests/testthat/test-documents.R | 22 +++--- tests/testthat/test-fields.R | 31 +++++--- tests/testthat/test-mdtodoc.R | 45 ++++++----- tests/testthat/test-utils.R | 1 - 13 files changed, 202 insertions(+), 154 deletions(-) diff --git a/R/documents.R b/R/documents.R index daaca95..e01dc66 100644 --- a/R/documents.R +++ b/R/documents.R @@ -14,7 +14,7 @@ document_retrieve <- function(doc_id, api_key = get_api_key()) { } document_post <- function(body, api_key = get_api_key()) { - body$tags <- paste0(c("rspacer", body$tags), collapse= ",") + body$tags <- paste0(c("rspacer", body$tags), collapse = ",") request() |> httr2::req_url_path_append("documents") |> httr2::req_headers(`apiKey` = api_key) |> @@ -28,7 +28,7 @@ document_post <- function(body, api_key = get_api_key()) { } document_replace <- function(body, existing_document_id, api_key = get_api_key()) { - ifelse("rspacer" %in% stringr::str_split_1(body$tags, ","), body$tags, paste0(c("rspacer", body$tags), collapse= ",")) + ifelse("rspacer" %in% stringr::str_split_1(body$tags, ","), body$tags, paste0(c("rspacer", body$tags), collapse = ",")) request() |> httr2::req_url_path_append("documents", parse_rspace_id(existing_document_id)) |> httr2::req_headers(`apiKey` = api_key) |> @@ -45,7 +45,8 @@ document_replace <- function(body, existing_document_id, api_key = get_api_key() #' #' Global search for a term #' -#' Global search for a term, works identically to the simple "All" search in RSpace Workspace. Must be >= 3 characters long. +#' Global search for a term, works identically to the simple "All" search in RSpace Workspace. +#' Must be >= 3 characters long. #' #' @param query description #' @param ... query parameters as documented in @@ -71,9 +72,9 @@ document_search <- function(query, ..., api_key = get_api_key()) { #' @param verbose whether to print the matching document/form #' @inheritParams document_retrieve #' @export -doc_to_form_id <- function(doc_id, verbose = T, api_key = get_api_key()) { +doc_to_form_id <- function(doc_id, verbose = TRUE, api_key = get_api_key()) { json <- document_retrieve(doc_id, api_key) - if(verbose) { + if (verbose) { cli::cli_inform(c( "{.field Document}: {json$globalId} ({json$name})", "{.field Form}:\t {json$form$globalId} ({json$form$name})" @@ -98,26 +99,27 @@ doc_to_form_id <- function(doc_id, verbose = T, api_key = get_api_key()) { #' Returns `FALSE` if no files are attached to the field. #' @export document_list_attachments <- function(doc_id, field_id = NULL, field_name = NULL, api_key = get_api_key()) { - if(is.null(doc_id)) cli::cli_abort("Specify the documnt identifier `doc_id`") + if (is.null(doc_id)) cli::cli_abort("Specify the documnt identifier `doc_id`") # Check field id and/or name - if(is.null(field_id)& is.null(field_name)) cli::cli_abort("Specify `field_id` or `field_name`") - if(!is.null(field_id)& !is.null(field_name)) cli::cli_abort("Specify only `field_id` or `field_name`") - if(!is.null(field_id) & !is.numeric(field_id)) cli::cli_abort("`field_id` should be a number") - if(!is.null(field_name) & !is.character(field_name)) cli::cli_abort("`field_name` should be a string") + if (is.null(field_id) && is.null(field_name)) cli::cli_abort("Specify `field_id` or `field_name`") + if (!is.null(field_id) && !is.null(field_name)) cli::cli_abort("Specify only `field_id` or `field_name`") + if (!is.null(field_id) && !is.numeric(field_id)) cli::cli_abort("`field_id` should be a number") + if (!is.null(field_name) && !is.character(field_name)) cli::cli_abort("`field_name` should be a string") fields <- doc_get_fields(doc_id) - if(!is.null(field_name)) fields <- dplyr::filter(fields, name == field_name) - if(!is.null(field_id)) fields <- fields[field_id,] + if (!is.null(field_name)) fields <- dplyr::filter(fields, name == field_name) + if (!is.null(field_id)) fields <- fields[field_id, ] fields |> dplyr::pull(files) |> unlist(recursive = F) -> files # Return FALSE if no files are attached - if(is.null(files)) return(FALSE) + if (is.null(files)) { + return(FALSE) + } files |> fields_to_data_frame() -> attachment_list return(attachment_list) - } diff --git a/R/fields.R b/R/fields.R index 97872c1..7855848 100644 --- a/R/fields.R +++ b/R/fields.R @@ -2,14 +2,16 @@ data_frame_to_fields <- function(fields_data_frame) { fields <- lapply(1:nrow(fields_data_frame), function(row_nr) { - list("name" = fields_data_frame$name[row_nr], - "content" = fields_data_frame$content[row_nr]) + list( + "name" = fields_data_frame$name[row_nr], + "content" = fields_data_frame$content[row_nr] + ) }) names(fields) <- fields_data_frame$name return(fields) } -fields_to_data_frame <- function(fields){ +fields_to_data_frame <- function(fields) { tibble::tibble(fields = fields) |> tidyr::unnest_wider("fields") } @@ -31,16 +33,17 @@ doc_get_fields <- function(doc_id, api_key = get_api_key()) { #' doc_body$fields <- put_all_fields_in_one_field(doc_body$fields) #' } #' -put_all_fields_in_one_field <- function(doc_body_fields, use_html_sep = T){ +put_all_fields_in_one_field <- function(doc_body_fields, use_html_sep = T) { text_content <- fields_to_data_frame(doc_body_fields) - if(use_html_sep){ + if (use_html_sep) { text_content <- text_content |> dplyr::mutate(content = paste0("

", as.character(content), "

")) } # Collapse content into one field text_content <- text_content |> - dplyr::pull(content) |> paste(collapse = "\n") + dplyr::pull(content) |> + paste(collapse = "\n") return(list(list("content" = text_content))) } diff --git a/R/files.R b/R/files.R index 0b5d011..6ea841a 100644 --- a/R/files.R +++ b/R/files.R @@ -28,7 +28,7 @@ file_upload <- function(path, api_key = get_api_key()) { #' If not, the download is canceled and `FALSE` is returned. #' @export file_download <- function(file_id, path = ".", api_key = get_api_key()) { - if(fs::is_dir(path)) { + if (fs::is_dir(path)) { # determine file name request() |> httr2::req_url_path_append("files", parse_rspace_id(file_id)) |> @@ -39,7 +39,7 @@ file_download <- function(file_id, path = ".", api_key = get_api_key()) { path <- fs::path(path, json$name) } - if(!can_overwrite(path)) { + if (!can_overwrite(path)) { cli::cli_inform(" Cancelling download") return(invisible(FALSE)) } @@ -50,5 +50,5 @@ file_download <- function(file_id, path = ".", api_key = get_api_key()) { httr2::req_perform(path = path) |> httr2::resp_check_status() -> resp cli::cli_inform("Downloaded to {.path {resp$body}} ({file.size(resp$body)} bytes)") - return(invisible(path)) + return(invisible(path)) } diff --git a/R/mdtodoc.R b/R/mdtodoc.R index aa187bd..8dff7cb 100644 --- a/R/mdtodoc.R +++ b/R/mdtodoc.R @@ -1,6 +1,6 @@ html_to_doc_body <- function(path, verbose = T) { xml <- xml2::read_html(path) - title <- rvest::html_element(xml, xpath = "//title") |> rvest::html_text() + title <- rvest::html_element(xml, xpath = "//title") |> rvest::html_text() # Select sections that have a h2 child sections <- rvest::html_elements(xml, xpath = "//body//section[h2]") @@ -10,12 +10,12 @@ html_to_doc_body <- function(path, verbose = T) { section_content <- purrr::map(section_content, ~ .x[-1]) #< remove heading names(section_content) <- section_heading - if(verbose) { + if (verbose) { cli::cli_inform("{.field Title}: {title}") purrr::iwalk(section_heading, ~ cli::cli_inform("{.field - Section {.y}}: {.x}")) } - fields = purrr::imap(section_content, ~ list( + fields <- purrr::imap(section_content, ~ list( name = .y, content = .x )) @@ -26,39 +26,39 @@ html_to_doc_body <- function(path, verbose = T) { ) } -excel_rspace_document_name <- function(path, sections, document_name = NULL){ - if(!is.null(document_name)){ - if(!is.character(document_name)) cli::cli_abort(message = c("x" = "Document name should be a character string or NULL")) +excel_rspace_document_name <- function(path, sections, document_name = NULL) { + if (!is.null(document_name)) { + if (!is.character(document_name)) cli::cli_abort(message = c("x" = "Document name should be a character string or NULL")) title <- document_name - } else if("Title" %in% sections$name){ - title <- dplyr::filter(sections, name == "Title") |> dplyr::pull(content) - } else if("Name" %in% sections$name){ - title <- dplyr::filter(sections, name == "Name") |> dplyr::pull(content) - } else if("title" %in% sections$name){ - title <- dplyr::filter(sections, name == "title") |> dplyr::pull(content) - } else if("name" %in% sections$name){ - title <- dplyr::filter(sections, name == "name") |> dplyr::pull(content) - } else{ + } else if ("Title" %in% sections$name) { + title <- dplyr::filter(sections, name == "Title") |> dplyr::pull(content) + } else if ("Name" %in% sections$name) { + title <- dplyr::filter(sections, name == "Name") |> dplyr::pull(content) + } else if ("title" %in% sections$name) { + title <- dplyr::filter(sections, name == "title") |> dplyr::pull(content) + } else if ("name" %in% sections$name) { + title <- dplyr::filter(sections, name == "name") |> dplyr::pull(content) + } else { title <- tools::file_path_sans_ext(path) |> basename() } return(title) } excel_to_doc_body <- function(path, document_name = NULL, verbose = T, file_type = NULL) { - if(!file.exists(path)) cli::cli_abort(message = c("x" = glue::glue("File not found: {path}"))) - if(is.null(file_type)){ - file_type = tools::file_ext(path) + if (!file.exists(path)) cli::cli_abort(message = c("x" = glue::glue("File not found: {path}"))) + if (is.null(file_type)) { + file_type <- tools::file_ext(path) } - if(!file_type %in% c("xlsx", "csv", "tsv")) cli::cli_abort(message = c("x" = glue::glue("file_type is {file_type}. It should be xlsx, csv or tsv. Specify file_type manually or rename the input file."))) + if (!file_type %in% c("xlsx", "csv", "tsv")) cli::cli_abort(message = c("x" = glue::glue("file_type is {file_type}. It should be xlsx, csv or tsv. Specify file_type manually or rename the input file."))) sections <- switch(file_type, - "xlsx" = readxl::read_excel(path, col_names = c("name", "content")), - "csv" = readr::read_csv(path, col_names = c("name", "content")), - "tsv" = readr::read_tsv(path, col_names = c("name", "content")) + "xlsx" = readxl::read_excel(path, col_names = c("name", "content")), + "csv" = readr::read_csv(path, col_names = c("name", "content")), + "tsv" = readr::read_tsv(path, col_names = c("name", "content")) ) # Set the RSpace entry title title <- excel_rspace_document_name(path, sections, document_name) - if(verbose) { + if (verbose) { cli::cli_inform("{.field Title}: {title}") purrr::iwalk(section_heading, ~ cli::cli_inform("{.field - Section {.y}}: {.x}")) } @@ -70,34 +70,36 @@ excel_to_doc_body <- function(path, document_name = NULL, verbose = T, file_type ) } -attachment_upload <- function(doc_body, attachment, api_key){ +attachment_upload <- function(doc_body, attachment, api_key) { # Test if the attachment list has the correct format - if(!is.list(attachment)) cli::cli_abort(message = c("x" = "attachment is not provided as a list")) - if(!identical(sort(names(attachment)), c("field", "path"))) cli::cli_abort(message = c("x" = "attachment is either missing the field number or the path")) - if(as.numeric(attachment$field) > length(doc_body$fields)) cli::cli_abort(message = c("x" = stringr::str_glue("attachment field number is higher than the total number of fields: {length(doc_body$fields)}"))) + if (!is.list(attachment)) cli::cli_abort(message = c("x" = "attachment is not provided as a list")) + if (!identical(sort(names(attachment)), c("field", "path"))) cli::cli_abort(message = c("x" = "attachment is either missing the field number or the path")) + if (as.numeric(attachment$field) > length(doc_body$fields)) cli::cli_abort(message = c("x" = stringr::str_glue("attachment field number is higher than the total number of fields: {length(doc_body$fields)}"))) # Upload the attachment and add its name and path to to doc_body json <- file_upload(attachment$path, api_key) - doc_body$fields[[attachment$field]]$content <- glue::glue(doc_body$fields[[attachment$field]]$content, - "

Inserted

") + doc_body$fields[[attachment$field]]$content <- glue::glue( + doc_body$fields[[attachment$field]]$content, + "

Inserted

" + ) return(doc_body) } -add_information_to_doc_body <- function(doc_body, template_id = NULL, folder_id = NULL, tags = NULL, attachment = NULL, api_key = get_api_key()){ - if(!is.null(template_id)){ +add_information_to_doc_body <- function(doc_body, template_id = NULL, folder_id = NULL, tags = NULL, attachment = NULL, api_key = get_api_key()) { + if (!is.null(template_id)) { form_id <- parse_rspace_id(doc_to_form_id(template_id, verbose = F)) - doc_body$form = list(id = form_id) + doc_body$form <- list(id = form_id) } - if(!is.null(folder_id)) { + if (!is.null(folder_id)) { doc_body$parentFolderId <- parse_rspace_id(folder_id) } - if(!is.null(tags)) { + if (!is.null(tags)) { doc_body$tags <- paste(tags, collapse = ",") } - if(!is.null(attachment)) { + if (!is.null(attachment)) { doc_body <- attachment_upload(doc_body, attachment, api_key) } @@ -123,17 +125,18 @@ document_create_from_html <- function(path, template_id = NULL, folder_id = NULL existing_document_id = NULL, api_key = get_api_key()) { doc_body <- html_to_doc_body(path, verbose = F) - if(!is.null(existing_document_id)){ + if (!is.null(existing_document_id)) { template_id <- existing_document_id } - if(!is.null(template_id)) { + if (!is.null(template_id)) { template_fields <- doc_get_fields(template_id) - if(length(doc_body$fields) != nrow(template_fields)) + if (length(doc_body$fields) != nrow(template_fields)) { cli::cli_abort("Document has different number of fields ({length(doc_body_fields)}) than template ({nrow(template_fields)})") + } doc_body$fields <- purrr::map2(doc_body$fields, template_fields$type, ~ { - if(.y %in% c("string", "date")) { + if (.y %in% c("string", "date")) { .x$content <- rvest::html_text(.x$content) } else { .x$content <- as.character(.x$content) |> paste(collapse = "\n") @@ -148,7 +151,7 @@ document_create_from_html <- function(path, template_id = NULL, folder_id = NULL doc_body <- add_information_to_doc_body(doc_body, template_id, folder_id, tags, attachment, api_key) # Create or replace the document - if(is.null(existing_document_id)){ + if (is.null(existing_document_id)) { json <- document_post(doc_body) } else { json <- document_replace(doc_body, existing_document_id) @@ -184,7 +187,7 @@ document_append_from_html <- function(path, existing_document_id, tags = NULL, a dplyr::pull(type) doc_body$fields <- purrr::map2(doc_body$fields, doc_body_types, ~ { - if(.y %in% c("string", "date")) { + if (.y %in% c("string", "date")) { .x$content <- rvest::html_text(.x$content) } else { .x$content <- as.character(.x$content) |> paste(collapse = "\n") @@ -196,34 +199,39 @@ document_append_from_html <- function(path, existing_document_id, tags = NULL, a new_fields <- fields_to_data_frame(doc_body$fields) # Warn users when fields are missing in the html that are present in the existing document - if(length(setdiff(current_fields$name, new_fields$name)) > 0){ - if(allow_missing_fields){ - cli::cli_warn(message = paste0("Some fields are missing in the html to append: ", + if (length(setdiff(current_fields$name, new_fields$name)) > 0) { + if (allow_missing_fields) { + cli::cli_warn(message = paste0( + "Some fields are missing in the html to append: ", paste0(setdiff(current_fields$name, new_fields$name), collapse = ", "), - ". Other fields will be added.")) - } else{ + ". Other fields will be added." + )) + } else { cli::cli_abort(message = paste0("Some fields are missing in the html to append: ", paste0(setdiff(current_fields$name, new_fields$name), collapse = ", "), ". Specify allow_missing_fields = T if you still want to append the matching fields.", - collapse = ", ")) + collapse = ", " + )) } } # Warn users when fields are missing in the existing document that are in the html - if(length(setdiff(new_fields$name, current_fields$name)) > 0){ - if(allow_missing_fields){ + if (length(setdiff(new_fields$name, current_fields$name)) > 0) { + if (allow_missing_fields) { cli::cli_warn(message = paste0("The following fields are not in the RSpace document: ", paste0(setdiff(new_fields$name, current_fields$name), collapse = ", "), - ". These will be ignored.")) - } else{ + ". These will be ignored." + )) + } else { cli::cli_abort(message = paste0("The following fields are not in the RSpace document: ", paste0(setdiff(new_fields$name, current_fields$name), collapse = ", "), ". Specify allow_missing_fields = T if you want to ignore these missing fields.", - collapse = ", ")) + collapse = ", " + )) } } # Merge the old and new fields. - new_fields <- left_join(current_fields, new_fields, by = "name") |> + new_fields <- dplyr::left_join(current_fields, new_fields, by = "name") |> dplyr::filter(!is.na(content.y) | content.y != "") |> tidyr::unite(content, content.x, content.y, sep = "\n", na.rm = T) |> dplyr::mutate(id = as.character(id)) @@ -232,11 +240,11 @@ document_append_from_html <- function(path, existing_document_id, tags = NULL, a doc_body$fields <- data_frame_to_fields(new_fields) # Update tags and upload attachments - if(!is.null(tags)) { + if (!is.null(tags)) { doc_body$tags <- paste(tags, collapse = ",") } - if(!is.null(attachment)) { + if (!is.null(attachment)) { # TODO maybe use the columnIndex because I already removed all non-altered field numbers? doc_body <- attachment_upload(doc_body, attachment, api_key) } @@ -267,14 +275,15 @@ document_append_from_html <- function(path, existing_document_id, tags = NULL, a document_create_from_excel <- function(path, file_type = NULL, document_name = NULL, template_id = NULL, folder_id = NULL, tags = NULL, attachment = NULL, api_key = get_api_key(), existing_document_id = NULL) { doc_body <- excel_to_doc_body(path, document_name = document_name, verbose = F, file_type = file_type) - if(!is.null(existing_document_id)){ + if (!is.null(existing_document_id)) { template_id <- existing_document_id } - if(!is.null(template_id)) { + if (!is.null(template_id)) { template_fields <- doc_get_fields(template_id) - if(length(doc_body$fields) != nrow(template_fields)) + if (length(doc_body$fields) != nrow(template_fields)) { cli::cli_abort("Document has different number of fields ({length(doc_body_fields)}) than template ({nrow(template_fields)})") + } } else { # TODO Basic Document can have only 1 field doc_body$fields <- put_all_fields_in_one_field(doc_body$fields) @@ -283,7 +292,7 @@ document_create_from_excel <- function(path, file_type = NULL, document_name = N doc_body <- add_information_to_doc_body(doc_body, template_id = template_id, folder_id = folder_id, tags = tags, attachment = attachment) # Create or replace the document - if(is.null(existing_document_id)){ + if (is.null(existing_document_id)) { json <- document_post(doc_body) } else { json <- document_replace(doc_body, existing_document_id) diff --git a/R/utils.R b/R/utils.R index 8616f9f..1acec9b 100644 --- a/R/utils.R +++ b/R/utils.R @@ -43,7 +43,9 @@ ui_yes <- function(x, .envir = parent.frame()) { } can_overwrite <- function(path) { - if(!fs::file_exists(path)) return(TRUE) + if (!fs::file_exists(path)) { + return(TRUE) + } ui_yes("Overwrite pre-existing file {.file {path}}?") } diff --git a/man/api_status.Rd b/man/api_status.Rd index 187e401..801eb1a 100644 --- a/man/api_status.Rd +++ b/man/api_status.Rd @@ -13,6 +13,8 @@ api_status(api_key = get_api_key()) Function to check availability of the API service } \examples{ +\dontrun{ api_status() +} } diff --git a/man/doc_to_form_id.Rd b/man/doc_to_form_id.Rd index eb2b8b6..cb7b647 100644 --- a/man/doc_to_form_id.Rd +++ b/man/doc_to_form_id.Rd @@ -4,7 +4,7 @@ \alias{doc_to_form_id} \title{Get the form id used for a document} \usage{ -doc_to_form_id(doc_id, verbose = T, api_key = get_api_key()) +doc_to_form_id(doc_id, verbose = TRUE, api_key = get_api_key()) } \arguments{ \item{doc_id}{Unique identifier of the document} diff --git a/man/document_search.Rd b/man/document_search.Rd index 5e75ec3..847710b 100644 --- a/man/document_search.Rd +++ b/man/document_search.Rd @@ -15,5 +15,6 @@ document_search(query, ..., api_key = get_api_key()) \item{api_key}{RSpace API key} } \description{ -Global search for a term, works identically to the simple "All" search in RSpace Workspace. Must be >= 3 characters long. +Global search for a term, works identically to the simple "All" search in RSpace Workspace. +Must be >= 3 characters long. } diff --git a/tests/testthat/helper.R b/tests/testthat/helper.R index 3abfc97..bf9e4b7 100644 --- a/tests/testthat/helper.R +++ b/tests/testthat/helper.R @@ -1,4 +1,4 @@ -helper_get_sections <- function(){ +helper_get_sections <- function() { # Example sections objects sections <- tibble::tibble( name = c("Title", "Name", "title", "name", "date", "results"), @@ -7,7 +7,7 @@ helper_get_sections <- function(){ return(sections) } -helper_get_fields <- function(){ +helper_get_fields <- function() { fields <- list( Title = list(name = "Title", content = "The title"), Name = list(name = "Name", content = "test"), @@ -19,7 +19,7 @@ helper_get_fields <- function(){ return(fields) } -helper_get_unnamed_fields <- function(){ +helper_get_unnamed_fields <- function() { fields <- list( list(name = "Title", content = "The title"), list(name = "Name", content = "test"), @@ -31,60 +31,66 @@ helper_get_unnamed_fields <- function(){ return(fields) } -helper_get_fields_df <- function(){ +helper_get_fields_df <- function() { # This is the structure of an RSpace fields object with two attached files. files_list <- list( - NULL,NULL,NULL, + NULL, NULL, NULL, + list( list( - list(id = 123, globalId = "GL123", name = "fake_file1.csv", caption = NULL, - contentType = "text/csv", created = "1970-01-01T13:01:59.154Z", - size = 47, version = 2, "_links" = "not used now"), - list(id = 124, globalId = "GL124", name = "fake_file2.csv", caption = NULL, - contentType = "text/csv", created = "1970-01-01T13:01:59.154Z", - size = 47, version = 2, "_links" = "not used now") + id = 123, globalId = "GL123", name = "fake_file1.csv", caption = NULL, + contentType = "text/csv", created = "1970-01-01T13:01:59.154Z", + size = 47, version = 2, "_links" = "not used now" ), - NULL - ) + list( + id = 124, globalId = "GL124", name = "fake_file2.csv", caption = NULL, + contentType = "text/csv", created = "1970-01-01T13:01:59.154Z", + size = 47, version = 2, "_links" = "not used now" + ) + ), + NULL + ) # Files part is not working :( fields_df <- tibble::tibble( - "id" = c(101,102,103,104,105), - "globalId" = c("FD101","FD102","FD103","FD104","FD105"), + "id" = c(101, 102, 103, 104, 105), + "globalId" = c("FD101", "FD102", "FD103", "FD104", "FD105"), "name" = c("Title", "Name", "date", "results", "other"), "type" = c("string", "string", "date", "text", "number"), - "content" = c("A", "B", "01-01-1970","

more text

", 1), - "lastModified" = rep("1970-01-01T13:01:59.154Z",5), - "columnIndex" = seq(1,5), + "content" = c("A", "B", "01-01-1970", "

more text

", 1), + "lastModified" = rep("1970-01-01T13:01:59.154Z", 5), + "columnIndex" = seq(1, 5), "files" = files_list, - "listOfMaterials" = rep(NA,5), - "_links" = rep(NA,5) + "listOfMaterials" = rep(NA, 5), + "_links" = rep(NA, 5) ) return(fields_df) } -helper_small_structured_document <- function(){ +helper_small_SD <- function() { small_SD <- list( - form = list(globalId = "some form ID"), - fields = helper_get_unnamed_fields() + form = list(globalId = "some form ID"), + fields = helper_get_unnamed_fields() ) return(small_SD) } -helper_larger_structured_document <- function(){ +helper_larger_SD <- function() { larger_SD <- list( id = 123456, globalId = "SD123456", name = "Test upload of a document", tags = "rspacer,testing", - form = list(id = 56789, - globalId = "FM56789", - name = "Assay template name"), + form = list( + id = 56789, + globalId = "FM56789", + name = "Assay template name" + ), fields = list( list(id = 120, globalId = "FD120", name = "Template Used", type = "string", content = "something here"), - list(id = 121, globalId = "FD121", name = "Title", type = "string", content = "something here"), - list(id = 122, globalId = "FD122", name = "Date", type = "date", content = "01-01-2020"), - list(id = 123, globalId = "FD123", name = "Text field", type = "text", content = "

A paragraph

\n

 

\n

And a header

") + list(id = 121, globalId = "FD121", name = "Title", type = "string", content = "something here"), + list(id = 122, globalId = "FD122", name = "Date", type = "date", content = "01-01-2020"), + list(id = 123, globalId = "FD123", name = "Text field", type = "text", + content = "

A paragraph

\n

 

\n

And a header

") ) ) return(larger_SD) } - diff --git a/tests/testthat/test-documents.R b/tests/testthat/test-documents.R index d43fc39..aa7f63f 100644 --- a/tests/testthat/test-documents.R +++ b/tests/testthat/test-documents.R @@ -1,7 +1,9 @@ -test_that("document_list_attachments",{ +test_that("document_list_attachments", { local_mocked_bindings( - get_api_key = function() return("API key used for testing"), - doc_get_fields = function(doc_id){ + get_api_key = function() { + return("API key used for testing") + }, + doc_get_fields = function(doc_id) { helper_get_fields_df() } ) @@ -11,15 +13,17 @@ test_that("document_list_attachments",{ # Should return a tibble created with two attachments: 2 rows, 9 columns. expect_type(document_list_attachments("ABC", field_name = "results"), typeof(tibble::tibble())) - expect_equal(ncol(document_list_attachments("ABC", field_name = "results")),9) - expect_equal(nrow(document_list_attachments("ABC", field_name = "results")),2) + expect_equal(ncol(document_list_attachments("ABC", field_name = "results")), 9) + expect_equal(nrow(document_list_attachments("ABC", field_name = "results")), 2) expect_type(document_list_attachments("ABC", field_id = 4), typeof(tibble::tibble())) - expect_equal(ncol(document_list_attachments("ABC", field_id = 4)),9) - expect_equal(nrow(document_list_attachments("ABC", field_id = 4)),2) + expect_equal(ncol(document_list_attachments("ABC", field_id = 4)), 9) + expect_equal(nrow(document_list_attachments("ABC", field_id = 4)), 2) - expect_equal(document_list_attachments("ABC", field_name = "results"), - document_list_attachments("ABC", field_id = 4)) + expect_equal( + document_list_attachments("ABC", field_name = "results"), + document_list_attachments("ABC", field_id = 4) + ) # Error if input is incorrect expect_error(document_list_attachments()) diff --git a/tests/testthat/test-fields.R b/tests/testthat/test-fields.R index ea8e633..de35d86 100644 --- a/tests/testthat/test-fields.R +++ b/tests/testthat/test-fields.R @@ -23,21 +23,32 @@ test_that("fields_to_data_frame creates a tibble", { test_that("doc_get_fields creates a fields data frame", { local_mocked_bindings( - get_api_key = function() return("API key used for testing"), - document_retrieve = function(doc_id, api_key = get_api_key()){ - helper_larger_structured_document() + get_api_key = function() { + return("API key used for testing") + }, + document_retrieve = function(doc_id, api_key = get_api_key()) { + helper_larger_SD() } ) - expect_equal(doc_get_fields("some incorrect id"), - fields_to_data_frame(helper_larger_structured_document()$fields)) + expect_equal( + doc_get_fields("some incorrect id"), + fields_to_data_frame(helper_larger_SD()$fields) + ) }) test_that("put_all_fields_in_one_field can collapse fields", { # Rspace needs a list with a list with at least content. fields <- helper_get_fields() - expect_equal(put_all_fields_in_one_field(fields), - list(list(content = "

The title

\n

test

\n

small title

\n

smaller name

\n

25-09-2024

\n

creating fields

"))) - expect_equal(put_all_fields_in_one_field(fields, use_html_sep = F), - list(list(content = "The title\ntest\nsmall title\nsmaller name\n25-09-2024\ncreating fields"))) - # TODO run with fields in helper_small_structured_document and helper_larger_structured_document + expect_equal( + put_all_fields_in_one_field(fields), + list(list( + content = + "

The title

\n

test

\n

small title

\n

smaller name

\n

25-09-2024

\n

creating fields

" + )) + ) + expect_equal( + put_all_fields_in_one_field(fields, use_html_sep = FALSE), + list(list(content = "The title\ntest\nsmall title\nsmaller name\n25-09-2024\ncreating fields")) + ) + # TODO run with fields in helper_small_SD and helper_larger_SD }) diff --git a/tests/testthat/test-mdtodoc.R b/tests/testthat/test-mdtodoc.R index ace1a30..9104b18 100644 --- a/tests/testthat/test-mdtodoc.R +++ b/tests/testthat/test-mdtodoc.R @@ -3,38 +3,47 @@ test_that("add_information_to_doc_body can add information to a list", { expect_equal(add_information_to_doc_body(list()), list()) # template_id expect_error(add_information_to_doc_body(list(), template_id = "incorrect ID")) - #TODO: how to test an API? - #TODO below how to check a correct but non-existing template and think about the attachments - #expect_error(add_information_to_doc_body(list(), template_id = "SD001")) - #expect_equal(add_information_to_doc_body(list(), folder_id = "SD123", tags = "test", attachment = list("7" = "hoi.txt"))) - - expect_equal(add_information_to_doc_body(list(), folder_id = "SD123", tags = "test"), - list(parentFolderId = 123, tags = "test")) + # TODO: how to test an API? + # TODO below how to check a correct but non-existing template and think about the attachments + # expect_error(add_information_to_doc_body(list(), template_id = "SD001")) + # expect_equal(add_information_to_doc_body(list(), folder_id = "SD123", tags = "test", attachment = list("7" = "hoi.txt"))) + expect_equal( + add_information_to_doc_body(list(), folder_id = "SD123", tags = "test"), + list(parentFolderId = 123, tags = "test") + ) }) test_that("excel_rspace_document_name can process a document name", { sections <- helper_get_sections() # Check document name - expect_equal(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = NULL, document_name = "thisone"), "thisone") + expect_equal(excel_rspace_document_name( + path = "minimal_excel.xlsx", + sections = NULL, document_name = "thisone" + ), "thisone") expect_error(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = NULL, document_name = NA)) expect_error(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = NULL, document_name = 123)) # Check to get name from sections in the order that is in the user manual - expect_equal(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = sections, document_name = "thisone"), "thisone") + expect_equal(excel_rspace_document_name( + path = "minimal_excel.xlsx", + sections = sections, document_name = "thisone" + ), "thisone") expect_equal(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = sections), "The title") - expect_equal(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = sections[2:6,]), "test") - expect_equal(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = sections[3:6,]), "small title") - expect_equal(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = sections[4:6,]), "smaller name") + expect_equal(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = sections[2:6, ]), "test") + expect_equal(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = sections[3:6, ]), "small title") + expect_equal(excel_rspace_document_name(path = "minimal_excel.xlsx", sections = sections[4:6, ]), "smaller name") # Get the name from a file path - expect_equal(excel_rspace_document_name(path = "some_path/minimal_excel.xlsx", sections = sections[5:6,]), - "minimal_excel") + expect_equal( + excel_rspace_document_name(path = "some_path/minimal_excel.xlsx", sections = sections[5:6, ]), + "minimal_excel" + ) }) -test_that("excel_to_doc_body can create a doc body",{ +test_that("excel_to_doc_body can create a doc body", { # one that should work - expect_type(excel_to_doc_body(path = testthat::test_path("minimal_excel.xlsx"), verbose = F, file_type = "xlsx"), "list") + expect_type(excel_to_doc_body(path = testthat::test_path("minimal_excel.xlsx"), verbose = FALSE, file_type = "xlsx"), "list") # not existing file - expect_error(excel_to_doc_body(path = "not_existing_file.xlsx", verbose = F, file_type = ".xlsx")) + expect_error(excel_to_doc_body(path = "not_existing_file.xlsx", verbose = FALSE, file_type = ".xlsx")) # incorrect excel file type specified - expect_error(excel_to_doc_body(path = testthat::test_path("minimal_excel.xlsx"), verbose = F, file_type = ".xlsx")) + expect_error(excel_to_doc_body(path = testthat::test_path("minimal_excel.xlsx"), verbose = FALSE, file_type = ".xlsx")) }) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R index 34d82e8..4d8a32c 100644 --- a/tests/testthat/test-utils.R +++ b/tests/testthat/test-utils.R @@ -5,7 +5,6 @@ test_that("parsing rspace ids works", { expect_equal(parse_rspace_id("GL258158"), 258158) expect_equal(parse_rspace_id("FL242398"), 242398) expect_equal(parse_rspace_id("NB242194"), 242194) - # expect_equal(parse_rspace_id("https://leiden.researchspace.com/globalId/SD242392"), 242392) }) test_that("is_url works", { From 2b3a8fc0586498457f887d1da8898bc238938e06 Mon Sep 17 00:00:00 2001 From: Hanneke Leegwater Date: Fri, 20 Dec 2024 13:11:39 +0100 Subject: [PATCH 4/5] Test on multiple R versions --- .github/workflows/R-CMD-check.yaml | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 0967791..f4952da 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -5,21 +5,36 @@ on: branches: [main, master, dev, fix_problems_CI] pull_request: -name: R-CMD-check.yaml +name: R-CMD-check permissions: read-all jobs: R-CMD-check: - runs-on: ubuntu-latest + runs-on: ${{ matrix.config.os }} + + name: ${{ matrix.config.os }} (${{ matrix.config.r }}) + + strategy: + fail-fast: false + matrix: + config: + - {os: macos-latest, r: 'release'} + - {os: windows-latest, r: 'release'} + - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} + - {os: ubuntu-latest, r: 'release'} + - {os: ubuntu-latest, r: 'oldrel-1'} + env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes + steps: - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2 with: + r-version: ${{ matrix.config.r }} use-public-rspm: true - uses: r-lib/actions/setup-r-dependencies@v2 From ba06fb8cd32699058ed67763677f3df68eb296ec Mon Sep 17 00:00:00 2001 From: Hanneke Leegwater Date: Fri, 20 Dec 2024 14:04:46 +0100 Subject: [PATCH 5/5] Prepare to merge branch --- .github/workflows/R-CMD-check.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index f4952da..78a4a3f 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -2,7 +2,7 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: [main, master, dev, fix_problems_CI] + branches: [main, master, dev] pull_request: name: R-CMD-check