Skip to content

Commit

Permalink
Create base test for Qt exporter tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Sep 8, 2023
1 parent 54b2a5b commit e2f9f77
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 136 deletions.
56 changes: 56 additions & 0 deletions glue_plotly/html_exporters/qt/tests/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os

from mock import patch

from glue_qt.app import GlueApplication


class TestQtExporter:

viewer_type = None
tool_id = None

def setup_method(self, method):
self.data = self.make_data()
self.app = GlueApplication()
self.app.session.data_collection.append(self.data)
self.viewer = self.app.new_data_viewer(self.viewer_type)
self.viewer.add_data(self.data)
for subtool in self.viewer.toolbar.tools['save'].subtools:
if subtool.tool_id == self.tool_id:
self.tool = subtool
break
else:
raise Exception(f"Could not find {self.tool_id} tool in viewer")

def teardown_method(self, method):
self.viewer.close(warn=False)
self.viewer = None
self.app.close()
self.app = None

def auto_accept_hoverdialog(self):
def exec_replacement(dialog):
dialog.select_all()
dialog.accept()
return exec_replacement

def auto_accept_messagebox(self):
def exec_replacement(box):
box.accept()
return exec_replacement

def export_figure(self, tmpdir, output_filename):
output_path = tmpdir.join(output_filename).strpath
with patch('qtpy.compat.getsavefilename') as fd:
fd.return_value = output_path, 'html'
self.tool.activate()
return output_path

def test_default(self, tmpdir):
output_path = self.export_figure(tmpdir, 'test_default.html')
assert os.path.exists(output_path)

def make_data(self):
raise NotImplementedError()

39 changes: 6 additions & 33 deletions glue_plotly/html_exporters/qt/tests/test_dendrogram.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
import os

from mock import patch

from glue.core import Data
from glue_qt.app import GlueApplication
from glue_qt.plugins.dendro_viewer.data_viewer import DendrogramViewer

from .base import TestQtExporter

class TestDendrogram:

def setup_method(self, method):
self.data = Data(label='dendrogram', parent=[-1, 0, 1, 1], height=[1.3, 2.2, 3.2, 4.4])
self.app = GlueApplication()
self.app.session.data_collection.append(self.data)
self.viewer = self.app.new_data_viewer(DendrogramViewer)
self.viewer.add_data(self.data)
for subtool in self.viewer.toolbar.tools['save'].subtools:
if subtool.tool_id == 'save:plotlydendro':
self.tool = subtool
break
else:
raise Exception("Could not find save:plotlydendro tool in viewer")

def teardown_method(self, method):
self.viewer.close(warn=False)
self.viewer = None
self.app.close()
self.app = None
class TestDendrogram(TestQtExporter):

def export_figure(self, tmpdir, output_filename):
output_path = tmpdir.join(output_filename).strpath
with patch('qtpy.compat.getsavefilename') as fd:
fd.return_value = output_path, 'html'
self.tool.activate()
return output_path
viewer_type = DendrogramViewer
tool_id = 'save:plotlydendro'

def test_default(self, tmpdir):
output_path = self.export_figure(tmpdir, 'test_default.html')
assert os.path.exists(output_path)
def make_data(self):
return Data(label='dendrogram', parent=[-1, 0, 1, 1], height=[1.3, 2.2, 3.2, 4.4])
46 changes: 11 additions & 35 deletions glue_plotly/html_exporters/qt/tests/test_histogram.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,20 @@
import os

from mock import patch

from glue.core import Data
from glue_qt.app import GlueApplication
from glue_qt.viewers.histogram import HistogramViewer

from .base import TestQtExporter

class TestHistogram:

def setup_method(self, method):
self.data = Data(x=[40, 41, 37, 63, 78, 35, 19, 100, 35, 86, 84, 99,
87, 56, 2, 71, 22, 36, 10, 1, 26, 70, 45, 20, 8],
label='d1')
self.app = GlueApplication()
self.app.session.data_collection.append(self.data)
self.viewer = self.app.new_data_viewer(HistogramViewer)
self.viewer.add_data(self.data)
for subtool in self.viewer.toolbar.tools['save'].subtools:
if subtool.tool_id == 'save:plotlyhist':
self.tool = subtool
break
else:
raise Exception("Could not find save:plotlyhist tool in viewer")

def teardown_method(self, method):
self.viewer.close(warn=False)
self.viewer = None
self.app.close()
self.app = None
class TestHistogram(TestQtExporter):

