Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error messages for invalid NodeID #1826

Merged
merged 8 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -473,3 +473,28 @@ end
@test logger.logs[3].message ==
"Missing priority parameter(s) for a FlowDemand / static node in the allocation problem."
end

@testitem "Invalid NodeID index" begin
visr marked this conversation as resolved.
Show resolved Hide resolved
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
Loading