Skip to content

Commit 552136c

Browse files
cdienerMidnighter
authored andcommitted
separate test and coverage
1 parent b838b54 commit 552136c

File tree

7 files changed

+108
-88
lines changed

7 files changed

+108
-88
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
python -m pip install tox tox-gh-actions
3636
- name: Test with tox
3737
run:
38-
tox -- --cov-report=xml --benchmark-skip
38+
tox -- --benchmark-skip
3939
timeout-minutes: 60
4040
- name: Report coverage
4141
shell: bash

src/cobra/test/test_flux_analysis/test_deletion.py

+29-28
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Callable, List
66

77
import numpy as np
8+
import
89
import pandas as pd
910
import pytest
1011

@@ -288,6 +289,7 @@ def test_double_gene_deletion_benchmark(
288289
benchmark(double_gene_deletion, large_model, gene_list1=genes, processes=1)
289290

290291

292+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
291293
def test_double_gene_deletion(model: Model) -> None:
292294
"""Test double gene deletion."""
293295
genes = [
@@ -365,18 +367,17 @@ def test_double_gene_deletion(model: Model) -> None:
365367
"b4025": 0.863,
366368
},
367369
}
368-
if __name__ == "__main__":
369-
solution = double_gene_deletion(model, gene_list1=genes, processes=3)
370-
solution_one_process = double_gene_deletion(
371-
model, gene_list1=genes, processes=1
372-
)
370+
solution = double_gene_deletion(model, gene_list1=genes, processes=3)
371+
solution_one_process = double_gene_deletion(
372+
model, gene_list1=genes, processes=1
373+
)
373374

374-
for rxn_a, sub in growth_dict.items():
375-
for rxn_b, growth in sub.items():
376-
sol = solution.knockout[{rxn_a, rxn_b}]
377-
sol_one = solution_one_process.knockout[{rxn_a, rxn_b}]
378-
assert np.isclose(sol.growth, growth, atol=1e-3)
379-
assert np.isclose(sol_one.growth, growth, atol=1e-3)
375+
for rxn_a, sub in growth_dict.items():
376+
for rxn_b, growth in sub.items():
377+
sol = solution.knockout[{rxn_a, rxn_b}]
378+
sol_one = solution_one_process.knockout[{rxn_a, rxn_b}]
379+
assert np.isclose(sol.growth, growth, atol=1e-3)
380+
assert np.isclose(sol_one.growth, growth, atol=1e-3)
380381

381382

382383
def test_double_gene_knockout_bug(large_model: Model) -> None:
@@ -415,6 +416,7 @@ def test_double_reaction_deletion_benchmark(
415416
benchmark(double_reaction_deletion, large_model, reaction_list1=reactions)
416417

417418

419+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
418420
def test_double_reaction_deletion(model: Model) -> None:
419421
"""Test double reaction deletion."""
420422
reactions = ["FBA", "ATPS4r", "ENO", "FRUpts2"]
@@ -424,23 +426,22 @@ def test_double_reaction_deletion(model: Model) -> None:
424426
"ENO": {"FRUpts2": 0.0},
425427
}
426428

427-
if __name__ == "__main__":
428-
solution = double_reaction_deletion(
429-
model, reaction_list1=reactions, processes=3
430-
)
431-
solution_one_process = double_reaction_deletion(
432-
model, reaction_list1=reactions, processes=1
433-
)
434-
for (rxn_a, sub) in growth_dict.items():
435-
for rxn_b, growth in sub.items():
436-
sol = solution.knockout[{rxn_a, rxn_b}]
437-
sol_one = solution_one_process.knockout[{rxn_a, rxn_b}]
438-
if math.isnan(growth):
439-
assert math.isnan(sol.growth)
440-
assert math.isnan(sol_one.growth)
441-
else:
442-
assert np.isclose(sol.growth, growth, atol=1e-3)
443-
assert np.isclose(sol_one.growth, growth, atol=1e-3)
429+
solution = double_reaction_deletion(
430+
model, reaction_list1=reactions, processes=3
431+
)
432+
solution_one_process = double_reaction_deletion(
433+
model, reaction_list1=reactions, processes=1
434+
)
435+
for (rxn_a, sub) in growth_dict.items():
436+
for rxn_b, growth in sub.items():
437+
sol = solution.knockout[{rxn_a, rxn_b}]
438+
sol_one = solution_one_process.knockout[{rxn_a, rxn_b}]
439+
if math.isnan(growth):
440+
assert math.isnan(sol.growth)
441+
assert math.isnan(sol_one.growth)
442+
else:
443+
assert np.isclose(sol.growth, growth, atol=1e-3)
444+
assert np.isclose(sol_one.growth, growth, atol=1e-3)
444445

