Skip to content

Commit

Permalink
Unittests: Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
VesnaT committed Oct 14, 2016
1 parent 88d226c commit c8341a9
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 155 deletions.
70 changes: 70 additions & 0 deletions Orange/widgets/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import unittest

import numpy as np

from Orange.base import SklLearner, SklModel
from PyQt4.QtGui import (QApplication, QComboBox, QSpinBox, QDoubleSpinBox,
QSlider)
import sip

from Orange.data import Table
from Orange.misc.flagged_data import FLAGGED_SIGNAL_NAME, FLAGGED_FEATURE_NAME
from Orange.preprocess import RemoveNaNColumns, Randomize
from Orange.preprocess.preprocess import PreprocessorList
from Orange.classification.base_classification import (LearnerClassification,
Expand Down Expand Up @@ -476,3 +479,70 @@ def get_value(learner, name):
self.assertFalse(self.widget.Error.active)
else:
self.assertTrue(self.widget.Error.active)


class WidgetOutputsTestMixin:
"""Class for widget's outputs testing.
Contains init method to set up testing parameters and a test method, which
checks Selected Data and Flagged Data outputs.
Since widgets have different ways of selecting data instances, _select_data
method should be implemented when subclassed.
If output's expected domain differs from input's domain, parameter
same_input_output_domain should be set to False.
If Selected Data and Flagged Data domains differ, override method
_compare_selected_flagged_domains.
"""

def init(self):
self.data = Table("iris")
self.same_input_output_domain = True

def test_outputs(self):
self.send_signal(self.signal_name, self.signal_data)

# only needed in TestOWMDS
if type(self).__name__ == "TestOWMDS":
from PyQt4.QtCore import QEvent
self.widget.customEvent(QEvent(QEvent.User))
self.widget.commit()

# check selected data output
self.assertIsNone(self.get_output("Selected Data"))

# check flagged data output
flagged = self.get_output(FLAGGED_SIGNAL_NAME)
self.assertEqual(0, np.sum([i[FLAGGED_FEATURE_NAME] for i in flagged]))

# select data instances
self._select_data()

# check selected data output
selected = self.get_output("Selected Data")
self.assertGreater(len(selected), 0)
if self.same_input_output_domain:
self.assertEqual(selected.domain, self.data.domain)

# check flagged data output
flagged = self.get_output(FLAGGED_SIGNAL_NAME)
self.assertEqual(len(selected),
np.sum([i[FLAGGED_FEATURE_NAME] for i in flagged]))

# compare selected and flagged data domains
self._compare_selected_flagged_domains(selected, flagged)

# check output when data is removed
self.send_signal(self.signal_name, None)
self.assertIsNone(self.get_output("Selected Data"))
self.assertIsNone(self.get_output(FLAGGED_SIGNAL_NAME))

def _select_data(self):
raise NotImplementedError("Subclasses should implement select_data")

def _compare_selected_flagged_domains(self, selected, flagged):
selected_vars = selected.domain.variables + selected.domain.metas
flagged_vars = flagged.domain.variables + flagged.domain.metas
self.assertTrue(all((var in flagged_vars for var in selected_vars)))
50 changes: 12 additions & 38 deletions Orange/widgets/unsupervised/tests/test_owdistancemap.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
import random
import numpy as np
from Orange.data import Table
from Orange.misc.flagged_data import FLAGGED_SIGNAL_NAME, FLAGGED_FEATURE_NAME
from Orange.distance import Euclidean
from Orange.widgets.unsupervised.owdistancemap import OWDistanceMap
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin


class TestOWDistanceMap(WidgetTest):
def setUp(self):
self.widget = self.create_widget(OWDistanceMap)
self.iris = Table("iris")
self.iris_distances = Euclidean(self.iris)

def test_outputs(self):
self.send_signal("Distances", self.iris_distances)
class TestOWDistanceMap(WidgetTest, WidgetOutputsTestMixin):
@classmethod
def setUpClass(cls):
super().setUpClass()
WidgetOutputsTestMixin.init(cls)

# check selected data output
self.assertIsNone(self.get_output("Selected Data"))
cls.signal_name = "Distances"
cls.signal_data = Euclidean(cls.data)

# check flagged data output
flagged = self.get_output(FLAGGED_SIGNAL_NAME)
self.assertEqual(0, np.sum([i[FLAGGED_FEATURE_NAME] for i in flagged]))
def setUp(self):
self.widget = self.create_widget(OWDistanceMap)

# select data points
points = random.sample(range(0, len(self.iris)), 20)
def _select_data(self):
points = random.sample(range(0, len(self.data)), 20)
self.widget._selection = points
self.widget.commit()

# check selected data output
selected = self.get_output("Selected Data")
self.assertEqual(len(selected), len(points))

# check flagged data output
flagged = self.get_output(FLAGGED_SIGNAL_NAME)
self.assertEqual(len(selected),
np.sum([i[FLAGGED_FEATURE_NAME] for i in flagged]))

# compare selected and flagged data domains
selected_vars = selected.domain.variables + selected.domain.metas
flagged_vars = flagged.domain.variables + flagged.domain.metas
self.assertTrue(all((var in flagged_vars for var in selected_vars)))

# check output when data is removed
self.send_signal("Distances", None)
self.assertIsNone(self.get_output("Selected Data"))
self.assertIsNone(self.get_output(FLAGGED_SIGNAL_NAME))
50 changes: 16 additions & 34 deletions Orange/widgets/unsupervised/tests/test_owhierarchicalclustering.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
import numpy as np
from Orange.data import Table
from Orange.misc.flagged_data import FLAGGED_SIGNAL_NAME, FLAGGED_FEATURE_NAME
from Orange.misc.flagged_data import FLAGGED_SIGNAL_NAME
from Orange.distance import Euclidean
from Orange.widgets.unsupervised.owhierarchicalclustering import \
OWHierarchicalClustering
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin


class TestOWHierarchicalClustering(WidgetTest):
def setUp(self):
self.widget = self.create_widget(OWHierarchicalClustering)
self.iris = Table("iris")
self.iris_distances = Euclidean(self.iris)

def test_outputs(self):
self.send_signal("Distances", self.iris_distances)
class TestOWHierarchicalClustering(WidgetTest, WidgetOutputsTestMixin):
@classmethod
def setUpClass(cls):
super().setUpClass()
WidgetOutputsTestMixin.init(cls)

# check selected data output
self.assertIsNone(self.get_output("Selected Data"))
cls.distances = Euclidean(cls.data)
cls.signal_name = "Distances"
cls.signal_data = cls.distances
cls.same_input_output_domain = False

# check flagged data output
flagged = self.get_output(FLAGGED_SIGNAL_NAME)
self.assertEqual(0, np.sum([i[FLAGGED_FEATURE_NAME] for i in flagged]))
def setUp(self):
self.widget = self.create_widget(OWHierarchicalClustering)

# select a cluster
def _select_data(self):
items = self.widget.dendrogram._items
cluster = items[next(iter(items))]
self.widget.dendrogram.set_selected_items([cluster])

# check selected data output
selected = self.get_output("Selected Data")
self.assertGreater(len(selected), 0)

# check flagged data output
flagged = self.get_output(FLAGGED_SIGNAL_NAME)
self.assertEqual(len(selected),
np.sum([i[FLAGGED_FEATURE_NAME] for i in flagged]))

# compare selected and flagged data domains
def _compare_selected_flagged_domains(self, selected, flagged):
self.assertTrue(all((var in flagged.domain.variables
for var in selected.domain.variables)))
self.assertNotIn("Other", selected.domain.metas[0].values)
Expand All @@ -48,14 +35,9 @@ def test_outputs(self):
all((var in [var.name for var in flagged.domain.metas]
for var in [var.name for var in selected.domain.metas])))

