Skip to content

Commit

Permalink
Read cluster desc bugfix when cluster exist (#242)
Browse files Browse the repository at this point in the history
* fix bug readClusterDesc when no cluster + cover ST and RES case

* add test (init test for thermal + res)
  • Loading branch information
Nekmek7 authored Apr 2, 2024
1 parent a20a721 commit 06fdf64
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 18 deletions.
32 changes: 16 additions & 16 deletions R/readClusterDesc.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ readClusterSTDesc <- function(opts = simOptions()) {
path <- file.path(opts$inputPath, dir)

columns <- .generate_columns_by_type(dir = dir)
api_study <- is_api_study(opts)

if(opts$typeLoad == 'api'){
if(api_study){

jsoncld <- read_secure_json(paste0(path, "&depth=4"), token = opts$token, timeout = opts$timeout, config = opts$httr_config)
res <- rbindlist(mapply(function(X1, Y1){
Expand All @@ -125,13 +126,6 @@ readClusterSTDesc <- function(opts = simOptions()) {
clusters[, .SD, .SDcols = order(names(clusters))]
},jsoncld, names(jsoncld), SIMPLIFY = FALSE), fill = TRUE)

if(length(res) == 0){
warning("No cluster description available.", call. = FALSE)
res <- setNames(data.table(matrix(nrow = 0, ncol = 2 + length(columns))), c("area", "cluster", columns))
}else{
res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])]
}


}else{

Expand All @@ -149,15 +143,21 @@ readClusterSTDesc <- function(opts = simOptions()) {
clusters[, c(ncol(clusters), 1:(ncol(clusters) - 1))]
})

if(length(res) == 0){
warning("No cluster description available.", call. = FALSE)
res <- setNames(data.table(matrix(nrow = 0, ncol = 2 + length(columns))), c("area", "cluster", columns))
}else{
res <- as.data.table(res)
setnames(res, "name", "cluster")

res$cluster <- as.factor(tolower(res$cluster))
}

if(length(res) == 0){
mandatory_cols <- c("area","cluster")
warning("No cluster description available.", call. = FALSE)
res <- setNames(data.table(matrix(nrow = 0, ncol = length(mandatory_cols) + length(columns))), c(mandatory_cols, columns))
}else{
if(api_study){
mandatory_cols <- c("area", "name", "group")
additional_cols <- setdiff(colnames(res),mandatory_cols)
res <- res[, .SD, .SDcols = c(mandatory_cols, additional_cols)]
}
res <- as.data.table(res)
setnames(res, "name", "cluster")
res$cluster <- as.factor(tolower(res$cluster))
}

res
Expand Down
40 changes: 38 additions & 2 deletions tests/testthat/test-readClusterDesc.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@

## v860 ----
# v860 ----

path_study_test <- grep(pattern = "86", x = studyPathSV8, value = TRUE)
opts_study_test <- setSimulationPath(path_study_test, simulation = "input")

#minimal columns
mandatory_cols <- c("area","cluster")

## Thermal ----
test_that("test read cluster", {

# function setSimulationPath() provide areas names with st-storage clusters
areas <- opts_study_test$areasWithClusters

# read clusters informations
input <- readClusterDesc()

# tests
testthat::expect_true("data.table" %in% class(input))
testthat::expect_true(all(areas %in% unique(readClusterDesc()$area)))
testthat::expect_true(all(mandatory_cols %in% colnames(input)))
testthat::expect_true(nrow(input) == length(input$cluster))

})

## Renewables ----
test_that("test read cluster renewables", {

# function setSimulationPath() provide areas names with st-storage clusters
areas_res <- opts_study_test$areasWithRESClusters

#Study not renewables (need activateRES() from antaresEditObject)
expect_error(readClusterResDesc(),
regexp = "readClusterDesc is available only on studies with 'renewable-generation-modelling'")

})

## st-storage ----
test_that("test read cluster st-storage v860", {

# function setSimulationPath() provide areas names with st-storage clusters
Expand All @@ -14,5 +47,8 @@ test_that("test read cluster st-storage v860", {

# tests
testthat::expect_true("data.table" %in% class(input_st))
testthat::expect_true(areas_st %in% unique(readClusterSTDesc()$area))
testthat::expect_true(all(areas_st %in% unique(readClusterSTDesc()$area)))
testthat::expect_true(all(mandatory_cols %in% colnames(input_st)))
testthat::expect_true(nrow(input_st) == length(input_st$cluster))

})

0 comments on commit 06fdf64

Please sign in to comment.