Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ant1795v2 #261

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export(setSimulationPathAPI)
export(setTimeoutAPI)
export(showAliases)
export(simOptions)
export(transform_antares_version)
export(viewAntares)
export(writeDigest)
import(bit64)
Expand Down Expand Up @@ -105,6 +106,8 @@ importFrom(stats,as.formula)
importFrom(stats,setNames)
importFrom(stats,weighted.mean)
importFrom(stringi,stri_replace_last_fixed)
importFrom(stringr,str_count)
importFrom(stringr,str_detect)
importFrom(stringr,str_match)
importFrom(stringr,str_replace)
importFrom(stringr,str_split)
Expand Down
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 str_detect
#'
#' @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))
}




14 changes: 14 additions & 0 deletions man/transform_antares_version.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions tests/testthat/test-setSimulationPath.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Loading