Skip to content

Commit

Permalink
Merge pull request #15 from psrenergy/feature/tests
Browse files Browse the repository at this point in the history
Tests updates (2)
  • Loading branch information
raphasampaio authored Nov 23, 2023
2 parents 75d97a0 + 4a7ab7c commit 1f07471
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 190 deletions.
4 changes: 0 additions & 4 deletions test/data/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
example_2.csv
example_array_1.csv
example_convert_1.csv
gerter.csv
65 changes: 30 additions & 35 deletions test/read_and_write_hourly.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
function test_read_and_write_hourly()
FILE_GERTER = joinpath(".", "data", "gerter")
path = joinpath(".", "data", "hourly")

STAGES = 3
SCENARIOS = 2
AGENTS = ["X", "Y", "Z"]
UNIT = "MW"
STAGE_TYPE = PSRI.STAGE_MONTH
INITIAL_STAGE = 2
INITIAL_YEAR = 2006
stages = 3
scenarios = 2
agents = ["X", "Y", "Z"]
unit = "MW"
stage_type = PSRI.STAGE_MONTH
initial_stage = 2
initial_year = 2006

gerter = PSRI.open(
GrafCSV.Writer,
FILE_GERTER,
path,
is_hourly = true,
scenarios = SCENARIOS,
stages = STAGES,
agents = AGENTS,
unit = UNIT,
scenarios = scenarios,
stages = stages,
agents = agents,
unit = unit,
# optional:
stage_type = STAGE_TYPE,
initial_stage = INITIAL_STAGE,
initial_year = INITIAL_YEAR,
stage_type = stage_type,
initial_stage = initial_stage,
initial_year = initial_year,
)

# Loop de gravacao
for stage in 1:STAGES
for scenario in 1:SCENARIOS
for stage in 1:stages
for scenario in 1:scenarios
for block in 1:PSRI.blocks_in_stage(gerter, stage)
X = 10_000.0 * stage + 1000.0 * scenario + block
Y = block + 0.0
Expand All @@ -41,26 +40,25 @@ function test_read_and_write_hourly()
end
end

# Finaliza gravacao
PSRI.close(gerter)

ior = PSRI.open(
GrafCSV.Reader,
FILE_GERTER,
path,
is_hourly = true,
)

@test PSRI.max_stages(ior) == STAGES
@test PSRI.max_scenarios(ior) == SCENARIOS
@test PSRI.max_stages(ior) == stages
@test PSRI.max_scenarios(ior) == scenarios
@test PSRI.max_blocks(ior) == 744
@test PSRI.stage_type(ior) == STAGE_TYPE
@test PSRI.initial_stage(ior) == INITIAL_STAGE
@test PSRI.initial_year(ior) == INITIAL_YEAR
@test PSRI.data_unit(ior) == UNIT
@test PSRI.agent_names(ior) == ["X", "Y", "Z"]
@test PSRI.stage_type(ior) == stage_type
@test PSRI.initial_stage(ior) == initial_stage
@test PSRI.initial_year(ior) == initial_year
@test PSRI.data_unit(ior) == unit
@test PSRI.agent_names(ior) == agents

for stage in 1:STAGES
for scenario in 1:SCENARIOS
for stage in 1:stages
for scenario in 1:scenarios
for block in 1:PSRI.blocks_in_stage(ior, stage)
@test PSRI.current_stage(ior) == stage
@test PSRI.current_scenario(ior) == scenario
Expand All @@ -85,10 +83,7 @@ function test_read_and_write_hourly()
GC.gc()
GC.gc()

try
rm(FILE_GERTER * ".csv")
catch
end
safe_remove(path * ".csv")

return nothing
end
end
79 changes: 34 additions & 45 deletions test/read_and_write_monthly.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
function test_read_and_write_monthly()
FILE_PATH = joinpath(".", "data", "example_2")
path = joinpath(".", "data", "monthly")

STAGES = 12
BLOCKS = 3
SCENARIOS = 4
STAGE_TYPE = PSRI.STAGE_MONTH
INITIAL_STAGE = 1
INITIAL_YEAR = 2006
UNIT = "MW"
stages = 12
blocks = 3
scenarios = 4
agents = ["X", "Y", "Z"]
stage_type = PSRI.STAGE_MONTH
initial_stage = 1
initial_year = 2006
unit = "MW"

iow = PSRI.open(
GrafCSV.Writer,
FILE_PATH,
blocks = BLOCKS,
scenarios = SCENARIOS,
stages = STAGES,
agents = ["X", "Y", "Z"],
unit = UNIT,
path,
blocks = blocks,
scenarios = scenarios,
stages = stages,
agents = agents,
unit = unit,
# optional:
stage_type = STAGE_TYPE,
initial_stage = INITIAL_STAGE,
initial_year = INITIAL_YEAR,
stage_type = stage_type,
initial_stage = initial_stage,
initial_year = initial_year,
)

# ---------------------------------------------
# Parte 3 - Gravacao dos registros do resultado
# ---------------------------------------------

# Loop de gravacao
for stage in 1:STAGES, scenario in 1:SCENARIOS, block in 1:BLOCKS
for stage in 1:stages, scenario in 1:scenarios, block in 1:blocks
X = stage + scenario + 0.0
Y = scenario - stage + 0.0
Z = stage + scenario + block * 100.0
Expand All @@ -41,28 +37,25 @@ function test_read_and_write_monthly()
)
end

# Finaliza gravacao
PSRI.close(iow)

ior = PSRI.open(
GrafCSV.Reader,
FILE_PATH,
path,
)

@test PSRI.max_stages(ior) == STAGES
@test PSRI.max_scenarios(ior) == SCENARIOS
@test PSRI.max_blocks(ior) == BLOCKS
@test PSRI.stage_type(ior) == STAGE_TYPE
@test PSRI.initial_stage(ior) == INITIAL_STAGE
@test PSRI.initial_year(ior) == INITIAL_YEAR
@test PSRI.data_unit(ior) == UNIT

# obtem número de colunas
@test PSRI.agent_names(ior) == ["X", "Y", "Z"]

for stage in 1:STAGES
for scenario in 1:SCENARIOS
for block in 1:BLOCKS
@test PSRI.max_stages(ior) == stages
@test PSRI.max_scenarios(ior) == scenarios
@test PSRI.max_blocks(ior) == blocks
@test PSRI.stage_type(ior) == stage_type
@test PSRI.initial_stage(ior) == initial_stage
@test PSRI.initial_year(ior) == initial_year
@test PSRI.data_unit(ior) == unit
@test PSRI.agent_names(ior) == agents

for stage in 1:stages
for scenario in 1:scenarios
for block in 1:blocks
@test PSRI.current_stage(ior) == stage
@test PSRI.current_scenario(ior) == scenario
@test PSRI.current_block(ior) == block
Expand All @@ -85,16 +78,12 @@ function test_read_and_write_monthly()
@test_throws ErrorException PSRI.convert_file(
GrafCSV.Reader,
GrafCSV.Writer,
FILE_PATH,
path,
)

ior = nothing

try
rm(FILE_PATH * ".csv")
catch
println("Failed to delete: $FILE_PATH")
end
safe_remove(path * ".csv")

return nothing
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using Test

const PSRI = GrafCSV.PSRClassesInterface

include("utils.jl")
include("read_and_write_monthly.jl")
include("read_and_write_hourly.jl")
include("time_series_utils.jl")
Expand All @@ -13,4 +14,4 @@ function test_all()
@testset "Utils" begin test_time_series_utils() end
end

test_all()
test_all()
Loading

0 comments on commit 1f07471

Please sign in to comment.