diff --git a/NAMESPACE b/NAMESPACE index 30fea51..a78c225 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,9 +1,6 @@ # Generated by roxygen2: do not edit by hand export(connect_to_rcc_billing_db) -export(convert_schema_to_sqlite) -export(create_and_load_test_table) -export(create_table) export(draft_communication_record_from_line_item) export(fix_data_in_invoice_line_item) export(fix_data_in_invoice_line_item_communication) @@ -36,9 +33,7 @@ export(get_user_rights_and_info) export(get_user_rights_and_info_v1) export(invoice_line_item_df_from) export(mutate_columns_to_posixct) -export(mysql_schema_to_sqlite) export(next_n_months) -export(populate_table) export(previous_month) export(previous_n_months) export(sequester_projects) diff --git a/R/data.R b/R/data.R index c703769..d2d1966 100644 --- a/R/data.R +++ b/R/data.R @@ -122,162 +122,6 @@ #'} "ctsit_staff_employment_periods" -#' @title redcap_projects_test_data -#' @description A redcap_projects suitable for billing tests -#' @format A data frame with 5 rows and 149 variables: -#' \describe{ -#' \item{\code{project_id}}{double Project ID} -#' \item{\code{app_title}}{character The project name as displayed} -#' \item{\code{project_pi_firstname}}{character Principal Investigator First Name} -#' \item{\code{project_pi_lastname}}{character Principal Investigator Last Name} -#' \item{\code{project_pi_email}}{character Principal Investigator Email} -#' \item{\code{project_name}}{character The original project name as a keyword} -#' \item{\code{creation_time}}{double Project Creation Datetime} -#' \item{\code{status}}{integer} -#' \item{\code{production_time}}{double} -#' \item{\code{inactive_time}}{double} -#' \item{\code{completed_time}}{double} -#' \item{\code{completed_by}}{character} -#' \item{\code{data_locked}}{integer} -#' \item{\code{log_event_table}}{character} -#' \item{\code{created_by}}{integer} -#' \item{\code{draft_mode}}{integer} -#' \item{\code{surveys_enabled}}{integer} -#' \item{\code{repeatforms}}{integer} -#' \item{\code{scheduling}}{integer} -#' \item{\code{purpose}}{integer} -#' \item{\code{purpose_other}}{character} -#' \item{\code{show_which_records}}{integer} -#' \item{\code{__SALT__}}{character} -#' \item{\code{count_project}}{integer} -#' \item{\code{investigators}}{character} -#' \item{\code{project_note}}{character} -#' \item{\code{online_offline}}{integer} -#' \item{\code{auth_meth}}{character} -#' \item{\code{double_data_entry}}{integer} -#' \item{\code{project_language}}{character} -#' \item{\code{project_encoding}}{character} -#' \item{\code{is_child_of}}{character} -#' \item{\code{date_shift_max}}{integer} -#' \item{\code{institution}}{character} -#' \item{\code{site_org_type}}{character} -#' \item{\code{grant_cite}}{character} -#' \item{\code{project_contact_name}}{character} -#' \item{\code{project_contact_email}}{character} -#' \item{\code{headerlogo}}{character} -#' \item{\code{auto_inc_set}}{integer} -#' \item{\code{custom_data_entry_note}}{character} -#' \item{\code{custom_index_page_note}}{character} -#' \item{\code{order_id_by}}{character} -#' \item{\code{custom_reports}}{character} -#' \item{\code{report_builder}}{character} -#' \item{\code{disable_data_entry}}{integer} -#' \item{\code{google_translate_default}}{character} -#' \item{\code{require_change_reason}}{integer} -#' \item{\code{dts_enabled}}{integer} -#' \item{\code{project_pi_mi}}{character} -#' \item{\code{project_pi_alias}}{character} -#' \item{\code{project_pi_username}}{character} -#' \item{\code{project_pi_pub_exclude}}{integer} -#' \item{\code{project_pub_matching_institution}}{character} -#' \item{\code{project_irb_number}}{character} -#' \item{\code{project_grant_number}}{character} -#' \item{\code{history_widget_enabled}}{integer} -#' \item{\code{secondary_pk}}{character} -#' \item{\code{secondary_pk_display_value}}{integer} -#' \item{\code{secondary_pk_display_label}}{integer} -#' \item{\code{custom_record_label}}{character} -#' \item{\code{display_project_logo_institution}}{integer} -#' \item{\code{imported_from_rs}}{integer} -#' \item{\code{display_today_now_button}}{integer} -#' \item{\code{auto_variable_naming}}{integer} -#' \item{\code{randomization}}{integer} -#' \item{\code{enable_participant_identifiers}}{integer} -#' \item{\code{survey_email_participant_field}}{character} -#' \item{\code{survey_phone_participant_field}}{character} -#' \item{\code{data_entry_trigger_url}}{character} -#' \item{\code{template_id}}{integer} -#' \item{\code{date_deleted}}{double Date of project deletion or NA if not deleted} -#' \item{\code{data_resolution_enabled}}{integer} -#' \item{\code{field_comment_edit_delete}}{integer} -#' \item{\code{realtime_webservice_enabled}}{integer} -#' \item{\code{realtime_webservice_type}}{character} -#' \item{\code{realtime_webservice_offset_days}}{double} -#' \item{\code{realtime_webservice_offset_plusminus}}{character} -#' \item{\code{last_logged_event}}{double} -#' \item{\code{edoc_upload_max}}{integer} -#' \item{\code{file_attachment_upload_max}}{integer} -#' \item{\code{survey_queue_custom_text}}{character} -#' \item{\code{survey_queue_hide}}{integer} -#' \item{\code{survey_auth_enabled}}{integer} -#' \item{\code{survey_auth_field1}}{character} -#' \item{\code{survey_auth_event_id1}}{integer} -#' \item{\code{survey_auth_field2}}{character} -#' \item{\code{survey_auth_event_id2}}{integer} -#' \item{\code{survey_auth_field3}}{character} -#' \item{\code{survey_auth_event_id3}}{integer} -#' \item{\code{survey_auth_min_fields}}{character} -#' \item{\code{survey_auth_apply_all_surveys}}{integer} -#' \item{\code{survey_auth_custom_message}}{character} -#' \item{\code{survey_auth_fail_limit}}{integer} -#' \item{\code{survey_auth_fail_window}}{integer} -#' \item{\code{twilio_enabled}}{integer} -#' \item{\code{twilio_modules_enabled}}{character} -#' \item{\code{twilio_hide_in_project}}{integer} -#' \item{\code{twilio_account_sid}}{character} -#' \item{\code{twilio_auth_token}}{character} -#' \item{\code{twilio_from_number}}{double} -#' \item{\code{twilio_voice_language}}{character} -#' \item{\code{twilio_option_voice_initiate}}{integer} -#' \item{\code{twilio_option_sms_initiate}}{integer} -#' \item{\code{twilio_option_sms_invite_make_call}}{integer} -#' \item{\code{twilio_option_sms_invite_receive_call}}{integer} -#' \item{\code{twilio_option_sms_invite_web}}{integer} -#' \item{\code{twilio_default_delivery_preference}}{character} -#' \item{\code{twilio_request_inspector_checked}}{double} -#' \item{\code{twilio_request_inspector_enabled}}{integer} -#' \item{\code{twilio_append_response_instructions}}{integer} -#' \item{\code{twilio_multiple_sms_behavior}}{character} -#' \item{\code{twilio_delivery_preference_field_map}}{character} -#' \item{\code{two_factor_exempt_project}}{integer} -#' \item{\code{two_factor_force_project}}{integer} -#' \item{\code{disable_autocalcs}}{integer} -#' \item{\code{custom_public_survey_links}}{character} -#' \item{\code{pdf_custom_header_text}}{character} -#' \item{\code{pdf_show_logo_url}}{integer} -#' \item{\code{pdf_hide_secondary_field}}{integer} -#' \item{\code{pdf_hide_record_id}}{integer} -#' \item{\code{shared_library_enabled}}{integer} -#' \item{\code{allow_delete_record_from_log}}{integer} -#' \item{\code{delete_file_repository_export_files}}{integer} -#' \item{\code{custom_project_footer_text}}{character} -#' \item{\code{custom_project_footer_text_link}}{character} -#' \item{\code{google_recaptcha_enabled}}{integer} -#' \item{\code{datamart_allow_repeat_revision}}{integer} -#' \item{\code{datamart_allow_create_revision}}{integer} -#' \item{\code{datamart_enabled}}{integer} -#' \item{\code{break_the_glass_enabled}}{integer} -#' \item{\code{datamart_cron_enabled}}{integer} -#' \item{\code{datamart_cron_end_date}}{double} -#' \item{\code{fhir_include_email_address_project}}{integer} -#' \item{\code{file_upload_vault_enabled}}{integer} -#' \item{\code{file_upload_versioning_enabled}}{integer} -#' \item{\code{missing_data_codes}}{character} -#' \item{\code{record_locking_pdf_vault_enabled}}{integer} -#' \item{\code{record_locking_pdf_vault_custom_text}}{character} -#' \item{\code{fhir_cdp_auto_adjudication_enabled}}{integer} -#' \item{\code{fhir_cdp_auto_adjudication_cronjob_enabled}}{integer} -#' \item{\code{project_dashboard_min_data_points}}{integer} -#' \item{\code{bypass_branching_erase_field_prompt}}{integer} -#' \item{\code{protected_email_mode}}{integer} -#' \item{\code{protected_email_mode_custom_text}}{character} -#' \item{\code{protected_email_mode_trigger}}{character} -#' \item{\code{protected_email_mode_logo}}{integer} -#' \item{\code{hide_filled_forms}}{integer} -#' \item{\code{form_activation_survey_autocontinue}}{integer} -#' } -"redcap_projects_test_data" - #' @title one_deleted_project_record #' @description A single REDCap project record for a deleted project #' @format A data frame with 1 rows and 149 variables: @@ -590,24 +434,6 @@ #' } "projects_table_fragment" -#' @title redcap_entity_project_ownership_test_data -#' @description A test dataset for the redcap_entity_project_ownership table -#' @format A data frame with 5 rows and 10 variables: -#' \describe{ -#' \item{\code{id}}{integer Primary key} -#' \item{\code{created}}{double Date the record was created as a unix timestamp} -#' \item{\code{updated}}{double Date the record was last updated as a unix timestamp} -#' \item{\code{pid}}{double REDCap project_id of the project} -#' \item{\code{username}}{character Username of the project owner if it exists} -#' \item{\code{email}}{character Email address of the project owner if username does not exist} -#' \item{\code{firstname}}{character Firstname of the project owner if username does not exist} -#' \item{\code{lastname}}{character Lastname of the project owner if username does not exist} -#' \item{\code{billable}}{integer A boolean to indicate if the project should be billed} -#' \item{\code{sequestered}}{integer A boolean to indicate if the project is sequestered} -#' } -#' @details DETAILS -"redcap_entity_project_ownership_test_data" - #' @title redcap_user_information_test_data #' @description Test dataset for redcap_user_information #' @format A data frame with 5 rows and 49 variables: @@ -727,19 +553,6 @@ #' @details DETAILS "fiscal_years" -#' @title cleanup_project_ownership_test_data -#' @description A named list of dataframes used to test the functions written for cleanup_project_ownership.R -#' @format A named list of 5 dataframes: -#' \describe{ -#' \item{\code{redcap_user_information}}{REDCap Core table} -#' \item{\code{redcap_projects}}{REDCap Core table} -#' \item{\code{redcap_user_rights}}{REDCap Core table} -#' \item{\code{redcap_user_roles}}{REDCap Core table} -#' \item{\code{redcap_project_last_users}}{local table} -#' } -#' @details DETAILS -"cleanup_project_ownership_test_data" - #' @title log_event_tables #' @description A vector of the names of the 9 redcap log event tables #' @format A vector with 9 elements diff --git a/R/devtools.R b/R/devtools.R index 56f7eaf..e69de29 100644 --- a/R/devtools.R +++ b/R/devtools.R @@ -1,151 +0,0 @@ -#' Locates a MySQL schema file for table_name, converts it to a sqlite schema -#' and returns that schema. -#' -#' @param table_name, the name of the table to convert -#' -#' @returns sqlite schema for table_name -#' -#' @examples -#' \dontrun{ -#' convert_schema_to_sqlite(table_name = "service_type") -#' } -#' @export -convert_schema_to_sqlite <- function(table_name) { - schema_file_name <- paste0(table_name, ".sql") - pl_to_sqlite <- system.file("", "to_sqlite.pl", package = "rcc.billing") - - # read original - original_schema_file <- system.file("schema", schema_file_name, package = "rcc.billing") - - if (original_schema_file == "") { - stop(paste("Schema file does not exist for", table_name)) - } - - # convert to sqlite - cmd <- paste("cat", original_schema_file, "|", "perl", pl_to_sqlite) - - result <- system(cmd, intern = TRUE) |> paste(collapse = "") - return(result) -} - -#' converts an in-memory schema to a sqlite schema -#' and returns that schema. -#' -#' @param schema, a MySQL/MariaDB Schema -#' -#' @returns sqlite schema for `schema` -#' -#' @examples -#' \dontrun{ -#' mysql_schema_to_sqlite(schema) -#' } -#' @export -mysql_schema_to_sqlite <- function(schema) { - # find the perl script that does the conversion - pl_to_sqlite <- system.file("", "to_sqlite.pl", package = "rcc.billing") - - # construct conversion command - cmd <- paste("perl", pl_to_sqlite) - - result <- system(cmd, input = schema, intern = TRUE) |> paste(collapse = "") - return(result) -} - -#' Creates a table based on a schema. -#' -#' @param conn, a DBI connection object -#' @param schema, the ddl to execute against conn -#' -#' @examples -#' \dontrun{ -#' table_name <- "service_type" -#' conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") -#' -#' schema <- convert_schema_to_sqlite(table_name) -#' create_table(conn = conn, schema = schema) -#' } -#' @export -create_table <- function(conn, schema) { - schemata <- stringr::str_split(schema, pattern = ";\n+")[[1]] - schemata <- schemata[schemata != ""] - - for (schema in schemata) { - # create table - result <- DBI::dbSendQuery(conn, schema) - # close result set to avoid warning - DBI::dbClearResult(result) - } -} - -#' Populates table_name with the corresponding test data found in /data. -#' -#' @param conn, a DBI connection object -#' @param table_name, the table to populate with test data -#' @param use_test_data, whether to use "_test_data" -#' -#' @examples -#' \dontrun{ -#' conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") -#' populate_table(conn = conn, table_name = "service_type") -#' } -#' @export -populate_table <- function(conn, table_name, use_test_data = FALSE) { - data_ref <- table_name - - if (isTRUE(use_test_data)) { - data_ref <- paste0(data_ref, "_test_data") - } - - # get test data - data <- get0(data_ref) - - # write sample data - result <- DBI::dbAppendTable( - conn = conn, - name = table_name, - value = data, - overwrite = TRUE - ) - - result <- DBI::dbGetQuery(conn, paste("select * from", table_name)) - return(result) -} - -#' create_and_load_test_table -#' -#' Create a named table for which we have stored schema and optionally load the stored test data into it -#' -#' @param conn, a DBI Connection object -#' @param table_name, the name of the table -#' @param load_test_data, a logical to indicate if test data should be loaded -#' @param is_sqllite, a logical to indicate if the DBI object is a a SQLLite DB -#' -#' @return The test data as read back from the new table or NULL -#' @export -#' -#' @examples -#' conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") -#' result <- create_and_load_test_table( -#' conn = conn, -#' table_name = "invoice_line_item_communications", -#' is_sqllite = TRUE -#' ) -create_and_load_test_table <- function(conn, table_name, load_test_data = TRUE, is_sqllite = FALSE) { - schema_file_name <- paste0(table_name, ".sql") - original_schema_file <- system.file("schema", schema_file_name, package = "rcc.billing") - schema <- dplyr::if_else(is_sqllite, - convert_schema_to_sqlite(table_name = table_name), - readr::read_file(original_schema_file) - ) - create_table( - conn = conn, - schema = schema - ) - if (load_test_data) { - populate_table( - conn = conn, - table_name = table_name, - use_test_data = T - ) - } -} diff --git a/R/redcap_projects.R b/R/redcap_projects.R index aa31408..2596715 100644 --- a/R/redcap_projects.R +++ b/R/redcap_projects.R @@ -44,12 +44,12 @@ get_last_project_user <- function(con, pid) { #' @export #' #' @examples +#' \dontrun{ #' get_projects_needing_new_owners( -#' redcap_entity_project_ownership = -#' cleanup_project_ownership_test_data$redcap_entity_project_ownership, -#' redcap_user_information = -#' cleanup_project_ownership_test_data$redcap_user_information +#' redcap_entity_project_ownership = redcap_entity_project_ownership, +#' redcap_user_information = redcap_user_information #' ) +#' } get_projects_needing_new_owners <- function(redcap_entity_project_ownership, redcap_user_information) { projects_needing_new_owners <- redcap_entity_project_ownership |> @@ -71,12 +71,12 @@ get_projects_needing_new_owners <- function(redcap_entity_project_ownership, #' @export #' #' @examples +#' \dontrun{ #' get_projects_without_owners( -#' redcap_projects = -#' cleanup_project_ownership_test_data$redcap_projects, -#' redcap_entity_project_ownership = -#' cleanup_project_ownership_test_data$redcap_entity_project_ownership +#' redcap_projects = redcap_projects, +#' redcap_entity_project_ownership = redcap_entity_project_ownership #' ) +#' } get_projects_without_owners <- function(redcap_projects, redcap_entity_project_ownership) { redcap_projects |> @@ -97,11 +97,12 @@ get_projects_without_owners <- function(redcap_projects, #' @export #' #' @examples +#' \dontrun{ #' get_project_pis( -#' redcap_projects = -#' cleanup_project_ownership_test_data$redcap_projects, +#' redcap_projects = redcap_projects, #' return_project_ownership_format = TRUE #' ) +#' } get_project_pis <- function(redcap_projects, return_project_ownership_format = FALSE) { non_blank_project_pis <- redcap_projects |> @@ -456,10 +457,12 @@ get_reassigned_line_items <- function(sent_line_items, rc_conn) { redcap_entity_project_ownership <- dplyr::tbl(rc_conn, "redcap_entity_project_ownership") |> dplyr::filter(.data$pid %in% !!sent_line_items$project_id) |> dplyr::mutate_at("pid", as.character) |> + dplyr::mutate(dplyr::across("pid", ~stringr::str_replace(., "\\.0", ""))) |> dplyr::select(-c("id", "created", "updated")) |> dplyr::collect() - reassigned_line_items <- sent_line_items |> + reassigned_line_items <- + sent_line_items |> dplyr::left_join(redcap_entity_project_ownership, by = c("project_id" = "pid")) |> dplyr::filter( .data$gatorlink != .data$username | @@ -501,14 +504,13 @@ get_reassigned_line_items <- function(sent_line_items, rc_conn) { #' @export #' #' @examples +#' \dontrun{ #' get_research_projects_not_using_viable_pi_data( -#' redcap_projects = -#' cleanup_project_ownership_test_data$redcap_projects, -#' redcap_entity_project_ownership = -#' cleanup_project_ownership_test_data$redcap_entity_project_ownership, -#' redcap_user_information = -#' cleanup_project_ownership_test_data$redcap_user_information +#' redcap_projects = redcap_projects, +#' redcap_entity_project_ownership = redcap_entity_project_ownership, +#' redcap_user_information = redcap_user_information #' ) +#' } get_research_projects_not_using_viable_pi_data <- function(redcap_projects, redcap_entity_project_ownership, redcap_user_information) { diff --git a/R/transform.R b/R/transform.R index 0038f28..9cdd7ec 100644 --- a/R/transform.R +++ b/R/transform.R @@ -125,7 +125,7 @@ fix_data_in_invoice_line_item_communication <- function(data) { #' #' @examples #' \dontrun{ -#' fix_data_in_redcap_projects(redcap_projects_test_data) +#' fix_data_in_redcap_projects(redcap_projects) #' } #' @export fix_data_in_redcap_projects <- function(data) { diff --git a/data-raw/redcap_user_information.R b/data-raw/redcap_user_information.R index 0cf895d..2fa4e0d 100644 --- a/data-raw/redcap_user_information.R +++ b/data-raw/redcap_user_information.R @@ -1,4 +1,4 @@ -## Create redcap_projects_test_data from invoice_line_items_test_data +## Create redcap_user_information from the database of a testing system library(redcapcustodian) library(rcc.billing) @@ -18,7 +18,7 @@ my_table <- "redcap_user_information" # collect() %>% # filter(ui_id >= 3) # -# # werite the test data +# # write the test data # usethis::use_data(redcap_user_information_test_data, overwrite = T) # # # write the schema diff --git a/man/cleanup_project_ownership_test_data.Rd b/man/cleanup_project_ownership_test_data.Rd deleted file mode 100644 index 4c7e962..0000000 --- a/man/cleanup_project_ownership_test_data.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data.R -\docType{data} -\name{cleanup_project_ownership_test_data} -\alias{cleanup_project_ownership_test_data} -\title{cleanup_project_ownership_test_data} -\format{ -A named list of 5 dataframes: -\describe{ - \item{\code{redcap_user_information}}{REDCap Core table} - \item{\code{redcap_projects}}{REDCap Core table} - \item{\code{redcap_user_rights}}{REDCap Core table} - \item{\code{redcap_user_roles}}{REDCap Core table} - \item{\code{redcap_project_last_users}}{local table} -} -} -\usage{ -cleanup_project_ownership_test_data -} -\description{ -A named list of dataframes used to test the functions written for cleanup_project_ownership.R -} -\details{ -DETAILS -} -\keyword{datasets} diff --git a/man/convert_schema_to_sqlite.Rd b/man/convert_schema_to_sqlite.Rd deleted file mode 100644 index b5edd10..0000000 --- a/man/convert_schema_to_sqlite.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/devtools.R -\name{convert_schema_to_sqlite} -\alias{convert_schema_to_sqlite} -\title{Locates a MySQL schema file for table_name, converts it to a sqlite schema -and returns that schema.} -\usage{ -convert_schema_to_sqlite(table_name) -} -\arguments{ -\item{table_name, }{the name of the table to convert} -} -\value{ -sqlite schema for table_name -} -\description{ -Locates a MySQL schema file for table_name, converts it to a sqlite schema -and returns that schema. -} -\examples{ -\dontrun{ -convert_schema_to_sqlite(table_name = "service_type") -} -} diff --git a/man/create_and_load_test_table.Rd b/man/create_and_load_test_table.Rd deleted file mode 100644 index fe2c3a5..0000000 --- a/man/create_and_load_test_table.Rd +++ /dev/null @@ -1,36 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/devtools.R -\name{create_and_load_test_table} -\alias{create_and_load_test_table} -\title{create_and_load_test_table} -\usage{ -create_and_load_test_table( - conn, - table_name, - load_test_data = TRUE, - is_sqllite = FALSE -) -} -\arguments{ -\item{conn, }{a DBI Connection object} - -\item{table_name, }{the name of the table} - -\item{load_test_data, }{a logical to indicate if test data should be loaded} - -\item{is_sqllite, }{a logical to indicate if the DBI object is a a SQLLite DB} -} -\value{ -The test data as read back from the new table or NULL -} -\description{ -Create a named table for which we have stored schema and optionally load the stored test data into it -} -\examples{ -conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") -result <- create_and_load_test_table( - conn = conn, - table_name = "invoice_line_item_communications", - is_sqllite = TRUE -) -} diff --git a/man/create_table.Rd b/man/create_table.Rd deleted file mode 100644 index 66f517b..0000000 --- a/man/create_table.Rd +++ /dev/null @@ -1,25 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/devtools.R -\name{create_table} -\alias{create_table} -\title{Creates a table based on a schema.} -\usage{ -create_table(conn, schema) -} -\arguments{ -\item{conn, }{a DBI connection object} - -\item{schema, }{the ddl to execute against conn} -} -\description{ -Creates a table based on a schema. -} -\examples{ -\dontrun{ -table_name <- "service_type" -conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") - -schema <- convert_schema_to_sqlite(table_name) -create_table(conn = conn, schema = schema) -} -} diff --git a/man/fix_data_in_redcap_projects.Rd b/man/fix_data_in_redcap_projects.Rd index 897c9f6..fe34a78 100644 --- a/man/fix_data_in_redcap_projects.Rd +++ b/man/fix_data_in_redcap_projects.Rd @@ -18,6 +18,6 @@ This allows testing in SQLite3 while production is MariaDB } \examples{ \dontrun{ -fix_data_in_redcap_projects(redcap_projects_test_data) +fix_data_in_redcap_projects(redcap_projects) } } diff --git a/man/get_project_pis.Rd b/man/get_project_pis.Rd index 19f66b3..b3e95a8 100644 --- a/man/get_project_pis.Rd +++ b/man/get_project_pis.Rd @@ -19,9 +19,10 @@ a dataframe of project_PI details from redcap_projects Returns a dataframe of all project_PI details in redcap_projects for PIs with an email address in project_pi_email } \examples{ +\dontrun{ get_project_pis( - redcap_projects = - cleanup_project_ownership_test_data$redcap_projects, + redcap_projects = redcap_projects, return_project_ownership_format = TRUE ) } +} diff --git a/man/get_projects_needing_new_owners.Rd b/man/get_projects_needing_new_owners.Rd index 12d6d38..413ab4d 100644 --- a/man/get_projects_needing_new_owners.Rd +++ b/man/get_projects_needing_new_owners.Rd @@ -21,10 +21,10 @@ a vector of project IDs Returns the project IDs of projects that are owned by a REDCap user that has no primary email address } \examples{ +\dontrun{ get_projects_needing_new_owners( - redcap_entity_project_ownership = - cleanup_project_ownership_test_data$redcap_entity_project_ownership, - redcap_user_information = - cleanup_project_ownership_test_data$redcap_user_information + redcap_entity_project_ownership = redcap_entity_project_ownership, + redcap_user_information = redcap_user_information ) } +} diff --git a/man/get_projects_without_owners.Rd b/man/get_projects_without_owners.Rd index e0cfc0e..c16fab0 100644 --- a/man/get_projects_without_owners.Rd +++ b/man/get_projects_without_owners.Rd @@ -18,10 +18,10 @@ a vector of project IDs Returns the project_ids of projects without owners } \examples{ +\dontrun{ get_projects_without_owners( - redcap_projects = - cleanup_project_ownership_test_data$redcap_projects, - redcap_entity_project_ownership = - cleanup_project_ownership_test_data$redcap_entity_project_ownership + redcap_projects = redcap_projects, + redcap_entity_project_ownership = redcap_entity_project_ownership ) } +} diff --git a/man/get_research_projects_not_using_viable_pi_data.Rd b/man/get_research_projects_not_using_viable_pi_data.Rd index 74afc8a..48a717c 100644 --- a/man/get_research_projects_not_using_viable_pi_data.Rd +++ b/man/get_research_projects_not_using_viable_pi_data.Rd @@ -24,12 +24,11 @@ a vector of project IDs Returns the project_ids of projects that have no viable PI data } \examples{ +\dontrun{ get_research_projects_not_using_viable_pi_data( - redcap_projects = - cleanup_project_ownership_test_data$redcap_projects, - redcap_entity_project_ownership = - cleanup_project_ownership_test_data$redcap_entity_project_ownership, - redcap_user_information = - cleanup_project_ownership_test_data$redcap_user_information + redcap_projects = redcap_projects, + redcap_entity_project_ownership = redcap_entity_project_ownership, + redcap_user_information = redcap_user_information ) } +} diff --git a/man/mysql_schema_to_sqlite.Rd b/man/mysql_schema_to_sqlite.Rd deleted file mode 100644 index 3a61f0b..0000000 --- a/man/mysql_schema_to_sqlite.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/devtools.R -\name{mysql_schema_to_sqlite} -\alias{mysql_schema_to_sqlite} -\title{converts an in-memory schema to a sqlite schema -and returns that schema.} -\usage{ -mysql_schema_to_sqlite(schema) -} -\arguments{ -\item{schema, }{a MySQL/MariaDB Schema} -} -\value{ -sqlite schema for `schema` -} -\description{ -converts an in-memory schema to a sqlite schema -and returns that schema. -} -\examples{ -\dontrun{ -mysql_schema_to_sqlite(schema) -} -} diff --git a/man/populate_table.Rd b/man/populate_table.Rd deleted file mode 100644 index ddf77a7..0000000 --- a/man/populate_table.Rd +++ /dev/null @@ -1,24 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/devtools.R -\name{populate_table} -\alias{populate_table} -\title{Populates table_name with the corresponding test data found in /data.} -\usage{ -populate_table(conn, table_name, use_test_data = FALSE) -} -\arguments{ -\item{conn, }{a DBI connection object} - -\item{table_name, }{the table to populate with test data} - -\item{use_test_data, }{whether to use "_test_data"} -} -\description{ -Populates table_name with the corresponding test data found in /data. -} -\examples{ -\dontrun{ -conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") -populate_table(conn = conn, table_name = "service_type") -} -} diff --git a/man/redcap_entity_project_ownership_test_data.Rd b/man/redcap_entity_project_ownership_test_data.Rd deleted file mode 100644 index 7d23f26..0000000 --- a/man/redcap_entity_project_ownership_test_data.Rd +++ /dev/null @@ -1,31 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data.R -\docType{data} -\name{redcap_entity_project_ownership_test_data} -\alias{redcap_entity_project_ownership_test_data} -\title{redcap_entity_project_ownership_test_data} -\format{ -A data frame with 5 rows and 10 variables: -\describe{ - \item{\code{id}}{integer Primary key} - \item{\code{created}}{double Date the record was created as a unix timestamp} - \item{\code{updated}}{double Date the record was last updated as a unix timestamp} - \item{\code{pid}}{double REDCap project_id of the project} - \item{\code{username}}{character Username of the project owner if it exists} - \item{\code{email}}{character Email address of the project owner if username does not exist} - \item{\code{firstname}}{character Firstname of the project owner if username does not exist} - \item{\code{lastname}}{character Lastname of the project owner if username does not exist} - \item{\code{billable}}{integer A boolean to indicate if the project should be billed} - \item{\code{sequestered}}{integer A boolean to indicate if the project is sequestered} -} -} -\usage{ -redcap_entity_project_ownership_test_data -} -\description{ -A test dataset for the redcap_entity_project_ownership table -} -\details{ -DETAILS -} -\keyword{datasets} diff --git a/man/redcap_projects_test_data.Rd b/man/redcap_projects_test_data.Rd deleted file mode 100644 index 75311b9..0000000 --- a/man/redcap_projects_test_data.Rd +++ /dev/null @@ -1,167 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data.R -\docType{data} -\name{redcap_projects_test_data} -\alias{redcap_projects_test_data} -\title{redcap_projects_test_data} -\format{ -A data frame with 5 rows and 149 variables: -\describe{ - \item{\code{project_id}}{double Project ID} - \item{\code{app_title}}{character The project name as displayed} - \item{\code{project_pi_firstname}}{character Principal Investigator First Name} - \item{\code{project_pi_lastname}}{character Principal Investigator Last Name} - \item{\code{project_pi_email}}{character Principal Investigator Email} - \item{\code{project_name}}{character The original project name as a keyword} - \item{\code{creation_time}}{double Project Creation Datetime} - \item{\code{status}}{integer} - \item{\code{production_time}}{double} - \item{\code{inactive_time}}{double} - \item{\code{completed_time}}{double} - \item{\code{completed_by}}{character} - \item{\code{data_locked}}{integer} - \item{\code{log_event_table}}{character} - \item{\code{created_by}}{integer} - \item{\code{draft_mode}}{integer} - \item{\code{surveys_enabled}}{integer} - \item{\code{repeatforms}}{integer} - \item{\code{scheduling}}{integer} - \item{\code{purpose}}{integer} - \item{\code{purpose_other}}{character} - \item{\code{show_which_records}}{integer} - \item{\code{__SALT__}}{character} - \item{\code{count_project}}{integer} - \item{\code{investigators}}{character} - \item{\code{project_note}}{character} - \item{\code{online_offline}}{integer} - \item{\code{auth_meth}}{character} - \item{\code{double_data_entry}}{integer} - \item{\code{project_language}}{character} - \item{\code{project_encoding}}{character} - \item{\code{is_child_of}}{character} - \item{\code{date_shift_max}}{integer} - \item{\code{institution}}{character} - \item{\code{site_org_type}}{character} - \item{\code{grant_cite}}{character} - \item{\code{project_contact_name}}{character} - \item{\code{project_contact_email}}{character} - \item{\code{headerlogo}}{character} - \item{\code{auto_inc_set}}{integer} - \item{\code{custom_data_entry_note}}{character} - \item{\code{custom_index_page_note}}{character} - \item{\code{order_id_by}}{character} - \item{\code{custom_reports}}{character} - \item{\code{report_builder}}{character} - \item{\code{disable_data_entry}}{integer} - \item{\code{google_translate_default}}{character} - \item{\code{require_change_reason}}{integer} - \item{\code{dts_enabled}}{integer} - \item{\code{project_pi_mi}}{character} - \item{\code{project_pi_alias}}{character} - \item{\code{project_pi_username}}{character} - \item{\code{project_pi_pub_exclude}}{integer} - \item{\code{project_pub_matching_institution}}{character} - \item{\code{project_irb_number}}{character} - \item{\code{project_grant_number}}{character} - \item{\code{history_widget_enabled}}{integer} - \item{\code{secondary_pk}}{character} - \item{\code{secondary_pk_display_value}}{integer} - \item{\code{secondary_pk_display_label}}{integer} - \item{\code{custom_record_label}}{character} - \item{\code{display_project_logo_institution}}{integer} - \item{\code{imported_from_rs}}{integer} - \item{\code{display_today_now_button}}{integer} - \item{\code{auto_variable_naming}}{integer} - \item{\code{randomization}}{integer} - \item{\code{enable_participant_identifiers}}{integer} - \item{\code{survey_email_participant_field}}{character} - \item{\code{survey_phone_participant_field}}{character} - \item{\code{data_entry_trigger_url}}{character} - \item{\code{template_id}}{integer} - \item{\code{date_deleted}}{double Date of project deletion or NA if not deleted} - \item{\code{data_resolution_enabled}}{integer} - \item{\code{field_comment_edit_delete}}{integer} - \item{\code{realtime_webservice_enabled}}{integer} - \item{\code{realtime_webservice_type}}{character} - \item{\code{realtime_webservice_offset_days}}{double} - \item{\code{realtime_webservice_offset_plusminus}}{character} - \item{\code{last_logged_event}}{double} - \item{\code{edoc_upload_max}}{integer} - \item{\code{file_attachment_upload_max}}{integer} - \item{\code{survey_queue_custom_text}}{character} - \item{\code{survey_queue_hide}}{integer} - \item{\code{survey_auth_enabled}}{integer} - \item{\code{survey_auth_field1}}{character} - \item{\code{survey_auth_event_id1}}{integer} - \item{\code{survey_auth_field2}}{character} - \item{\code{survey_auth_event_id2}}{integer} - \item{\code{survey_auth_field3}}{character} - \item{\code{survey_auth_event_id3}}{integer} - \item{\code{survey_auth_min_fields}}{character} - \item{\code{survey_auth_apply_all_surveys}}{integer} - \item{\code{survey_auth_custom_message}}{character} - \item{\code{survey_auth_fail_limit}}{integer} - \item{\code{survey_auth_fail_window}}{integer} - \item{\code{twilio_enabled}}{integer} - \item{\code{twilio_modules_enabled}}{character} - \item{\code{twilio_hide_in_project}}{integer} - \item{\code{twilio_account_sid}}{character} - \item{\code{twilio_auth_token}}{character} - \item{\code{twilio_from_number}}{double} - \item{\code{twilio_voice_language}}{character} - \item{\code{twilio_option_voice_initiate}}{integer} - \item{\code{twilio_option_sms_initiate}}{integer} - \item{\code{twilio_option_sms_invite_make_call}}{integer} - \item{\code{twilio_option_sms_invite_receive_call}}{integer} - \item{\code{twilio_option_sms_invite_web}}{integer} - \item{\code{twilio_default_delivery_preference}}{character} - \item{\code{twilio_request_inspector_checked}}{double} - \item{\code{twilio_request_inspector_enabled}}{integer} - \item{\code{twilio_append_response_instructions}}{integer} - \item{\code{twilio_multiple_sms_behavior}}{character} - \item{\code{twilio_delivery_preference_field_map}}{character} - \item{\code{two_factor_exempt_project}}{integer} - \item{\code{two_factor_force_project}}{integer} - \item{\code{disable_autocalcs}}{integer} - \item{\code{custom_public_survey_links}}{character} - \item{\code{pdf_custom_header_text}}{character} - \item{\code{pdf_show_logo_url}}{integer} - \item{\code{pdf_hide_secondary_field}}{integer} - \item{\code{pdf_hide_record_id}}{integer} - \item{\code{shared_library_enabled}}{integer} - \item{\code{allow_delete_record_from_log}}{integer} - \item{\code{delete_file_repository_export_files}}{integer} - \item{\code{custom_project_footer_text}}{character} - \item{\code{custom_project_footer_text_link}}{character} - \item{\code{google_recaptcha_enabled}}{integer} - \item{\code{datamart_allow_repeat_revision}}{integer} - \item{\code{datamart_allow_create_revision}}{integer} - \item{\code{datamart_enabled}}{integer} - \item{\code{break_the_glass_enabled}}{integer} - \item{\code{datamart_cron_enabled}}{integer} - \item{\code{datamart_cron_end_date}}{double} - \item{\code{fhir_include_email_address_project}}{integer} - \item{\code{file_upload_vault_enabled}}{integer} - \item{\code{file_upload_versioning_enabled}}{integer} - \item{\code{missing_data_codes}}{character} - \item{\code{record_locking_pdf_vault_enabled}}{integer} - \item{\code{record_locking_pdf_vault_custom_text}}{character} - \item{\code{fhir_cdp_auto_adjudication_enabled}}{integer} - \item{\code{fhir_cdp_auto_adjudication_cronjob_enabled}}{integer} - \item{\code{project_dashboard_min_data_points}}{integer} - \item{\code{bypass_branching_erase_field_prompt}}{integer} - \item{\code{protected_email_mode}}{integer} - \item{\code{protected_email_mode_custom_text}}{character} - \item{\code{protected_email_mode_trigger}}{character} - \item{\code{protected_email_mode_logo}}{integer} - \item{\code{hide_filled_forms}}{integer} - \item{\code{form_activation_survey_autocontinue}}{integer} -} -} -\usage{ -redcap_projects_test_data -} -\description{ -A redcap_projects suitable for billing tests -} -\keyword{datasets} diff --git a/data/cleanup_project_ownership_test_data.rda b/tests/testthat/cleanup_project_ownership/cleanup_project_ownership_test_data.rda similarity index 100% rename from data/cleanup_project_ownership_test_data.rda rename to tests/testthat/cleanup_project_ownership/cleanup_project_ownership_test_data.rda diff --git a/data-raw/cleanup_project_ownership_test_data.R b/tests/testthat/cleanup_project_ownership/make_test_data.R similarity index 88% rename from data-raw/cleanup_project_ownership_test_data.R rename to tests/testthat/cleanup_project_ownership/make_test_data.R index 2ac7f3f..ec6309a 100644 --- a/data-raw/cleanup_project_ownership_test_data.R +++ b/tests/testthat/cleanup_project_ownership/make_test_data.R @@ -51,4 +51,6 @@ library(dotenv) # redcap_project_last_users = redcap_project_last_users # ) # -# usethis::use_data(cleanup_project_ownership_test_data, overwrite = T) +# save(cleanup_project_ownership_test_data, +# file = testthat::test_path("cleanup_project_ownership", +# "cleanup_project_ownership_test_data.rda")) diff --git a/tests/testthat/helper-package-specific.R b/tests/testthat/helper-package-specific.R index 6ad419a..bbc2262 100644 --- a/tests/testthat/helper-package-specific.R +++ b/tests/testthat/helper-package-specific.R @@ -23,3 +23,155 @@ get_user_rights_and_info_test_tables <- c( "redcap_user_rights", "redcap_user_roles" ) + +#' Locates a MySQL schema file for table_name, converts it to a sqlite schema +#' and returns that schema. +#' +#' @param table_name, the name of the table to convert +#' +#' @returns sqlite schema for table_name +#' +#' @examples +#' \dontrun{ +#' convert_schema_to_sqlite(table_name = "service_type") +#' } +#' @export +convert_schema_to_sqlite <- function(table_name) { + schema_file_name <- paste0(table_name, ".sql") + pl_to_sqlite <- system.file("", "to_sqlite.pl", package = "rcc.billing") + + # read original + original_schema_file <- system.file("schema", schema_file_name, package = "rcc.billing") + + if (original_schema_file == "") { + stop(paste("Schema file does not exist for", table_name)) + } + + # convert to sqlite + cmd <- paste("cat", original_schema_file, "|", "perl", pl_to_sqlite) + + result <- system(cmd, intern = TRUE) |> paste(collapse = "") + return(result) +} + +#' converts an in-memory schema to a sqlite schema +#' and returns that schema. +#' +#' @param schema, a MySQL/MariaDB Schema +#' +#' @returns sqlite schema for `schema` +#' +#' @examples +#' \dontrun{ +#' mysql_schema_to_sqlite(schema) +#' } +#' @export +mysql_schema_to_sqlite <- function(schema) { + # find the perl script that does the conversion + pl_to_sqlite <- system.file("", "to_sqlite.pl", package = "rcc.billing") + + # construct conversion command + cmd <- paste("perl", pl_to_sqlite) + + result <- system(cmd, input = schema, intern = TRUE) |> paste(collapse = "") + return(result) +} + +#' Creates a table based on a schema. +#' +#' @param conn, a DBI connection object +#' @param schema, the ddl to execute against conn +#' +#' @examples +#' \dontrun{ +#' table_name <- "service_type" +#' conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") +#' +#' schema <- convert_schema_to_sqlite(table_name) +#' create_table(conn = conn, schema = schema) +#' } +#' @export +create_table <- function(conn, schema) { + schemata <- stringr::str_split(schema, pattern = ";\n+")[[1]] + schemata <- schemata[schemata != ""] + + for (schema in schemata) { + # create table + result <- DBI::dbSendQuery(conn, schema) + # close result set to avoid warning + DBI::dbClearResult(result) + } +} + +#' Populates table_name with the corresponding test data found in /data. +#' +#' @param conn, a DBI connection object +#' @param table_name, the table to populate with test data +#' @param use_test_data, whether to use "_test_data" +#' +#' @examples +#' \dontrun{ +#' conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") +#' populate_table(conn = conn, table_name = "service_type") +#' } +#' @export +populate_table <- function(conn, table_name, use_test_data = FALSE) { + data_ref <- table_name + + if (isTRUE(use_test_data)) { + data_ref <- paste0(data_ref, "_test_data") + } + + # get test data + data <- get0(data_ref) + + # write sample data + result <- DBI::dbAppendTable( + conn = conn, + name = table_name, + value = data, + overwrite = TRUE + ) + + result <- DBI::dbGetQuery(conn, paste("select * from", table_name)) + return(result) +} + +#' create_and_load_test_table +#' +#' Create a named table for which we have stored schema and optionally load the stored test data into it +#' +#' @param conn, a DBI Connection object +#' @param table_name, the name of the table +#' @param load_test_data, a logical to indicate if test data should be loaded +#' @param is_sqllite, a logical to indicate if the DBI object is a a SQLLite DB +#' +#' @return The test data as read back from the new table or NULL +#' @export +#' +#' @examples +#' conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") +#' result <- create_and_load_test_table( +#' conn = conn, +#' table_name = "invoice_line_item_communications", +#' is_sqllite = TRUE +#' ) +create_and_load_test_table <- function(conn, table_name, load_test_data = TRUE, is_sqllite = FALSE) { + schema_file_name <- paste0(table_name, ".sql") + original_schema_file <- system.file("schema", schema_file_name, package = "rcc.billing") + schema <- dplyr::if_else(is_sqllite, + convert_schema_to_sqlite(table_name = table_name), + readr::read_file(original_schema_file) + ) + create_table( + conn = conn, + schema = schema + ) + if (load_test_data) { + populate_table( + conn = conn, + table_name = table_name, + use_test_data = T + ) + } +} diff --git a/data-raw/redcap_entity_project_ownership_test_data.R b/tests/testthat/redcap_entity_project_ownership/make_test_data.R similarity index 85% rename from data-raw/redcap_entity_project_ownership_test_data.R rename to tests/testthat/redcap_entity_project_ownership/make_test_data.R index c482aae..c953abd 100644 --- a/data-raw/redcap_entity_project_ownership_test_data.R +++ b/tests/testthat/redcap_entity_project_ownership/make_test_data.R @@ -4,6 +4,9 @@ library(rcc.billing) library(tidyverse) library(lubridate) +load(file = testthat::test_path("redcap_projects", + "redcap_projects_test_data.rda")) + redcap_entity_project_ownership_test_data <- redcap_projects_test_data %>% select( @@ -38,4 +41,6 @@ redcap_entity_project_ownership_test_data <- mutate(billable = if_else(pid == 2345, NA_real_, billable)) %>% select(id, created, updated, pid, username, email, firstname, lastname, billable, sequestered) -usethis::use_data(redcap_entity_project_ownership_test_data, overwrite = T) +save(redcap_entity_project_ownership_test_data, + file = testthat::test_path("redcap_entity_project_ownership", + "redcap_entity_project_ownership_test_data.rda")) diff --git a/data/redcap_entity_project_ownership_test_data.rda b/tests/testthat/redcap_entity_project_ownership/test_data.rda similarity index 100% rename from data/redcap_entity_project_ownership_test_data.rda rename to tests/testthat/redcap_entity_project_ownership/test_data.rda diff --git a/data-raw/redcap_projects_test_data.R b/tests/testthat/redcap_projects/make_test_data.R similarity index 100% rename from data-raw/redcap_projects_test_data.R rename to tests/testthat/redcap_projects/make_test_data.R diff --git a/data/redcap_projects_test_data.rda b/tests/testthat/redcap_projects/redcap_projects_test_data.rda similarity index 100% rename from data/redcap_projects_test_data.rda rename to tests/testthat/redcap_projects/redcap_projects_test_data.rda diff --git a/tests/testthat/test-invoice_line_item.R b/tests/testthat/test-invoice_line_item.R index 62d3047..60be662 100644 --- a/tests/testthat/test-invoice_line_item.R +++ b/tests/testthat/test-invoice_line_item.R @@ -14,7 +14,7 @@ testthat::test_that("service_type sqlite schema is created and correct test data date_received = as.POSIXct(date_received, tz = "UTC") ) - sqlite_schema <- convert_schema_to_sqlite(table_name = table_name) + sqlite_schema <- convert_schema_to_sqlite(table_name) create_table( conn = conn, schema = sqlite_schema @@ -23,8 +23,9 @@ testthat::test_that("service_type sqlite schema is created and correct test data conn = conn, table_name = table_name, use_test_data = TRUE - ) %>% fix_data_in_invoice_line_item() + ) |> fix_data_in_invoice_line_item() expect_identical(test_data, dplyr::as_tibble(results)) DBI::dbDisconnect(conn) }) + diff --git a/tests/testthat/test-invoice_line_item_communications.R b/tests/testthat/test-invoice_line_item_communications.R index be14d57..977ada8 100644 --- a/tests/testthat/test-invoice_line_item_communications.R +++ b/tests/testthat/test-invoice_line_item_communications.R @@ -13,7 +13,7 @@ testthat::test_that("service_type sqlite schema is created and correct test data date_sent = as.POSIXct(date_sent, tz = "UTC"), date_received = as.POSIXct(date_received, tz = "UTC") ) - sqlite_schema <- convert_schema_to_sqlite(table_name = table_name) + sqlite_schema <- convert_schema_to_sqlite(table_name) create_table( conn = mem_conn, schema = sqlite_schema diff --git a/tests/testthat/test-redcap_entity_project_ownership.R b/tests/testthat/test-redcap_entity_project_ownership.R deleted file mode 100644 index 6f02c78..0000000 --- a/tests/testthat/test-redcap_entity_project_ownership.R +++ /dev/null @@ -1,19 +0,0 @@ -test_that("redcap_entity_project_ownership sqlite schema is created and correct test data is returned", { - table_name <- "redcap_entity_project_ownership" - test_data <- get0(paste0(table_name, "_test_data")) - conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") - - sqlite_schema <- convert_schema_to_sqlite(table_name = table_name) - create_table( - conn = conn, - schema = sqlite_schema - ) - results <- populate_table( - conn = conn, - table_name = table_name, - use_test_data = T - ) - - DBI::dbDisconnect(conn) - testthat::expect_equal(dplyr::as_tibble(results), test_data) -}) diff --git a/tests/testthat/test-redcap_projects.R b/tests/testthat/test-redcap_projects.R index 937e39c..a5dcc35 100644 --- a/tests/testthat/test-redcap_projects.R +++ b/tests/testthat/test-redcap_projects.R @@ -1,22 +1,5 @@ -testthat::test_that("service_type sqlite schema is created and correct test data is returned", { - table_name <- "redcap_projects" - test_data <- get0(paste0(table_name, "_test_data")) - conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") - - sqlite_schema <- convert_schema_to_sqlite(table_name = table_name) - create_table( - conn = conn, - schema = sqlite_schema - ) - results <- populate_table( - conn = conn, - table_name = table_name, - use_test_data = T - ) %>% fix_data_in_redcap_projects() - - DBI::dbDisconnect(conn) - testthat::expect_equal(dplyr::as_tibble(results), test_data) -}) +# load cleanup_project_ownership_test_data into memory +load(file = testthat::test_path("cleanup_project_ownership", "cleanup_project_ownership_test_data.rda")) testthat::test_that("get_projects_needing_new_owners returns the correct vector of project IDs", { expected_result <- seq(from = 29, to = 33) @@ -75,7 +58,7 @@ testthat::test_that("get_creators returns unsuspended creators in RCPO format", testthat::expect_equal( expected_result, get_creators( - redcap_projects = cleanup_project_ownership_test_data$redcap_projects %>% + redcap_projects = cleanup_project_ownership_test_data$redcap_projects |> dplyr::filter(project_id >= 28), redcap_user_information = cleanup_project_ownership_test_data$redcap_user_information, redcap_staff_employment_periods = ctsit_staff_employment_periods, @@ -93,7 +76,7 @@ testthat::test_that("get_creators returns any creator in RCPO format", { testthat::expect_equal( expected_result, get_creators( - redcap_projects = cleanup_project_ownership_test_data$redcap_projects %>% + redcap_projects = cleanup_project_ownership_test_data$redcap_projects |> dplyr::filter(project_id >= 28), redcap_user_information = cleanup_project_ownership_test_data$redcap_user_information, redcap_staff_employment_periods = ctsit_staff_employment_periods, @@ -112,7 +95,7 @@ testthat::test_that("get_privileged_user returns unsuspended, high-privilege use testthat::expect_equal( expected_result, get_privileged_user( - redcap_projects = cleanup_project_ownership_test_data$redcap_projects %>% + redcap_projects = cleanup_project_ownership_test_data$redcap_projects |> dplyr::filter(project_id >= 28), redcap_user_information = cleanup_project_ownership_test_data$redcap_user_information, redcap_staff_employment_periods = ctsit_staff_employment_periods, @@ -133,7 +116,7 @@ testthat::test_that("get_privileged_user returns unsuspended users with any priv testthat::expect_equal( expected_result, get_privileged_user( - redcap_projects = cleanup_project_ownership_test_data$redcap_projects %>% + redcap_projects = cleanup_project_ownership_test_data$redcap_projects |> dplyr::filter(project_id >= 28), redcap_user_information = cleanup_project_ownership_test_data$redcap_user_information, redcap_staff_employment_periods = ctsit_staff_employment_periods, @@ -147,42 +130,24 @@ testthat::test_that("get_privileged_user returns unsuspended users with any priv test_that("update_billable_by_ownership", { - expected_result <- tribble( + expected_result <- dplyr::tribble( ~pid, ~username, ~billable, 2345, NA, 1, 6490, "tls", 0 ) - conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") - # populate project ownership table - po_table_name <- "redcap_entity_project_ownership" - po_test_data <- get0(paste0(po_table_name, "_test_data")) - po_sqlite_schema <- convert_schema_to_sqlite(table_name = po_table_name) - create_table( - conn = conn, - schema = po_sqlite_schema - ) - populate_table( - conn = conn, - table_name = po_table_name, - use_test_data = T - ) + redcapcustodian::set_script_run_time(lubridate::ymd_hms("2023-01-01 12:00:00")) - rcp_table_name <- "redcap_projects" - rcp_test_data <- get0(paste0(rcp_table_name, "_test_data")) - rcp_sqlite_schema <- convert_schema_to_sqlite(table_name = rcp_table_name) - create_table( - conn = conn, - schema = rcp_sqlite_schema - ) - populate_table( - conn = conn, - table_name = rcp_table_name, - use_test_data = T - ) + conn <- DBI::dbConnect(duckdb::duckdb(), dbdir = ":memory:") + + load(file = testthat::test_path("redcap_entity_project_ownership", "test_data.rda")) + duckdb::duckdb_register(conn, "redcap_entity_project_ownership", redcap_entity_project_ownership_test_data) + + load(file = testthat::test_path("redcap_projects", "redcap_projects_test_data.rda")) + duckdb::duckdb_register(conn, "redcap_projects", redcap_projects_test_data) output <- update_billable_by_ownership(conn) - results <- output$update_records %>% + results <- output$update_records |> dplyr::select(pid, username, billable) DBI::dbDisconnect(conn) @@ -191,25 +156,18 @@ test_that("update_billable_by_ownership", { test_that("update_billable_if_owned_by_ctsit", { - expected_result <- tribble( + expected_result <- dplyr::tribble( ~pid, ~username, ~billable, 6490, "tls", 0 ) - conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") + + redcapcustodian::set_script_run_time(lubridate::ymd_hms("2023-01-01 12:00:00")) + + conn <- DBI::dbConnect(duckdb::duckdb(), dbdir = ":memory:") # populate project ownership table - po_table_name <- "redcap_entity_project_ownership" - po_test_data <- get0(paste0(po_table_name, "_test_data")) - po_sqlite_schema <- convert_schema_to_sqlite(table_name = po_table_name) - create_table( - conn = conn, - schema = po_sqlite_schema - ) - populate_table( - conn = conn, - table_name = po_table_name, - use_test_data = T - ) + load(file = testthat::test_path("redcap_entity_project_ownership", "test_data.rda")) + duckdb::dbWriteTable(conn, "redcap_entity_project_ownership", redcap_entity_project_ownership_test_data) # hack the data for tls show that the project show owns is billable sql = "update redcap_entity_project_ownership set billable = 1 where username = 'tls'" @@ -218,21 +176,11 @@ test_that("update_billable_if_owned_by_ctsit", { statement = sql ) - rcp_table_name <- "redcap_projects" - rcp_test_data <- get0(paste0(rcp_table_name, "_test_data")) - rcp_sqlite_schema <- convert_schema_to_sqlite(table_name = rcp_table_name) - create_table( - conn = conn, - schema = rcp_sqlite_schema - ) - populate_table( - conn = conn, - table_name = rcp_table_name, - use_test_data = T - ) + load(file = testthat::test_path("redcap_projects", "redcap_projects_test_data.rda")) + duckdb::dbWriteTable(conn, "redcap_projects", redcap_projects_test_data) output <- update_billable_if_owned_by_ctsit(conn) - results <- output$update_records %>% + results <- output$update_records |> dplyr::select(pid, username, billable) DBI::dbDisconnect(conn) @@ -243,22 +191,21 @@ test_that("update_billable_if_owned_by_ctsit", { testthat::test_that( "get_reassigned_line_items returns a df with project ownership data from redcap_entity_project_ownership", { - table_names <- - c("redcap_entity_project_ownership", "invoice_line_item") + redcapcustodian::set_script_run_time(lubridate::ymd_hms("2023-01-01 12:00:00")) - conn <- DBI::dbConnect(RSQLite::SQLite(), dbname = ":memory:") + conn <- DBI::dbConnect(duckdb::duckdb(), dbdir = ":memory:") - for (table_name in table_names) { - create_and_load_test_table( - table_name = table_name, - conn = conn, - load_test_data = T, - is_sqllite = T - ) - } + # populate project ownership table + load(file = testthat::test_path("redcap_entity_project_ownership", "test_data.rda")) + redcap_entity_project_ownership <- redcap_entity_project_ownership_test_data |> + dplyr::mutate(project_id = stringr::str_replace(pid, "\\.0", "")) + duckdb::dbWriteTable(conn, "redcap_entity_project_ownership", redcap_entity_project_ownership) + + invoice_line_item <- readRDS(testthat::test_path("invoice_line_item", "invoice_line_item.rds")) + duckdb::dbWriteTable(conn, "invoice_line_item", invoice_line_item) sent_line_items <- - get_unpaid_redcap_prod_per_project_line_items(conn) %>% + rcc.billing::get_unpaid_redcap_prod_per_project_line_items(conn) |> dplyr::mutate( pi_last_name = dplyr::if_else( pi_last_name == "Chase", @@ -283,21 +230,21 @@ testthat::test_that( ) reassigned_line_items <- - get_reassigned_line_items(sent_line_items, conn) %>% - dplyr::select(pi_last_name, pi_first_name, pi_email, gatorlink) %>% + rcc.billing::get_reassigned_line_items(sent_line_items, rc_conn = conn) |> + dplyr::select(pi_last_name, pi_first_name, pi_email, gatorlink) |> dplyr::arrange(dplyr::desc(pi_last_name)) expected_result <- - dplyr::tbl(conn, "redcap_entity_project_ownership") %>% - dplyr::filter(pid %in% !!sent_line_items$project_id) %>% - dplyr::mutate_at("pid", as.character) %>% + dplyr::tbl(conn, "redcap_entity_project_ownership") |> + dplyr::filter(pid %in% !!sent_line_items$project_id) |> + dplyr::mutate_at("pid", as.character) |> dplyr::select( pi_last_name = lastname, pi_first_name = firstname, pi_email = email, gatorlink = username - ) %>% - dplyr::arrange(dplyr::desc(pi_last_name)) %>% + ) |> + dplyr::arrange(dplyr::desc(pi_last_name)) |> dplyr::collect() DBI::dbDisconnect(conn) @@ -308,8 +255,8 @@ testthat::test_that( testthat::test_that("get_research_projects_not_using_viable_pi_data can detect a project with non-viable PI data", { redcap_projects <- - cleanup_project_ownership_test_data$redcap_projects %>% - mutate(purpose = 2) %>% + cleanup_project_ownership_test_data$redcap_projects |> + mutate(purpose = 2) |> mutate(project_pi_email = if_else( project_id %in% c(21, 22, 25), "you@example.org", @@ -317,9 +264,9 @@ testthat::test_that("get_research_projects_not_using_viable_pi_data can detect a )) redcap_entity_project_ownership <- - cleanup_project_ownership_test_data$redcap_entity_project_ownership %>% - mutate(username = if_else(pid %in% c(22,25), as.character(NA), username)) %>% - mutate(email = if_else(pid == 22, "you@example.org", email)) %>% + cleanup_project_ownership_test_data$redcap_entity_project_ownership |> + mutate(username = if_else(pid %in% c(22,25), as.character(NA), username)) |> + mutate(email = if_else(pid == 22, "you@example.org", email)) |> mutate(email = if_else(pid == 25, "not_the_pi@example.org", email)) redcap_user_information <-