Skip to content

Commit

Permalink
bump the test coverage to reasonable numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
exaexa committed Sep 14, 2023
1 parent 27c3a7a commit 87f3e73
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
14 changes: 12 additions & 2 deletions docs/src/metabolic-modeling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,22 @@ collect(keys(c))

@test 2 == length((keys(c)))#src

# ## Adding combined constraints
# ## Value and constraint arithmetics

# Values may be combined additively and multiplied by real constants; which
# allows us to easily create more complex linear combination of any values
# already occurring in the model:
c.fluxes.R_PFK.value - 2 * c.fluxes.R_ACALD.value
3 * c.fluxes.R_PFK.value - c.fluxes.R_ACALD.value / 2

# For simplicity, you can also scale whole constraints, but it is impossible to
# add them together because the meaning of the bounds would get broken:
(3 * c.fluxes.R_PFK, -c.fluxes.R_ACALD / 2)

# To process constraints in bulk, you may use `C.value` for easier access to
# values and making constraints.
sum(C.value.(values(c.fluxes)))

# ## Adding combined constraints

# Metabolic modeling relies on the fact that the total rates of any metabolite
# getting created and consumed by the reaction equals to zero (which
Expand Down
1 change: 1 addition & 0 deletions src/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Base.@kwdef struct Constraint
bound::Bound = nothing
end

Base.:-(a::Constraint) = -1 * a
Base.:*(a::Real, b::Constraint) = b * a
Base.:*(a::Constraint, b::Real) = Constraint(
value = a.value * b,
Expand Down
35 changes: 35 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

import ConstraintTrees as C
import SparseArrays as SP

@testset "Values" begin
x = C.Value(SP.sparse([5.0, 0, 6.0, 0])) + C.Value(idxs = [2, 3], weights = [5.0, 4.0])
@test x.idxs == [1, 2, 3]
@test x.weights == [5.0, 5.0, 10.0]
end

@testset "Constraint tree operations" begin
ct1 = C.allocate_variables(keys = [:a, :b])
ct2 = C.allocate_variables(keys = [:c, :d])

@test collect(propertynames(ct1)) == [:a, :b]
@test [k for (k, _) in ct2] == [:c, :d]
@test eltype(ct2) == Pair{Symbol,C.ConstraintTreeElem}
@test collect(keys((:x^ct1 * :x^ct2).x)) == [:a, :b, :c, :d]
@test_throws ErrorException ct1 * ct1
@test_throws ErrorException :a^ct1 * ct1
@test_throws ErrorException ct1 * :a^ct1
end

@testset "Constraint tree operations" begin
ct = C.allocate_variables(keys = [:a, :b])
@test_throws BoundsError C.solution_tree(ct, [1.0])
st = C.solution_tree(ct, [123.0, 321.0])
@test st.a == 123.0
@test st[:b] == 321.0
@test collect(propertynames(st)) == [:a, :b]
@test collect(keys(st)) == [:a, :b]
@test sum([v for (_, v) in st]) == 444.0
@test sum(values(st)) == 444.0
@test eltype(st) == Pair{Symbol,C.SolutionTreeElem}
end
8 changes: 7 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@ import ConstraintTrees
using Test

@testset "ConstraintTrees tests" begin
include("../docs/src/metabolic-modeling.jl")
@testset "Metabolic modeling" begin
include("../docs/src/metabolic-modeling.jl")
end

@testset "Miscellaneous methods" begin
include("misc.jl")
end
end

0 comments on commit 87f3e73

Please sign in to comment.