Skip to content

Commit 72f05e3

Browse files
committed
shorten tests
1 parent 9568f50 commit 72f05e3

File tree

3 files changed

+25
-179
lines changed

3 files changed

+25
-179
lines changed

src/external_files.jl

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/links.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,25 @@ Base.:(==)(a::Link, b::Link) =
8282
a.offset == b.offset && a.path == b.path && a.external_file == b.external_file
8383

8484
Base.hash(link::Link, h::UInt) = hash((link.offset, link.path, link.external_file), h)
85+
86+
"""
87+
load_external_dataset(current_file::JLDFile, link::Link)
88+
89+
Load a dataset from an external file referenced by an external link.
90+
"""
91+
function load_external_dataset(current_file, link::Link)
92+
external_file = normpath(joinpath(dirname(current_file.path), link.external_file))
93+
isfile(external_file) || throw(ArgumentError("External file not found: $external_file"))
94+
95+
# Check if file is already open
96+
lock(OPEN_FILES_LOCK) do
97+
if external_file in keys(OPEN_FILES)
98+
existing_file = OPEN_FILES[external_file].value
99+
!isnothing(existing_file) && return existing_file[link.path]
100+
end
101+
end
102+
103+
jldopen(external_file, "r") do f
104+
f[link.path]
105+
end
106+
end

test/links.jl

Lines changed: 3 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -91,39 +91,6 @@ using JLD2, Test
9191
end
9292
end
9393

94-
@testset "Group Link Storage" begin
95-
@testset "Basic Link Operations" begin
96-
# Create a temporary test file
97-
fn = joinpath(mktempdir(), "test_links.jld2")
98-
99-
jldopen(fn, "w") do f
100-
# Test that groups now use Link storage
101-
@test eltype(f.root_group.unwritten_links) == Pair{String, JLD2.Link}
102-
@test eltype(f.root_group.written_links) == Pair{String, JLD2.Link}
103-
104-
# Test lookup_link function
105-
@test JLD2.lookup_link(f.root_group, "nonexistent") === nothing
106-
107-
# Test setting a link directly
108-
offset = JLD2.RelOffset(UInt64(1000))
109-
hard_link = JLD2.Link(offset)
110-
f.root_group["test_hardlink"] = hard_link
111-
112-
retrieved_link = JLD2.lookup_link(f.root_group, "test_hardlink")
113-
@test retrieved_link !== nothing
114-
@test JLD2.is_hard_link(retrieved_link)
115-
@test retrieved_link.offset == offset
116-
117-
# Test lookup_link functionality
118-
link = JLD2.lookup_link(f.root_group, "test_hardlink")
119-
@test link !== nothing && JLD2.is_hard_link(link) && link.offset == offset
120-
@test JLD2.lookup_link(f.root_group, "nonexistent") === nothing
121-
end
122-
end
123-
end
124-
125-
using JLD2, Test
126-
12794
@testset "Phase 2: External Link Creation API" begin
12895

12996
jldopen("test_external.jld2", "w") do f
@@ -147,76 +114,12 @@ using JLD2, Test
147114
f["soft_link_to_data"] = JLD2.Link("/original_data")
148115
end
149116

150-
# Step 3: Test group internal state
151-
@testset "Group State Management" begin
152-
jldopen("test_main.jld2", "r") do f
153-
# After file is written and closed, all links should be in written_links
154-
root = f.root_group
155-
@test isempty(root.unwritten_links)
156-
@test !isempty(root.written_links)
157-
158-
# Check that external links are properly stored
159-
@test haskey(root.written_links, "external_data")
160-
@test haskey(root.written_links, "direct_external")
161-
@test haskey(root.written_links, "soft_link_to_data")
162-
163-
# Check link types
164-
@test JLD2.is_external_link(root.written_links["external_data"])
165-
@test JLD2.is_external_link(root.written_links["direct_external"])
166-
@test JLD2.is_soft_link(root.written_links["soft_link_to_data"])
167-
168-
# Test that original data is a hard link
169-
@test JLD2.is_hard_link(root.written_links["original_data"])
170-
end
171-
end
172-
173-
# Step 4: Test link properties
174-
@testset "Link Properties" begin
175-
jldopen("test_main.jld2", "r") do f
176-
root = f.root_group
177-
178-
# Test external link properties
179-
ext_link = root.written_links["external_data"]
180-
@test ext_link.external_file == "test_external.jld2"
181-
@test ext_link.path == "/dataset1"
182-
183-
# Test soft link properties
184-
soft_link = root.written_links["soft_link_to_data"]
185-
@test soft_link.path == "/original_data"
186-
end
187-
end
188-
189-
@testset "Link Lookup Functions" begin
190-
jldopen("test_main.jld2", "r") do f
191-
root = f.root_group
192-
193-
# Test lookup_link function
194-
ext_link = JLD2.lookup_link(root, "external_data")
195-
@test JLD2.is_external_link(ext_link)
196-
@test ext_link.external_file == "test_external.jld2"
197-
198-
# Test that lookup_link works for hard links and returns correct link types
199-
hard_link = JLD2.lookup_link(root, "original_data")
200-
@test hard_link !== nothing && JLD2.is_hard_link(hard_link)
201-
202-
# Test that lookup_link returns the correct link type for external links
203-
ext_link_direct = JLD2.lookup_link(root, "external_data")
204-
@test ext_link_direct !== nothing && JLD2.is_external_link(ext_link_direct)
205-
end
206-
end
207-
208117
# Clean up test files
209-
for file in ["test_main.jld2", "test_external.jld2", "test_chain.jld2"]
118+
for file in ["test_main.jld2", "test_external.jld2"]
210119
isfile(file) && rm(file)
211120
end
212121
end
213122

