diff --git a/R/aggregate_alignment_loanbook_exposure.R b/R/aggregate_alignment_loanbook_exposure.R index 04d06371..a957925e 100644 --- a/R/aggregate_alignment_loanbook_exposure.R +++ b/R/aggregate_alignment_loanbook_exposure.R @@ -219,7 +219,7 @@ validate_input_data_aggregate_alignment_loanbook_exposure <- function(data, matched, group_vars, .by = NULL) { - stop_if_not_expected_columns( + assert_expected_columns( data = data, cols = c( group_vars, @@ -230,7 +230,7 @@ validate_input_data_aggregate_alignment_loanbook_exposure <- function(data, ) ) - stop_if_not_expected_columns( + assert_expected_columns( data = matched, cols = c( "id_loan", diff --git a/R/analyse.R b/R/analyse.R index 996452bc..9ee6e0bf 100644 --- a/R/analyse.R +++ b/R/analyse.R @@ -23,8 +23,8 @@ analyse <- function(config) { dir_analysis <- get_dir_analysis(config) - stop_if_not_length(dir_analysis, 1L) - stop_if_not_inherits(dir_analysis, "character") + assert_length(dir_analysis, 1L) + assert_inherits(dir_analysis, "character") if (dir.exists(dir_analysis)) { ask_for_permission( diff --git a/R/stop_if_.R b/R/assert_.R similarity index 87% rename from R/stop_if_.R rename to R/assert_.R index 27ae2fdf..d6d56ff1 100644 --- a/R/stop_if_.R +++ b/R/assert_.R @@ -1,4 +1,4 @@ -#' stop_if_not_inherits +#' assert_inherits #' #' @param x an object to be checked #' @param cls a string defining the expected object class @@ -7,7 +7,7 @@ #' #' @noRd -stop_if_not_inherits <- function(x, cls) { +assert_inherits <- function(x, cls) { if (isFALSE(inherits(x, cls))) { arg <- deparse(substitute(x)) cli::cli_abort( @@ -21,7 +21,7 @@ stop_if_not_inherits <- function(x, cls) { } -#' stop_if_not_length +#' assert_length #' #' @param x an object to be checked #' @param len an integer defining the expected length of the object @@ -30,7 +30,7 @@ stop_if_not_inherits <- function(x, cls) { #' #' @noRd -stop_if_not_length <- function(x, len) { +assert_length <- function(x, len) { if (length(x) != len) { arg <- deparse(substitute(x)) cli::cli_abort( @@ -44,7 +44,7 @@ stop_if_not_length <- function(x, len) { } -#' stop_if_dir_not_found +#' assert_dir_exists #' #' @param path a string defining the path to a directory #' @param desc a string describing the type of directory to be added to the @@ -54,7 +54,7 @@ stop_if_not_length <- function(x, len) { #' #' @noRd -stop_if_dir_not_found <- function(path, desc = NULL) { +assert_dir_exists <- function(path, desc = NULL) { if (isFALSE(dir.exists(path))) { if (is.null(desc)) { msg <- "Directory not found at path:" @@ -73,7 +73,7 @@ stop_if_dir_not_found <- function(path, desc = NULL) { } -#' stop_if_file_not_found +#' assert_file_exists #' #' @param path a string defining the path to a file #' @param desc a string describing the type of file to be added to the error msg @@ -82,7 +82,7 @@ stop_if_dir_not_found <- function(path, desc = NULL) { #' #' @noRd -stop_if_file_not_found <- function(path, desc = NULL) { +assert_file_exists <- function(path, desc = NULL) { if (isFALSE(file.exists(path))) { if (is.null(desc)) { msg <- "File not found at path:" @@ -101,7 +101,7 @@ stop_if_file_not_found <- function(path, desc = NULL) { } -#' stop_if_sheet_not_found +#' assert_sheet_exists #' #' @param sheet a string defining the name of a sheet #' @param path a string defining the path to a XLS/X file @@ -110,7 +110,7 @@ stop_if_file_not_found <- function(path, desc = NULL) { #' #' @noRd -stop_if_sheet_not_found <- function(sheet, path) { +assert_sheet_exists <- function(sheet, path) { if (isFALSE(sheet %in% readxl::excel_sheets(path))) { cli::cli_abort( message = c( @@ -124,7 +124,7 @@ stop_if_sheet_not_found <- function(sheet, path) { } -#' stop_if_not_expected_columns +#' assert_expected_columns #' #' @param data a data frame to be checked #' @param cols a vector of expected column names @@ -134,7 +134,7 @@ stop_if_sheet_not_found <- function(sheet, path) { #' #' @noRd -stop_if_not_expected_columns <- function(data, cols, desc = NULL) { +assert_expected_columns <- function(data, cols, desc = NULL) { if (isFALSE(all(cols %in% names(data)))) { if (is.null(desc)) { msg <- "Data does not contain all of the expected columns." @@ -153,7 +153,7 @@ stop_if_not_expected_columns <- function(data, cols, desc = NULL) { } -#' stop_if_no_files_found +#' assert_any_file_exists #' #' @param files a vector of files found #' @param dir a dir where files were not found @@ -165,7 +165,7 @@ stop_if_not_expected_columns <- function(data, cols, desc = NULL) { #' #' @noRd -stop_if_no_files_found <- function(files, dir, dir_param, desc) { +assert_any_file_exists <- function(files, dir, dir_param, desc) { if (length(files) == 0) { cli::cli_abort( message = c( @@ -179,7 +179,7 @@ stop_if_no_files_found <- function(files, dir, dir_param, desc) { } -#' stop_if_sector_split_not_one +#' assert_sector_split_is_one #' #' @param data a data frame to be checked #' @@ -187,7 +187,7 @@ stop_if_no_files_found <- function(files, dir, dir_param, desc) { #' #' @noRd -stop_if_sector_split_not_one <- function(data) { +assert_sector_split_is_one <- function(data) { check_sector_split <- dplyr::summarise( data, diff --git a/R/calculate_company_alignment_metric.R b/R/calculate_company_alignment_metric.R index d9867b62..ae68412d 100644 --- a/R/calculate_company_alignment_metric.R +++ b/R/calculate_company_alignment_metric.R @@ -592,7 +592,7 @@ validate_input_args_calculate_company_tech_deviation <- function(scenario_source validate_input_data_calculate_company_tech_deviation <- function(data, technology_direction) { - stop_if_not_expected_columns( + assert_expected_columns( data = data, cols = c( "sector", "technology", "year", "region", "scenario_source", "name_abcd", @@ -601,7 +601,7 @@ validate_input_data_calculate_company_tech_deviation <- function(data, ) ) - stop_if_not_expected_columns( + assert_expected_columns( data = technology_direction, cols = c( "scenario_source", "scenario", "sector", "technology", "region", @@ -656,7 +656,7 @@ validate_input_args_calculate_company_aggregate_alignment_tms <- function(scenar validate_input_data_calculate_company_aggregate_alignment_tms <- function(data, scenario) { - stop_if_not_expected_columns( + assert_expected_columns( data = data, cols = c( "sector", "technology", "year", "region", "scenario_source", "name_abcd", @@ -735,7 +735,7 @@ validate_input_args_calculate_company_aggregate_alignment_sda <- function(scenar validate_input_data_calculate_company_aggregate_alignment_sda <- function(data) { - stop_if_not_expected_columns( + assert_expected_columns( data = data, cols = c( "sector", "year", "region", "scenario_source", "name_abcd", diff --git a/R/helper_functions.R b/R/helper_functions.R index cf4c4686..e28f05f6 100644 --- a/R/helper_functions.R +++ b/R/helper_functions.R @@ -77,8 +77,8 @@ apply_sector_split_to_loans <- function(data, check_and_prepare_by_group <- function(by_group) { if (!is.null(by_group)) { - stop_if_not_inherits(by_group, "character") - stop_if_not_length(by_group, 1) + assert_inherits(by_group, "character") + assert_length(by_group, 1) if (by_group == "NULL") { by_group <- NULL } else { diff --git a/R/match_loanbooks.R b/R/match_loanbooks.R index 227fcd5e..960321ee 100644 --- a/R/match_loanbooks.R +++ b/R/match_loanbooks.R @@ -51,47 +51,47 @@ match_loanbooks <- function(config) { } # validate config values---- - stop_if_not_length(dir_loanbooks, 1L) - stop_if_not_inherits(dir_loanbooks, "character") - stop_if_dir_not_found(dir_loanbooks, desc = "Input - loanbooks") + assert_length(dir_loanbooks, 1L) + assert_inherits(dir_loanbooks, "character") + assert_dir_exists(dir_loanbooks, desc = "Input - loanbooks") - stop_if_not_length(dir_prepared_abcd, 1L) - stop_if_not_inherits(dir_prepared_abcd, "character") - stop_if_dir_not_found(dir_prepared_abcd, desc = "Output - prepare ABCD") - stop_if_file_not_found(file.path(dir_prepared_abcd, "abcd_final.csv"), desc = "ABCD final") + assert_length(dir_prepared_abcd, 1L) + assert_inherits(dir_prepared_abcd, "character") + assert_dir_exists(dir_prepared_abcd, desc = "Output - prepare ABCD") + assert_file_exists(file.path(dir_prepared_abcd, "abcd_final.csv"), desc = "ABCD final") - stop_if_not_length(dir_matched_loanbooks, 1L) - stop_if_not_inherits(dir_matched_loanbooks, "character") - stop_if_dir_not_found(dir_matched_loanbooks, desc = "Output - Matched loanbooks") + assert_length(dir_matched_loanbooks, 1L) + assert_inherits(dir_matched_loanbooks, "character") + assert_dir_exists(dir_matched_loanbooks, desc = "Output - Matched loanbooks") - stop_if_not_length(matching_by_sector, 1L) - stop_if_not_inherits(matching_by_sector, "logical") + assert_length(matching_by_sector, 1L) + assert_inherits(matching_by_sector, "logical") - stop_if_not_length(matching_min_score, 1L) - stop_if_not_inherits(matching_min_score, "numeric") + assert_length(matching_min_score, 1L) + assert_inherits(matching_min_score, "numeric") - stop_if_not_length(matching_method, 1L) - stop_if_not_inherits(matching_method, "character") + assert_length(matching_method, 1L) + assert_inherits(matching_method, "character") - stop_if_not_length(matching_p, 1L) - stop_if_not_inherits(matching_p, "numeric") + assert_length(matching_p, 1L) + assert_inherits(matching_p, "numeric") # TODO: check for data.frame - # stop_if_not_length(matching_overwrite, 1L) - # stop_if_not_inherits(matching_overwrite, "numeric") + # assert_length(matching_overwrite, 1L) + # assert_inherits(matching_overwrite, "numeric") # # TODO: check for join_object - # stop_if_not_length(matching_join_id, 1L) - # stop_if_not_inherits(matching_join_id, "numeric") + # assert_length(matching_join_id, 1L) + # assert_inherits(matching_join_id, "numeric") - stop_if_not_length(matching_use_manual_sector_classification, 1L) - stop_if_not_inherits(matching_use_manual_sector_classification, "logical") + assert_length(matching_use_manual_sector_classification, 1L) + assert_inherits(matching_use_manual_sector_classification, "logical") # path to manual sector classification only required if boolean TRUE if (matching_use_manual_sector_classification) { - stop_if_not_length(path_manual_sector_classification, 1L) - stop_if_not_inherits(path_manual_sector_classification, "character") - stop_if_file_not_found(path_manual_sector_classification, desc = "Manual sector classification") + assert_length(path_manual_sector_classification, 1L) + assert_inherits(path_manual_sector_classification, "character") + assert_file_exists(path_manual_sector_classification, desc = "Manual sector classification") } # load data---- @@ -114,7 +114,7 @@ match_loanbooks <- function(config) { ## load raw loan books---- list_raw <- list.files(path = dir_loanbooks, pattern = "[.]csv$") - stop_if_no_files_found(list_raw, dir_loanbooks, "dir_input", "raw loan book CSVs") + assert_any_file_exists(list_raw, dir_loanbooks, "dir_input", "raw loan book CSVs") raw_lbk <- readr::read_csv( file = file.path(dir_loanbooks, list_raw), diff --git a/R/plots.R b/R/plots.R index e1c2c46c..36383b0d 100644 --- a/R/plots.R +++ b/R/plots.R @@ -5,7 +5,7 @@ plot_match_success_rate <- function(data, currency, by_group) { # validate inputs - stop_if_not_inherits(data, "data.frame") + assert_inherits(data, "data.frame") expected_cols <- c( by_group, "sector", @@ -14,14 +14,14 @@ plot_match_success_rate <- function(data, "match_success_rate", "metric_type" ) - stop_if_not_expected_columns(data, expected_cols, desc = "Input") + assert_expected_columns(data, expected_cols, desc = "Input") - stop_if_not_inherits(metric_type, "character") + assert_inherits(metric_type, "character") - stop_if_not_inherits(match_success_type, "character") + assert_inherits(match_success_type, "character") - stop_if_not_length(currency, 1L) - stop_if_not_inherits(currency, "character") + assert_length(currency, 1L) + assert_inherits(currency, "character") data <- data %>% dplyr::filter(.data[["sector"]] != "not in scope") %>% @@ -379,30 +379,30 @@ validate_input_args_generate_individual_outputs <- function(output_directory, sector, start_year, time_horizon) { - stop_if_not_length(output_directory, 1L) - stop_if_not_inherits(output_directory, "character") + assert_length(output_directory, 1L) + assert_inherits(output_directory, "character") - stop_if_not_length(by_group, 1L) + assert_length(by_group, 1L) - stop_if_not_length(by_group_value, 1L) + assert_length(by_group_value, 1L) - stop_if_not_length(scenario_source, 1L) - stop_if_not_inherits(scenario_source, "character") + assert_length(scenario_source, 1L) + assert_inherits(scenario_source, "character") - stop_if_not_length(target_scenario, 1L) - stop_if_not_inherits(target_scenario, "character") + assert_length(target_scenario, 1L) + assert_inherits(target_scenario, "character") - stop_if_not_length(region, 1L) - stop_if_not_inherits(region, "character") + assert_length(region, 1L) + assert_inherits(region, "character") - stop_if_not_length(sector, 1L) - stop_if_not_inherits(sector, "character") + assert_length(sector, 1L) + assert_inherits(sector, "character") - stop_if_not_length(start_year, 1L) - stop_if_not_inherits(start_year, "integer") + assert_length(start_year, 1L) + assert_inherits(start_year, "integer") - stop_if_not_length(time_horizon, 1L) - stop_if_not_inherits(time_horizon, "integer") + assert_length(time_horizon, 1L) + assert_inherits(time_horizon, "integer") invisible() } @@ -413,7 +413,7 @@ validate_input_data_generate_individual_outputs <- function(data, target_type, by_group) { if (target_type == "sda") { - stop_if_not_expected_columns( + assert_expected_columns( data = data, cols = c( "sector", "year", "region", "scenario_source", "emission_factor_metric", @@ -421,7 +421,7 @@ validate_input_data_generate_individual_outputs <- function(data, ) ) } else if (target_type == "tms") { - stop_if_not_expected_columns( + assert_expected_columns( data = data, cols = c( "sector", "technology", "year", "region", "scenario_source", "metric", @@ -431,7 +431,7 @@ validate_input_data_generate_individual_outputs <- function(data, ) } - stop_if_not_expected_columns( + assert_expected_columns( data = matched_prioritized, cols = c( by_group, "name_abcd", "sector", "sector_abcd", "loan_size_outstanding", diff --git a/R/prepare_abcd.R b/R/prepare_abcd.R index 0d34856a..3cf4a217 100644 --- a/R/prepare_abcd.R +++ b/R/prepare_abcd.R @@ -22,8 +22,8 @@ prepare_abcd <- function(config) { dir_prepared_abcd <- get_dir_prepared_abcd(config) - stop_if_not_length(dir_prepared_abcd, 1L) - stop_if_not_inherits(dir_prepared_abcd, "character") + assert_length(dir_prepared_abcd, 1L) + assert_inherits(dir_prepared_abcd, "character") if (dir.exists(dir_prepared_abcd)) { ask_for_permission( diff --git a/R/prepare_sector_split.R b/R/prepare_sector_split.R index aded9d87..6c6f7028 100644 --- a/R/prepare_sector_split.R +++ b/R/prepare_sector_split.R @@ -188,7 +188,7 @@ prepare_sector_split <- function(config) { ) ### check that the sum of the sector split of each company is 1---- - stop_if_sector_split_not_one(sector_split_all_companies) + assert_sector_split_is_one(sector_split_all_companies) ## calculate primary energy-based sector split for energy sectors---- # keep only companies that are active in multiple energy sectors @@ -261,7 +261,7 @@ prepare_sector_split <- function(config) { dplyr::filter(.data[["company_id"]] %in% .env[["company_ids_primary_energy_split"]]) ### check that the sum of the primary energy based sector split of each company is 1---- - stop_if_sector_split_not_one(sector_split_multi_energy_companies) + assert_sector_split_is_one(sector_split_multi_energy_companies) ## combine the sector splits---- # we want to use the plain equal weights split for companies that do not operate in more than one energy sector @@ -288,7 +288,7 @@ prepare_sector_split <- function(config) { ) ### check that the sum of the combined sector split of each company is 1---- - stop_if_sector_split_not_one(sector_split_all_companies_final) + assert_sector_split_is_one(sector_split_all_companies_final) ## write output---- sector_split_multi_energy_companies %>% diff --git a/R/prioritise_and_diagnose.R b/R/prioritise_and_diagnose.R index 3e797a6f..d721378c 100644 --- a/R/prioritise_and_diagnose.R +++ b/R/prioritise_and_diagnose.R @@ -23,8 +23,8 @@ prioritise_and_diagnose <- function(config) { dir_prioritized_loanbooks_and_diagnostics <- get_dir_prioritized_loanbooks_and_diagnostics(config) - stop_if_not_length(dir_prioritized_loanbooks_and_diagnostics, 1L) - stop_if_not_inherits(dir_prioritized_loanbooks_and_diagnostics, "character") + assert_length(dir_prioritized_loanbooks_and_diagnostics, 1L) + assert_inherits(dir_prioritized_loanbooks_and_diagnostics, "character") if (dir.exists(dir_prioritized_loanbooks_and_diagnostics)) { ask_for_permission( diff --git a/R/remove_inactive_companies.R b/R/remove_inactive_companies.R index 86373cf7..260e0df3 100644 --- a/R/remove_inactive_companies.R +++ b/R/remove_inactive_companies.R @@ -13,29 +13,29 @@ remove_inactive_companies <- function(config) { time_frame <- get_time_frame(config) # validate config values---- - stop_if_not_length(path_abcd, 1L) - stop_if_not_inherits(path_abcd, "character") - stop_if_file_not_found(path_abcd, desc = "ABCD") + assert_length(path_abcd, 1L) + assert_inherits(path_abcd, "character") + assert_file_exists(path_abcd, desc = "ABCD") - stop_if_not_length(sheet_abcd, 1L) - stop_if_not_inherits(sheet_abcd, "character") - stop_if_sheet_not_found(sheet_abcd, path_abcd) + assert_length(sheet_abcd, 1L) + assert_inherits(sheet_abcd, "character") + assert_sheet_exists(sheet_abcd, path_abcd) if (!is.null(remove_inactive_companies)) { - stop_if_not_length(remove_inactive_companies, 1L) - stop_if_not_inherits(remove_inactive_companies, "logical") + assert_length(remove_inactive_companies, 1L) + assert_inherits(remove_inactive_companies, "logical") } - stop_if_not_length(start_year, 1L) - stop_if_not_inherits(start_year, "integer") + assert_length(start_year, 1L) + assert_inherits(start_year, "integer") - stop_if_not_length(time_frame, 1L) - stop_if_not_inherits(time_frame, "integer") + assert_length(time_frame, 1L) + assert_inherits(time_frame, "integer") # load data---- abcd <- read_abcd_raw(path_abcd, sheet_abcd) - stop_if_not_expected_columns(abcd, cols_abcd, desc = "ABCD") + assert_expected_columns(abcd, cols_abcd, desc = "ABCD") # optional: remove inactive companies---- diff --git a/R/run_aggregate_alignment_metric.R b/R/run_aggregate_alignment_metric.R index 1feaaf8e..0b90d5a4 100644 --- a/R/run_aggregate_alignment_metric.R +++ b/R/run_aggregate_alignment_metric.R @@ -46,7 +46,7 @@ run_aggregate_alignment_metric <- function(config) { # read matched and prioritized loan book---- list_matched_prioritized <- list.files(path = dir_prio_diagnostics, pattern = "^matched_prio_.*csv$") - stop_if_no_files_found(list_matched_prioritized, dir_prio_diagnostics, "dir_prio_diagnostics", "matched prioritized loan book CSVs") + assert_any_file_exists(list_matched_prioritized, dir_prio_diagnostics, "dir_prio_diagnostics", "matched prioritized loan book CSVs") matched_prioritized <- readr::read_csv( file = file.path(dir_prio_diagnostics, list_matched_prioritized), diff --git a/R/run_calculate_loanbook_coverage.R b/R/run_calculate_loanbook_coverage.R index ea10a962..ce7d3940 100644 --- a/R/run_calculate_loanbook_coverage.R +++ b/R/run_calculate_loanbook_coverage.R @@ -13,15 +13,15 @@ run_calculate_loanbook_coverage <- function(config) { by_group_ext <- if (is.null(by_group)) { "_meta" } else { paste0("_", by_group) } # validate config values---- - stop_if_not_length(dir_prio_diagnostics, 1L) - stop_if_not_inherits(dir_prio_diagnostics, "character") - stop_if_dir_not_found(dir_prio_diagnostics, desc = "Output - Matched loanbooks") + assert_length(dir_prio_diagnostics, 1L) + assert_inherits(dir_prio_diagnostics, "character") + assert_dir_exists(dir_prio_diagnostics, desc = "Output - Matched loanbooks") - stop_if_not_length(scenario_source_input, 1L) - stop_if_not_inherits(scenario_source_input, "character") + assert_length(scenario_source_input, 1L) + assert_inherits(scenario_source_input, "character") - stop_if_not_length(start_year, 1L) - stop_if_not_inherits(start_year, "integer") + assert_length(start_year, 1L) + assert_inherits(start_year, "integer") # load data ---- ## read abcd data---- @@ -38,7 +38,7 @@ run_calculate_loanbook_coverage <- function(config) { ## read matched prioritized loan books---- list_matched_prioritized <- list.files(path = dir_prio_diagnostics, pattern = "^matched_prio_.*csv$") - stop_if_no_files_found(list_matched_prioritized, dir_prio_diagnostics, "dir_prio_diagnostics", "matched prioritized loan book CSVs") + assert_any_file_exists(list_matched_prioritized, dir_prio_diagnostics, "dir_prio_diagnostics", "matched prioritized loan book CSVs") matched_prioritized <- readr::read_csv( file = file.path(dir_prio_diagnostics, list_matched_prioritized), diff --git a/R/run_calculate_match_success_rate.R b/R/run_calculate_match_success_rate.R index 19733584..0dceaf95 100644 --- a/R/run_calculate_match_success_rate.R +++ b/R/run_calculate_match_success_rate.R @@ -21,41 +21,41 @@ run_calculate_match_success_rate <- function(config) { match_success_rate_plot_resolution <- get_match_plot_resolution(config) # validate config values---- - stop_if_not_length(dir_loanbooks, 1L) - stop_if_not_inherits(dir_loanbooks, "character") - stop_if_dir_not_found(dir_loanbooks, desc = "Input - loanbooks") + assert_length(dir_loanbooks, 1L) + assert_inherits(dir_loanbooks, "character") + assert_dir_exists(dir_loanbooks, desc = "Input - loanbooks") - stop_if_not_length(dir_prioritized_loanbooks_and_diagnostics, 1L) - stop_if_not_inherits(dir_prioritized_loanbooks_and_diagnostics, "character") - stop_if_dir_not_found(dir_prioritized_loanbooks_and_diagnostics, desc = "Output - Prioritized loanbooks and diagnostics") + assert_length(dir_prioritized_loanbooks_and_diagnostics, 1L) + assert_inherits(dir_prioritized_loanbooks_and_diagnostics, "character") + assert_dir_exists(dir_prioritized_loanbooks_and_diagnostics, desc = "Output - Prioritized loanbooks and diagnostics") - stop_if_not_length(matching_use_manual_sector_classification, 1L) - stop_if_not_inherits(matching_use_manual_sector_classification, "logical") + assert_length(matching_use_manual_sector_classification, 1L) + assert_inherits(matching_use_manual_sector_classification, "logical") # path to manual sector classification only required if boolean TRUE if (matching_use_manual_sector_classification) { - stop_if_not_length(path_manual_sector_classification, 1L) - stop_if_not_inherits(path_manual_sector_classification, "character") - stop_if_file_not_found(path_manual_sector_classification, desc = "Manual sector classification") + assert_length(path_manual_sector_classification, 1L) + assert_inherits(path_manual_sector_classification, "character") + assert_file_exists(path_manual_sector_classification, desc = "Manual sector classification") } - stop_if_not_length(match_success_rate_plot_width, 1L) - stop_if_not_inherits(match_success_rate_plot_width, "integer") + assert_length(match_success_rate_plot_width, 1L) + assert_inherits(match_success_rate_plot_width, "integer") - stop_if_not_length(match_success_rate_plot_height, 1L) - stop_if_not_inherits(match_success_rate_plot_height, "integer") + assert_length(match_success_rate_plot_height, 1L) + assert_inherits(match_success_rate_plot_height, "integer") - stop_if_not_length(match_success_rate_plot_units, 1L) - stop_if_not_inherits(match_success_rate_plot_units, "character") + assert_length(match_success_rate_plot_units, 1L) + assert_inherits(match_success_rate_plot_units, "character") - stop_if_not_length(match_success_rate_plot_resolution, 1L) - stop_if_not_inherits(match_success_rate_plot_resolution, "integer") + assert_length(match_success_rate_plot_resolution, 1L) + assert_inherits(match_success_rate_plot_resolution, "integer") # load data---- ## load raw loan books---- list_raw <- list.files(dir_loanbooks)[grepl("csv$", list.files(dir_loanbooks))] - stop_if_no_files_found(list_raw, dir_loanbooks, "dir_loanbooks", "raw loan book CSVs") + assert_any_file_exists(list_raw, dir_loanbooks, "dir_loanbooks", "raw loan book CSVs") if (is.null(by_group) || by_group != "group_id") { raw_lbk <- readr::read_csv( @@ -81,7 +81,7 @@ run_calculate_match_success_rate <- function(config) { ## load matched prioritized loan books---- list_matched_prioritized <- list.files(path = dir_prioritized_loanbooks_and_diagnostics, pattern = "^matched_prio_.*csv$") - stop_if_no_files_found(list_matched_prioritized, dir_prioritized_loanbooks_and_diagnostics, "dir_prioritized_loanbooks_and_diagnostics", "matched prioritized loan book CSVs") + assert_any_file_exists(list_matched_prioritized, dir_prioritized_loanbooks_and_diagnostics, "dir_prioritized_loanbooks_and_diagnostics", "matched prioritized loan book CSVs") matched_prioritized <- readr::read_csv( file = file.path(dir_prioritized_loanbooks_and_diagnostics, list_matched_prioritized), diff --git a/R/run_match_prioritize.R b/R/run_match_prioritize.R index 7e236ebb..d3bbd9d9 100644 --- a/R/run_match_prioritize.R +++ b/R/run_match_prioritize.R @@ -14,16 +14,17 @@ run_match_prioritize <- function(config) { sector_split_type_select <- get_sector_split_type(config) # validate config values---- - stop_if_not_length(dir_prepared_abcd, 1L) - stop_if_not_inherits(dir_prepared_abcd, "character") - stop_if_dir_not_found(dir_prepared_abcd, desc = "Output - prepare ABCD") - stop_if_file_not_found(file.path(dir_prepared_abcd, "abcd_final.csv"), desc = "ABCD final") - stop_if_not_length(dir_matched_loanbooks, 1L) - stop_if_not_inherits(dir_matched_loanbooks, "character") - stop_if_dir_not_found(dir_matched_loanbooks, desc = "Output - Matched loanbooks") - stop_if_not_inherits(apply_sector_split, "logical") + assert_length(dir_prepared_abcd, 1L) + assert_inherits(dir_prepared_abcd, "character") + assert_dir_exists(dir_prepared_abcd, desc = "Output - prepare ABCD") + assert_file_exists(file.path(dir_prepared_abcd, "abcd_final.csv"), desc = "ABCD final") + assert_length(dir_matched_loanbooks, 1L) + assert_inherits(dir_matched_loanbooks, "character") + assert_dir_exists(dir_matched_loanbooks, desc = "Output - Matched loanbooks") + assert_inherits(apply_sector_split, "logical") + if (apply_sector_split) { - stop_if_not_inherits(sector_split_type_select, "character") + assert_inherits(sector_split_type_select, "character") } if (!is.null(match_prio_priority)) { @@ -50,7 +51,7 @@ run_match_prioritize <- function(config) { # load data---- ## load manually matched files---- list_matched_manual <- list.files(path = dir_matched_loanbooks, pattern = "^matched_lbk_.*_manual[.]csv$") - stop_if_no_files_found(list_matched_manual, dir_matched_loanbooks, "dir_output", "manually matched loan book CSVs matching the pattern {.code ^matched_lbk_.*_manual[.]csv$}") + assert_any_file_exists(list_matched_manual, dir_matched_loanbooks, "dir_output", "manually matched loan book CSVs matching the pattern {.code ^matched_lbk_.*_manual[.]csv$}") matched_lbk_manual <- readr::read_csv( file = file.path(dir_matched_loanbooks, list_matched_manual), diff --git a/R/run_pacta.R b/R/run_pacta.R index d5105f5c..9024c96e 100644 --- a/R/run_pacta.R +++ b/R/run_pacta.R @@ -64,7 +64,7 @@ run_pacta <- function(config) { # read matched and prioritized loan book---- list_matched_prioritized <- list.files(path = dir_prioritized_loanbooks_and_diagnostics, pattern = "^matched_prio_.*csv$") - stop_if_no_files_found(list_matched_prioritized, dir_prioritized_loanbooks_and_diagnostics, "dir_prioritized_loanbooks_and_diagnostics", "matched prioritized loan book CSVs") + assert_any_file_exists(list_matched_prioritized, dir_prioritized_loanbooks_and_diagnostics, "dir_prioritized_loanbooks_and_diagnostics", "matched prioritized loan book CSVs") matched_prioritized <- readr::read_csv( file = file.path(dir_prioritized_loanbooks_and_diagnostics, list_matched_prioritized), diff --git a/tests/testthat/test-assert_.R b/tests/testthat/test-assert_.R new file mode 100644 index 00000000..118e6fe9 --- /dev/null +++ b/tests/testthat/test-assert_.R @@ -0,0 +1,103 @@ +test_that("assert_inherits()", { + expect_no_condition(assert_inherits(1L, "integer")) + expect_no_condition(assert_inherits(1, "numeric")) + expect_no_condition(assert_inherits(1.1, "numeric")) + expect_no_condition(assert_inherits("A", "character")) + expect_no_condition(assert_inherits(list(a = 1), "list")) + expect_no_condition(assert_inherits(data.frame(a = 1), "data.frame")) + + expect_condition(assert_inherits(1, "integer")) + expect_condition(assert_inherits(1.1, "integer")) + expect_condition(assert_inherits("A", "integer")) + expect_condition(assert_inherits(list(a = 1), "integer")) + expect_condition(assert_inherits(data.frame(a = 1), "integer")) + + expect_condition(assert_inherits(1L, "numeric")) + expect_condition(assert_inherits("A", "numeric")) + expect_condition(assert_inherits(list(a = 1), "numeric")) + expect_condition(assert_inherits(data.frame(a = 1), "numeric")) + + expect_condition(assert_inherits(1L, "character")) + expect_condition(assert_inherits(1, "character")) + expect_condition(assert_inherits(1.1, "character")) + expect_condition(assert_inherits(list(a = 1), "character")) + expect_condition(assert_inherits(data.frame(a = 1), "character")) + + expect_condition(assert_inherits(1L, "list")) + expect_condition(assert_inherits(1, "list")) + expect_condition(assert_inherits(1.1, "list")) + expect_condition(assert_inherits("A", "list")) + expect_condition(assert_inherits(data.frame(a = 1), "list")) + + expect_condition(assert_inherits(1L, "data.frame")) + expect_condition(assert_inherits(1, "data.frame")) + expect_condition(assert_inherits(1.1, "data.frame")) + expect_condition(assert_inherits("A", "data.frame")) + expect_condition(assert_inherits(list(a = 1), "data.frame")) +}) + + +test_that("assert_length()", { + expect_no_condition(assert_length(NA, 1)) + expect_no_condition(assert_length(numeric(0), 0)) + expect_no_condition(assert_length(1, 1)) + expect_no_condition(assert_length(1:2, 2)) + expect_no_condition(assert_length(character(0), 0)) + expect_no_condition(assert_length("A", 1)) + expect_no_condition(assert_length(c("A", "B"), 2)) + expect_no_condition(assert_length(list(), 0)) + expect_no_condition(assert_length(list(a = 1), 1)) + expect_no_condition(assert_length(list(a = 1, b = 2), 2)) + + expect_condition(assert_length(NULL, 1)) + expect_condition(assert_length(numeric(0), 1)) + expect_condition(assert_length(1, 2)) + expect_condition(assert_length(1:2, 1)) + expect_condition(assert_length(character(0), 1)) + expect_condition(assert_length("A", 2)) + expect_condition(assert_length(c("A", "B"), 1)) + expect_condition(assert_length(list(), 1)) + expect_condition(assert_length(list(a = 1), 2)) + expect_condition(assert_length(list(a = 1, b = 2), 1)) +}) + + +test_that("assert_dir_exists()", { + tmp_dir <- tempdir() + expect_no_condition(assert_dir_exists(tmp_dir)) + expect_condition(assert_dir_exists(file.path(tmp_dir, "XYZ"))) +}) + + +test_that("assert_file_exists()", { + tmp_file <- tempfile() + file.create(tmp_file) + expect_no_condition(assert_file_exists(tmp_file)) + expect_condition(assert_file_exists(file.path(".", tmp_file))) + unlink(tmp_file) +}) + + +test_that("assert_sheet_exists()", { + tmp_file <- tempfile() + writexl::write_xlsx(list("test" = data.frame(a = 1)), tmp_file) + expect_no_condition(assert_sheet_exists("test", tmp_file)) + expect_condition(assert_sheet_exists("xyz", tmp_file)) + unlink(tmp_file) +}) + + +test_that("assert_expected_columns()", { + data <- data.frame(a = 1, b = 2, c = 3) + expect_no_condition(assert_expected_columns(data, c("a"))) + expect_no_condition(assert_expected_columns(data, c("b"))) + expect_no_condition(assert_expected_columns(data, c("c"))) + expect_no_condition(assert_expected_columns(data, c("a", "b"))) + expect_no_condition(assert_expected_columns(data, c("b", "c"))) + expect_no_condition(assert_expected_columns(data, c("a", "b", "c"))) + expect_no_condition(assert_expected_columns(data, c("b", "c", "a"))) + + expect_condition(assert_expected_columns(data, c("x"))) + expect_condition(assert_expected_columns(data, c("x", "y", "z"))) + expect_condition(assert_expected_columns(data, c("a", "x"))) +}) diff --git a/tests/testthat/test-stop_if_.R b/tests/testthat/test-stop_if_.R deleted file mode 100644 index a805aba9..00000000 --- a/tests/testthat/test-stop_if_.R +++ /dev/null @@ -1,103 +0,0 @@ -test_that("stop_if_not_inherits()", { - expect_no_condition(stop_if_not_inherits(1L, "integer")) - expect_no_condition(stop_if_not_inherits(1, "numeric")) - expect_no_condition(stop_if_not_inherits(1.1, "numeric")) - expect_no_condition(stop_if_not_inherits("A", "character")) - expect_no_condition(stop_if_not_inherits(list(a = 1), "list")) - expect_no_condition(stop_if_not_inherits(data.frame(a = 1), "data.frame")) - - expect_condition(stop_if_not_inherits(1, "integer")) - expect_condition(stop_if_not_inherits(1.1, "integer")) - expect_condition(stop_if_not_inherits("A", "integer")) - expect_condition(stop_if_not_inherits(list(a = 1), "integer")) - expect_condition(stop_if_not_inherits(data.frame(a = 1), "integer")) - - expect_condition(stop_if_not_inherits(1L, "numeric")) - expect_condition(stop_if_not_inherits("A", "numeric")) - expect_condition(stop_if_not_inherits(list(a = 1), "numeric")) - expect_condition(stop_if_not_inherits(data.frame(a = 1), "numeric")) - - expect_condition(stop_if_not_inherits(1L, "character")) - expect_condition(stop_if_not_inherits(1, "character")) - expect_condition(stop_if_not_inherits(1.1, "character")) - expect_condition(stop_if_not_inherits(list(a = 1), "character")) - expect_condition(stop_if_not_inherits(data.frame(a = 1), "character")) - - expect_condition(stop_if_not_inherits(1L, "list")) - expect_condition(stop_if_not_inherits(1, "list")) - expect_condition(stop_if_not_inherits(1.1, "list")) - expect_condition(stop_if_not_inherits("A", "list")) - expect_condition(stop_if_not_inherits(data.frame(a = 1), "list")) - - expect_condition(stop_if_not_inherits(1L, "data.frame")) - expect_condition(stop_if_not_inherits(1, "data.frame")) - expect_condition(stop_if_not_inherits(1.1, "data.frame")) - expect_condition(stop_if_not_inherits("A", "data.frame")) - expect_condition(stop_if_not_inherits(list(a = 1), "data.frame")) -}) - - -test_that("stop_if_not_length()", { - expect_no_condition(stop_if_not_length(NA, 1)) - expect_no_condition(stop_if_not_length(numeric(0), 0)) - expect_no_condition(stop_if_not_length(1, 1)) - expect_no_condition(stop_if_not_length(1:2, 2)) - expect_no_condition(stop_if_not_length(character(0), 0)) - expect_no_condition(stop_if_not_length("A", 1)) - expect_no_condition(stop_if_not_length(c("A", "B"), 2)) - expect_no_condition(stop_if_not_length(list(), 0)) - expect_no_condition(stop_if_not_length(list(a = 1), 1)) - expect_no_condition(stop_if_not_length(list(a = 1, b = 2), 2)) - - expect_condition(stop_if_not_length(NULL, 1)) - expect_condition(stop_if_not_length(numeric(0), 1)) - expect_condition(stop_if_not_length(1, 2)) - expect_condition(stop_if_not_length(1:2, 1)) - expect_condition(stop_if_not_length(character(0), 1)) - expect_condition(stop_if_not_length("A", 2)) - expect_condition(stop_if_not_length(c("A", "B"), 1)) - expect_condition(stop_if_not_length(list(), 1)) - expect_condition(stop_if_not_length(list(a = 1), 2)) - expect_condition(stop_if_not_length(list(a = 1, b = 2), 1)) -}) - - -test_that("stop_if_dir_not_found()", { - tmp_dir <- tempdir() - expect_no_condition(stop_if_dir_not_found(tmp_dir)) - expect_condition(stop_if_dir_not_found(file.path(tmp_dir, "XYZ"))) -}) - - -test_that("stop_if_file_not_found()", { - tmp_file <- tempfile() - file.create(tmp_file) - expect_no_condition(stop_if_file_not_found(tmp_file)) - expect_condition(stop_if_file_not_found(file.path(".", tmp_file))) - unlink(tmp_file) -}) - - -test_that("stop_if_sheet_not_found()", { - tmp_file <- tempfile() - writexl::write_xlsx(list("test" = data.frame(a = 1)), tmp_file) - expect_no_condition(stop_if_sheet_not_found("test", tmp_file)) - expect_condition(stop_if_sheet_not_found("xyz", tmp_file)) - unlink(tmp_file) -}) - - -test_that("stop_if_not_expected_columns()", { - data <- data.frame(a = 1, b = 2, c = 3) - expect_no_condition(stop_if_not_expected_columns(data, c("a"))) - expect_no_condition(stop_if_not_expected_columns(data, c("b"))) - expect_no_condition(stop_if_not_expected_columns(data, c("c"))) - expect_no_condition(stop_if_not_expected_columns(data, c("a", "b"))) - expect_no_condition(stop_if_not_expected_columns(data, c("b", "c"))) - expect_no_condition(stop_if_not_expected_columns(data, c("a", "b", "c"))) - expect_no_condition(stop_if_not_expected_columns(data, c("b", "c", "a"))) - - expect_condition(stop_if_not_expected_columns(data, c("x"))) - expect_condition(stop_if_not_expected_columns(data, c("x", "y", "z"))) - expect_condition(stop_if_not_expected_columns(data, c("a", "x"))) -})