Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermebodin committed Sep 12, 2024
1 parent 3ae99ad commit 8891b8a
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 174 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Quiver"
uuid = "cdbb3f72-2527-4dbd-9d0e-93533a5519ac"
authors = ["raphasampaio", "guilhermebodin"]
version = "0.1.2"
version = "0.1.3"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
134 changes: 134 additions & 0 deletions test/test_convert.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
module TestConvert

using Dates
using Quiver
using Test

function binary_to_csv()
filename = joinpath(@__DIR__, "test_binary_to_csv")

initial_date = DateTime(2006, 1, 1)
num_stages = 10
dates = collect(initial_date:Dates.Month(1):initial_date + Dates.Month(num_stages - 1))
num_scenarios = 12
num_blocks_per_stage = Int32.(Dates.daysinmonth.(dates) .* 24)
num_time_series = 3

dimensions = ["stage", "scenario", "block"]
labels = ["agent_$i" for i in 1:num_time_series]
time_dimension = "stage"
dimension_size = [num_stages, num_scenarios, maximum(num_blocks_per_stage)]

writer = Quiver.Writer{Quiver.binary}(
filename;
dimensions,
labels,
time_dimension,
dimension_size,
initial_date = initial_date
)

for stage in 1:num_stages
for scenario in 1:num_scenarios
for block in 1:num_blocks_per_stage[stage]
data = [stage, scenario, block]
Quiver.write!(writer, data; stage, scenario, block)
end
end
end

Quiver.close!(writer)

Quiver.convert(filename, Quiver.binary, Quiver.csv)

reader = Quiver.Reader{Quiver.csv}(filename)
for stage in 1:num_stages
for scenario in 1:num_scenarios
for block in 1:num_blocks_per_stage[stage]
Quiver.next_dimension!(reader)
@test reader.data == [stage, scenario, block]
end
end
end

Quiver.close!(reader)

rm("$filename.$(Quiver.file_extension(Quiver.binary))")
rm("$filename.$(Quiver.file_extension(Quiver.csv))")
rm("$filename.toml")
end

function csv_to_binary()
filename = joinpath(@__DIR__, "test_csv_to_binary")

initial_date = DateTime(2006, 1, 1)
num_stages = 10
dates = collect(initial_date:Dates.Month(1):initial_date + Dates.Month(num_stages - 1))
num_scenarios = 12
num_blocks_per_stage = Int32.(Dates.daysinmonth.(dates) .* 24)
num_time_series = 3

dimensions = ["stage", "scenario", "block"]
labels = ["agent_$i" for i in 1:num_time_series]
time_dimension = "stage"
dimension_size = [num_stages, num_scenarios, maximum(num_blocks_per_stage)]

writer = Quiver.Writer{Quiver.csv}(
filename;
dimensions,
labels,
time_dimension,
dimension_size,
initial_date = initial_date
)

for stage in 1:num_stages
for scenario in 1:num_scenarios
for block in 1:num_blocks_per_stage[stage]
data = [stage, scenario, block]
Quiver.write!(writer, data; stage, scenario, block)
end
end
end

Quiver.close!(writer)

Quiver.convert(filename, Quiver.csv, Quiver.binary)

reader = Quiver.Reader{Quiver.binary}(filename)
for stage in 1:num_stages
for scenario in 1:num_scenarios
for block in 1:num_blocks_per_stage[stage]
Quiver.goto!(reader; stage, scenario, block)
@test reader.data == [stage, scenario, block]
end
end
end

Quiver.close!(reader)

rm("$filename.$(Quiver.file_extension(Quiver.csv))")
rm("$filename.$(Quiver.file_extension(Quiver.binary))")
rm("$filename.toml")
end

function test_convert()
binary_to_csv()
csv_to_binary()
end

function runtests()
Base.GC.gc()
Base.GC.gc()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
end

TestConvert.runtests()

end
88 changes: 88 additions & 0 deletions test/test_merge.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module TestMerge

using Dates
using Quiver
using Test

function merge_files(impl)
filename = joinpath(@__DIR__, "test_read_write_merge")
num_files = 3
filenames = ["$(filename)_$(i)" for i in 1:num_files]

initial_date = DateTime(2006, 1, 1)
num_stages = 10
num_scenarios = 12
num_blocks = 24
num_time_series = 3

dimensions = ["stage", "scenario", "block"]
time_dimension = "stage"
dimension_size = [num_stages, num_scenarios, num_blocks]

for file in 1:num_files
labels = ["agent_$file"]
writer = Quiver.Writer{impl}(
filenames[file];
dimensions,
labels,
time_dimension,
dimension_size,
initial_date = initial_date
)

for stage in 1:num_stages
for scenario in 1:num_scenarios
for block in 1:num_blocks
data = [stage, scenario, block + scenario][file]
Quiver.write!(writer, [data]; stage, scenario, block)
end
end
end

Quiver.close!(writer)
end

output_filename = joinpath(@__DIR__, "test_read_write_merge_merged")
Quiver.merge(output_filename, filenames, impl)

reader = Quiver.Reader{impl}(output_filename)
for stage in 1:num_stages
for scenario in 1:num_scenarios
for block in 1:num_blocks
Quiver.goto!(reader; stage, scenario, block)
@test reader.data == [stage, scenario, block + scenario]
end
end
end

Quiver.close!(reader)

for filename in filenames
rm("$filename.$(Quiver.file_extension(impl))")
rm("$filename.toml")
end
rm("$output_filename.$(Quiver.file_extension(impl))")
rm("$output_filename.toml")
end

function test_merge()
for impl in Quiver.implementations()
merge_files(impl)
end
end

function runtests()
Base.GC.gc()
Base.GC.gc()
for name in names(@__MODULE__; all = true)
if startswith("$name", "test_")
@testset "$(name)" begin
getfield(@__MODULE__, name)()
end
end
end
end

TestMerge.runtests()

end
Loading

0 comments on commit 8891b8a

Please sign in to comment.