445446

446447
def test_deletion_accessor(small_model: Model) -> None:

src/cobra/test/test_flux_analysis/test_variability.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Callable, List
44

55
import numpy as np
6+
import os
67
import pandas as pd
78
import pytest
89

@@ -84,11 +85,11 @@ def test_flux_variability(
8485
assert np.allclose(fva_out, fva_results)
8586

8687

88+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
8789
def test_parallel_flux_variability(
8890
model: Model, fva_results: pd.DataFrame, all_solvers: List[str]
8991
) -> None:
9092
"""Test parallel FVA."""
91-
if __name__ == "__main__":
9293
model.solver = all_solvers
9394
fva_out = flux_variability_analysis(model, processes=2)
9495
fva_out.sort_index(inplace=True)

src/cobra/test/test_sampling/test_optgp.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import TYPE_CHECKING, Callable
44

55
import numpy as np
6+
import os
67
import pytest
78

89
from cobra.sampling import OptGPSampler
@@ -23,10 +24,10 @@ def optgp(model: "Model") -> OptGPSampler:
2324
return sampler
2425

2526

27+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
2628
def test_optgp_init_benchmark(model: "Model", benchmark: Callable) -> None:
2729
"""Benchmark inital OptGP sampling."""
28-
if __name__ == "__main__":
29-
benchmark(lambda: OptGPSampler(model, processes=2))
30+
benchmark(lambda: OptGPSampler(model, processes=2))
3031

3132

3233
def test_optgp_sample_benchmark(optgp: "Model", benchmark: Callable) -> None:

src/cobra/test/test_sampling/test_sampling.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test functionalities of flux sampling methods."""
22

33
import numpy as np
4+
import os
45
import pytest
56

67
from cobra.core import Metabolite, Model, Reaction
@@ -20,11 +21,12 @@ def test_single_optgp(model: Model) -> None:
2021
assert s.shape == (10, len(model.reactions))
2122

2223

23-
def test_multi_optgp(model: Model) -> None:
24+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
25+
def test_multi_optgp(model: Model) -> None: # pragma: no cover
2426
"""Test OptGP sampling (multi sample)."""
25-
if __name__ == "__main__":
26-
s = sample(model, 10, processes=2)
27-
assert s.shape == (10, len(model.reactions))
27+
s = sample(model, 10, processes=2)
28+
assert s.shape == (10, len(model.reactions))
29+
raise ValueError(__name__)
2830

2931

3032
def test_wrong_method(model: Model) -> None:

src/cobra/test/test_util/test_process_pool.py

+51-50
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from typing import Iterable, Tuple
55

6+
import os
67
import pytest
78
from pytest_mock import MockerFixture
89

@@ -24,6 +25,7 @@ def summation(*args: Iterable[int]) -> int:
2425
return sum(args)
2526

2627

28+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
2729
@pytest.mark.parametrize(
2830
"attributes",
2931
[
@@ -36,93 +38,92 @@ def summation(*args: Iterable[int]) -> int:
3638
)
3739
def test_init(attributes: dict) -> None:
3840
"""Test that a process pool can be initialized with each of its arguments."""
39-
if __name__ == "__main__":
40-
ProcessPool(**attributes)
41+
ProcessPool(**attributes)
4142

4243

44+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
4345
def test_close(mocker: MockerFixture) -> None:
4446
"""Test that the composed pool is closed as well."""
45-
if __name__ == "__main__":
46-
pool = ProcessPool(processes=3)
47-
mock = mocker.patch.object(pool, "_pool", autospec=True)
48-
pool.close()
49-
mock.close.assert_called_once()
47+
pool = ProcessPool(processes=3)
48+
mock = mocker.patch.object(pool, "_pool", autospec=True)
49+
pool.close()
50+
mock.close.assert_called_once()
5051

5152

53+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
5254
def test_with_context(mocker: MockerFixture) -> None:
5355
"""Test that the composed pool's context is managed as well."""
54-
if __name__ == "__main__":
55-
pool = ProcessPool(processes=3)
56-
mock = mocker.patch.object(pool, "_pool", autospec=True)
57-
with pool:
58-
pass
59-
mock.__enter__.assert_called_once()
60-
mock.__exit__.assert_called_once()
56+
pool = ProcessPool(processes=3)
57+
mock = mocker.patch.object(pool, "_pool", autospec=True)
58+
with pool:
59+
pass
60+
mock.__enter__.assert_called_once()
61+
mock.__exit__.assert_called_once()
6162

6263

64+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
6365
def test_apply() -> None:
6466
"""Test that a function can be applied."""
65-
if __name__ == "__main__":
66-
with ProcessPool(processes=3) as pool:
67-
assert pool.apply(square, (3,)) == 9
67+
with ProcessPool(processes=3) as pool:
68+
assert pool.apply(square, (3,)) == 9
6869

6970

71+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
7072
def test_apply_async() -> None:
7173
"""Test that a function can be applied asynchronously."""
72-
if __name__ == "__main__":
73-
with ProcessPool(processes=3) as pool:
74-
assert pool.apply_async(square, (3,)).get() == 9
74+
with ProcessPool(processes=3) as pool:
75+
assert pool.apply_async(square, (3,)).get() == 9
7576

7677

78+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
7779
def test_map() -> None:
7880
"""Test that a function can be mapped over an iterable of values."""
79-
if __name__ == "__main":
80-
with ProcessPool(processes=3) as pool:
81-
assert sum(pool.map(square, [2] * 6)) == 24
81+
with ProcessPool(processes=3) as pool:
82+
assert sum(pool.map(square, [2] * 6)) == 24
8283

8384

85+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
8486
def test_map_async() -> None:
8587
"""Test that a function can be mapped over an iterable of values asynchronously."""
86-
if __name__ == "__main__":
87-
with ProcessPool(processes=3) as pool:
88-
assert sum(pool.map_async(square, [2] * 6).get()) == 24
88+
with ProcessPool(processes=3) as pool:
89+
assert sum(pool.map_async(square, [2] * 6).get()) == 24
8990

9091

92+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
9193
def test_imap() -> None:
9294
"""Test that mapped function results can be iterated."""
93-
if __name__ == "__main__":
94-
with ProcessPool(processes=3) as pool:
95-
total = 0
96-
for result in pool.imap(square, [2] * 6):
97-
total += result
98-
assert total == 24
95+
with ProcessPool(processes=3) as pool:
96+
total = 0
97+
for result in pool.imap(square, [2] * 6):
98+
total += result
99+
assert total == 24
99100

100101

102+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
101103
def test_imap_unordered() -> None:
102104
"""Test that mapped function results can be iterated in any order."""
103-
if __name__ == "__main__":
104-
with ProcessPool(processes=3) as pool:
105-
assert sum(pool.imap_unordered(square, [2] * 6)) == 24
105+
with ProcessPool(processes=3) as pool:
106+
assert sum(pool.imap_unordered(square, [2] * 6)) == 24
106107

107108

109+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
108110
def test_starmap() -> None:
109111
"""Test that a function can be starmapped over many iterables."""
110-
if __name__ == "__main__":
111-
with ProcessPool(processes=3) as pool:
112-
assert (
113-
sum(pool.starmap(summation, [range(10), range(10), range(10)])) == 135
114-
)
112+
with ProcessPool(processes=3) as pool:
113+
assert (
114+
sum(pool.starmap(summation, [range(10), range(10), range(10)])) == 135
115+
)
115116

116117

118+
@pytest.mark.skipif("SKIP_MP" in os.environ, reason="unsafe for parallel execution")
117119
def test_starmap_async() -> None:
118-
"""Test that a function can be starmapped over many iterables asynchronously."""
119-
if __name__ == "__main":
120-
with ProcessPool(processes=3) as pool:
121-
assert (
122-
sum(
123-
pool.starmap_async(
124-
summation, [range(10), range(10), range(10)]
125-
).get()
126-
)
127-
== 135
120+
"""Test that a function can be starmapped over many iterables asynchronously."""
121+
with ProcessPool(processes=3) as pool:
122+
assert (
123+
sum(
124+
pool.starmap_async(
125+
summation, [range(10), range(10), range(10)]
126+
).get()
128127
)
128+
== 135
129+
)

tox.ini

+16-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,24 @@ python =
66
3.6: install, py36
77
3.7: install, py37
88
3.8: install, py38
9-
3.9: install, py39
9+
3.9: install, py39, coverage
1010
3.10: install, py310
1111

1212
[testenv]
13+
extras =
14+
array
15+
deps=
16+
jsonschema
17+
osqp~=0.6 ; python_version < "3.10"
18+
pytest
19+
pytest-benchmark
20+
pytest-mock
21+
pytest-raises
22+
commands =
23+
pytest {posargs: --benchmark-skip}
24+
25+
[testenv:coverage]
26+
setenv = SKIP_MP = 1
1327
extras =
1428
array
1529
deps=
@@ -21,7 +35,7 @@ deps=
2135
pytest-mock
2236
pytest-raises
2337
commands =
24-
pytest --cov=cobra --cov-report=term {posargs: --benchmark-skip}
38+
pytest --cov=cobra --cov-report=xml {posargs: --benchmark-skip}
2539

2640
[testenv:isort]
2741
skip_install = True

0 commit comments

Comments
 (0)