Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_fishery_dynamics.py creating test file using pytest for fishery_dynamics.py file #107

Merged
merged 23 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b15af1c
test_fishery_dynamics.py creating test file using pytest for fishery_…
Jaydon2005 Aug 16, 2024
851be46
Update test_fishery_dynamics.py
Jaydon2005 Aug 19, 2024
bd78988
Update test_fishery_dynamics.py
Jaydon2005 Aug 23, 2024
fed5d91
Update test_fishery_dynamics.py
Jaydon2005 Aug 23, 2024
18e6e96
Update test_fishery_dynamics.py
Jaydon2005 Aug 27, 2024
49fc9dd
Update test_fishery_dynamics.py
Jaydon2005 Aug 27, 2024
6ad85cf
Fixing colorbar typo for test_fishery_dynamics
Jaydon2005 Aug 27, 2024
a543f43
git commit -m "fix colorbar typo"
Jaydon2005 Aug 27, 2024
a9a9eca
fix colorbar typo
Jaydon2005 Aug 27, 2024
f6ff643
Changed imports
Jaydon2005 Aug 27, 2024
de0deb3
merge branch 'Jaydon2005-patch-1' of https://github.com/IMMM-SFA/msd_…
Jaydon2005 Aug 27, 2024
3e8ab34
Merge branch 'dev' into Jaydon2005-patch-1
Jaydon2005 Aug 27, 2024
b10b955
test_fishery_dynamics.py Updated all tests passed changed imports and…
Jaydon2005 Oct 17, 2024
8634df6
Removed comments and added pytest.warns to capture the Deprecation Wa…
Jaydon2005 Oct 21, 2024
9a20eeb
Updated test_fishery_dynamics.py file removed pytest.warns context ma…
Jaydon2005 Oct 22, 2024
c4174df
Update fishery_dynamics.py Replaced plt.cm.get_cmap("Blues") and plt.…
Jaydon2005 Oct 24, 2024
f892993
Changed fishery_dynamics.py back to original
Jaydon2005 Oct 24, 2024
707d3a2
Removed mocker from def tests
Jaydon2005 Oct 28, 2024
c2f25c3
Add from pytest_mock import mocker
Jaydon2005 Oct 28, 2024
912f867
added from pytest_mock import MockerFixture changed back to test_fish…
Jaydon2005 Oct 28, 2024
9017de8
Update requirements.txt, added pytest-mock>=3.0.0
Jaydon2005 Dec 9, 2024
d917934
Changed to original code
Jaydon2005 Dec 9, 2024
7890c0a
Update 01_test.yml added run: | and pytest-mock
Jaydon2005 Dec 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/01_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
python -m pip install --upgrade pip
pip install .

- name: Install pytest-mock
run: |
pip install pytest-mock

- name: Test and generate coverage report
run: |
pip install pytest
Expand All @@ -49,3 +53,10 @@ jobs:
if: failure() # This step will only run if any previous step fails
run: |
echo "Build or tests failed. Please check the logs."

- name: Test and generate coverage report
run: |
pip install pytest
pip install pytest-cov
pip install pytest-mock # Ensure pytest-mock is installed before testing
pytest --cov=./ --cov-report=xml # Run tests and generate a coverage report
83 changes: 83 additions & 0 deletions msdbook/tests/test_fishery_dynamics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import pytest
import numpy as np
import matplotlib.pyplot as plt
from pytest_mock import MockerFixture
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.testing.decorators import check_figures_equal
from msdbook.fishery_dynamics import plot_objective_performance, plot_factor_performance

@pytest.fixture
def sample_data():
"""Fixture to provide sample data for testing."""
objective_performance = np.array([
[1, 2, 3, 4, 5],
[2, 3, 4, 5, 6],
[3, 4, 5, 6, 7]
])
profit_solution = 1
robust_solution = 2
param_values = np.random.rand(100, 7)
collapse_days = np.random.rand(100, 2) * 10
b = np.linspace(0, 1, 10)
m = np.linspace(0, 1, 10)
a = np.linspace(0, 2, 10)
return {
'objective_performance': objective_performance,
'profit_solution': profit_solution,
'robust_solution': robust_solution,
'param_values': param_values,
'collapse_days': collapse_days,
'b': b,
'm': m,
'a': a
}


def test_plot_objective_performance(sample_data, mocker):
erexer marked this conversation as resolved.
Show resolved Hide resolved
"""Test the plot_objective_performance function."""
fig, ax = plt.subplots()
mocker.patch('matplotlib.pyplot.figure', return_value=fig)

plot_objective_performance(
sample_data['objective_performance'],
sample_data['profit_solution'],
sample_data['robust_solution']
)

# Ensure figure and axes are created
assert plt.gcf() == fig
assert len(fig.axes) > 0

# Check for colorbars in the figure
colorbars = [c for a in fig.axes for c in a.collections if isinstance(c, plt.cm.ScalarMappable)]
assert len(colorbars) > 0


def test_plot_factor_performance(sample_data, mocker):
"""Test the plot_factor_performance function."""
fig, axs = plt.subplots(1, 2, subplot_kw={'projection': '3d'})
mocker.patch('matplotlib.pyplot.figure', return_value=fig)

# Reshape b, m, a to 2D arrays for plotting
b, m = np.meshgrid(sample_data['b'], sample_data['m'])
a = np.tile(sample_data['a'], (len(sample_data['m']), 1))

plot_factor_performance(
sample_data['param_values'],
sample_data['collapse_days'],
b,
m,
a
)

# Ensure figure and axes are created
assert plt.gcf() == fig
assert len(fig.axes) >= 3

for ax in axs:
assert isinstance(ax, Axes3D)

# Check for colorbars in the figure
colorbars = [c for a in fig.axes for c in a.collections if isinstance(c, plt.cm.ScalarMappable)]
assert len(colorbars) > 0

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ test = [
"pytest>=7.0.0",
"pytest-mock>=3.10",
]

[project.urls]
Homepage = "https://uc-ebook.org/"
Repository = "https://github.com/IMMM-SFA/msd_uncertainty_ebook"
Expand Down
Loading