Skip to content

Commit

Permalink
Merge pull request #577 from JuliaIO/restructuretests
Browse files Browse the repository at this point in the history
add testfiles and remove obsolete tests
  • Loading branch information
JonasIsensee authored Jul 28, 2024
2 parents 6493645 + 91dc003 commit 01bdb84
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 218 deletions.
6 changes: 3 additions & 3 deletions test/Artifacts.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[testfiles]
git-tree-sha1 = "ab278bd01e5e230fd6403df8a9f0ecfead8f673f"
git-tree-sha1 = "f258e1c3d2c53482fc7609571540f590c15d8f0f"
lazy = true

[[testfiles.download]]
sha256 = "4db286cb7a53d22368ac9f0305064f1b70e01936d3ec05961d00df3ab660769a"
url = "https://github.com/JonasIsensee/JLD2TestFiles/archive/refs/tags/v0.1.3.tar.gz"
sha256 = "ddf05bb0b3b982959334fcc2dfc1dc64abafae54c3b24343fb51b5db845c6d94"
url = "https://github.com/JonasIsensee/JLD2TestFiles/archive/refs/tags/v0.1.5.tar.gz"
10 changes: 0 additions & 10 deletions test/consistency.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,4 @@ for ioty in [JLD2.MmapIO, IOStream]
close(f)
end

cmd = """
using JLD2
f = jldopen($(repr(fn)), true, true, true, $(ioty))
write(f, "x", 1:10)
"""
run(`$(Base.julia_cmd()) -e $cmd`)

f = jldopen(fn, false, false, false, ioty)
@test read(f, "x") == 1:10
close(f)
end
75 changes: 0 additions & 75 deletions test/functions_and_calls.jl

This file was deleted.

64 changes: 0 additions & 64 deletions test/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,70 +401,6 @@ end
@test load(path, "D") == D
end


@testset "Recoverable changes in structs, Issue #354" begin
tmpdir = mktempdir()
atexit(() -> rm(tmpdir; force = true, recursive = true))

my_object_filename = joinpath(tmpdir, "my_object.jld2")
saving_filename = joinpath(tmpdir, "saving.jl")
loading_filename = joinpath(tmpdir, "loading.jl")

saving_contents = """
append!(Base.LOAD_PATH, $(Base.LOAD_PATH))
unique!(Base.LOAD_PATH)
using JLD2
struct A; x::Int; end
struct B; a::A; end
struct C; x::Int; end
struct D; c::C; end
jldsave("$(my_object_filename)"; b=B(A(42)), d=D(C(42)))
"""

loading_contents = """
append!(Base.LOAD_PATH, $(Base.LOAD_PATH))
unique!(Base.LOAD_PATH)
using JLD2, Test
struct A; x::Float64; end
struct B; a::A; end
b = load("$(my_object_filename)", "b")
@test b == B(A(42.0))
struct C; x::Tuple{Int,Int}; end
struct D; c::C; end
d = load("$(my_object_filename)", "d")
# Reconstructed type has correct value
@test d.c.x == 42
Base.convert(::Type{Tuple{Int,Int}}, x::Int) = (x, 2x)
d = load("$(my_object_filename)", "d")
@test d.c.x == (42, 84)
"""

rm(my_object_filename; force = true, recursive = true)
rm(saving_filename; force = true, recursive = true)
rm(loading_filename; force = true, recursive = true)

if Sys.iswindows()
saving_contents = replace(saving_contents, '\\' => "\\\\")
loading_contents = replace(loading_contents, '\\' => "\\\\")
end

write(saving_filename, saving_contents)
write(loading_filename, loading_contents)

saving_cmd = `$(Base.julia_cmd()) $(saving_filename)`
loading_cmd = `$(Base.julia_cmd()) $(loading_filename)`

rm(my_object_filename; force = true, recursive = true)

@test better_success(saving_cmd)
@test better_success(loading_cmd)

rm(tmpdir; force = true, recursive = true)
end

# Test for saving long NTuples
@testset "Long NTuples" begin
cd(mktempdir()) do
Expand Down
64 changes: 0 additions & 64 deletions test/readas.jl

