Skip to content

Commit

Permalink
reach a pretty good coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
exaexa committed Oct 26, 2023
1 parent 108c37f commit 772234b
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 14 deletions.
25 changes: 19 additions & 6 deletions docs/src/canonical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,34 @@ A.run_fbcmodel_type_tests(Model);
# For testing the values, you need to provide an existing file that contains
# the model. Let's create some contents first:

import AbstractFBCModels.CanonicalModel: Reaction, Metabolite
import AbstractFBCModels.CanonicalModel: Reaction, Metabolite, Gene

m = Model()
m.reactions["forward"] =
Reaction(name = "import", stoichiometry = Dict("m1" => -1.0, "m2" => 1.0))
m.reactions["and_back"] =
Reaction(name = "export", stoichiometry = Dict("m2" => -1.0, "m1" => 1.0))
m.metabolites["m1"] = Metabolite(compartment = "inside")
m.metabolites["m2"] = Metabolite(compartment = "outside")
m.genes["g1"] = Gene()
m.genes["g2"] = Gene()
m.reactions["forward"] = Reaction(
name = "import",
stoichiometry = Dict("m1" => -1.0, "m2" => 1.0),
gene_association_dnf = [["g1"], ["g2"]],
)
m.reactions["and_back"] =
Reaction(name = "export", stoichiometry = Dict("m2" => -1.0, "m1" => 1.0))
nothing #hide

# We should immediately find the basic accessors working:
A.stoichiometry(m)

# We can now write the model to disk and try to load it with the default
# We can check various side things, such as which reactions would and would not work given all gene products disappear:
products_available = [
A.reaction_gene_products_available(m, rid, _ -> false) for
rid in ["forward", "and_back"]
]

@test products_available == [false, nothing] #src

# We can now also write the model to disk and try to load it with the default
# loading function:
mktempdir() do dir
path = joinpath(dir, "model.canonical-serialized-fbc")
Expand Down
36 changes: 33 additions & 3 deletions docs/src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import AbstractFBCModels as A

A.accessors()

@test length(A.accessors) == 27 #src
@test length(A.accessors()) == 27 #src

# You do not need to overload all of them (e.g., if you model does not have any
# genes you can completely omit all gene-related functions). The main required
Expand Down Expand Up @@ -57,6 +57,36 @@ A.accessors()
#
# ## Downloading models for testing
#
# For reproducibility,
# For reproducibility, it is often viable to check downloaded files for
# validity, using e.g. checksums. Since this is a common operation, we provide
# `download_data_file`, which is an appropriate wrapper for
# `Downloads.download`:

#TODO download file:///something
mktempdir() do dir
origin = joinpath(dir, "origin")
url = "file://$origin"
dest = joinpath(dir, "model")
open(origin, "w") do f
write(f, "hello")
end
A.download_data_file( #src
url, #src
dest, #src
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", #src
) #src
x = A.download_data_file( #src
url, #src
dest, #src
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", #src
) # src
@test x == dest #src
@test read(origin) == read(dest) #src
open(dest, "w") do f #src
write(f, "olleh") #src
end #src
@test_warn "different" A.download_data_file( #src
url, #src
dest, #src
"2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824", #src
) #src
end
6 changes: 3 additions & 3 deletions src/accessors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Return identifiers of all genes contained in the model. Empty if none.
Genes are also sometimes called "gene products" but we write genes for
simplicity.
"""
genes(::AbstractFBCModel)::Vector{String} = unimplemented(typeof(a), :genes)
genes(a::AbstractFBCModel)::Vector{String} = unimplemented(typeof(a), :genes)

"""
$(TYPEDSIGNATURES)
Expand All @@ -60,7 +60,7 @@ by [`genes`](@ref)).
This may be more efficient than calling [`genes`](@ref) and measuring the
array.
"""
n_genes(::AbstractFBCModel)::Int = unimplemented(typeof(a), :n_genes)
n_genes(a::AbstractFBCModel)::Int = unimplemented(typeof(a), :n_genes)

"""
$(TYPEDSIGNATURES)
Expand All @@ -86,7 +86,7 @@ $(TYPEDSIGNATURES)
Get the sparse balance vector of a model, which usually corresponds to the
accumulation term associated with stoichiometric matrix.
"""
balance(a::AbstractFBCModel)::SparseVec = spzeros(length(n_metabolites(a)))
balance(a::AbstractFBCModel)::SparseVec = spzeros(n_metabolites(a))

"""
$(TYPEDSIGNATURES)
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function download_data_file(url, path, hash)
return path
end

Downloads.download(url, path)
path = download(url, path)
check_cached_file_hash(path, hash)
return path
end
Expand Down
42 changes: 42 additions & 0 deletions test/defaults.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

@testset "behavior of accessors on abstract type" begin

import AbstractFBCModels as A

struct NotAModel <: A.AbstractFBCModel end

m = NotAModel()

@test_throws ErrorException A.reactions(m)
@test_throws ErrorException A.metabolites(m)
@test_throws ErrorException A.genes(m)
@test_throws ErrorException A.n_reactions(m)
@test_throws ErrorException A.n_metabolites(m)
@test_throws ErrorException A.n_genes(m)
@test_throws ErrorException A.stoichiometry(m)
@test_throws ErrorException A.balance(m)
@test_throws ErrorException A.bounds(m)
@test_throws ErrorException A.objective(m)

@test isnothing(A.reaction_gene_products_available(m, "", _ -> True))
@test isnothing(A.reaction_gene_association_dnf(m, ""))
@test_throws ErrorException A.reaction_stoichiometry(m, "")
@test isnothing(A.metabolite_formula(m, ""))
@test isnothing(A.metabolite_charge(m, ""))
@test isnothing(A.metabolite_compartment(m, ""))

@test isempty(A.reaction_annotations(m, ""))
@test isempty(A.metabolite_annotations(m, ""))
@test isempty(A.gene_annotations(m, ""))
@test isempty(A.reaction_notes(m, ""))
@test isempty(A.metabolite_notes(m, ""))
@test isempty(A.gene_notes(m, ""))
@test isnothing(A.reaction_name(m, ""))
@test isnothing(A.metabolite_name(m, ""))
@test isnothing(A.gene_name(m, ""))

@test_throws ErrorException A.load(NotAModel, ".")
@test_throws ErrorException A.save(m, ".")
@test_throws ErrorException A.filename_extensions(NotAModel)
@test_throws ErrorException show(stdout, MIME"text/plain"(), m)
end
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import AbstractFBCModels as A
include("../docs/src/canonical.jl")
end
@testset "Utilities" begin
include("../docs/src/canonical.jl")
include("../docs/src/utilities.jl")
end

include("defaults.jl")
end

0 comments on commit 772234b

Please sign in to comment.