Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle unexpected return value #2157

Merged
merged 1 commit into from
Oct 11, 2021
Merged

Conversation

BjarneHerland
Copy link
Contributor

Issue
Resolves #2004

Approach
Handle the unexpected return-value properly

@BjarneHerland BjarneHerland requested review from jondequinor and removed request for jondequinor October 7, 2021 13:58
@BjarneHerland
Copy link
Contributor Author

I think I'll include a test for this case before review...

@jondequinor
Copy link
Contributor

I think I'll include a test for this case before review...

Yes, this part of ERT is very poorly tested. There are numerous examples of qt testing, however, so setup should be fairly copy-paste.

@BjarneHerland
Copy link
Contributor Author

The application-code uses static methods from Qt to display modal windows. The proposed test applies a delayed timer and relies on the application-instance to provide a hook for the window in order to close it programmatically.

Note that the modal dialogs must not use native widgets in the test because closing these crashes Python. We assume that these methods return the same values for native and Qt-based widgets.

Copy link
Contributor

@jondequinor jondequinor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The application-code uses static methods from Qt to display modal windows. The proposed test applies a delayed timer and relies on the application-instance to provide a hook for the window in order to close it programmatically.

Did you take a look at the other QT tests? You're not supposed to instantiate QApplication directly because it will probably not work as intended: https://pytest-qt.readthedocs.io/en/latest/qapplication.html

If you're simply testing some dialogs, use qtbot as we do elsewhere.

@BjarneHerland
Copy link
Contributor Author

Did you take a look at the other QT tests? You're not supposed to instantiate QApplication directly because it will probably not work as intended: https://pytest-qt.readthedocs.io/en/latest/qapplication.html

Good point - will check out the exit-issue mentioned here.

If you're simply testing some dialogs, use qtbot as we do elsewhere.

IMO it's not obvious how qtbot helps getting a reference to the widget spun up by the static methods we need to test. But I'll give it another hard look on Monday.

A completely different approach is to parse the __doc__ attribute of said static methods and verify the expected return-type, but this feels a little hackish... what do you think?

@BjarneHerland
Copy link
Contributor Author

A completely different approach is to parse the __doc__ attribute of said static methods and verify the expected return-type, but this feels a little hackish... what do you think?

Hmm... when phrasing the goal of the test and the quoted question, I realize that adding static typing achieves the same goal. The missing piece is to set up mypy to include ert_gui in the analysis. Please consider this approach instead....

@BjarneHerland BjarneHerland requested review from jondequinor and removed request for markusdregi October 9, 2021 18:51
@jondequinor
Copy link
Contributor

jondequinor commented Oct 11, 2021

No, we add a test. Here it is:

from qtpy.QtWidgets import QFileDialog
from ert_gui.ertwidgets.pathchooser import PathChooser
from ert_gui.ertwidgets.models.path_model import PathModel


def test_selectfile(qtbot, tmpdir, monkeypatch):
    model = PathModel(tmpdir, must_be_a_file=True)
    widget = PathChooser(model)
    qtbot.addWidget(widget)

    monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args: ("foo", "bar"))

    with qtbot.waitActive(widget):
        widget.show()
    widget.selectPath()

@jondequinor
Copy link
Contributor

Change

    with qtbot.waitActive(widget):
        widget.show()

to something like

widget.show()
qbot.add_widget(widget)
qtbot.waitForWindowShown(widget)
…
asserts

Activation requires a window manager, but we don't need that here. So my bad!

Copy link
Contributor

@jondequinor jondequinor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super! Thanks

@BjarneHerland BjarneHerland enabled auto-merge (squash) October 11, 2021 11:25
@BjarneHerland BjarneHerland merged commit 6ac828a into equinor:main Oct 11, 2021
@BjarneHerland BjarneHerland deleted the issue_2004 branch January 26, 2022 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash in "CSV Export" plugin dialogue box
2 participants