Skip to content

Commit

Permalink
fix createBindingConstraintBulk() with NULL or mixed values + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
berthetclement committed Jul 17, 2024
1 parent 4678fd0 commit 7771360
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 5 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ NEW FEATURES (Antares v8.8) :
BUGFIXES :

* `createBindingConstraint()` in API mode (for study <v870) created with "hourly" timeStep all the time
* `createBindingConstraint()` / `editBindingConstraint()` in TEXT mode, bad values in time series
* `createBindingConstraint()` / `editBindingConstraint()` in TEXT mode, bad values in time series
* `createBindingConstraintBulk()` with no VALUES and with a mix
* side effects with `readClusterDesc()` / `readClusterResDesc()` / `readClusterSTDesc()`


Expand Down
13 changes: 9 additions & 4 deletions R/createBindingConstraint.R
Original file line number Diff line number Diff line change
Expand Up @@ -791,10 +791,15 @@ createBindingConstraintBulk <- function(constraints,
assertthat::assert_that(inherits(constraints, "list"))

# check input object
all_dim_group <- do.call("rbind", lapply(constraints, function(x){
data.table(name_group <- x$group,
dim_group <- dim(x$values[[1]])[2])})
)
all_dim_group <- do.call("rbind",
c(lapply(constraints, function(x){
data.table(name_group <- x$group,
dim_group <- dim(x$values[[1]])[2])}),
fill=TRUE))

# no check dimension if NULL values
if(dim(all_dim_group)[2]<2)
return()

# no duplicated
all_dim_group <- unique(all_dim_group)
Expand Down
105 changes: 105 additions & 0 deletions tests/testthat/test-createBindingConstraint.R
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,111 @@ test_that("test bad dimension object with existing object in study v8.7", {
regexp = "Problem dimension with group"
)

})

test_that("test NULL VALUES in study v8.7", {
BC_NULL_VALUES <- list(
name = paste0("constraints_bulkNULL"),
id = paste0("constraints_bulkNULL"),
values = NULL,
enabled = FALSE,
timeStep = "hourly",
operator = "both",
coefficients = list("at%fr" = 1),
group= "group_bulk",
overwrite = TRUE
)

createBindingConstraintBulk(list(BC_NULL_VALUES))

# tests
testthat::expect_true("constraints_bulkNULL" %in%
names(readBindingConstraints()))

# read real value
operator_bc <- c("_lt", "_gt")
path_bc <- file.path(opts_test$inputPath, "bindingconstraints")
path_file_bc <- paste0(file.path(path_bc, "constraints_bulkNULL"),
operator_bc, ".txt")

# read .txt (test values)
res <- lapply(path_file_bc,
antaresRead:::fread_antares,
opts = opts_test)

res <- unlist(res)

# txt files are empty
testthat::expect_equal(res, NULL)

})

test_that("test mixed VALUES in study v8.7", {
BC_MIX_VALUES <- list(
list(
name = paste0("constraints_bulkNULL"),
id = paste0("constraints_bulkNULL"),
values = NULL,
enabled = FALSE,
timeStep = "hourly",
operator = "both",
coefficients = list("at%fr" = 1),
group= "group_bulk",
overwrite = TRUE
),
list(
name = paste0("constraints_bulk_value"),
id = paste0("constraints_bulk_value"),
values = scenar_values,
enabled = FALSE,
timeStep = "hourly",
operator = "greater",
coefficients = list("at%fr" = 1),
group= "group_bulk",
overwrite = TRUE
))

createBindingConstraintBulk(BC_MIX_VALUES)

# tests
testthat::expect_true(all(
c("constraints_bulkNULL", "constraints_bulk_value") %in%
names(readBindingConstraints())))

# read real value
# NULL
operator_bc <- c("_lt", "_gt")
path_bc <- file.path(opts_test$inputPath, "bindingconstraints")
path_file_bc <- paste0(file.path(path_bc, "constraints_bulkNULL"),
operator_bc, ".txt")

# read .txt (test values)
res <- lapply(path_file_bc,
antaresRead:::fread_antares,
opts = opts_test)

res <- unlist(res)

# txt files are empty
testthat::expect_equal(res, NULL)

# VALUE
operator_bc <- c("_gt")
path_bc <- file.path(opts_test$inputPath, "bindingconstraints")
path_file_bc <- paste0(file.path(path_bc, "constraints_bulk_value"),
operator_bc, ".txt")

# read .txt (test values)
res <- lapply(path_file_bc,
antaresRead:::fread_antares,
opts = opts_test)

# txt files
testthat::expect_equal(head(res[[1]]),
head(data.table::as.data.table(scenar_values$gt)))



})

# remove temporary study ----
Expand Down

0 comments on commit 7771360

Please sign in to comment.