Skip to content

Commit

Permalink
Create a base class for plot helpers to reduce code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Karan Desai committed Aug 20, 2016
1 parent ee320fe commit c5a3c7a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 82 deletions.
4 changes: 2 additions & 2 deletions tardis/tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from tardis import __githash__ as tardis_githash
from tardis.tests.integration_tests.report import DokuReport
from tardis.tests.integration_tests.plot_helpers import PlotUploader, LocalPlotSaver
from tardis.tests.integration_tests.plot_helpers import LocalPlotSaver, RemotePlotSaver


def pytest_configure(config):
Expand Down Expand Up @@ -63,7 +63,7 @@ def plot_object(request):
report_save_mode = integration_tests_config['report']['save_mode']

if report_save_mode == "remote":
return PlotUploader(request, request.config.dokureport.dokuwiki_url)
return RemotePlotSaver(request, request.config.dokureport.dokuwiki_url)
else:
return LocalPlotSaver(request, os.path.join(
request.config.dokureport.report_dirpath, "assets")
Expand Down
140 changes: 60 additions & 80 deletions tardis/tests/integration_tests/plot_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,23 @@
from tardis import __githash__ as tardis_githash


thumbnail_html_remote = """
<div class="image" style="float: left">
<a href="#">
<img src= "{dokuwiki_url}lib/exe/fetch.php?media=reports:{githash}:{name}.png" />
</a>
</div>
"""


class PlotUploader(object):
def __init__(self, request, dokuwiki_url):
"""A helper class to collect plots from integration tests and upload
them to DokuWiki.
class BasePlotSaver(object):
def __init__(self, request, dokuwiki_url=None, assets_dirpath=None):
"""Base Class for RemotePlotSaver and LocalPlotSaver classes.
These help in uploading plots to DokuWiki instance or saving them
locally in a directory.
Parameters
----------
request : _pytest.python.RequestObject
dokuwiki_url : str
assets_dirpath : str
"""
self.request = request
self._plots = list()
self.plot_html = list()
self.dokuwiki_url = dokuwiki_url
self.assets_dirpath = assets_dirpath

def add(self, plot, name):
"""Accept a plot figure and add it to ``self._plots``.
Expand All @@ -40,8 +34,52 @@ def add(self, plot, name):
"""
self._plots.append((plot, name))

def save(self, plot, filepath, report):
"""Mark pass / fail and save a plot with ``name`` to ``filepath``.
Parameters
----------
plot : matplotlib.pyplot.figure
filepath : str
report : _pytest.runner.TestReport
"""
axes = plot.axes[0]

if report.passed:
axes.text(0.8, 0.8, 'passed', transform=axes.transAxes,
bbox={'facecolor': 'green', 'alpha': 0.5, 'pad': 10})
else:
axes.text(0.8, 0.8, 'failed', transform=axes.transAxes,
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})

plot.savefig(filepath)

def get_extras(self):
"""Return ``self.plot_html`` which is further added into html report.
Returns
-------
list
List of strings containing raw html snippet to embed images.
"""
return self.plot_html


thumbnail_html_remote = """
<div class="image" style="float: left">
<a href="#">
<img src= "{dokuwiki_url}lib/exe/fetch.php?media=reports:{githash}:{name}.png" />
</a>
</div>
"""


class RemotePlotSaver(BasePlotSaver):
def __init__(self, request, dokuwiki_url):
super(RemotePlotSaver, self).__init__(request, dokuwiki_url=dokuwiki_url)

def upload(self, report):
"""Upload the content of self._plots to dokuwiki.
"""Upload content of ``self._plots`` to ``self.dokuwiki_url``.
Parameters
----------
Expand All @@ -51,16 +89,7 @@ def upload(self, report):

for plot, name in self._plots:
plot_file = tempfile.NamedTemporaryFile(suffix=".png")
axes = plot.axes[0]

if report.passed:
axes.text(0.8, 0.8, 'passed', transform=axes.transAxes,
bbox={'facecolor': 'green', 'alpha': 0.5, 'pad': 10})
else:
axes.text(0.8, 0.8, 'failed', transform=axes.transAxes,
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})

plot.savefig(plot_file.name)
self.save(plot, plot_file.name, report)

self.request.config.dokureport.doku_conn.medias.add(
"reports:{0}:{1}.png".format(tardis_githash[:7], name),
Expand All @@ -76,16 +105,6 @@ def upload(self, report):
)
plot_file.close()

def get_extras(self):
"""Return ``self.plot_html`` which is further added into html report.
Returns
-------
list
List of strings containing raw html snippet to embed images.
"""
return self.plot_html


thumbnail_html_local = """
<div class="image" style="float: left">
Expand All @@ -96,34 +115,12 @@ def get_extras(self):
"""


class LocalPlotSaver(object):
class LocalPlotSaver(BasePlotSaver):
def __init__(self, request, assets_dirpath):
"""A helper class to collect plots from integration tests and save
them in a particular directory locally.
Parameters
----------
request : _pytest.python.RequestObject
"""
self.request = request
self._plots = list()
self.plot_html = list()
self.assets_dirpath = assets_dirpath

def add(self, plot, name):
"""Accept a plot figure and add it to ``self._plots``.
Parameters
----------
plot : matplotlib.pyplot.figure
name : str
"""
self._plots.append((plot, name))
super(LocalPlotSaver, self).__init__(request, assets_dirpath=assets_dirpath)

def upload(self, report):
"""Upload (save) content of ``self._plots`` to ``self.assets_dirpath``.
"""Save content of ``self._plots`` to ``self.assets_dirpath``.
Parameters
----------
Expand All @@ -132,27 +129,10 @@ def upload(self, report):
"""

for plot, name in self._plots:
axes = plot.axes[0]

if report.passed:
axes.text(0.8, 0.8, 'passed', transform=axes.transAxes,
bbox={'facecolor': 'green', 'alpha': 0.5, 'pad': 10})
else:
axes.text(0.8, 0.8, 'failed', transform=axes.transAxes,
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 10})

plot.savefig(os.path.join(self.assets_dirpath, "{0}.png".format(name)))
self.save(plot, os.path.join(
self.assets_dirpath, "{0}.png".format(name)), report
)

self.plot_html.append(extras.html(
thumbnail_html_local.format(name=name))
)

def get_extras(self):
"""Return ``self.plot_html`` which is further added into html report.
Returns
-------
list
List of strings containing raw html snippet to embed images.
"""
return self.plot_html

0 comments on commit c5a3c7a

Please sign in to comment.