tests/testthat/test-add_scenario.R

# add via user name --------------------------------------------
path <- system.file("testdata", "three1", package = "mm.reoptimise")
scenario_list <- create_scenario_list(path)
scenario <- scenario_list[[1]]

# set the scenario
period <- get_scenario_period(scenario, type = "all")
kpi1 <- get_scenario_kpi1(scenario, period[1])
kpi2 <- get_scenario_kpi2(scenario, period[1], kpi1[1])
kpi3 <- get_scenario_kpi3(scenario, period[1], kpi1[1], kpi2[1])
val <- NA
scenario <- set_scenario(scenario, period[1], budget = val, kpi1[2], kpi2[1], kpi3[1])

# run optimisation
scenario <- run_optimization(
      scenario = scenario)

myname <- "my new scenario"
user_name <- "my new scenario"
updated_scenario_list <- add_scenario(scenario_list, scenario, myname)

# # test function
test_that("output is correct type and size",{

      expect_equal(length(updated_scenario_list), length(scenario_list)+1)

})

# # test function
test_that("function can successfully retreive scenario based on name",{

      expect_equal(select_scenario(updated_scenario_list, myname)$name$name, myname)

       unique_id <- updated_scenario_list %>%
            purrr::map(1) %>%
            dplyr::bind_rows() %>%
             dplyr::pull(unique_id)

       expect_equal(unique_id %>% unique() %>% length(), 3)

      })

# add scenario works if scenario hasn't been set or ran yet --------------------------------------------
path <- system.file("testdata", "three2", package = "mm.reoptimise")
scenario_list <- create_scenario_list(path)
scenario <- scenario_list[[1]]

# add scenario
updated_scenario_list <- add_scenario(scenario_list,
                                      scenario,
                                      user_name = "new")

# # test function
test_that("output is correct type and size",{

   expect_equal(length(updated_scenario_list), length(scenario_list)+1)

})

# # test function
test_that("function can successfully retreive scenario based on name",{

   expect_equal(select_scenario(updated_scenario_list, "new")$name$name, "new")

   unique_id <- updated_scenario_list %>%
      purrr::map(1) %>%
      dplyr::bind_rows() %>%
      dplyr::pull(unique_id)

   expect_equal(unique_id %>% unique() %>% length(), 2)

})

df <- summarise_scenarios(updated_scenario_list)

# if you provide a name of an existing scenario, function will update the scenario --------------------------------------------
scenario <- updated_scenario_list[[2]]

updated_scenario_list_T <- add_scenario(updated_scenario_list,
                                        scenario,
                                        "new")

df_after <- summarise_scenarios(updated_scenario_list)


# # test function
test_that("output is correct type and size",{

   expect_equal(length(updated_scenario_list), 2)

})

# # test function
test_that("function can successfully retreive scenario based on name",{

   expect_equal(select_scenario(updated_scenario_list, "new")$name$name, "new")

   unique_id <- updated_scenario_list %>%
      purrr::map(1) %>%
      dplyr::bind_rows() %>%
      dplyr::pull(unique_id)

   expect_equal(unique_id %>% unique() %>% length(), 2)

})

# updating a list in the scenario (2) ---------------------------------
path <- system.file("testdata", "three5", package = "mm.reoptimise")
scenario_list <- create_scenario_list(path)
myscenario <- scenario_list[[1]]
scenario_list <- mm.reoptimise::add_scenario(scenario_list, myscenario, "new")

selected_scenario <- scenario_list[[2]]

selected_scenario <- mm.reoptimise::set_scenario(scenario = selected_scenario,
                                                 period = "month",
                                                 budget = "",
                                                 kpi1 = "Profit")
selected_scenario <- mm.reoptimise::run_optimization(selected_scenario)

scenario_list <- mm.reoptimise::add_scenario(scenario_list,
                                               selected_scenario,
                                               user_name = "new")


# # test function
test_that("output is correct type and size",{

   expect_equal(length(updated_scenario_list), 2)
   expect_equal(list_out_scenarios(scenario_list), c("zero", "new"))
   expect_true((selected_scenario$name$description %>% nchar)>0)

})

# Check the description isn't getting overwritten - when scenario isn't already in list --------------------------
path <- system.file("testdata", "three5", package = "mm.reoptimise")
scenario_list <- create_scenario_list(path)
myscenario <- scenario_list[[1]]
myscenario$name$description

updated_scenario_list <- mm.reoptimise::add_scenario(scenario_list, myscenario, "new")
selected_scenario <- mm.reoptimise::select_scenario(updated_scenario_list, "new")

test_that("output is correct type and size",{

   expect_equal(length(updated_scenario_list), 2)
   expect_equal(list_out_scenarios(updated_scenario_list), c("zero", "new"))
   expect_true((selected_scenario$name$description %>% nchar)>0)

})
cath-parkinson/mm.reoptimise documentation built on May 12, 2022, 3:34 p.m.