Skip to content

Commit

Permalink
100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexEMG committed Nov 14, 2021
1 parent 5703f00 commit 46601c7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions LotkaVolterraModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,6 @@ def check_equilibrium(a=1.0, b=0.1, c=1.5, d=0.75):

if __name__ == "__main__":
import doctest

print("Starting doctests") # not required!
doctest.testmod()
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is an openly developed project and we welcome community contributions, espe
We are happy to receive code extensions, bug fixes, documentation updates, etc.
If you are a new user, we recommend checking out the detailed Github Guides.

# Testing the code
# Unit-tests

We use [doctests](https://docs.python.org/3/library/doctest.html) and [pytest](https://docs.pytest.org/en/6.2.x/contents.html).

Expand Down Expand Up @@ -42,3 +42,4 @@ coverage run -m pytest
coverage report
```

This is also available via a bash script `./tests.sh`.
55 changes: 53 additions & 2 deletions test_LVM.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,57 @@ def test_jacobian():
target_jac = np.array([[2.0, -0.0], [0.0, -1.2]])
assert (jac == target_jac).all()


def test_equilibria():
eq = LotkaVolterraModel.population_equilibrium(a=1.0, b=0.1, c=1.5, d=0.75)
assert sum(eq[0])==0 # equal to 0
a=1.0
b=0.1
c=1.5
d=0.75
eq = LotkaVolterraModel.population_equilibrium(a=a, b=b, c=c, d=d)

# Checking the first and second fixpoint:
assert sum(np.abs(eq[0])) == 0 # equal to 0
assert (eq[1] == (c / (d * b), a / b)).all()

def test_check_equilibrium():
return_value = LotkaVolterraModel.check_equilibrium()
assert return_value == None

import sys

def test_printoutput_checkeq(capsys):
'''
Unit-test that check the print statements in LotkaVolterraModel.check_equilibrium()
Notes:
------
Based on capture: https://docs.pytest.org/en/6.2.x/capture.html
'''
LotkaVolterraModel.check_equilibrium() # run check equlibrium and
captured = capsys.readouterr() # capture the output

first, second,_ = (captured.out).split('\n') # separating different lines
assert first == "[0. 0.] is a fix point!"
assert second == "[20. 10.] is a fix point!"

def test_printoutput_checkeq_notfixpoint(capsys):

# Overwrite dX_dt (with a different version so that equilibria are no longer fixpoints)
def dX_dt(X, a=1.0, b=0.1, c=1.5, d=0.75):
return np.array([a + X[0] - b * X[0] * X[1], -c * X[1] + d * b * X[0] * X[1]])

LotkaVolterraModel.dX_dt = dX_dt
LotkaVolterraModel.check_equilibrium() # run check equlibrium and
captured = capsys.readouterr() # capture the output

first, second,_ = (captured.out).split('\n') # separating different lines
assert first == "[0. 0.] is not a fix point!"
assert second == "[20. 10.] is not a fix point!"

def test_main(capsys):
''' Silly test that runs LVM.py as script.py '''
import imp
runpy = imp.load_source('__main__', 'LotkaVolterraModel.py')
captured = capsys.readouterr() # capture the output
first, _ = (captured.out).split('\n') # separating different lines
assert first == "Starting doctests"
3 changes: 3 additions & 0 deletions tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Run tests
coverage run -m pytest
coverage report

0 comments on commit 46601c7

Please sign in to comment.