Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Barth committed Feb 15, 2024
1 parent dca9606 commit 6a1d250
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
73 changes: 73 additions & 0 deletions test/test_cdm.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using CommonDataModel: iswritable, attribnames, parentdataset, load!, dataset
using Dates
using DiskArrays
using NCDatasets
using Test
using ZarrDatasets


fname = tempname()
mkpath(fname)

nczarr_name = "file://" * fname * "#mode=zarr"
@debug "filenames " nczarr_name fname
v = randn(2,3)
ds = NCDataset(nczarr_name,"c")
defVar(ds,"var",v,("lon","lat"),attrib = Dict(
"foo" => "bar",
"int_attribute" => 1,
"float_attribute" => 1.,
"scale_factor" => 1.23))
ds.attrib["title"] = "test file"
close(ds)

ds = NCDataset(nczarr_name)
dsz = ZarrDataset(fname)

@test Set(dimnames(dsz)) == Set(dimnames(ds))

for (name,len) in ds.dim
@test dsz.dim[name] == len
end

for (varname,v) in ds
@test haskey(dsz,varname)

v2 = dsz[varname]
@test Array(v2) == Array(v)

for (attribname,attribval) in v.attrib
@test v2.attrib[attribname] == attribval
end
end

for (attribname,attribval) in ds.attrib
@test dsz.attrib[attribname] == attribval
end

io = IOBuffer()
show(io,dsz)
str = String(take!(io))
@test occursin("title",str)

@test !iswritable(dsz)
@test "title" in attribnames(dsz)
@test isnothing(parentdataset(dsz))

zvar = ZarrDataset(fname) do ds3
Array(ds3["var"])
end

@test DiskArrays.haschunks(dsz["var"]) == DiskArrays.Chunked()
@test length(DiskArrays.eachchunk(dsz["var"])) 1
@test zvar == Array(ds["var"])

v = dsz["var"].var
buffer = zeros(eltype(v),size(v))
load!(v,buffer,:,:)

@test buffer == Array(ds["var"].var)

@test dataset(dsz["var"]) == dsz
close(ds)
close(dsz)
78 changes: 78 additions & 0 deletions test/test_multifile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using CommonDataModel: iswritable, attribnames, parentdataset, load!, dataset, MFDataset
using Dates
using DiskArrays
using NCDatasets
using Test
using ZarrDatasets


fnames = [tempname(), tempname()]
v = randn(2,3,length(fnames))

nczarr_names = ["file://" * fname * "#mode=zarr" for fname in fnames]

for i = 1:length(fnames)
mkpath(fnames[i])
ds = NCDataset(nczarr_names[i],"c")
defVar(ds,"var",v[:,:,i:i],("lon","lat","time"),attrib = Dict(
"foo" => "bar",
"int_attribute" => 1,
"float_attribute" => 1.,
"scale_factor" => 1.23))
ds.attrib["title"] = "test file"
close(ds)
end

ds = NCDataset(nczarr_names,aggdim="time")
dsz = ZarrDataset(fnames,aggdim="time")

@test Set(dimnames(dsz)) == Set(dimnames(ds))

for (name,len) in ds.dim
@test dsz.dim[name] == len
end

for (varname,v) in ds
# @test haskey(dsz,varname)

v2 = dsz[varname]
@test Array(v2) == Array(v)

for (attribname,attribval) in v.attrib
@test v2.attrib[attribname] == attribval
end
end

for (attribname,attribval) in ds.attrib
@test dsz.attrib[attribname] == attribval
end

io = IOBuffer()
show(io,dsz)
str = String(take!(io))
@test occursin("title",str)

@test !iswritable(dsz)
@test "title" in attribnames(dsz)
#@test isnothing(parentdataset(dsz))

zvar = ZarrDataset(fname) do ds3
Array(ds3["var"])
end

#=
#@test DiskArrays.haschunks(dsz["var"]) == DiskArrays.Chunked()
@test length(DiskArrays.eachchunk(dsz["var"])) ≥ 1
@test zvar == Array(ds["var"])
v = dsz["var"].var
buffer = zeros(eltype(v),size(v))
load!(v,buffer,:,:)
@test buffer == Array(ds["var"].var)
@test dataset(dsz["var"]) == dsz
close(ds)
close(dsz)
=#

0 comments on commit 6a1d250

Please sign in to comment.