Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions Orange/widgets/data/oweditdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class VariableEditor(QWidget):

def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.var = None
self.setup_gui()

def setup_gui(self):
Expand Down Expand Up @@ -216,7 +217,7 @@ def get_data(self):
labels = self.labels_model.get_dict()

# Is the variable actually changed.
if not self.is_same():
if self.var is not None and not self.is_same():
var = type(self.var)(name)
var.attributes.update(labels)
self.var = var
Expand All @@ -231,7 +232,8 @@ def is_same(self):
name = str(self.name_edit.text())
labels = self.labels_model.get_dict()

return self.var and name == self.var.name and labels == self.var.attributes
return (self.var is not None and name == self.var.name and
labels == self.var.attributes)

def clear(self):
"""Clear the editor state.
Expand Down Expand Up @@ -319,7 +321,7 @@ def get_data(self):
labels = self.labels_model.get_dict()
values = map(str, self.values_model)

if not self.is_same():
if self.var is not None and not self.is_same():
var = type(self.var)(name, values=values)
var.attributes.update(labels)
self.var = var
Expand All @@ -331,8 +333,9 @@ def get_data(self):
def is_same(self):
"""Is the current model state the same as the input.
"""
values = map(str, self.values_model)
return VariableEditor.is_same(self) and self.var.values == values
values = list(map(str, self.values_model))
return (VariableEditor.is_same(self) and self.var is not None and
self.var.values == values)

def clear(self):
"""Clear the model state.
Expand Down
60 changes: 57 additions & 3 deletions Orange/widgets/data/tests/test_oweditdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

from AnyQt.QtCore import QModelIndex, Qt

from Orange.data import ContinuousVariable, DiscreteVariable, Table, Domain
from Orange.data import ContinuousVariable, DiscreteVariable, \
StringVariable, Table, Domain
from Orange.widgets.data.oweditdomain import EditDomainReport, OWEditDomain, \
ContinuousVariableEditor
ContinuousVariableEditor, DiscreteVariableEditor, VariableEditor
from Orange.widgets.data.owcolor import OWColor, ColorRole
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.tests.base import WidgetTest, GuiTest

SECTION_NAME = "NAME"

Expand Down Expand Up @@ -138,3 +139,56 @@ def test_list_attributes_remain_lists(self):
self.widget.unconditional_commit()
t2 = self.get_output("Data")
self.assertEqual(t2.domain["a"].attributes["list"], [1, 2, 4])


class TestEditors(GuiTest):
def test_variable_editor(self):
w = VariableEditor()
self.assertIs(w.get_data(), None)

v = StringVariable(name="S")
v.attributes.update({"A": 1, "B": "b"},)
w.set_data(v)

self.assertEqual(w.name_edit.text(), v.name)
self.assertEqual(w.labels_model.get_dict(), v.attributes)
self.assertTrue(w.is_same())

w.set_data(None)
self.assertEqual(w.name_edit.text(), "")
self.assertEqual(w.labels_model.get_dict(), {})
self.assertIs(w.get_data(), None)

def test_continuous_editor(self):
w = ContinuousVariableEditor()
self.assertIs(w.get_data(), None)

v = ContinuousVariable("X", number_of_decimals=5)
v.attributes.update({"A": 1, "B": "b"})
w.set_data(v)

self.assertEqual(w.name_edit.text(), v.name)
self.assertEqual(w.labels_model.get_dict(), v.attributes)
self.assertTrue(w.is_same())

w.set_data(None)
self.assertEqual(w.name_edit.text(), "")
self.assertEqual(w.labels_model.get_dict(), {})
self.assertIs(w.get_data(), None)

def test_discrete_editor(self):
w = DiscreteVariableEditor()
self.assertIs(w.get_data(), None)

v = DiscreteVariable("C", values=["a", "b", "c"])
v.attributes.update({"A": 1, "B": "b"})
w.set_data(v)

self.assertEqual(w.name_edit.text(), v.name)
self.assertEqual(w.labels_model.get_dict(), v.attributes)
self.assertTrue(w.is_same())

w.set_data(None)
self.assertEqual(w.name_edit.text(), "")
self.assertEqual(w.labels_model.get_dict(), {})
self.assertIs(w.get_data(), None)