# check output when data is removed
self.send_signal("Distances", None)
self.assertIsNone(self.get_output("Selected Data"))
self.assertIsNone(self.get_output(FLAGGED_SIGNAL_NAME))

def test_selection_box_output(self):
"""Check output if Selection method changes"""
self.send_signal("Distances", self.iris_distances)
self.send_signal("Distances", self.distances)
self.assertIsNone(self.get_output("Selected Data"))
self.assertIsNotNone(self.get_output(FLAGGED_SIGNAL_NAME))

Expand Down
54 changes: 13 additions & 41 deletions Orange/widgets/unsupervised/tests/test_owmds.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,25 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring
import random
import numpy as np
from PyQt4.QtCore import QEvent
from Orange.data import Table
from Orange.misc.flagged_data import FLAGGED_SIGNAL_NAME, FLAGGED_FEATURE_NAME
from Orange.distance import Euclidean
from Orange.widgets.unsupervised.owmds import OWMDS
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin


class TestOWMDS(WidgetTest):
def setUp(self):
self.widget = self.create_widget(OWMDS)
self.iris = Table("iris")
self.iris_distances = Euclidean(self.iris)
class TestOWMDS(WidgetTest, WidgetOutputsTestMixin):
@classmethod
def setUpClass(cls):
super().setUpClass()
WidgetOutputsTestMixin.init(cls)

def test_outputs(self):
self.send_signal("Distances", self.iris_distances)
self.widget.customEvent(QEvent(QEvent.User))
self.widget.commit()
cls.signal_name = "Distances"
cls.signal_data = Euclidean(cls.data)
cls.same_input_output_domain = False

# check selected data output
self.assertIsNone(self.get_output("Selected Data"))

# check flagged data output
flagged = self.get_output(FLAGGED_SIGNAL_NAME)
self.assertEqual(0, np.sum([i[FLAGGED_FEATURE_NAME] for i in flagged]))
def setUp(self):
self.widget = self.create_widget(OWMDS)

# select data points
points = random.sample(range(0, len(self.iris)), 20)
def _select_data(self):
points = random.sample(range(0, len(self.data)), 20)
self.widget.select_indices(points)
self.widget.commit()

# check selected data output
selected = self.get_output("Selected Data")
self.assertEqual(len(selected), len(points))

# check flagged data output
flagged = self.get_output(FLAGGED_SIGNAL_NAME)
self.assertEqual(len(selected),
np.sum([i[FLAGGED_FEATURE_NAME] for i in flagged]))

# compare selected and flagged data domains
selected_vars = selected.domain.variables + selected.domain.metas
flagged_vars = flagged.domain.variables + flagged.domain.metas
self.assertTrue(all((var in flagged_vars for var in selected_vars)))

# check output when data is removed
self.send_signal("Distances", None)
self.assertIsNone(self.get_output("Selected Data"))
self.assertIsNone(self.get_output(FLAGGED_SIGNAL_NAME))
Loading

0 comments on commit c8341a9

Please sign in to comment.