Skip to content

Commit 6a1d250

Browse files
add tests
1 parent dca9606 commit 6a1d250

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

test/test_cdm.jl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using CommonDataModel: iswritable, attribnames, parentdataset, load!, dataset
2+
using Dates
3+
using DiskArrays
4+
using NCDatasets
5+
using Test
6+
using ZarrDatasets
7+
8+
9+
fname = tempname()
10+
mkpath(fname)
11+
12+
nczarr_name = "file://" * fname * "#mode=zarr"
13+
@debug "filenames " nczarr_name fname
14+
v = randn(2,3)
15+
ds = NCDataset(nczarr_name,"c")
16+
defVar(ds,"var",v,("lon","lat"),attrib = Dict(
17+
"foo" => "bar",
18+
"int_attribute" => 1,
19+
"float_attribute" => 1.,
20+
"scale_factor" => 1.23))
21+
ds.attrib["title"] = "test file"
22+
close(ds)
23+
24+
ds = NCDataset(nczarr_name)
25+
dsz = ZarrDataset(fname)
26+
27+
@test Set(dimnames(dsz)) == Set(dimnames(ds))
28+
29+
for (name,len) in ds.dim
30+
@test dsz.dim[name] == len
31+
end
32+
33+
for (varname,v) in ds
34+
@test haskey(dsz,varname)
35+
36+
v2 = dsz[varname]
37+
@test Array(v2) == Array(v)
38+
39+
for (attribname,attribval) in v.attrib
40+
@test v2.attrib[attribname] == attribval
41+
end
42+
end
43+
44+
for (attribname,attribval) in ds.attrib
45+
@test dsz.attrib[attribname] == attribval
46+
end
47+
48+
io = IOBuffer()
49+
show(io,dsz)
50+
str = String(take!(io))
51+
@test occursin("title",str)
52+
53+
@test !iswritable(dsz)
54+
@test "title" in attribnames(dsz)
55+
@test isnothing(parentdataset(dsz))
56+
57+
zvar = ZarrDataset(fname) do ds3
58+
Array(ds3["var"])
59+
end
60+
61+
@test DiskArrays.haschunks(dsz["var"]) == DiskArrays.Chunked()
62+
@test length(DiskArrays.eachchunk(dsz["var"])) 1
63+
@test zvar == Array(ds["var"])
64+
65+
v = dsz["var"].var
66+
buffer = zeros(eltype(v),size(v))
67+
load!(v,buffer,:,:)
68+
69+
@test buffer == Array(ds["var"].var)
70+
71+
@test dataset(dsz["var"]) == dsz
72+
close(ds)
73+
close(dsz)

test/test_multifile.jl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using CommonDataModel: iswritable, attribnames, parentdataset, load!, dataset, MFDataset
2+
using Dates
3+
using DiskArrays
4+
using NCDatasets
5+
using Test
6+
using ZarrDatasets
7+
8+
9+
fnames = [tempname(), tempname()]
10+
v = randn(2,3,length(fnames))
11+
12+
nczarr_names = ["file://" * fname * "#mode=zarr" for fname in fnames]
13+
14+
for i = 1:length(fnames)
15+
mkpath(fnames[i])
16+
ds = NCDataset(nczarr_names[i],"c")
17+
defVar(ds,"var",v[:,:,i:i],("lon","lat","time"),attrib = Dict(
18+
"foo" => "bar",
19+
"int_attribute" => 1,
20+
"float_attribute" => 1.,
21+
"scale_factor" => 1.23))
22+
ds.attrib["title"] = "test file"
23+
close(ds)
24+
end
25+
26+
ds = NCDataset(nczarr_names,aggdim="time")
27+
dsz = ZarrDataset(fnames,aggdim="time")
28+
29+
@test Set(dimnames(dsz)) == Set(dimnames(ds))
30+
31+
for (name,len) in ds.dim
32+
@test dsz.dim[name] == len
33+
end
34+
35+
for (varname,v) in ds
36+
# @test haskey(dsz,varname)
37+
38+
v2 = dsz[varname]
39+
@test Array(v2) == Array(v)
40+
41+
for (attribname,attribval) in v.attrib
42+
@test v2.attrib[attribname] == attribval
43+
end
44+
end
45+
46+
for (attribname,attribval) in ds.attrib
47+
@test dsz.attrib[attribname] == attribval
48+
end
49+
50+
io = IOBuffer()
51+
show(io,dsz)
52+
str = String(take!(io))
53+
@test occursin("title",str)
54+
55+
@test !iswritable(dsz)
56+
@test "title" in attribnames(dsz)
57+
#@test isnothing(parentdataset(dsz))
58+
59+
zvar = ZarrDataset(fname) do ds3
60+
Array(ds3["var"])
61+
end
62+
63+
#=
64+
65+
#@test DiskArrays.haschunks(dsz["var"]) == DiskArrays.Chunked()
66+
@test length(DiskArrays.eachchunk(dsz["var"])) ≥ 1
67+
@test zvar == Array(ds["var"])
68+
69+
v = dsz["var"].var
70+
buffer = zeros(eltype(v),size(v))
71+
load!(v,buffer,:,:)
72+
73+
@test buffer == Array(ds["var"].var)
74+
75+
@test dataset(dsz["var"]) == dsz
76+
close(ds)
77+
close(dsz)
78+
=#

0 commit comments

Comments
 (0)