diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 1474613d..1634e76c 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -2,9 +2,9 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: [main, master] + branches: [main, master, develop] pull_request: - branches: [main, master] + branches: [main, master, develop] name: R-CMD-check diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index 3ae39f94..3cd19899 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -2,9 +2,9 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: [main, master] + branches: [main, master, develop] pull_request: - branches: [main, master] + branches: [main, master, develop] name: test-coverage diff --git a/NEWS.md b/NEWS.md index f4174eee..5d80e499 100644 --- a/NEWS.md +++ b/NEWS.md @@ -24,6 +24,8 @@ BREAKING CHANGES : * `api_get()` has a new parameter to control JSON file parsing * `readInputThermal()` default value when no time series in the selected clusters. * `readInputRES()` default value when no time series in the selected clusters +* `readClusterDesc()`/ `readClusterRESDesc()` / `readClusterSTDesc()` +return empty dataTable and warning if no cluster in Antares study. # antaresRead 2.6.0 diff --git a/R/readClusterDesc.R b/R/readClusterDesc.R index 4f0e9195..dad0515e 100644 --- a/R/readClusterDesc.R +++ b/R/readClusterDesc.R @@ -89,7 +89,6 @@ readClusterSTDesc <- function(opts = simOptions()) { } - .readClusterDesc <- function(opts = simOptions(), dir = "thermal/clusters") { @@ -107,7 +106,11 @@ readClusterSTDesc <- function(opts = simOptions()) { path <- file.path(opts$inputPath, dir) - if(opts$typeLoad == 'api'){ + columns <- .generate_columns_by_type(dir = dir) + api_study <- is_api_study(opts) + + 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){ clusters <- rbindlist( @@ -123,10 +126,6 @@ readClusterSTDesc <- function(opts = simOptions()) { clusters[, .SD, .SDcols = order(names(clusters))] },jsoncld, names(jsoncld), SIMPLIFY = FALSE), fill = TRUE) - if(length(res) == 0) - stop("Cannot find cluster description.", call. = FALSE) - - res <- res[, .SD, .SDcols = c("area", "name", "group", names(res)[!names(res) %in%c("area", "name", "group")])] }else{ @@ -146,12 +145,37 @@ readClusterSTDesc <- function(opts = simOptions()) { } - if(length(res) == 0) stop("Cannot find cluster description.", call. = FALSE) + 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 <- as.data.table(res) - setnames(res, "name", "cluster") + res +} + +.generate_columns_by_type <- function(dir = c("thermal/clusters", "renewables/clusters", "st-storage/clusters")) { - res$cluster <- as.factor(tolower(res$cluster)) - res + columns <- switch( + dir, + "thermal/clusters" = c("group","enabled","must_run","unit_count","nominal_capacity", + "min_stable_power","spinning","min_up_time","min_down_time", + "co2","marginal_cost","fixed_cost","startup_cost","market_bid_cost", + "spread_cost","ts_gen","volatility_forced","volatility_planned", + "law_forced","law_planned"), + + "renewables/clusters" = c("group","ts_interpretation","enabled","unit_count","nominal_capacity") + #"st-storage/clusters" = #ATTENTE DEV COTé API + ) + return(columns) } diff --git a/tests/testthat/test-readClusterDesc.R b/tests/testthat/test-readClusterDesc.R index f62dd764..c7d14398 100644 --- a/tests/testthat/test-readClusterDesc.R +++ b/tests/testthat/test-readClusterDesc.R @@ -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 @@ -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)) + })