def test_dataframemodel_set_data_complex(monkeypatch): """ Test that editing complex dtypes is handled gracefully in df editor. Unit regression test for issue #6115 . """ MockQMessageBox = Mock() attr_to_patch = ('spyder_modelx.widgets' + '.mxdataview.QMessageBox') monkeypatch.setattr(attr_to_patch, MockQMessageBox) test_params = [(1, numpy.complex128), (2, numpy.complex64), (3, complex)] for count, complex_type in test_params: test_df = DataFrame(numpy.arange(10, 15), dtype=complex_type) model = DataFrameModel(test_df.copy()) index = model.createIndex(2, 0) assert not model.setData(index, '42') MockQMessageBox.critical.assert_called_with(ANY, "Error", ANY) assert MockQMessageBox.critical.call_count == count try: assert numpy.sum(test_df[0].values == model.df.values) == len(test_df) except AttributeError: assert numpy.sum(test_df[0].as_matrix() == model.df.as_matrix()) == len(test_df)
def test_dataframemodel_set_data_overflow(monkeypatch): """ Test that entry of an overflowing integer is caught and handled properly. Unit regression test for issue #6114 . """ MockQMessageBox = Mock() attr_to_patch = ('spyder_modelx.widgets' + '.mxdataview.QMessageBox') monkeypatch.setattr(attr_to_patch, MockQMessageBox) # Numpy doesn't raise the OverflowError for ints smaller than 64 bits if not os.name == 'nt': int32_bit_exponent = 66 else: int32_bit_exponent = 34 test_parameters = [(1, numpy.int32, int32_bit_exponent), (2, numpy.int64, 66)] for idx, int_type, bit_exponent in test_parameters: test_df = DataFrame(numpy.arange(7, 11), dtype=int_type) model = DataFrameModel(test_df.copy()) index = model.createIndex(2, 0) assert not model.setData(index, str(int(2 ** bit_exponent))) MockQMessageBox.critical.assert_called_with(ANY, "Error", ANY) assert MockQMessageBox.critical.call_count == idx try: assert numpy.sum(test_df[0].values == model.df.values) == len(test_df) except AttributeError: assert numpy.sum(test_df[0].as_matrix() == model.df.as_matrix()) == len(test_df)
def test_dataframemodel_set_data_bool(monkeypatch): """Test that bools are editible in df and false-y strs are detected.""" MockQMessageBox = Mock() attr_to_patch = ('spyder_modelx.widgets' + '.mxdataview.QMessageBox') monkeypatch.setattr(attr_to_patch, MockQMessageBox) test_params = [numpy.bool_, numpy.bool, bool] test_strs = ['foo', 'false', 'f', '0', '0.', '0.0', '', ' '] expected_df = DataFrame([1, 0, 0, 0, 0, 0, 0, 0, 0], dtype=bool) for bool_type in test_params: test_df = DataFrame([0, 1, 1, 1, 1, 1, 1, 1, 0], dtype=bool_type) model = DataFrameModel(test_df.copy()) for idx, test_str in enumerate(test_strs): assert model.setData(model.createIndex(idx, 0), test_str) assert not MockQMessageBox.critical.called try: assert numpy.sum(expected_df[0].values == model.df.values[:, 0]) == len(expected_df) except AttributeError: assert numpy.sum(expected_df[0].as_matrix() == model.df.as_matrix()[:, 0]) == len(expected_df)