Skip to content

Commit

Permalink
Re-add some minimal tests for deprecated @submodel
Browse files Browse the repository at this point in the history
  • Loading branch information
penelopeysm committed Dec 13, 2024
1 parent cda42f0 commit a987195
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
23 changes: 1 addition & 22 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,28 +382,7 @@ module Issue537 end
@test demo2()() == 42
end

@testset "@submodel is deprecated" begin
@model inner() = x ~ Normal()
@model outer() = @submodel x = inner()
@test_logs(
(
:warn,
"`@submodel model` and `@submodel prefix=... model` are deprecated; see `to_submodel` for the up-to-date syntax.",
),
outer()()
)

@model outer_with_prefix() = @submodel prefix = "sub" x = inner()
@test_logs(
(
:warn,
"`@submodel model` and `@submodel prefix=... model` are deprecated; see `to_submodel` for the up-to-date syntax.",
),
outer_with_prefix()()
)
end

@testset "submodel" begin
@testset "to_submodel" begin
# No prefix, 1 level.
@model function demo1(x)
return x ~ Normal()
Expand Down
57 changes: 57 additions & 0 deletions test/deprecated.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@testset "deprecated" begin
@testset "@submodel" begin
@testset "is deprecated" begin
@model inner() = x ~ Normal()
@model outer() = @submodel x = inner()
@test_logs(
(
:warn,
"`@submodel model` and `@submodel prefix=... model` are deprecated; see `to_submodel` for the up-to-date syntax.",
),
outer()()
)

@model outer_with_prefix() = @submodel prefix = "sub" x = inner()
@test_logs(
(
:warn,
"`@submodel model` and `@submodel prefix=... model` are deprecated; see `to_submodel` for the up-to-date syntax.",
),
outer_with_prefix()()
)
end

@testset "prefixing still works correctly" begin
@model inner() = x ~ Normal()
@model function outer()
a = @submodel inner()
b = @submodel prefix = "sub" inner()
return a, b
end
@test outer()() isa Tuple{Float64,Float64}
vi = VarInfo(outer())
@test @varname(x) in keys(vi)
@test @varname(var"sub.x") in keys(vi)
end

@testset "logp is still accumulated properly" begin
@model inner_assume() = x ~ Normal()
@model inner_observe(x, y) = y ~ Normal(x)
@model function outer(b)
a = @submodel inner_assume()
@submodel inner_observe(a, b)
end
y_val = 1.0
model = outer(y_val)
@test model() == y_val

x_val = 1.5
vi = VarInfo(outer(b))
vn_x = @varname(x)
DynamicPPL.setindex!!(vi, x_val, vn_x)
@test logprior(model, vi) logpdf(Normal(), x_val)
@test loglikelihood(model, vi) logpdf(Normal(x_val), y_val)
@test logjoint(model, vi) logpdf(Normal(), x_val) + logpdf(Normal(x_val), y_val)
end
end
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ include("test_util.jl")
include("serialization.jl")
include("pointwise_logdensities.jl")
include("lkj.jl")
include("deprecated.jl")
end

if GROUP == "All" || GROUP == "Group2"
Expand Down

0 comments on commit a987195

Please sign in to comment.