Skip to content

Commit

Permalink
bug(datasets): keychecker fails to check some wrong arguments (#193)
Browse files Browse the repository at this point in the history
* semver(datasets): v0.1.1

Closes #186

* ci(sdt): add the keychecker unit test

* ci(sdt): add the test for the wrong layer argument

* bug(datasets): correctly catches keywords that are not supported

* ci(datasets): fix the unit test to catch the correct error

* bug(datasets): fix a typo in the unit test error message
  • Loading branch information
tpoisot authored May 14, 2023
1 parent 385831f commit dd9248f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion SimpleSDMDatasets/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SimpleSDMDatasets"
uuid = "2c7d61d0-5c73-410d-85b2-d2e7fbbdcefa"
authors = ["Timothée Poisot <timothee.poisot@umontreal.ca>"]
version = "0.1.1"
version = "0.1.2"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
33 changes: 26 additions & 7 deletions SimpleSDMDatasets/src/keychecker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ function keychecker(data::R; kwargs...) where {R <: RasterData}
# Check for allowed extra keys
for k in keys(kwargs)
if ~(k in [:month, :layer, :resolution])
if isnothing(extrakeys(data))
error("The keyword argument $(k) is not supported by the $(R) dataset")
end
if k in keys(extrakeys(data))
if ~(values(kwargs)[k] in extrakeys(data)[k])
error(
Expand All @@ -48,44 +51,56 @@ function keychecker(data::R; kwargs...) where {R <: RasterData}
end
end
end

return nothing
end

function keychecker(data::R, future::F; kwargs...) where {R <: RasterData, F <: Projection}

@assert SimpleSDMDatasets.provides(data, future)

# Check for month
if :month in keys(kwargs)
if isnothing(months(data, future))
error("The $(R) dataset does not allow for month as a keyword argument under $(F)")
error(
"The $(R) dataset does not allow for month as a keyword argument under $(F)",
)
end
if ~(values(kwargs).month in months(data))
error("The month $(values(kwargs).month) is not supported by the $(R) dataset under $(F)")
error(
"The month $(values(kwargs).month) is not supported by the $(R) dataset under $(F)",
)
end
end

# Check for timespan
if :timespan in keys(kwargs)
if isnothing(timespans(data, future))
error("The $(R) dataset does not allow for timespan as a keyword argument under $(F)")
error(
"The $(R) dataset does not allow for timespan as a keyword argument under $(F)",
)
end
if ~(values(kwargs).timespan in timespans(data, future))
error("The timespan $(values(kwargs).month) is not supported by the $(R) dataset under $(F)")
error(
"The timespan $(values(kwargs).month) is not supported by the $(R) dataset under $(F)",
)
end
end

# Check for layer
if :layer in keys(kwargs)
if isnothing(layers(data))
error("The $(R) dataset does not allow for layer as a keyword argument under $(F)")
error(
"The $(R) dataset does not allow for layer as a keyword argument under $(F)",
)
end
if values(kwargs).layer isa Integer
if ~(1 <= values(kwargs).layer <= length(layers(data)))
error("The $(R) dataset only has $(length(layers(data))) layers under $(F)")
end
elseif ~(values(kwargs).layer in layers(data))
error("The layer $(values(kwargs).layer) is not supported by the $(R) dataset under $(F)")
error(
"The layer $(values(kwargs).layer) is not supported by the $(R) dataset under $(F)",
)
end
end

Expand All @@ -104,6 +119,9 @@ function keychecker(data::R, future::F; kwargs...) where {R <: RasterData, F <:
# Check for allowed extra keys
for k in keys(kwargs)
if ~(k in [:month, :layer, :resolution, :timespan])
if isnothing(extrakeys(data))
error("The keyword argument $(k) is not supported by the $(R) dataset")
end
if k in keys(extrakeys(data))
if ~(values(kwargs)[k] in extrakeys(data)[k])
error(
Expand All @@ -113,5 +131,6 @@ function keychecker(data::R, future::F; kwargs...) where {R <: RasterData, F <:
end
end
end

return nothing
end
9 changes: 9 additions & 0 deletions test/edgecases/03_layers_keycheck.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module TestThatWrongKeywordsAreCaught

using SpeciesDistributionToolkit
using Test

provider = RasterData(WorldClim2, BioClim)
@test_throws "The keyword argument layers is not" SimpleSDMPredictor(provider; layers=3)

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tests = [
"read/write layers" => "01_integration_read.jl",
"EDGE: stitch bounding box" => "edgecases/01_stitch_wrong_bb.jl",
"EDGE: clip & GDAL" => "edgecases/02_clip_gdalwarp.jl",
"EDGE: keychecker" => "edgecases/03_layers_keycheck.jl",
]

for test in tests
Expand Down

2 comments on commit dd9248f

@tpoisot
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register subdir=SimpleSDMDatasets

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/83564

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a SimpleSDMDatasets-v0.1.2 -m "<description of version>" dd9248ffcaabc21c712519ac5723eaaf83b5dbd7
git push origin SimpleSDMDatasets-v0.1.2

Please sign in to comment.