|
| 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 |
0 commit comments