Skip to content

Commit

Permalink
TEST: Update Gantt chart tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
shnizzedy committed Nov 18, 2024
1 parent 376d6e2 commit e644bdd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
32 changes: 25 additions & 7 deletions nipype/pipeline/plugins/tests/test_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Tests for workflow callbacks
"""
from datetime import datetime
from pathlib import Path
from time import sleep
import json
import pytest
import nipype.interfaces.utility as niu
import nipype.pipeline.engine as pe
from nipype.utils.draw_gantt_chart import _convert_string_to_datetime

try:
import pandas

Check warning on line 15 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L14-L15

Added lines #L14 - L15 were not covered by tests
Expand Down Expand Up @@ -71,22 +75,22 @@ def test_callback_exception(tmpdir, plugin, stop_on_first_crash):

@pytest.mark.parametrize("plugin", ["Linear", "MultiProc", "LegacyMultiProc"])
@pytest.mark.skipif(not has_pandas, reason="Test requires pandas")
def test_callback_gantt(tmpdir, plugin):
def test_callback_gantt(tmp_path: Path, plugin: str) -> None:
import logging

Check warning on line 79 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L76-L79

Added lines #L76 - L79 were not covered by tests

from os import path

Check warning on line 81 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L81

Added line #L81 was not covered by tests

from nipype.utils.profiler import log_nodes_cb
from nipype.utils.draw_gantt_chart import generate_gantt_chart

Check warning on line 84 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L83-L84

Added lines #L83 - L84 were not covered by tests

log_filename = path.join(tmpdir, "callback.log")
log_filename = tmp_path / "callback.log"
logger = logging.getLogger("callback")
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(log_filename)
logger.addHandler(handler)

Check warning on line 90 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L86-L90

Added lines #L86 - L90 were not covered by tests

# create workflow
wf = pe.Workflow(name="test", base_dir=tmpdir.strpath)
wf = pe.Workflow(name="test", base_dir=str(tmp_path))
f_node = pe.Node(

Check warning on line 94 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L93-L94

Added lines #L93 - L94 were not covered by tests
niu.Function(function=func, input_names=[], output_names=[]), name="f_node"
)
Expand All @@ -98,7 +102,21 @@ def test_callback_gantt(tmpdir, plugin):
plugin_args["n_procs"] = 8
wf.run(plugin=plugin, plugin_args=plugin_args)

Check warning on line 103 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L102-L103

Added lines #L102 - L103 were not covered by tests

generate_gantt_chart(
path.join(tmpdir, "callback.log"), 1 if plugin == "Linear" else 8
)
assert path.exists(path.join(tmpdir, "callback.log.html"))
with open(log_filename, "r") as _f:
loglines = _f.readlines()

Check warning on line 106 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L105-L106

Added lines #L105 - L106 were not covered by tests

# test missing duration
first_line = json.loads(loglines[0])

Check warning on line 109 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L109

Added line #L109 was not covered by tests
if "duration" in first_line:
del first_line["duration"]
loglines[0] = json.dumps(first_line)

Check warning on line 112 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L111-L112

Added lines #L111 - L112 were not covered by tests

# test duplicate timestamp warning
loglines.append(loglines[-1])

Check warning on line 115 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L115

Added line #L115 was not covered by tests

with open(log_filename, "w") as _f:
_f.write("".join(loglines))

Check warning on line 118 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L117-L118

Added lines #L117 - L118 were not covered by tests

with pytest.warns(Warning):
generate_gantt_chart(str(log_filename), 1 if plugin == "Linear" else 8)
assert (tmp_path / "callback.log.html").exists()

Check warning on line 122 in nipype/pipeline/plugins/tests/test_callback.py

View check run for this annotation

Codecov / codecov/patch

nipype/pipeline/plugins/tests/test_callback.py#L120-L122

Added lines #L120 - L122 were not covered by tests
24 changes: 17 additions & 7 deletions nipype/utils/draw_gantt_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,25 @@ def log_to_dict(logfile):

nodes_list = [json.loads(l) for l in lines]

def _convert_string_to_datetime(datestring):
try:
def _convert_string_to_datetime(

Check warning on line 105 in nipype/utils/draw_gantt_chart.py

View check run for this annotation

Codecov / codecov/patch

nipype/utils/draw_gantt_chart.py#L105

Added line #L105 was not covered by tests
datestring: str | datetime.datetime,
) -> datetime.datetime:
"""Convert a date string to a datetime object."""
if isinstance(datestring, datetime.datetime):
datetime_object = datestring

Check warning on line 110 in nipype/utils/draw_gantt_chart.py

View check run for this annotation

Codecov / codecov/patch

nipype/utils/draw_gantt_chart.py#L110

Added line #L110 was not covered by tests
elif isinstance(datestring, str):
date_format = (

Check warning on line 112 in nipype/utils/draw_gantt_chart.py

View check run for this annotation

Codecov / codecov/patch

nipype/utils/draw_gantt_chart.py#L112

Added line #L112 was not covered by tests
"%Y-%m-%dT%H:%M:%S.%f%z"
if "+" in datestring
else "%Y-%m-%dT%H:%M:%S.%f"
)
datetime_object: datetime.datetime = datetime.datetime.strptime(
datestring, "%Y-%m-%dT%H:%M:%S.%f"
datestring, date_format
)
return datetime_object
except Exception as _:
pass
return datestring
else:
msg = f"{datestring} is not a string or datetime object."
raise TypeError(msg)
return datetime_object

Check warning on line 123 in nipype/utils/draw_gantt_chart.py

View check run for this annotation

Codecov / codecov/patch

nipype/utils/draw_gantt_chart.py#L121-L123

Added lines #L121 - L123 were not covered by tests

date_object_node_list: list = list()
for n in nodes_list:
Expand Down

0 comments on commit e644bdd

Please sign in to comment.