Skip to content

Commit

Permalink
Bugfix: error if invalid Parameter name in .yaml file (#24)
Browse files Browse the repository at this point in the history
* Bugfix: error if invalid Parameter name in .yaml file

* add test for invalid Parameter

* better error message printing
  • Loading branch information
sjdaines authored May 18, 2022
1 parent 886b858 commit 779d035
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "PALEOboxes"
uuid = "804b410e-d900-4b2a-9ecd-f5a06d4c1fd4"
authors = ["Stuart Daines <[email protected]>"]
version = "0.18.4"
version = "0.18.5"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Expand Down
15 changes: 12 additions & 3 deletions src/Reaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ function create_reaction_from_config(

# set parameters
allpars = get_parameters(newreaction)
conf_parameters = get(conf_reaction, "parameters", Dict{Any,Any}())
conf_parameters = copy(get(conf_reaction, "parameters", Dict{Any,Any}()))
for par in allpars
rawvalue = par.v
par_modified = false
Expand All @@ -370,7 +370,7 @@ function create_reaction_from_config(
par_modified = true
end
if !isnothing(conf_parameters) && haskey(conf_parameters, par.name) # empty 'parameters:' will return nothing
rawvalue = conf_parameters[par.name]
rawvalue = pop!(conf_parameters, par.name)
par_modified = true
end
par_modstr = ("[Default] ", "[config.yaml] ")[1+par_modified]
Expand All @@ -384,7 +384,16 @@ function create_reaction_from_config(
setvalue!(par, value)
end
end


if !isempty(conf_parameters)
io = IOBuffer()
write(io, "reaction $(fullname(newreaction)) has no Parameter(s):\n")
for (k, v) in conf_parameters
write(io, " $k: $v\n")
end
error(String(take!(io)))
end

# Read Variable configuration
# These are applied later by _configure_variables! after Variables are created
newreaction.base._conf_variable_links = get(conf_reaction, "variable_links", nothing)
Expand Down
21 changes: 21 additions & 0 deletions test/configbase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,24 @@ model_with_loop:
oceanfloor:
reactions:


# two reactions that depend on each other - sort_reactions! should fail
model_with_invalid_parameter:
domains:
ocean:

reactions:
julia_paleo_mock:
class: ReactionPaleoMock
parameters:
par1: 0.15 # double parameter
misspelt_par: something # no Parameter with this name
variable_links:
phy*: mock_phy*

oceansurface:
reactions:

oceanfloor:
reactions:

10 changes: 8 additions & 2 deletions test/runbasetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using BenchmarkTools

include("ReactionPaleoMockModule.jl")

@testset "PB.jl base" begin
@testset "PALEOboxes base" begin
model = PB.create_model_from_config("configbase.yaml", "model1")

PB.show_methods_setup(model)
Expand Down Expand Up @@ -90,8 +90,14 @@ include("ReactionPaleoMockModule.jl")
end


@testset "PB.jl loop" begin
@testset "PALEOboxes base loop" begin

@test_throws ErrorException("The input graph contains at least one loop.") PB.create_model_from_config("configbase.yaml", "model_with_loop")

end

@testset "PALEOboxes base invalid parameter name" begin

@test_throws ErrorException("reaction ocean.julia_paleo_mock has no Parameter(s):\n misspelt_par: something\n") PB.create_model_from_config("configbase.yaml", "model_with_invalid_parameter")

end

2 comments on commit 779d035

@sjdaines
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()

@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/60482

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 v0.18.5 -m "<description of version>" 779d03549e4ef644e8b2793c7aa976ed029bcedc
git push origin v0.18.5

Please sign in to comment.