|
| 1 | +# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file. |
| 2 | + |
| 3 | +""" |
| 4 | +Test for Custom Signal creation bug fix |
| 5 | +======================================== |
| 6 | +
|
| 7 | +This test verifies that creating a Custom signal doesn't crash with AttributeError |
| 8 | +when xyarray is None. |
| 9 | +
|
| 10 | +Bug report: "Créer un signal Custom déclenche immédiatement l'erreur |
| 11 | +AttributeError: 'NoneType' object has no attribute 'T'." |
| 12 | +""" |
| 13 | + |
| 14 | +import pytest |
| 15 | +from guidata.qthelpers import qt_app_context |
| 16 | + |
| 17 | +from datalab.gui.newobject import CustomSignalParam, create_signal_gui |
| 18 | + |
| 19 | + |
| 20 | +def test_custom_signal_param_initialization(): |
| 21 | + """Test that CustomSignalParam initializes properly""" |
| 22 | + with qt_app_context(): |
| 23 | + param = CustomSignalParam() |
| 24 | + # Initially, xyarray should be None |
| 25 | + assert param.xyarray is None |
| 26 | + # But other defaults should exist |
| 27 | + assert param.size == 10 |
| 28 | + assert param.xmin == 0.0 |
| 29 | + assert param.xmax == 1.0 |
| 30 | + |
| 31 | + |
| 32 | +def test_custom_signal_param_setup_array(): |
| 33 | + """Test that setup_array creates the xyarray properly""" |
| 34 | + with qt_app_context(): |
| 35 | + param = CustomSignalParam() |
| 36 | + assert param.xyarray is None |
| 37 | + |
| 38 | + # Call setup_array to initialize |
| 39 | + param.setup_array(size=10, xmin=0.0, xmax=1.0) |
| 40 | + |
| 41 | + # Now xyarray should exist and be transposable |
| 42 | + assert param.xyarray is not None |
| 43 | + assert param.xyarray.shape == (10, 2) |
| 44 | + |
| 45 | + # Should be able to transpose without error |
| 46 | + transposed = param.xyarray.T |
| 47 | + assert transposed.shape == (2, 10) |
| 48 | + |
| 49 | + |
| 50 | +def test_custom_signal_param_generate_1d_data(): |
| 51 | + """Test that generate_1d_data works even with None xyarray""" |
| 52 | + with qt_app_context(): |
| 53 | + param = CustomSignalParam() |
| 54 | + assert param.xyarray is None |
| 55 | + |
| 56 | + # generate_1d_data should call setup_array internally |
| 57 | + x, y = param.generate_1d_data() |
| 58 | + |
| 59 | + # Should succeed without AttributeError |
| 60 | + assert x is not None |
| 61 | + assert y is not None |
| 62 | + assert len(x) == param.size |
| 63 | + assert len(y) == param.size |
| 64 | + |
| 65 | + |
| 66 | +def test_custom_signal_creation_forces_edit_mode(): |
| 67 | + """Test that creating a custom signal with edit=False forces edit=True |
| 68 | +
|
| 69 | + This test verifies the bug fix for: |
| 70 | + "Créer un signal Custom déclenche immédiatement l'erreur |
| 71 | + AttributeError: 'NoneType' object has no attribute 'T'." |
| 72 | +
|
| 73 | + The bug occurred when edit=False was passed (the default), which caused |
| 74 | + setup_array to not be called, leaving xyarray as None, which then crashed |
| 75 | + when trying to access xyarray.T. |
| 76 | +
|
| 77 | + The fix forces edit=True for CustomSignalParam to ensure the user is always |
| 78 | + prompted to set up the array properly. |
| 79 | + """ |
| 80 | + # This test can't actually test the dialog interaction, but we can verify |
| 81 | + # that the code doesn't crash immediately with AttributeError due to None xyarray |
| 82 | + # The test framework will handle the unattended mode appropriately |
| 83 | + with qt_app_context(): |
| 84 | + # In unattended mode, the dialogs will be auto-canceled, so this should |
| 85 | + # return None rather than crashing |
| 86 | + param = CustomSignalParam() |
| 87 | + # In actual usage with GUI, this would show dialogs |
| 88 | + # In unattended test mode, dialogs are auto-canceled |
| 89 | + # The important thing is it doesn't crash with AttributeError |
| 90 | + _signal = create_signal_gui(param, edit=False, parent=None) |
| 91 | + # In unattended mode, this will be None because dialogs are canceled |
| 92 | + # But it shouldn't have crashed with AttributeError |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + pytest.main([__file__, "-v"]) |
0 commit comments