Skip to content

Commit

Permalink
Merge pull request #251 from pbchase/move_test_data_1
Browse files Browse the repository at this point in the history
Move test data 1
  • Loading branch information
saipavan10-git authored Sep 9, 2024
2 parents c002c0a + 0d3e7e9 commit 146ea38
Show file tree
Hide file tree
Showing 30 changed files with 253 additions and 863 deletions.
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
Expand Down
187 changes: 0 additions & 187 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
151 changes: 0 additions & 151 deletions R/devtools.R
Original file line number Diff line number Diff line change
@@ -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
)
}
}
Loading

0 comments on commit 146ea38

Please sign in to comment.