Skip to content

Commit

Permalink
Error messages for invalid NodeID (#1826)
Browse files Browse the repository at this point in the history
Fixes #1824

---------

Co-authored-by: Martijn Visser <[email protected]>
  • Loading branch information
Jingru923 and visr committed Sep 19, 2024
1 parent e2a66ab commit 87f535f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions core/src/parameter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ function NodeID(type::NodeType.T, value::Integer, db::DB)::NodeID
),
),
)
@assert idx > 0
if idx <= 0
error("Node ID #$value of type $type is not in the Node table.")
end
return NodeID(type, value, idx)
end

Expand All @@ -72,7 +74,9 @@ function NodeID(value::Integer, db::DB)::NodeID
db,
"SELECT COUNT(*), node_type FROM Node WHERE node_type == (SELECT node_type FROM Node WHERE node_id == $value) AND node_id <= $value",
)
@assert only(idx) > 0
if only(idx) <= 0
error("Node ID #$value is not in the Node table.")
end
return NodeID(only(type), value, only(idx))
end

Expand Down
25 changes: 25 additions & 0 deletions core/test/validation_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,28 @@ end
@test logger.logs[3].message ==
"Missing priority parameter(s) for a FlowDemand / static node in the allocation problem."
end

@testitem "Node ID not in Node table" begin
using Ribasim
import SQLite
using Logging

toml_path = normpath(@__DIR__, "../../generated_testmodels/basic/ribasim.toml")

cfg = Ribasim.Config(toml_path)
db_path = Ribasim.input_path(cfg, cfg.database)
db = SQLite.DB(db_path)

logger = TestLogger()
with_logger(logger) do
@test_throws "Node ID #1 of type PidControl is not in the Node table." Ribasim.NodeID(
:PidControl,
1,
db,
)
end

with_logger(logger) do
@test_throws "Node ID #20 is not in the Node table." Ribasim.NodeID(20, db)
end
end

0 comments on commit 87f535f

Please sign in to comment.