Skip to content

Commit e753ad1

Browse files
committed
Split tests into multiple files
1 parent e6ce4a8 commit e753ad1

File tree

4 files changed

+416
-413
lines changed

4 files changed

+416
-413
lines changed

test/items/function.jl

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Function form tests
2+
3+
@testitem "cache save and load" begin
4+
using BSON, JLD2, Dates
5+
mktempdir(@__DIR__; prefix = "temp_") do dirpath
6+
@testset "$ext" for ext in ["bson", "jld2"]
7+
path = joinpath(dirpath, "functest.$ext")
8+
9+
# 1. Verify log messages for saving
10+
log = (:info, r"^Saved cached values to .+\.")
11+
@test_logs log cache(path) do
12+
x = collect(1:3)
13+
y = 4
14+
z = "test"
15+
return (; x = x, y = y, z = z)
16+
end
17+
18+
# 2. Delete cache and run again
19+
rm(path)
20+
out = cache(path) do
21+
x = collect(1:3)
22+
y = 4
23+
z = "test"
24+
return (; x = x, y = y, z = z)
25+
end
26+
27+
# 3. Verify the output
28+
@test out == (; x = [1, 2, 3], y = 4, z = "test")
29+
30+
# 4. Reset the output
31+
out = nothing
32+
33+
# 5. Verify log messages for loading
34+
log = (:info, r"^Loaded cached values from .+\.")
35+
@test_logs log cache(path) do
36+
x = collect(1:3)
37+
y = 4
38+
z = "test"
39+
return (; x = x, y = y, z = z)
40+
end
41+
42+
# 6. Load the output
43+
out = cache(path) do
44+
x = collect(1:3)
45+
y = 4
46+
z = "test"
47+
return (; x = x, y = y, z = z)
48+
end
49+
50+
# 7. Verify the output
51+
@test out == (; x = [1, 2, 3], y = 4, z = "test")
52+
53+
# 8. Verify the metadata
54+
if ext == "bson"
55+
data = BSON.load(path)
56+
version = data[:version]
57+
whenrun = data[:whenrun]
58+
runtime = data[:runtime]
59+
else
60+
data = JLD2.load(path)
61+
version = data["version"]
62+
whenrun = data["whenrun"]
63+
runtime = data["runtime"]
64+
end
65+
@test version isa VersionNumber
66+
@test whenrun isa Dates.DateTime
67+
@test runtime isa Real && runtime >= 0
68+
end
69+
end
70+
end
71+
72+
@testitem "cache with path == nothing" begin
73+
out = @test_logs cache(nothing) do
74+
x = collect(1:3)
75+
y = 4
76+
z = "test"
77+
return (; x = x, y = y, z = z)
78+
end
79+
@test out == (; x = [1, 2, 3], y = 4, z = "test")
80+
end

test/items/general.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# General tests
2+
3+
@testitem "unsupported file extensions" begin
4+
mktempdir(@__DIR__; prefix = "temp_") do dirpath
5+
badpath = joinpath(dirpath, "test.mat")
6+
7+
# Test with function form
8+
@test_throws ArgumentError cache(badpath) do
9+
return 42
10+
end
11+
12+
# Test with macro form
13+
@test_throws ArgumentError @cache badpath begin
14+
x = 1
15+
end
16+
end
17+
end
18+
19+
# Motivated by Pluto and based on test case from:
20+
# https://github.com/JuliaIO/BSON.jl/issues/25
21+
@testitem "@cache/cache in a module" begin
22+
module MyModule
23+
using CacheVariables, Test, DataFrames
24+
25+
mktempdir(@__DIR__; prefix = "temp_") do dirpath
26+
@testset "$ext" for ext in ["bson", "jld2"]
27+
modpath = joinpath(dirpath, "modtest.$ext")
28+
29+
# 1. Save and check that variables entered workspace correctly
30+
out = @cache modpath begin
31+
d = DataFrame(; a = 1:10, b = 'a':'j')
32+
"final output"
33+
end
34+
@test d == DataFrame(; a = 1:10, b = 'a':'j')
35+
@test out == "final output"
36+
37+
# 2. Reset the variables
38+
d = out = nothing
39+
40+
# 3. Load and check that variables entered workspace correctly
41+
out = @cache modpath begin
42+
d = DataFrame(; a = 1:10, b = 'a':'j')
43+
"final output"
44+
end
45+
@test d == DataFrame(; a = 1:10, b = 'a':'j')
46+
@test out == "final output"
47+
48+
# 4. Reset the variables and delete the file
49+
d = out = nothing
50+
rm(modpath)
51+
52+
# 5. Save and check the output
53+
out = cache(modpath; bson_mod = @__MODULE__) do
54+
return DataFrame(; a = 1:10, b = 'a':'j')
55+
end
56+
@test out == DataFrame(; a = 1:10, b = 'a':'j')
57+
58+
# 6. Reset the output
59+
out = nothing
60+
61+
# 7. Load and check the output
62+
out = cache(modpath; bson_mod = @__MODULE__) do
63+
return DataFrame(; a = 1:10, b = 'a':'j')
64+
end
65+
@test out == DataFrame(; a = 1:10, b = 'a':'j')
66+
end
67+
end
68+
end
69+
end

0 commit comments

Comments
 (0)