def export_figure(self, tmpdir, output_filename):
output_path = tmpdir.join(output_filename).strpath
with patch('qtpy.compat.getsavefilename') as fd:
fd.return_value = output_path, 'html'
self.tool.activate()
return output_path
viewer_type = HistogramViewer
tool_id = 'save:plotlyhist'

def test_default(self, tmpdir):
def setup_method(self, method):
super().setup_method(method)
self.viewer.state.x_att = self.data.id['x']
self.viewer.state.hist_n_bin = 6
output_path = self.export_figure(tmpdir, 'test_default.html')
assert os.path.exists(output_path)

def make_data(self):
return Data(x=[40, 41, 37, 63, 78, 35, 19, 100, 35, 86, 84, 99,
87, 56, 2, 71, 22, 36, 10, 1, 26, 70, 45, 20, 8],
label='d1')
42 changes: 8 additions & 34 deletions glue_plotly/html_exporters/qt/tests/test_image.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,15 @@
import os

from mock import patch
import numpy as np

from glue.core import Data
from glue_qt.app import GlueApplication
from glue_qt.viewers.image import ImageViewer
from glue_qt.viewers.image.data_viewer import ImageViewer

from numpy import arange, ones

class TestImage:
from .base import TestQtExporter

def setup_method(self, method):
self.data = Data(label='d1', x=np.arange(24).reshape((2, 3, 4)), y=np.ones((2, 3, 4)))
self.app = GlueApplication()
self.app.session.data_collection.append(self.data)
self.viewer = self.app.new_data_viewer(ImageViewer)
self.viewer.add_data(self.data)
for subtool in self.viewer.toolbar.tools['save'].subtools:
if subtool.tool_id == 'save:plotlyimage2d':
self.tool = subtool
break
else:
raise Exception("Could not find save:plotlyimage2d tool in viewer")

def teardown_method(self, method):
self.viewer.close(warn=False)
self.viewer = None
self.app.close()
self.app = None
class TestImage(TestQtExporter):

def export_figure(self, tmpdir, output_filename):
output_path = tmpdir.join(output_filename).strpath
with patch('qtpy.compat.getsavefilename') as fd:
fd.return_value = output_path, 'html'
self.tool.activate()
return output_path
viewer_type = ImageViewer
tool_id = 'save:plotlyimage'

def test_default(self, tmpdir):
output_path = self.export_figure(tmpdir, 'test_default.html')
assert os.path.exists(output_path)
def make_data(self):
return Data(label='d1', x=arange(24).reshape((2, 3, 4)), y=ones((2, 3, 4)))
44 changes: 10 additions & 34 deletions glue_plotly/html_exporters/qt/tests/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,19 @@
import os

from mock import patch

from glue.core import Data
from glue_qt.app import GlueApplication
from glue_qt.viewers.profile import ProfileViewer

from .base import TestQtExporter

class TestProfile:

def setup_method(self, method):
self.data = Data(x=[40, 41, 37, 63, 78, 35, 19, 100, 35, 86, 84, 99,
87, 56, 2, 71, 22, 36, 10, 1, 26, 70, 45, 20, 8],
label='d1')
self.app = GlueApplication()
self.app.session.data_collection.append(self.data)
self.viewer = self.app.new_data_viewer(ProfileViewer)
self.viewer.add_data(self.data)
for subtool in self.viewer.toolbar.tools['save'].subtools:
if subtool.tool_id == 'save:plotlyprofile':
self.tool = subtool
break
else:
raise Exception("Could not find save:plotlyprofile tool in viewer")
class TestProfile(TestQtExporter):

def teardown_method(self, method):
self.viewer.close(warn=False)
self.viewer = None
self.app.close()
self.app = None
viewer_type = ProfileViewer
tool_id = 'save:plotlyprofile'

def export_figure(self, tmpdir, output_filename):
output_path = tmpdir.join(output_filename).strpath
with patch('qtpy.compat.getsavefilename') as fd:
fd.return_value = output_path, 'html'
self.tool.activate()
return output_path
def make_data(self):
return Data(x=[40, 41, 37, 63, 78, 35, 19, 100, 35, 86, 84, 99,
87, 56, 2, 71, 22, 36, 10, 1, 26, 70, 45, 20, 8],
label='d1')

def test_default(self, tmpdir):
def setup_method(self, method):
super().setup_method(method)
self.viewer.state.x_att = self.data.id['Pixel Axis 0 [x]']
output_path = self.export_figure(tmpdir, 'test_default.html')
assert os.path.exists(output_path)

0 comments on commit e2f9f77

Please sign in to comment.