From 81c1180cdc3139c00789bc4eded1b16b36a47e21 Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Thu, 23 Nov 2023 12:10:20 -0300 Subject: [PATCH 1/2] tests updates --- test/data/.gitignore | 4 - test/read_and_write_hourly.jl | 59 +++++----- test/read_and_write_monthly.jl | 71 +++++------- test/runtests.jl | 3 +- test/time_series_utils.jl | 197 +++++++++++++++------------------ test/utils.jl | 7 ++ 6 files changed, 158 insertions(+), 183 deletions(-) create mode 100644 test/utils.jl diff --git a/test/data/.gitignore b/test/data/.gitignore index 862a104..e69de29 100644 --- a/test/data/.gitignore +++ b/test/data/.gitignore @@ -1,4 +0,0 @@ -example_2.csv -example_array_1.csv -example_convert_1.csv -gerter.csv \ No newline at end of file diff --git a/test/read_and_write_hourly.jl b/test/read_and_write_hourly.jl index ffc774d..2da142e 100644 --- a/test/read_and_write_hourly.jl +++ b/test/read_and_write_hourly.jl @@ -1,31 +1,30 @@ function test_read_and_write_hourly() - FILE_GERTER = joinpath(".", "data", "gerter") + path = joinpath(".", "data", "hourly") - STAGES = 3 - SCENARIOS = 2 + stages = 3 + scenarios = 2 AGENTS = ["X", "Y", "Z"] - UNIT = "MW" - STAGE_TYPE = PSRI.STAGE_MONTH - INITIAL_STAGE = 2 - INITIAL_YEAR = 2006 + 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, + scenarios = scenarios, + stages = stages, agents = AGENTS, - unit = UNIT, + 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 @@ -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.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"] - 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 @@ -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 \ No newline at end of file +end diff --git a/test/read_and_write_monthly.jl b/test/read_and_write_monthly.jl index 9951352..e93e288 100644 --- a/test/read_and_write_monthly.jl +++ b/test/read_and_write_monthly.jl @@ -1,34 +1,29 @@ 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 + 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, + path, + blocks = blocks, + scenarios = scenarios, + stages = stages, agents = ["X", "Y", "Z"], - unit = UNIT, + 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 @@ -41,28 +36,26 @@ 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 + @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 + 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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 76f2c57..c1a3eb5 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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") @@ -13,4 +14,4 @@ function test_all() @testset "Utils" begin test_time_series_utils() end end -test_all() \ No newline at end of file +test_all() diff --git a/test/time_series_utils.jl b/test/time_series_utils.jl index 8f12bd1..cae6bba 100644 --- a/test/time_series_utils.jl +++ b/test/time_series_utils.jl @@ -1,14 +1,8 @@ -function rm_bin_hdr(file::String) - rm(file * ".bin") - rm(file * ".hdr") - return nothing -end - function test_non_unique_agents() - FILE_PATH = joinpath(".", "data", "example_non_unique_agents") + path = joinpath(".", "data", "example_non_unique_agents") @test_throws ErrorException iow = PSRI.open( GrafCSV.Writer, - FILE_PATH, + path, blocks = 3, scenarios = 4, stages = 5, @@ -21,17 +15,19 @@ function test_non_unique_agents() end function test_convert_twice() - BLOCKS = 3 - SCENARIOS = 10 - STAGES = 12 + path1 = joinpath(".", "data", "convert_1") + path2 = joinpath(".", "data", "convert_2") + + blocks = 3 + scenarios = 10 + stages = 12 - FILE_PATH = joinpath(".", "data", "example_convert_1") iow = PSRI.open( PSRI.OpenBinary.Writer, - FILE_PATH, - blocks = BLOCKS, - scenarios = SCENARIOS, - stages = STAGES, + path1, + blocks = blocks, + scenarios = scenarios, + stages = stages, agents = ["X", "Y", "Z"], unit = "MW", # optional: @@ -39,55 +35,53 @@ function test_convert_twice() initial_year = 2006, ) - for estagio in 1:STAGES, serie in 1:SCENARIOS, bloco in 1:BLOCKS - X = estagio + serie + 0.0 - Y = serie - estagio + 0.0 - Z = estagio + serie + bloco * 100.0 + 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 PSRI.write_registry( iow, [X, Y, Z], - estagio, - serie, - bloco, + stage, + scenario, + block, ) end - # Finaliza gravacao PSRI.close(iow) PSRI.convert_file( PSRI.OpenBinary.Reader, GrafCSV.Writer, - FILE_PATH, + path1, ) ior = PSRI.open( GrafCSV.Reader, - FILE_PATH, + path1, use_header = false, ) - @test PSRI.max_stages(ior) == STAGES - @test PSRI.max_scenarios(ior) == SCENARIOS - @test PSRI.max_blocks(ior) == BLOCKS + @test PSRI.max_stages(ior) == stages + @test PSRI.max_scenarios(ior) == scenarios + @test PSRI.max_blocks(ior) == blocks @test PSRI.stage_type(ior) == PSRI.STAGE_MONTH @test PSRI.initial_stage(ior) == 1 @test PSRI.initial_year(ior) == 2006 @test PSRI.data_unit(ior) == "MW" - # obtem número de colunas @test PSRI.agent_names(ior) == ["X", "Y", "Z"] - for estagio in 1:STAGES - for serie in 1:SCENARIOS - for bloco in 1:BLOCKS - @test PSRI.current_stage(ior) == estagio - @test PSRI.current_scenario(ior) == serie - @test PSRI.current_block(ior) == bloco + 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 - X = estagio + serie - Y = serie - estagio - Z = estagio + serie + bloco * 100 + X = stage + scenario + Y = scenario - stage + Z = stage + scenario + block * 100 ref = [X, Y, Z] for agent in 1:3 @@ -101,41 +95,38 @@ function test_convert_twice() PSRI.close(ior) ior = nothing - FILE_PATH_2 = joinpath(".", "data", "example_convert_2") - PSRI.convert_file( GrafCSV.Reader, PSRI.OpenBinary.Writer, - FILE_PATH, - path_to = FILE_PATH_2, + path1, + path_to = path2, ) ior = PSRI.open( PSRI.OpenBinary.Reader, - FILE_PATH_2, + path2, use_header = false, ) - @test PSRI.max_stages(ior) == STAGES - @test PSRI.max_scenarios(ior) == SCENARIOS - @test PSRI.max_blocks(ior) == BLOCKS + @test PSRI.max_stages(ior) == stages + @test PSRI.max_scenarios(ior) == scenarios + @test PSRI.max_blocks(ior) == blocks @test PSRI.stage_type(ior) == PSRI.STAGE_MONTH @test PSRI.initial_stage(ior) == 1 @test PSRI.initial_year(ior) == 2006 @test PSRI.data_unit(ior) == "MW" - # obtem número de colunas @test PSRI.agent_names(ior) == ["X", "Y", "Z"] - for estagio in 1:STAGES - for serie in 1:SCENARIOS - for bloco in 1:BLOCKS - @test PSRI.current_stage(ior) == estagio - @test PSRI.current_scenario(ior) == serie - @test PSRI.current_block(ior) == bloco - X = estagio + serie - Y = serie - estagio - Z = estagio + serie + bloco * 100 + 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 + X = stage + scenario + Y = scenario - stage + Z = stage + scenario + block * 100 ref = [X, Y, Z] for agent in 1:3 @test ior[agent] == ref[agent] @@ -147,29 +138,29 @@ function test_convert_twice() PSRI.close(ior) - rm_bin_hdr(FILE_PATH) - rm_bin_hdr(FILE_PATH_2) - try - rm(FILE_PATH * ".csv") - catch - println("Failed to remove $(FILE_PATH).csv") - end + safe_remove("$path1.bin") + safe_remove("$path1.hdr") + + safe_remove("$path2.bin") + safe_remove("$path2.hdr") + + safe_remove("$path1.csv") return nothing end function test_file_to_array() - BLOCKS = 3 - SCENARIOS = 10 - STAGES = 12 + blocks = 3 + scenarios = 10 + stages = 12 - FILE_PATH = joinpath(".", "data", "example_array_1") + path = joinpath(".", "data", "example_array_1") iow = PSRI.open( PSRI.OpenBinary.Writer, - FILE_PATH, - blocks = BLOCKS, - scenarios = SCENARIOS, - stages = STAGES, + path, + blocks = blocks, + scenarios = scenarios, + stages = stages, agents = ["X", "Y", "Z"], unit = "MW", # optional: @@ -177,16 +168,16 @@ function test_file_to_array() initial_year = 2006, ) - for estagio in 1:STAGES, serie in 1:SCENARIOS, bloco in 1:BLOCKS - X = estagio + serie + 0.0 - Y = serie - estagio + 0.0 - Z = estagio + serie + bloco * 100.0 + 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 PSRI.write_registry( iow, [X, Y, Z], - estagio, - serie, - bloco, + stage, + scenario, + block, ) end @@ -194,26 +185,26 @@ function test_file_to_array() data, header = PSRI.file_to_array_and_header( PSRI.OpenBinary.Reader, - FILE_PATH; + path; use_header = false, ) data_order, header_order = PSRI.file_to_array_and_header( PSRI.OpenBinary.Reader, - FILE_PATH; + path; use_header = true, header = ["Y", "Z", "X"], ) @test data == PSRI.file_to_array( PSRI.OpenBinary.Reader, - FILE_PATH; + path; use_header = false, ) @test data_order == PSRI.file_to_array( PSRI.OpenBinary.Reader, - FILE_PATH; + path; use_header = true, header = ["Y", "Z", "X"], ) @@ -224,7 +215,7 @@ function test_file_to_array() PSRI.array_to_file( GrafCSV.Writer, - FILE_PATH, + path, data, agents = header, unit = "MW", @@ -233,31 +224,30 @@ function test_file_to_array() ior = PSRI.open( GrafCSV.Reader, - FILE_PATH, + path, use_header = false, ) - @test PSRI.max_stages(ior) == STAGES - @test PSRI.max_scenarios(ior) == SCENARIOS - @test PSRI.max_blocks(ior) == BLOCKS + @test PSRI.max_stages(ior) == stages + @test PSRI.max_scenarios(ior) == scenarios + @test PSRI.max_blocks(ior) == blocks @test PSRI.stage_type(ior) == PSRI.STAGE_MONTH @test PSRI.initial_stage(ior) == 1 @test PSRI.initial_year(ior) == 2006 @test PSRI.data_unit(ior) == "MW" - # obtem número de colunas @test PSRI.agent_names(ior) == ["X", "Y", "Z"] - for estagio in 1:STAGES - for serie in 1:SCENARIOS - for bloco in 1:BLOCKS - @test PSRI.current_stage(ior) == estagio - @test PSRI.current_scenario(ior) == serie - @test PSRI.current_block(ior) == bloco + 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 - X = estagio + serie - Y = serie - estagio - Z = estagio + serie + bloco * 100 + X = stage + scenario + Y = scenario - stage + Z = stage + scenario + block * 100 ref = [X, Y, Z] for agent in 1:3 @@ -271,12 +261,9 @@ function test_file_to_array() PSRI.close(ior) ior = nothing - rm_bin_hdr(FILE_PATH) - try - rm(FILE_PATH * ".csv") - catch - println("Failed to remove $(FILE_PATH).csv") - end + safe_remove("$path.bin") + safe_remove("$path.hdr") + safe_remove("$path.csv") return nothing end @@ -286,4 +273,4 @@ function test_time_series_utils() test_convert_twice() test_file_to_array() return nothing -end \ No newline at end of file +end diff --git a/test/utils.jl b/test/utils.jl new file mode 100644 index 0000000..7eca36a --- /dev/null +++ b/test/utils.jl @@ -0,0 +1,7 @@ +function safe_remove(path::AbstractString) + try + rm(path) + catch + println("Failed to remove $path") + end +end From 4a7ab7ccc983a22f8e02199e1c4ddacfa3d6cfb1 Mon Sep 17 00:00:00 2001 From: raphasampaio Date: Thu, 23 Nov 2023 12:19:47 -0300 Subject: [PATCH 2/2] updated --- test/read_and_write_hourly.jl | 6 +++--- test/read_and_write_monthly.jl | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/read_and_write_hourly.jl b/test/read_and_write_hourly.jl index 2da142e..09c2e21 100644 --- a/test/read_and_write_hourly.jl +++ b/test/read_and_write_hourly.jl @@ -3,7 +3,7 @@ function test_read_and_write_hourly() stages = 3 scenarios = 2 - AGENTS = ["X", "Y", "Z"] + agents = ["X", "Y", "Z"] unit = "MW" stage_type = PSRI.STAGE_MONTH initial_stage = 2 @@ -15,7 +15,7 @@ function test_read_and_write_hourly() is_hourly = true, scenarios = scenarios, stages = stages, - agents = AGENTS, + agents = agents, unit = unit, # optional: stage_type = stage_type, @@ -55,7 +55,7 @@ function test_read_and_write_hourly() @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.agent_names(ior) == agents for stage in 1:stages for scenario in 1:scenarios diff --git a/test/read_and_write_monthly.jl b/test/read_and_write_monthly.jl index e93e288..87de77f 100644 --- a/test/read_and_write_monthly.jl +++ b/test/read_and_write_monthly.jl @@ -4,6 +4,7 @@ function test_read_and_write_monthly() stages = 12 blocks = 3 scenarios = 4 + agents = ["X", "Y", "Z"] stage_type = PSRI.STAGE_MONTH initial_stage = 1 initial_year = 2006 @@ -15,7 +16,7 @@ function test_read_and_write_monthly() blocks = blocks, scenarios = scenarios, stages = stages, - agents = ["X", "Y", "Z"], + agents = agents, unit = unit, # optional: stage_type = stage_type, @@ -50,8 +51,7 @@ function test_read_and_write_monthly() @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.agent_names(ior) == agents for stage in 1:stages for scenario in 1:scenarios