def test_set_list(self): model = FilterCheckboxListModel() model.set_list(self.data) self.assertEqual(model._data, sorted(self.data)) self.assertEqual(model._data_set, set(self.data)) self.assertEqual(model._selected, set(self.data)) self.assertTrue(model._all_selected)
def test_click_select_all_when_all_selected(self): model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(0, 0) model.click_index(index) self.assertFalse(model._all_selected) self.assertEqual(model._selected, set())
def test_remove_filter(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') model.remove_filter() self.assertFalse(model._is_filtered) self.assertEqual(model._selected, set(self.data)) self.assertEqual(model.rowCount(), 8)
def test_click_selected_item(self): model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(2, 0) model.click_index(index) self.assertEqual(model._selected, set(self.data).difference(set(['a']))) self.assertFalse(model._all_selected)
def test_remove_items_selected(self): items = set('a') model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.beginResetModel") as br, \ mock.patch("tabularview_models.FilterCheckboxListModel.endResetModel") as er: model.remove_items(items) self.assertEqual(model._selected, set(self.data[1:])) self.assertTrue(model._all_selected)
def test_click_select_empty_when_unselected(self): model = FilterCheckboxListModel() model.set_list(self.data) model._empty_selected = False with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(1, 0) model.click_index(index) self.assertTrue(model._empty_selected) self.assertTrue(model._all_selected)
def test_apply_filter_with_replace(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') model.apply_filter() self.assertFalse(model._is_filtered) self.assertEqual(model._selected, set(self.data[3:])) self.assertEqual(model.rowCount(), 8) self.assertFalse(model._empty_selected)
def test_add_item_without_select_without_filter(self): new_item = ['aaaa'] model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.beginInsertRows") as bir, \ mock.patch("tabularview_models.FilterCheckboxListModel.endInsertRows") as eir, \ mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: model.add_item(new_item, selected=False) self.assertFalse(model._all_selected)
def test_remove_items_filtered_data_midle(self): items = set('bb') model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') with mock.patch("tabularview_models.FilterCheckboxListModel.beginResetModel") as br, \ mock.patch("tabularview_models.FilterCheckboxListModel.endResetModel") as er: model.remove_items(items) self.assertEqual(model._filter_index, [3,4])
def test_click_unselected_item(self): model = FilterCheckboxListModel() model.set_list(self.data) model._selected.discard('a') with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(2, 0) model.click_index(index) self.assertEqual(model._selected, set(self.data)) self.assertTrue(model._all_selected)
def test_add_item_with_select_without_filter(self): new_item = ['aaaa'] model = FilterCheckboxListModel() model.set_list(self.data) with mock.patch("tabularview_models.FilterCheckboxListModel.beginInsertRows") as bir, \ mock.patch("tabularview_models.FilterCheckboxListModel.endInsertRows") as eir, \ mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: model.add_item(new_item) self.assertEqual(model._data, sorted(self.data + new_item)) self.assertEqual(model._data_set, set(self.data + new_item))
def test_add_item_with_select_with_filter_middle(self): new_item = ['b1'] model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') with mock.patch("tabularview_models.FilterCheckboxListModel.beginInsertRows") as bir, \ mock.patch("tabularview_models.FilterCheckboxListModel.endInsertRows") as eir, \ mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: model.add_item(new_item) self.assertEqual(model._filter_index, [3,4,5,6]) self.assertEqual(model.data(model.index(1 + 2, 0)), new_item[0])
def test_apply_filter_with_add(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') model._add_to_selection = True model._selected_filtered.discard('bbb') model.apply_filter() self.assertFalse(model._is_filtered) self.assertEqual(model._selected, set(self.data[:5])) self.assertEqual(model.rowCount(), 8) self.assertFalse(model._all_selected)
def test_click_select_all_when_all_not_selected_and_filtered(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') with mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: index = model.index(2, 0) model.click_index(index) index = model.index(0, 0) model.click_index(index) self.assertTrue(model._all_selected) self.assertEqual(model._selected, set(self.data)) self.assertEqual(model._selected_filtered, set(self.data[3:]))
def test_remove_items_filtered_data_not_selected(self): items = set('b') model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') model._selected_filtered.discard('a') model._all_selected = False with mock.patch("tabularview_models.FilterCheckboxListModel.beginResetModel") as br, \ mock.patch("tabularview_models.FilterCheckboxListModel.endResetModel") as er: model.remove_items(items) self.assertEqual(model._selected_filtered, set(self.data[4:])) self.assertTrue(model._all_selected)
def test_add_item_with_select_with_filter_last(self): new_item = ['bbbb'] model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') with mock.patch("tabularview_models.FilterCheckboxListModel.beginInsertRows") as bir, \ mock.patch("tabularview_models.FilterCheckboxListModel.endInsertRows") as eir, \ mock.patch("tabularview_models.FilterCheckboxListModel.dataChanged") as dc: model.add_item(new_item) self.assertEqual(model._data, sorted(self.data + new_item)) self.assertEqual(model._data_set, set(self.data + new_item)) self.assertEqual(model._filter_index, [3,4,5,6]) self.assertEqual(model._selected_filtered, set(self.data[3:] + new_item)) self.assertEqual(model.data(model.index(3 + 2, 0)), new_item[0])
def __init__(self, parent=None): super().__init__(parent) # parameters self._filter_state = set() self._filter_empty_state = False self._search_text = '' self.search_delay = 200 # create ui elements self._ui_vertical_layout = QVBoxLayout(self) self._ui_list = QListView() self._ui_edit = QLineEdit() self._ui_edit.setPlaceholderText('Search') self._ui_edit.setClearButtonEnabled(True) self._ui_buttons = QDialogButtonBox(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) self._ui_vertical_layout.addWidget(self._ui_edit) self._ui_vertical_layout.addWidget(self._ui_list) self._ui_vertical_layout.addWidget(self._ui_buttons) # add models # used to limit search so it doesn't search when typing self._search_timer = QTimer() self._search_timer.setSingleShot(True) self._filter_model = FilterCheckboxListModel() self._filter_model.set_list(self._filter_state) self._ui_list.setModel(self._filter_model) # connect signals self._ui_list.clicked.connect(self._filter_model.click_index) self._search_timer.timeout.connect(self._filter_list) self._ui_edit.textChanged.connect(self._text_edited) self._ui_buttons.button(QDialogButtonBox.Ok).clicked.connect( self._apply_filter) self._ui_buttons.button(QDialogButtonBox.Cancel).clicked.connect( self._cancel_filter)
def test_set_filter_index(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertEqual(model._filter_index, [3, 4, 5])
def test_init_model(self): FilterCheckboxListModel()
def test_add_to_selection_when_filter(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertFalse(model._add_to_selection) self.assertFalse(model.data(model.index(1,0), Qt.CheckStateRole))
def test_rowCount_when_filter(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertEqual(model.rowCount(), 5)
def test_selected_when_filtered(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertEqual(model._selected, set(self.data)) self.assertEqual(model._selected_filtered, set(self.data[3:]))
def test_is_all_selected_when_not_empty_selected(self): model = FilterCheckboxListModel() model.set_list(self.data) model._empty_selected = False self.assertFalse(model._is_all_selected())
def test_is_all_selected_when_not_all_selected(self): model = FilterCheckboxListModel() model.set_list(self.data) model._selected.discard('a') self.assertFalse(model._is_all_selected())
def test_is_all_selected_when_all_selected(self): model = FilterCheckboxListModel() model.set_list(self.data) self.assertTrue(model._is_all_selected())
def test_get_data_when_filtered(self): model = FilterCheckboxListModel() model.set_list(self.data) model.set_filter('b') self.assertEqual(model.data(model.index(2,0)), 'b')