Skip to content

Commit

Permalink
transform_antares_version added to setSimulationPath.R
Browse files Browse the repository at this point in the history
  • Loading branch information
vargastat committed Sep 6, 2024
1 parent 9f16ee2 commit 17740e6
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
54 changes: 54 additions & 0 deletions R/setSimulationPath.R
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,57 @@ setSimulationPath <- function(path, simulation = NULL) {
return(list(binding = df_groups))
}
}

#' @title Convert the Antares number version
#'
#' @param antares_version Antares number version.
#'
#' @importFrom stringr str_count
#'
#' @export
transform_antares_version <- function(antares_version) {
antares_version <- as.character(antares_version)

# Handle single numeric version (e.g., "860", "890")
if (str_count(antares_version, "\\.") == 0) {
to_write <- antares_version
to_read <- as.numeric(antares_version) # Keep it numeric if no decimal
} else {
# Split major and minor parts
antares_version_splitted <- strsplit(antares_version, split = "\\.")[[1]]

# Ensure valid version format
if (length(antares_version_splitted) != 2 || any(!str_detect(antares_version_splitted, "^\\d+$"))) {
stop("Invalid antares_version format")
}

# Extract major and minor parts as numbers
major_version <- as.numeric(antares_version_splitted[1])
minor_version_str <- antares_version_splitted[2] # Keep minor version as string

# Check if minor version exceeds 2 digits
if (nchar(minor_version_str) > 2) {
stop("Minor version exceeds 2 digits limit.")
}

# Convert minor version to numeric for calculation
minor_version <- as.numeric(minor_version_str)

# Convert major version starting from 9.0 to 900, 10.0 to 1000, etc.
if (major_version >= 9) {
major_version_converted <- major_version * 100
} else {
stop("Major version must be 9 or higher.")
}

# Create a numeric value for comparison by treating the version as major.minor
to_write <- paste(major_version, minor_version_str, sep = ".")
to_read <- major_version_converted + minor_version # Major * 100, add minor
}

return(list("w" = to_write, "r" = to_read))
}




32 changes: 31 additions & 1 deletion tests/testthat/test-setSimulationPath.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ test_that("New meta data for areas with a ST cluster", {
expect_false(is.null(opts_study_test$areasWithSTClusters))
})


library(stringr)
# v870----
test_that("New meta data for group dimension of binding constraints", {
# read latest version study
Expand All @@ -207,3 +207,33 @@ test_that("New meta data for group dimension of binding constraints", {

expect_is(opts_study_test$binding, "data.table")
})

test_that("valid versions are transformed correctly", {
expect_equal(transform_antares_version("9.0")$r, 900)
expect_equal(transform_antares_version("9.45")$r, 945)
expect_equal(transform_antares_version("10.10")$r, 1010)
expect_equal(transform_antares_version("10.45")$r, 1045)
expect_equal(transform_antares_version("12.12")$r, 1212)
})

test_that("invalid minor versions with more than 2 digits raise an error", {
expect_error(transform_antares_version("10.113400000"), "Minor version exceeds 2 digits limit.")
expect_error(transform_antares_version("9.1234"), "Minor version exceeds 2 digits limit.")
})

test_that("invalid major versions less than 9 raise an error", {
expect_error(transform_antares_version("8.99"), "Major version must be 9 or higher.")
expect_error(transform_antares_version("7.10"), "Major version must be 9 or higher.")
})

test_that("single numeric versions work correctly", {
expect_equal(transform_antares_version("860")$r, 860)
expect_equal(transform_antares_version("890")$r, 890)
})

test_that("correct output for to_write field", {
expect_equal(transform_antares_version("9.0")$w, "9.0")
expect_equal(transform_antares_version("9.45")$w, "9.45")
expect_equal(transform_antares_version("10.10")$w, "10.10")
expect_equal(transform_antares_version("12.12")$w, "12.12")
})

0 comments on commit 17740e6

Please sign in to comment.