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) # test selection/deselection in the view w.set_data(v) view = w.values_edit model = view.model() assert model.rowCount() sel_model = view.selectionModel() model = sel_model.model() sel_model.select(model.index(0, 0), QItemSelectionModel.Select) sel_model.select(model.index(0, 0), QItemSelectionModel.Deselect)
def test_discrete_editor(self): w = DiscreteVariableEditor() self.assertEqual(w.get_data(), (None, [])) v = Categorical("C", ("a", "b", "c"), None, (("A", "1"), ("B", "b"))) w.set_data(v) self.assertEqual(w.name_edit.text(), v.name) self.assertEqual(w.labels_model.get_dict(), dict(v.annotations)) self.assertEqual(w.get_data(), (v, [])) w.set_data(None) self.assertEqual(w.name_edit.text(), "") self.assertEqual(w.labels_model.get_dict(), {}) self.assertEqual(w.get_data(), (None, [])) mapping = [("c", "C"), ("a", "A"), ("b", None), (None, "b")] w.set_data(v, [CategoriesMapping(mapping)]) w.grab() # run delegate paint method self.assertEqual(w.get_data(), (v, [CategoriesMapping(mapping)])) # test selection/deselection in the view w.set_data(v) view = w.values_edit model = view.model() assert model.rowCount() sel_model = view.selectionModel() model = sel_model.model() sel_model.select(model.index(0, 0), QItemSelectionModel.Select) sel_model.select(model.index(0, 0), QItemSelectionModel.Deselect)
def test_discrete_editor_merge_action(self): """ This function check whether results of dialog have effect on merging the attributes. The dialog itself is tested separately. """ w = DiscreteVariableEditor() v = Categorical("C", ("a", "b", "c"), (("A", "1"), ("B", "b")), False) w.set_data_categorical( v, [0, 0, 0, 1, 1, 2], [CategoriesMapping([ ("a", "AA"), ("b", "BB"), ("c", "CC"), ])]) view = w.values_edit model = view.model() selmodel = view.selectionModel() # type: QItemSelectionModel selmodel.select(QItemSelection(model.index(0, 0), model.index(1, 0)), QItemSelectionModel.ClearAndSelect) spy = QSignalSpy(w.variable_changed) w.merge_items.trigger() self.assertEqual(model.index(0, 0).data(Qt.EditRole), "other") self.assertEqual(model.index(1, 0).data(Qt.EditRole), "other") self.assertEqual(model.index(2, 0).data(Qt.EditRole), "CC") self.assertSequenceEqual(list(spy), [[]], 'variable_changed should emit exactly once')
def test_discrete_editor_merge_action(self): w = DiscreteVariableEditor() v = Categorical("C", ("a", "b", "c"), (("A", "1"), ("B", "b"))) w.set_data(v) action = w.merge_items self.assertFalse(action.isEnabled()) view = w.values_edit model = view.model() selmodel = view.selectionModel() # type: QItemSelectionModel selmodel.select( QItemSelection(model.index(0, 0), model.index(1, 0)), QItemSelectionModel.ClearAndSelect ) self.assertTrue(action.isEnabled()) # trigger the action, then find the active popup, and simulate entry spy = QSignalSpy(w.variable_changed) w.merge_items.trigger() cb = w.findChild(QComboBox) cb.setCurrentText("BA") cb.activated[str].emit("BA") cb.close() self.assertEqual(model.index(0, 0).data(Qt.EditRole), "BA") self.assertEqual(model.index(1, 0).data(Qt.EditRole), "BA") self.assertSequenceEqual( list(spy), [[]], 'variable_changed should emit exactly once' )
def test_discrete_editor(self): w = DiscreteVariableEditor() self.assertEqual(w.get_data(), (None, [])) v = Categorical("C", ("a", "b", "c"), (("A", "1"), ("B", "b"))) values = [0, 0, 0, 1, 1, 2] w.set_data_categorical(v, values) self.assertEqual(w.name_edit.text(), v.name) self.assertFalse(w.ordered_cb.isChecked()) self.assertEqual(w.labels_model.get_dict(), dict(v.annotations)) self.assertEqual(w.get_data(), (v, [])) w.set_data_categorical(None, None) self.assertEqual(w.name_edit.text(), "") self.assertEqual(w.labels_model.get_dict(), {}) self.assertEqual(w.get_data(), (None, [])) mapping = [ ("c", "C"), ("a", "A"), ("b", None), (None, "b") ] w.set_data_categorical(v, values, [CategoriesMapping(mapping)]) w.grab() # run delegate paint method self.assertEqual(w.get_data(), (v, [CategoriesMapping(mapping)])) w.set_data_categorical( v, values, [CategoriesMapping(mapping), ChangeOrdered(True)] ) self.assertTrue(w.ordered_cb.isChecked()) self.assertEqual( w.get_data()[1], [CategoriesMapping(mapping), ChangeOrdered(True)] ) # test selection/deselection in the view w.set_data_categorical(v, values) view = w.values_edit model = view.model() assert model.rowCount() sel_model = view.selectionModel() model = sel_model.model() sel_model.select(model.index(0, 0), QItemSelectionModel.Select) sel_model.select(model.index(0, 0), QItemSelectionModel.Deselect) # merge mapping mapping = [ ("a", "a"), ("b", "b"), ("c", "b") ] w.set_data_categorical(v, values, [CategoriesMapping(mapping)]) self.assertEqual(w.get_data()[1], [CategoriesMapping(mapping)]) self.assertEqual(model.data(model.index(0, 0), MultiplicityRole), 1) self.assertEqual(model.data(model.index(1, 0), MultiplicityRole), 2) self.assertEqual(model.data(model.index(2, 0), MultiplicityRole), 2) w.grab() model.setData(model.index(0, 0), "b", Qt.EditRole) self.assertEqual(model.data(model.index(0, 0), MultiplicityRole), 3) self.assertEqual(model.data(model.index(1, 0), MultiplicityRole), 3) self.assertEqual(model.data(model.index(2, 0), MultiplicityRole), 3) w.grab()
def test_discrete_editor_context_menu(self): w = DiscreteVariableEditor() v = Categorical("C", ("a", "b", "c"), (("A", "1"), ("B", "b")), False) w.set_data_categorical(v, []) view = w.values_edit model = view.model() pos = view.visualRect(model.index(0, 0)).center() with patch.object(QMenu, "setVisible", return_value=None) as m: contextMenu(view.viewport(), pos) m.assert_called() menu = view.findChild(QMenu) self.assertIsNotNone(menu) menu.close()
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)
def test_discrete_editor_merge_action(self): """ This function check whether results of dialog have effect on merging the attributes. The dialog itself is tested separately. """ w = DiscreteVariableEditor() v = Categorical("C", ("a", "b", "c"), (("A", "1"), ("B", "b")), False) w.set_data_categorical(v, [0, 0, 0, 1, 1, 2]) view = w.values_edit model = view.model() selmodel = view.selectionModel() # type: QItemSelectionModel selmodel.select(QItemSelection(model.index(0, 0), model.index(1, 0)), QItemSelectionModel.ClearAndSelect) # trigger the action, then find the active popup, and simulate entry w.merge_items.trigger() self.assertEqual(model.index(0, 0).data(Qt.EditRole), "other") self.assertEqual(model.index(1, 0).data(Qt.EditRole), "other")
def test_discrete_editor_rename_selected_items_action(self): w = DiscreteVariableEditor() v = Categorical("C", ("a", "b", "c"), (("A", "1"), ("B", "b")), False) w.set_data_categorical(v, []) action = w.rename_selected_items view = w.values_edit model = view.model() selmodel = view.selectionModel() # type: QItemSelectionModel selmodel.select(QItemSelection(model.index(0, 0), model.index(1, 0)), QItemSelectionModel.ClearAndSelect) # trigger the action, then find the active popup, and simulate entry spy = QSignalSpy(w.variable_changed) with patch.object(QComboBox, "setVisible", return_value=None) as m: action.trigger() m.assert_called() cb = view.findChild(QComboBox) cb.setCurrentText("BA") view.commitData(cb) self.assertEqual(model.index(0, 0).data(Qt.EditRole), "BA") self.assertEqual(model.index(1, 0).data(Qt.EditRole), "BA") self.assertSequenceEqual(list(spy), [[]], 'variable_changed should emit exactly once')
def test_discrete_editor_add_remove_action(self): w = DiscreteVariableEditor() v = Categorical("C", ("a", "b", "c"), (("A", "1"), ("B", "b"))) values = [0, 0, 0, 1, 1, 2] w.set_data_categorical(v, values) action_add = w.add_new_item action_remove = w.remove_item view = w.values_edit model, selection = view.model(), view.selectionModel() selection.clear() action_add.trigger() self.assertTrue(view.state() == view.EditingState) editor = view.focusWidget() assert isinstance(editor, QLineEdit) spy = QSignalSpy(model.dataChanged) QTest.keyClick(editor, Qt.Key_D) QTest.keyClick(editor, Qt.Key_Return) self.assertTrue(model.rowCount() == 4) # The commit to model is executed via a queued invoke self.assertTrue(bool(spy) or spy.wait()) self.assertEqual(model.index(3, 0).data(Qt.EditRole), "d") # remove it spy = QSignalSpy(model.rowsRemoved) action_remove.trigger() self.assertEqual(model.rowCount(), 3) self.assertEqual(len(spy), 1) _, first, last = spy[0] self.assertEqual((first, last), (3, 3)) # remove/drop and existing value selection.select(model.index(1, 0), QItemSelectionModel.ClearAndSelect) removespy = QSignalSpy(model.rowsRemoved) changedspy = QSignalSpy(model.dataChanged) action_remove.trigger() self.assertEqual(len(removespy), 0, "Should only mark item as removed") self.assertGreaterEqual(len(changedspy), 1, "Did not change data") w.grab()