Skip to content

Commit 8e23967

Browse files
test: test new assertions functionality
1 parent b5e6dd9 commit 8e23967

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

test/debugging.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using ModelingToolkit, OrdinaryDiffEq, StochasticDiffEq, SymbolicIndexingInterface
2+
using ModelingToolkit: t_nounits as t, D_nounits as D, ASSERTION_LOG_VARIABLE
3+
4+
@variables x(t)
5+
@brownian a
6+
@named inner_ode = ODESystem(D(x) ~ -sqrt(x), t; assertions = [(x > 0) => "ohno"])
7+
@named inner_sde = System([D(x) ~ -sqrt(x) + a], t; assertions = [(x > 0) => "ohno"])
8+
sys_ode = structural_simplify(inner_ode)
9+
sys_sde = structural_simplify(inner_sde)
10+
11+
@testset "`debug_system` adds assertions" begin
12+
@testset "$(typeof(sys))" for (Problem, sys, alg) in [
13+
(ODEProblem, sys_ode, Tsit5()), (SDEProblem, sys_sde, ImplicitEM())]
14+
dsys = debug_system(sys; functions = [])
15+
@test is_parameter(dsys, ASSERTION_LOG_VARIABLE)
16+
prob = Problem(dsys, [x => 1.0], (0.0, 5.0))
17+
sol = solve(prob, alg)
18+
@test !SciMLBase.successful_retcode(sol)
19+
prob.ps[ASSERTION_LOG_VARIABLE] = true
20+
sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg)
21+
@test !SciMLBase.successful_retcode(sol)
22+
end
23+
end
24+
25+
@testset "Hierarchical system" begin
26+
@testset "$(typeof(inner))" for (ctor, Problem, inner, alg) in [
27+
(ODESystem, ODEProblem, inner_ode, Tsit5()),
28+
(System, SDEProblem, inner_sde, ImplicitEM())]
29+
@mtkbuild outer = ctor(Equation[], t; systems = [inner])
30+
dsys = debug_system(outer; functions = [])
31+
@test is_parameter(dsys, ASSERTION_LOG_VARIABLE)
32+
prob = Problem(dsys, [inner.x => 1.0], (0.0, 5.0))
33+
sol = solve(prob, alg)
34+
@test !SciMLBase.successful_retcode(sol)
35+
prob.ps[ASSERTION_LOG_VARIABLE] = true
36+
sol = @test_logs (:error, r"ohno") match_mode=:any solve(prob, alg)
37+
@test !SciMLBase.successful_retcode(sol)
38+
end
39+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ end
9292
@safetestset "IfLifting Test" include("if_lifting.jl")
9393
@safetestset "Analysis Points Test" include("analysis_points.jl")
9494
@safetestset "Causal Variables Connection Test" include("causal_variables_connection.jl")
95+
@safetestset "Debugging Test" include("debugging.jl")
9596
end
9697
end
9798

0 commit comments

Comments
 (0)