This file was deleted.

1 change: 0 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ include("customserialization.jl")
include("compression.jl")
include("test_files.jl")
include("unpack_test.jl")
include("readas.jl")
include("dataset_api.jl")
86 changes: 85 additions & 1 deletion test/test_files.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ update Artifacts.toml:
lazy=true)
update artifact string below to reflect new version number
=#
testfiles = artifact"testfiles/JLD2TestFiles-0.1.3/artifacts"
testfiles = artifact"testfiles/JLD2TestFiles-0.1.5/artifacts"

@testset "HDF5 compat test files" begin
# These are test files copied from the HDF5.jl test suite
Expand Down Expand Up @@ -173,4 +173,88 @@ end
data = load(fn, "test")
@test propertynames(data) == (:val,)
@test data.val == 4
end

module RecoverableChangesInStructs
using JLD2, Test
fn = joinpath(Main.testfiles, "recoverable_changes_in structs.jld2")
# for saving:
# struct A; x::Int; end
# struct B; a::A; end

# struct C; x::Int; end
# struct D; c::C; end
# jldsave("$(my_object_filename)"; b=B(A(42)), d=D(C(42)))


struct A; x::Float64; end
struct B; a::A; end

struct C; x::Tuple{Int,Int}; end
struct D; c::C; end
@testset "Recoverable changes in structs, Issue #354" begin
b = load(fn, "b")
@test b == B(A(42.0))

d = load(fn, "d")
# Reconstructed type has correct value
@test d.c.x == 42
# This is evil
#Base.convert(::Type{Tuple{Int,Int}}, x::Int) = (x, 2x)
#d = load(fn, "d")
#@test d.c.x == (42, 84)
end
end



module Readas
using JLD2
struct UndefinedFunction <:Function
fun
end
(f::UndefinedFunction)(args...; kwargs...) = error("The function \$(f.fun) is not defined")

# Case when readas is defined
# If `F` doesn't exist when reading, should be read as `Foo{UndefinedFunction}`
struct Foo{F<:Function}
fun::F
end
struct FooSerialization
fun
end
JLD2.writeas(::Type{<:Foo}) = FooSerialization
Base.convert(::Type{<:FooSerialization}, f::Foo) = FooSerialization(f.fun)

JLD2.readas(::Type{<:FooSerialization}) = Foo
function Base.convert(::Type{<:Foo}, f::FooSerialization)
isa(f.fun, Function) && return Foo(f.fun)
return Foo(UndefinedFunction(f.fun))
end

# Case when readas is not defined (N)
# If `F` doesn't exist when reading, should be read as `FooNSerialization`
struct FooN{F<:Function}
fun::F
end
struct FooNSerialization
fun
end
JLD2.writeas(::Type{<:FooN}) = FooNSerialization
Base.convert(::Type{<:FooNSerialization}, f::FooN) = FooNSerialization(f.fun)
Base.convert(::Type{<:FooN}, f::FooNSerialization) = FooN(f.fun)
end

# jldsave("readas_foo_sin.jld2"; foo=Readas.Foo(sin))
# fun(x) = x^2
# jldsave("readas_foo_a.jld2"; foo=Readas.Foo(fun))
# jldsave("readas_foo_n_sin.jld2"; foo=Readas.FooN(sin))
# jldsave("readas_foo_n_a.jld2"; foo=Readas.FooN(fun))

@testset "readas api for struct upgrades" begin
getfoo(file) = jldopen(joinpath(testfiles,file)) do io; io["foo"]; end
@test getfoo("readas_foo_sin.jld2") isa Readas.Foo{typeof(sin)}
@test getfoo("readas_foo_n_sin.jld2") isa Readas.FooN{typeof(sin)}
@test getfoo("readas_foo_a.jld2") isa Readas.Foo{Readas.UndefinedFunction}
@test getfoo("readas_foo_n_a.jld2") isa Readas.FooNSerialization
end

0 comments on commit 01bdb84

Please sign in to comment.