Skip to content

Commit

Permalink
add tests for the functions that set and/or depend on global variables (
Browse files Browse the repository at this point in the history
  • Loading branch information
cjyetman authored Apr 26, 2024
1 parent 9cdcd38 commit 1efba41
Show file tree
Hide file tree
Showing 5 changed files with 364 additions and 5 deletions.
57 changes: 57 additions & 0 deletions tests/testthat/test-set_analysis_inputs_path.R
Original file line number Diff line number Diff line change
@@ -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))
})
7 changes: 2 additions & 5 deletions tests/testthat/test-set_portfolio_parameters.R
Original file line number Diff line number Diff line change
@@ -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)
)
}

Expand Down
227 changes: 227 additions & 0 deletions tests/testthat/test-set_project_parameters.R
Original file line number Diff line number Diff line change
@@ -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
)
})
37 changes: 37 additions & 0 deletions tests/testthat/test-set_web_parameters.R
Original file line number Diff line number Diff line change
@@ -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
)
})
41 changes: 41 additions & 0 deletions tests/testthat/test-set_webtool_paths.R
Original file line number Diff line number Diff line change
@@ -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"
)
})

0 comments on commit 1efba41

Please sign in to comment.