Skip to content

Commit

Permalink
Add sections to have more readable test file
Browse files Browse the repository at this point in the history
  • Loading branch information
kemihak committed Dec 1, 2023
1 parent cfebed5 commit 6e29d96
Showing 1 changed file with 143 additions and 134 deletions.
277 changes: 143 additions & 134 deletions tests/testthat/test-writeInputTS.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,124 @@ sapply(studies, function(study) {
})


# v860 ----
# >= 820 ----
## Alphabetical order links ----
test_that("Check if writeInputTS() writes time series link regardless alphabetical order", {

ant_version <- "8.2.0"
st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "aa"
area2 <- "zz"
createArea(area)
createArea(area2)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

createLink(from = area, to = area2, opts = opts)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt"))
path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt"))

dat_mat <- c(1,3,2,4)
dat_mat_inv <- c(4,2,3,1)
nb_cols <- length(dat_mat)

# alphabetical order
mat_multi_scen <- matrix(data = rep(dat_mat, each = 8760), ncol = nb_cols)
writeInputTS(data = mat_multi_scen, link = paste0(area,"%",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen[,seq((nb_cols/2)+1, nb_cols)]))

# no alphabetical order
mat_multi_scen_inv <- matrix(data = rep(dat_mat_inv, each = 8760), ncol = nb_cols)
writeInputTS(data = mat_multi_scen_inv, link = paste0(area2,"%",area), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen_inv[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen_inv[,seq((nb_cols/2)+1, nb_cols)]))

})


