From e2f9f771339771cb9961e063125de9d4e2d743a7 Mon Sep 17 00:00:00 2001 From: Jon Carifio Date: Fri, 8 Sep 2023 01:30:24 -0400 Subject: [PATCH] Create base test for Qt exporter tests. --- glue_plotly/html_exporters/qt/tests/base.py | 56 +++++++++++++++++++ .../qt/tests/test_dendrogram.py | 39 ++----------- .../html_exporters/qt/tests/test_histogram.py | 46 ++++----------- .../html_exporters/qt/tests/test_image.py | 42 +++----------- .../html_exporters/qt/tests/test_profile.py | 44 ++++----------- 5 files changed, 91 insertions(+), 136 deletions(-) create mode 100644 glue_plotly/html_exporters/qt/tests/base.py diff --git a/glue_plotly/html_exporters/qt/tests/base.py b/glue_plotly/html_exporters/qt/tests/base.py new file mode 100644 index 0000000..a1e7475 --- /dev/null +++ b/glue_plotly/html_exporters/qt/tests/base.py @@ -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() + diff --git a/glue_plotly/html_exporters/qt/tests/test_dendrogram.py b/glue_plotly/html_exporters/qt/tests/test_dendrogram.py index 175c8ae..61a5a2b 100644 --- a/glue_plotly/html_exporters/qt/tests/test_dendrogram.py +++ b/glue_plotly/html_exporters/qt/tests/test_dendrogram.py @@ -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]) diff --git a/glue_plotly/html_exporters/qt/tests/test_histogram.py b/glue_plotly/html_exporters/qt/tests/test_histogram.py index ca3d0af..8c84d71 100644 --- a/glue_plotly/html_exporters/qt/tests/test_histogram.py +++ b/glue_plotly/html_exporters/qt/tests/test_histogram.py @@ -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') diff --git a/glue_plotly/html_exporters/qt/tests/test_image.py b/glue_plotly/html_exporters/qt/tests/test_image.py index 534199f..b602574 100644 --- a/glue_plotly/html_exporters/qt/tests/test_image.py +++ b/glue_plotly/html_exporters/qt/tests/test_image.py @@ -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))) diff --git a/glue_plotly/html_exporters/qt/tests/test_profile.py b/glue_plotly/html_exporters/qt/tests/test_profile.py index 5e99895..8db5dc2 100644 --- a/glue_plotly/html_exporters/qt/tests/test_profile.py +++ b/glue_plotly/html_exporters/qt/tests/test_profile.py @@ -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)