diff --git a/tests/testthat/test-set_analysis_inputs_path.R b/tests/testthat/test-set_analysis_inputs_path.R new file mode 100644 index 0000000..c883fa2 --- /dev/null +++ b/tests/testthat/test-set_analysis_inputs_path.R @@ -0,0 +1,57 @@ +test_that("returns expected value when not overridden by a global variable", { + data_location_ext <- "xxx" + + output <- + callr::r( + func = function(set_analysis_inputs_path, data_location_ext) { + set_analysis_inputs_path(data_location_ext) + }, + args = list(set_analysis_inputs_path, data_location_ext) + ) + + expect_equal(output, data_location_ext) +}) + +test_that("returns expected value when overridden by a global variable `proj_data_location_ext`", { + data_location_ext <- "xxx" + proj_data_location_ext <- "yyy" + + output <- + callr::r( + func = function(set_analysis_inputs_path, + data_location_ext, + proj_data_location_ext) { + .GlobalEnv$proj_data_location_ext <- proj_data_location_ext + set_analysis_inputs_path(data_location_ext) + }, + args = list( + set_analysis_inputs_path, + data_location_ext, + proj_data_location_ext + ) + ) + + expect_equal(output, proj_data_location_ext) +}) + +test_that("returns expected value when overridden by a global variable `port_holdings_date`", { + data_location_ext <- "xxx" + port_holdings_date <- "zzz" + + output <- + callr::r( + func = function(set_analysis_inputs_path, + data_location_ext, + port_holdings_date) { + .GlobalEnv$port_holdings_date <- port_holdings_date + set_analysis_inputs_path(data_location_ext) + }, + args = list( + set_analysis_inputs_path, + data_location_ext, + port_holdings_date + ) + ) + + expect_equal(output, paste0("../pacta-data/", port_holdings_date)) +}) diff --git a/tests/testthat/test-set_portfolio_parameters.R b/tests/testthat/test-set_portfolio_parameters.R index d4710c3..21d8b48 100644 --- a/tests/testthat/test-set_portfolio_parameters.R +++ b/tests/testthat/test-set_portfolio_parameters.R @@ -1,17 +1,14 @@ test_that("`set_portfolio_parameters()` works as expected", { - skip_if_R_CMD_check() - test_set_portfolio_parameters_in_callr <- function(.param) { callr::r( - function(.params) { - devtools::load_all(quiet = TRUE) + function(set_portfolio_parameters, .params) { filepath <- tempfile(fileext = ".yml") yaml::write_yaml(list(default = list(parameters = .params)), filepath) set_portfolio_parameters(filepath) as.list.environment(.GlobalEnv) }, - args = list(parameters) + args = list(set_portfolio_parameters, parameters) ) } diff --git a/tests/testthat/test-set_project_parameters.R b/tests/testthat/test-set_project_parameters.R new file mode 100644 index 0000000..20ede31 --- /dev/null +++ b/tests/testthat/test-set_project_parameters.R @@ -0,0 +1,227 @@ +test_that("sets appropriate global variables", { + cfg <- + list( + default = list( + paths = list( + data_location_ext = "test_data_location_ext", + template_location = "test_template_location", + user_data_location = "test_user_data_location" + ), + reporting = list( + project_report_name = "test_project_report_name", + display_currency = "test_display_currency", + currency_exchange_value = 1.23 + ), + parameters = list( + timestamp = "test_timestamp", + dataprep_timestamp = "test_dataprep_timestamp", + start_year = 2023, + horizon_year = 2029, + select_scenario = "test_select_scenario", + scenario_other = "test_scenario_other", + portfolio_allocation_method = "test_portfolio_allocation_method", + scenario_geography = "test_scenario_geography" + ), + sectors = list( + tech_roadmap_sectors = "test_tech_roadmap_sectors", + pacta_sectors_not_analysed = "test_pacta_sectors_not_analysed", + green_techs = "test_green_techs", + alignment_techs = "test_alignment_techs" + ), + scenario_sources_list = "test_scenario_sources_list", + scenario_geography_list = "test_scenario_geography_list", + asset_types = "test_asset_types", + equity_market_list = "test_equity_market_list", + methodology = list( + has_map = FALSE, + has_credit = TRUE, + has_revenue = TRUE, + inc_emissionfactors = TRUE + ) + ) + ) + + config_path <- withr::local_file("config.yml") + yaml::write_yaml(x = cfg, file = config_path) + + test_globalenv_vars <- + callr::r( + func = function(set_project_parameters, config_path) { + set_project_parameters(config_path) + as.list.environment(.GlobalEnv) + }, + args = list(set_project_parameters, config_path) + ) + + expect_equal( + test_globalenv_vars$proj_data_location_ext, + cfg$default$paths$data_location_ext + ) + expect_equal( + test_globalenv_vars$project_report_name, + cfg$default$reporting$project_report_name + ) + expect_equal( + test_globalenv_vars$display_currency, + cfg$default$reporting$display_currency + ) + expect_equal( + test_globalenv_vars$currency_exchange_value, + cfg$default$reporting$currency_exchange_value + ) + expect_equal( + test_globalenv_vars$financial_timestamp, + cfg$default$parameters$timestamp + ) + expect_equal( + test_globalenv_vars$dataprep_timestamp, + cfg$default$parameters$dataprep_timestamp + ) + expect_equal( + test_globalenv_vars$start_year, + as.numeric(cfg$default$parameters$start_year) + ) + expect_equal( + test_globalenv_vars$time_horizon, + as.numeric(cfg$default$parameters$horizon_year) + ) + expect_equal( + test_globalenv_vars$select_scenario, + cfg$default$parameters$select_scenario + ) + expect_equal( + test_globalenv_vars$scenario_other, + cfg$default$parameters$scenario_other + ) + expect_equal( + test_globalenv_vars$portfolio_allocation_method, + cfg$default$parameters$portfolio_allocation_method + ) + expect_equal( + test_globalenv_vars$scenario_geography, + cfg$default$parameters$scenario_geography + ) + expect_equal( + test_globalenv_vars$tech_roadmap_sectors, + cfg$default$sectors$tech_roadmap_sectors + ) + expect_equal( + test_globalenv_vars$pacta_sectors_not_analysed, + cfg$default$sectors$pacta_sectors_not_analysed + ) + expect_equal( + test_globalenv_vars$sector_list, + c(cfg$default$sectors$tech_roadmap_sectors, cfg$default$sectors$pacta_sectors_not_analysed) + ) + expect_equal( + test_globalenv_vars$pacta_sectors_not_analysed, + cfg$default$sectors$pacta_sectors_not_analysed + ) + expect_equal( + test_globalenv_vars$scenario_sources_list, + cfg$default$scenario_sources_list + ) + expect_equal( + test_globalenv_vars$scenario_geographies_list, + cfg$default$scenario_geography_list + ) + expect_equal( + test_globalenv_vars$asset_types, + cfg$default$asset_types + ) + expect_equal( + test_globalenv_vars$equity_market_list, + cfg$default$equity_market_list + ) + expect_equal( + test_globalenv_vars$green_techs, + cfg$default$sectors$green_techs + ) + expect_equal( + test_globalenv_vars$alignment_techs, + cfg$default$sectors$alignment_techs + ) + expect_equal( + test_globalenv_vars$has_map, + FALSE + ) + expect_equal( + test_globalenv_vars$has_credit, + TRUE + ) + expect_equal( + test_globalenv_vars$has_revenue, + TRUE + ) + expect_equal( + test_globalenv_vars$inc_emission_factors, + TRUE + ) +}) + +test_that("sets appropriate global variables when methodological vars are not in config", { + cfg <- + list( + default = list( + paths = list( + data_location_ext = "test_data_location_ext", + template_location = "test_template_location", + user_data_location = "test_user_data_location" + ), + reporting = list( + project_report_name = "test_project_report_name", + display_currency = "test_display_currency", + currency_exchange_value = 1.23 + ), + parameters = list( + timestamp = "test_timestamp", + dataprep_timestamp = "test_dataprep_timestamp", + start_year = 2023, + horizon_year = 2029, + select_scenario = "test_select_scenario", + scenario_other = "test_scenario_other", + portfolio_allocation_method = "test_portfolio_allocation_method", + scenario_geography = "test_scenario_geography" + ), + sectors = list( + tech_roadmap_sectors = "test_tech_roadmap_sectors", + pacta_sectors_not_analysed = "test_pacta_sectors_not_analysed", + green_techs = "test_green_techs", + alignment_techs = "test_alignment_techs" + ), + scenario_sources_list = "test_scenario_sources_list", + scenario_geography_list = "test_scenario_geography_list", + asset_types = "test_asset_types", + equity_market_list = "test_equity_market_list" + ) + ) + + config_path <- withr::local_file("config.yml") + yaml::write_yaml(x = cfg, file = config_path) + + test_globalenv_vars <- + callr::r( + func = function(set_project_parameters, config_path) { + set_project_parameters(config_path) + as.list.environment(.GlobalEnv) + }, + args = list(set_project_parameters, config_path) + ) + + expect_equal( + test_globalenv_vars$has_map, + TRUE + ) + expect_equal( + test_globalenv_vars$has_credit, + FALSE + ) + expect_equal( + test_globalenv_vars$has_revenue, + FALSE + ) + expect_equal( + test_globalenv_vars$inc_emission_factors, + FALSE + ) +}) diff --git a/tests/testthat/test-set_web_parameters.R b/tests/testthat/test-set_web_parameters.R new file mode 100644 index 0000000..05f5713 --- /dev/null +++ b/tests/testthat/test-set_web_parameters.R @@ -0,0 +1,37 @@ +test_that("sets appropriate global variables", { + cfg <- + list( + default = list( + paths = list( + data_location_ext = "test_data_location_ext", + template_location = "test_template_location", + user_data_location = "test_user_data_location" + ) + ) + ) + + config_path <- withr::local_file("config.yml") + yaml::write_yaml(x = cfg, file = config_path) + + test_globalenv_vars <- + callr::r( + func = function(set_web_parameters, config_path) { + set_web_parameters(config_path) + as.list.environment(.GlobalEnv) + }, + args = list(set_web_parameters, config_path) + ) + + expect_equal( + test_globalenv_vars$data_location_ext, + cfg$default$paths$data_location_ext + ) + expect_equal( + test_globalenv_vars$template_path, + cfg$default$paths$template_location + ) + expect_equal( + test_globalenv_vars$user_results_path, + cfg$default$paths$user_data_location + ) +}) diff --git a/tests/testthat/test-set_webtool_paths.R b/tests/testthat/test-set_webtool_paths.R new file mode 100644 index 0000000..664c43d --- /dev/null +++ b/tests/testthat/test-set_webtool_paths.R @@ -0,0 +1,41 @@ +test_that("sets appropriate global variables", { + test_globalenv_vars <- + callr::r( + func = function(set_webtool_paths) { + .GlobalEnv$working_location <- "test_wd" + .GlobalEnv$portfolio_name_ref_all <- "test_port_name" + set_webtool_paths() + as.list.environment(.GlobalEnv) + }, + args = list(set_webtool_paths) + ) + + expect_equal( + test_globalenv_vars$project_location, + "test_wd/working_dir" + ) + expect_equal( + test_globalenv_vars$log_path, + "test_wd/working_dir/00_Log_Files/test_port_name" + ) + expect_equal( + test_globalenv_vars$par_file_path, + "test_wd/working_dir/10_Parameter_File" + ) + expect_equal( + test_globalenv_vars$raw_input_path, + "test_wd/working_dir/20_Raw_Inputs" + ) + expect_equal( + test_globalenv_vars$proc_input_path, + "test_wd/working_dir/30_Processed_Inputs" + ) + expect_equal( + test_globalenv_vars$results_path, + "test_wd/working_dir/40_Results" + ) + expect_equal( + test_globalenv_vars$outputs_path, + "test_wd/working_dir/50_Outputs" + ) +})