## Separator link type ----
test_that("Check if writeInputTS() writes links time series with argument link 'area1 - area2' or 'area1%area2'", {

ant_version <- "8.2.0"
st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "aa"
area2 <- "zz"
createArea(area)
createArea(area2)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

createLink(from = area, to = area2, opts = opts)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt"))
path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt"))

dat_mat_sep_1 <- c(1,3,2,4)
nb_cols <- length(dat_mat_sep_1)
mat_ts_sep_1 <- matrix(data = rep(dat_mat_sep_1, each = 8760), ncol = nb_cols)

dat_mat_sep_2 <- c(5,7,6,8)
nb_cols <- length(dat_mat_sep_2)
mat_ts_sep_2 <- matrix(data = rep(dat_mat_sep_2, each = 8760), ncol = nb_cols)

# link separator '%'
writeInputTS(data = mat_ts_sep_1, link = paste0(area,"%",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_ts_sep_1[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_ts_sep_1[,seq((nb_cols/2)+1, nb_cols)]))

# link separator ' - '
writeInputTS(data = mat_ts_sep_2, link = paste0(area," - ",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_ts_sep_2[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_ts_sep_2[,seq((nb_cols/2)+1, nb_cols)]))

})



# >= v860 ----
setup_study_860(sourcedir860)

#Avoid warning related to code writed outside test_that.
suppressWarnings(opts <- antaresRead::setSimulationPath(study_temp_path, "input"))

## Check column dimension ----
test_that("create mingen file data v860", {

#Initialize mingen data
M_mingen = matrix(0,8760,5)


# [management rules] for mingen data :
# file mod.txt (in /series) have to be same column dimension
# or column dimension of 1 or NULL (empty file)
# file mod.txt (in /series) have to be same column dimension
# or column dimension of 1 or NULL (empty file)

# check dimensions of mod.txt for every areas
path_file_mod <- file.path(opts$inputPath, "hydro", "series",
Expand All @@ -127,7 +229,7 @@ test_that("create mingen file data v860", {
names(list_dim) <- getAreas()

## trivial case
# mod.txt column dimension == 1
# mod.txt column dimension == 1
area_1 <- getAreas()[list_dim==1][1]

# write for an area with file mod.txt NULL or nb columns == 1
Expand All @@ -138,15 +240,15 @@ test_that("create mingen file data v860", {
read_ts_file <- readInputTS(mingen = "all", opts = opts)

# tests correct reading data
# check col name "mingen"
# check col name "mingen"
testthat::expect_true("mingen" %in% names(read_ts_file))
# check your area
# check your area
testthat::expect_true(area_1 %in% unique(read_ts_file$area))
# check dimension data for your area
# check dimension data for your area
testthat::expect_equal(dim(M_mingen)[2], max(read_ts_file[area %in% area_1, tsId]))


# mod.txt column dimension == 0 (empty file)
# mod.txt column dimension == 0 (empty file)
area_0 <- getAreas()[list_dim==0][1]

# write for an area with file mod.txt empty columns == 0
Expand All @@ -161,14 +263,14 @@ test_that("create mingen file data v860", {


## multi columns cas for mod.txt file
# mod.txt column dimension >= 1
# mod.txt column dimension >= 1
area_mult <- getAreas()[list_dim>1][1]

# write for an area with file mod.txt >1 columns
# error case cause mod.txt dimension
# error case cause mod.txt dimension
testthat::expect_error(writeInputTS(area = area_mult, type = "mingen",
data = M_mingen , overwrite = TRUE, opts = opts),
regexp = 'mingen \'data\' must be either a 8760\\*1 or 8760\\*3 matrix.')
data = M_mingen , overwrite = TRUE, opts = opts),
regexp = 'mingen \'data\' must be either a 8760\\*1 or 8760\\*3 matrix.')

# you can write only mingen file with dimension 1
writeInputTS(area = area_mult, type = "mingen",
Expand Down Expand Up @@ -199,9 +301,34 @@ test_that("create mingen file data v860", {
})


## Rollback to empty file ----
test_that("writeInputTS() in 8.6.0 : rollback to an empty file", {

ant_version <- "8.6.0"
st_test <- paste0("my_study_860_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "zone51"
createArea(area)
opts <- setSimulationPath(opts$studyPath, simulation = "input")

path_mingen <- file.path(opts$inputPath, "hydro", "series", area, "mingen.txt")
mat_mingen <- matrix(6,8760,5)
expect_error(writeInputTS(area = area,
data = mat_mingen,
type = "mingen",
opts = opts
)
,regexp = "can not be updated"
)
expect_true(file.size(path_mingen) == 0)

unlink(x = opts$studyPath, recursive = TRUE)
})

test_that("writeInputTS() in 8.6.0 : check if there is an error when control is enabled and data is inconsistent between mingen.txt and mod.txt", {

## Error mingen.txt vs mod.txt ----
test_that("writeInputTS() in 8.6.0 : check if there is an error when control is enabled and data is inconsistent between mingen.txt and mod.txt", {

ant_version <- "8.6.0"
st_test <- paste0("my_study_860_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
Expand Down Expand Up @@ -239,7 +366,7 @@ test_that("writeInputTS() in 8.6.0 : check if there is an error when control is
data = mat_mingen_false,
type = "mingen",
opts = opts
)
)
,regexp = "can not be updated"
)
# ref mingen
Expand Down Expand Up @@ -299,6 +426,7 @@ test_that("writeInputTS() in 8.6.0 : check if there is an error when control is
})


## Success mingen.txt vs mod.txt ----
test_that("writeInputTS() in 8.6.0 : check if new data is written when control is enabled and data is consistent between mingen.txt and mod.txt", {

ant_version <- "8.6.0"
Expand Down Expand Up @@ -383,6 +511,7 @@ test_that("writeInputTS() in 8.6.0 : check if new data is written when control i
})


## Success when disabled control ----
test_that("writeInputTS() in 8.6.0 : check if new data is written when control is disabled", {

ant_version <- "8.6.0"
Expand Down Expand Up @@ -449,123 +578,3 @@ test_that("writeInputTS() in 8.6.0 : check if new data is written when control i
})


test_that("Check if writeInputTS() writes time series link regardless alphabetical order", {

ant_version <- "8.2.0"
st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "aa"
area2 <- "zz"
createArea(area)
createArea(area2)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

createLink(from = area, to = area2, opts = opts)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt"))
path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt"))

dat_mat <- c(1,3,2,4)
dat_mat_inv <- c(4,2,3,1)
nb_cols <- length(dat_mat)

# alphabetical order
mat_multi_scen <- matrix(data = rep(dat_mat, each = 8760), ncol = nb_cols)
writeInputTS(data = mat_multi_scen, link = paste0(area,"%",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen[,seq((nb_cols/2)+1, nb_cols)]))

# no alphabetical order
mat_multi_scen_inv <- matrix(data = rep(dat_mat_inv, each = 8760), ncol = nb_cols)
writeInputTS(data = mat_multi_scen_inv, link = paste0(area2,"%",area), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_multi_scen_inv[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_multi_scen_inv[,seq((nb_cols/2)+1, nb_cols)]))

})


test_that("Check if writeInputTS() writes links time series with argument link 'area1 - area2' or 'area1%area2'", {

ant_version <- "8.2.0"
st_test <- paste0("my_study_820_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "aa"
area2 <- "zz"
createArea(area)
createArea(area2)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

createLink(from = area, to = area2, opts = opts)
suppressWarnings(opts <- setSimulationPath(opts$studyPath, simulation = "input"))

path_direct_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_direct.txt"))
path_indirect_link_file <- file.path(opts$inputPath, "links", area, "capacities", paste0(area2,"_indirect.txt"))

dat_mat_sep_1 <- c(1,3,2,4)
nb_cols <- length(dat_mat_sep_1)
mat_ts_sep_1 <- matrix(data = rep(dat_mat_sep_1, each = 8760), ncol = nb_cols)

dat_mat_sep_2 <- c(5,7,6,8)
nb_cols <- length(dat_mat_sep_2)
mat_ts_sep_2 <- matrix(data = rep(dat_mat_sep_2, each = 8760), ncol = nb_cols)

# link separator '%'
writeInputTS(data = mat_ts_sep_1, link = paste0(area,"%",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_ts_sep_1[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_ts_sep_1[,seq((nb_cols/2)+1, nb_cols)]))

# link separator ' - '
writeInputTS(data = mat_ts_sep_2, link = paste0(area," - ",area2), type = "tsLink", opts = opts)

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_direct_link_file),
as.data.table(mat_ts_sep_2[,seq(1, nb_cols/2)]))

expect_equal(antaresRead:::fread_antares(opts = opts,
file = path_indirect_link_file),
as.data.table(mat_ts_sep_2[,seq((nb_cols/2)+1, nb_cols)]))

})


test_that("writeInputTS() in 8.6.0 : rollback to an empty file", {

ant_version <- "8.6.0"
st_test <- paste0("my_study_860_", paste0(sample(letters,5),collapse = ""))
suppressWarnings(opts <- createStudy(path = pathstd, study_name = st_test, antares_version = ant_version))
area <- "zone51"
createArea(area)
opts <- setSimulationPath(opts$studyPath, simulation = "input")

path_mingen <- file.path(opts$inputPath, "hydro", "series", area, "mingen.txt")
mat_mingen <- matrix(6,8760,5)
expect_error(writeInputTS(area = area,
data = mat_mingen,
type = "mingen",
opts = opts
)
,regexp = "can not be updated"
)
expect_true(file.size(path_mingen) == 0)

unlink(x = opts$studyPath, recursive = TRUE)
})

0 comments on commit 6e29d96

Please sign in to comment.