214-
215-
# Phase 4: Advanced Error Handling & Edge Cases Test Suite
216-
# Tests for sophisticated circular reference detection, error recovery,
217-
# permission-based access control, and enhanced error handling
218-
219-
using JLD2, Test
220123
using JLD2: UnsupportedFeatureException
221124

222125
@testset "Soft Link Support" begin
@@ -230,41 +133,13 @@ using JLD2: UnsupportedFeatureException
230133
# Create hierarchical structure
231134
data_group = JLD2.Group(f, "data")
232135
measurements_group = JLD2.Group(data_group, "measurements")
233-
calibration_group = JLD2.Group(data_group, "calibration")
234-
results_group = JLD2.Group(f, "results")
235-
analysis_group = JLD2.Group(results_group, "analysis")
236-
237-
# Add some test data
238136
f["data/measurements/temperature"] = [20.5, 21.0, 19.8, 22.1]
239-
f["data/measurements/pressure"] = [101.3, 101.1, 101.5]
240-
f["data/calibration/offset"] = 0.5
241-
f["results/summary"] = "Test complete"
242-
243-
# Test absolute soft links (fully supported)
244-
measurements_group["abs_link_to_offset"] = JLD2.Link("/data/calibration/offset")
245-
analysis_group["abs_link_to_temp"] = JLD2.Link("/data/measurements/temperature")
246-
f.root_group["abs_link_to_summary"] = JLD2.Link("/results/summary")
247-
248-
# Test simple relative soft links (fully supported)
249-
measurements_group["rel_link_to_temp"] = JLD2.Link("temperature")
250-
measurements_group["rel_link_to_pressure"] = JLD2.Link("pressure")
251-
252-
# Test subdirectory relative navigation (fully supported)
137+
data_group["abs_link_to_temp"] = JLD2.Link("/data/measurements/temperature")
253138
data_group["rel_link_to_meas_temp"] = JLD2.Link("measurements/temperature")
254139
end
255140

256-
# Test reading through soft links
257141
jldopen(test_file, "r") do f
258-
# Test absolute soft link resolution
259-
@test f["data/measurements/abs_link_to_offset"] == 0.5
260-
@test f["results/analysis/abs_link_to_temp"] == [20.5, 21.0, 19.8, 22.1]
261-
@test f["abs_link_to_summary"] == "Test complete"
262-
263-
# Test simple relative soft link resolution
264-
@test f["data/measurements/rel_link_to_temp"] == [20.5, 21.0, 19.8, 22.1]
265-
@test f["data/measurements/rel_link_to_pressure"] == [101.3, 101.1, 101.5]
266-
267-
# Test subdirectory relative navigation
142+
@test f["data/abs_link_to_temp"] == [20.5, 21.0, 19.8, 22.1]
268143
@test f["data/rel_link_to_meas_temp"] == [20.5, 21.0, 19.8, 22.1]
269144
end
270145
end
@@ -293,26 +168,4 @@ using JLD2: UnsupportedFeatureException
293168
end
294169
end
295170
end
296-
297-
@testset "Soft Link Display and Inspection" begin
298-
# Test that soft links display correctly in group listings
299-
mktempdir() do dir
300-
test_file = joinpath(dir, "soft_link_display_test.jld2")
301-
302-
jldopen(test_file, "w") do f
303-
data_group = JLD2.Group(f, "data")
304-
f["data/original"] = [1, 2, 3, 4, 5]
305-
306-
# Create various link types for display testing
307-
data_group["soft_link"] = JLD2.Link("/data/original")
308-
f["data/hard_link"] = f["data/original"] # Hard link
309-
310-
# The display function should work without errors
311-
# Note: The exact display format may vary, so we just test that it doesn't error
312-
display_output = sprint(show, data_group)
313-
@test !isempty(display_output)
314-
@test isa(display_output, String)
315-
end
316-
end
317-
end
318171
end

0 commit comments

Comments
 (0)