Skip to content

Commit

Permalink
Add basic unittests for matplotlib plot
Browse files Browse the repository at this point in the history
  • Loading branch information
asmacdo committed Oct 9, 2024
1 parent b5d97a7 commit 007bb4c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/con_duct/suite/plot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env python3
import argparse
from datetime import datetime
import json
Expand Down
44 changes: 43 additions & 1 deletion test/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import unittest
from unittest.mock import MagicMock, mock_open, patch
import pytest
from con_duct.suite import main, pprint_json
from con_duct.suite import main, plot, pprint_json


class TestSuiteHelpers(unittest.TestCase):
Expand Down Expand Up @@ -53,3 +53,45 @@ def test_pprint_invalid_json(

mock_open.assert_called_with("dummy.json", "r")
mock_pprint.assert_not_called()


class TestPlotMatplotlib(unittest.TestCase):

@patch("con_duct.suite.plot.plt.savefig")
def test_matplotlib_plot_sanity(self, mock_plot_save: MagicMock) -> None:
args = argparse.Namespace(
command="plot",
file_path="test/data/mriqc-example/usage.json",
output="outfile",
func=plot.matplotlib_plot,
)
assert main.execute(args) == 0
mock_plot_save.assert_called_once_with("outfile")

@patch("con_duct.suite.plot.plt.savefig")
@patch("builtins.open", side_effect=FileNotFoundError)
def test_matplotlib_plot_file_not_found(
self, _mock_open: MagicMock, mock_plot_save: MagicMock
) -> None:
args = argparse.Namespace(
command="plot",
file_path="test/data/mriqc-example/usage.json",
output="outfile",
func=plot.matplotlib_plot,
)
assert main.execute(args) == 1
mock_plot_save.assert_not_called()

@patch("con_duct.suite.plot.plt.savefig")
@patch("builtins.open", new_callable=mock_open, read_data='{"invalid": "json"')
def test_matplotlib_plot_invalid_json(
self, _mock_open: MagicMock, mock_plot_save: MagicMock
) -> None:
args = argparse.Namespace(
command="plot",
file_path="test/data/mriqc-example/usage.json",
output="outfile",
func=plot.matplotlib_plot,
)
assert main.execute(args) == 1
mock_plot_save.assert_not_called()

0 comments on commit 007bb4c

Please sign in to comment.