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

Add more tests for Enum Editor #836

Merged
merged 8 commits into from
May 29, 2020
Merged

Conversation

ievacerny
Copy link
Contributor

Adds more tests for EnumEditor.

@ievacerny ievacerny self-assigned this May 22, 2020
@ievacerny ievacerny requested a review from kitchoi May 25, 2020 13:13
@ievacerny ievacerny marked this pull request as ready for review May 25, 2020 13:14
@ievacerny ievacerny changed the title [WIP] Add more tests for Enum Editor Add more tests for Enum Editor May 25, 2020
Copy link
Contributor

@kitchoi kitchoi left a comment

Choose a reason for hiding this comment

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

Thank you for adding the tests. This is great.
I noticed that the test fails on wx due to existing issues which you have opened issues for. It is good to have these tests first so that we can fix them with TDD.

Just one change request to add an else branch for future-proofing and consistency. The rest are nitpicks / non-actionable comments / may-be-out-of-scope suggestions that should not block this PR.



def get_all_button_status(widget):
""" Gets status of all radio buttons in the layout of a given widget."""
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: It might be worth noting the assumption that all the children widgets are radio buttons.


elif is_current_backend_qt4():
combobox.lineEdit().editingFinished.emit()

Copy link
Contributor

Choose a reason for hiding this comment

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

These functions allow the tests to be run on different toolkits with the minimal necessary toolkit-specific adaptations, which is quite nice. In the code base, there exists an alternative approach: a new test is created for each toolkit and the test is skipped altogether if the toolkit does not match the expected, e.g. https://github.com/enthought/traitsui/blob/master/traitsui/tests/test_actions.py

The alternative approach has one benefit: If traitsui should support a new toolkit, this would not cause a lot of existing tests targeting (wx or Qt) to fail.

In the approach here, if the toolkit is not null (so the tests are not skipped), and if both is_current_backend_wx and is_current_backend_qt4 return false, the functions do nothing. Then the tests may either do nothing meaningful, or fail in some mysterious way.

To be consistent with the other tests, we should add an else branch in these functions to raise SkipTest. <--- The only change request here.

We could also refactor the pattern to something along this line (that could be in a separate PR so not to block this one):


def toolkit_dispatch(func):
    """ Decorator for wrapping a default implementation of a toolkit-dependent
    function. Toolkit-specific implementation can be registered to the
    returned callable using one of the toolkit supported in ETSConfig.

    Example:

    @toolkit_dispatch
    def get_text(widget):
        # this is the default implementation
        # Typically it is appropriate to raise NotImplementedError.

    @get_text.register("wx")
    def get_text(widget):
        return widget.GetName()
    """

    toolkit_to_implementation = {}

    def register(toolkit):

        def wrapped(implementation):
            toolkit_to_implementation[toolkit] = implementation
        return wrapped

    def wrapper(*args, **kwargs):
        implementation = toolkit_to_implementation.get(ETSConfig.toolkit, func)
        return implementation(*args, **kwargs)

    wrapper.register = register
    return wrapper

It will be easier to add an implementation to a mapping, instead of modifying if-else branches inline. The implementations, being independent, will also be correctly decoupled from each other.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will add an else branch 👍 Although I don't see why tests for existing toolkits would start failing even if else branch isn't added -- am I missing something?

As for the new pattern, to me adding an elif branch to a helper method seems easier than going through this framework -- there is only one docstring and one signature to maintain and adding

elif new_toolkit:
    implementation

seems easier than

@function.register("new_toolkit")
def function(signature):
    implementation

Copy link
Contributor

Choose a reason for hiding this comment

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

Although I don't see why tests for existing toolkits would start failing even if else branch isn't added -- am I missing something?

Sorry for my weird way of putting things.

If the else branch isn't added, and if we have a new toolkit, when we run the test suite with this new toolkit, the tests here will not be skipped (because the toolkit isn't null). Since is_current_backend_wx and is_current_backend_qt4 will return false, the tests will likely fail because the helper functions are no-op in that new toolkit.

Copy link
Contributor

@kitchoi kitchoi May 26, 2020

Choose a reason for hiding this comment

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

there is only one docstring and one signature to maintain and adding

I would probably maintain the docstring once only like this:

@toolkit_dispatch
def get_something(widget):
    """ Docstring here """

@get_something.register("wx")
def _(widget):
    return widget.GetName()

This is similar to the functools.singledispatch: It is a functional way to achieve polymorphism.

traitsui/tests/editors/test_enum_editor.py Show resolved Hide resolved
traitsui/tests/editors/test_enum_editor.py Outdated Show resolved Hide resolved
editor = ui.get_editors("value")[0]
widget = editor.control

return gui, widget
Copy link
Contributor

Choose a reason for hiding this comment

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

Just an observation: The create_ui context manager being introduced in https://github.com/enthought/traitsui/pull/664/files could be useful here. I will try to review that PR soon.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure it would be that useful here because GUI for wx tests would still have to be set up and cleaned up manually.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we are missing the GUITestAssistant for Wx too: enthought/pyface#266

In any case, what I meant to say is that this is a pattern repeated so many times that it needs to be refactored. I was also meaning to reference an implementation where this use case is solved using a context manager.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I agree about refactoring but I don't think there's a need to rush the review on #664 because it wouldn't help here until the same exists for wx. And I don't think that's going to happen soon enough to make it into this PR.

Copy link
Contributor

@kitchoi kitchoi left a comment

Choose a reason for hiding this comment

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

LGTM!

traitsui/wx/ui_panel.py Outdated Show resolved Hide resolved
@ievacerny ievacerny merged commit 6fdce6d into master May 29, 2020
@ievacerny ievacerny deleted the tst/add-editors-with-list-tests branch May 29, 2020 17:21
@kitchoi kitchoi added this to the Release 7.0.1 milestone Jun 5, 2020
kitchoi pushed a commit that referenced this pull request Jul 8, 2020
* Add more tests for Enum Editor

* Add wx EnumEditor tests

* Add missing skipif decorator

* Add issue references to FIXMEs

* Address review comments
kitchoi added a commit that referenced this pull request Jul 8, 2020
* Fix the back button in the demo. (#799)

* Fixes for Qt DatetimeEditor (#803)

* Fixes for Qt DatetimeEditor.

* Fix handling of changes to editor bounds.

* Remove commented out code from editor example.

* And remove corresponding imports.

* Missed file with explicit defaults.

* Fixes to min/max and value out of range handling.

* Remove NameError and add default root_dir to tutor.py (#813)

* Remove NameError from tutor.py

* Take current directory as default

* Relax argument number constraint

* Rewrite nose tests to use unittest (#809)

* Rewrite nose tests to use unittest

* Rewrite remaining bare test functions

* Remove print statements from unit tests

* Replace assertEquals with assertEqual

* Fix error due to comparing None with int (#969)

* Use unittest discover instead of nose.core (#810)

* Merge pull request #829 from enthought/fix/ui-panel-alignment

Remove align centre which was causing problems

* Merge pull request #808 from enthought/fix/demo-fixes

A variety of fixes to the Demo app

* Add more tests for Enum Editor (#836)

* Add more tests for Enum Editor

* Add wx EnumEditor tests

* Add missing skipif decorator

* Add issue references to FIXMEs

* Address review comments

* Add check list editor tests (#837)

* Add check list editor tests

* Add wx tests for check list editor

* Simplify helper functions

* Add issue references to FIXMEs

* Address review comments

* Don't rely on empty list behaviour with combobox editor

* Add temporary fix to prevent wx test interactions

* Add set editor tests (#838)

* Add set editor tests

* Simplify helper functions

* Add wx tests for set editor

* Add issue reference to FIXME

* Address review comments

* Expand ordered flag tests

* Add image enum editor tests (#845)

* Add tests for image enum editor

* Docstring fixes

* Address review comments

* Skip tests that don't cleanup properly due to editor issue

* Remove unreliable image_cache checks

* Skip problematic tests on linux

* Address review comments

* Move value mapping from factory to individual editors (#848)

* Move value mapping from factory to individual editors

* Remove factory mapping references from image enum editor

* Remove unused wx helper function

* Address review comments

* Remove test FIXMEs

* Use instantiated tookit

* Merge pull request #850 from enthought/fix-demo-app-description-for-folder

Fix Demo application description if __init__.py or traits_ui_demo.jpg are not found

* BUG: Correctly implement auto-add functionality in ListStrModel. (#860)

* Move format_func, format_str and invalid traits from factory to editor (#859)

* Move format_func and format_str from factory to editor

* Move invalid from factory to editor as invalid_trait_name

* Merge pull request #871 from enthought/fix/segfault-row-reorder

FIX: Make sure new_row is positive

* Merge pull request #873 from enthought/maint-tabular-model

Maint: Normalize row index in TabularModel

* Make sure all UI in tests are disposed (#865)

* Dispose ui in test_visible_when_layout

* Wrap test with create_ui, and store_exceptions_on_all_threads

* Dispose ui and wrap it under store_exceptions... for test_ui

* Use create_ui in test_color_column

* Wrap tests with create_ui and store_exceptions_on_all_threads

* Dispose ui in test_actions

* Consistently dispose UI with create_ui in test_code_editor

* Skip two tests on wx that fail on its own

* Dispose UI in test_table_editor; these tests are failing on their own

* Remove skips; it was the developer's fault in not refreshing their devenv

* Dispose UI in test_csv_editor

* Make sure dispose must be called in test_date_editor

* Make sure dispose is called in test_date_range_editor

* Make sure dispose is called in test_datetime_editor

* Dispose ui in test_instance_editor

* Dispose ui in test_liststr_editor_selection

* Dispose ui in test_range_editor_spinner

* Dispose ui in test_range_editor_text

* Dispose ui in test_tree_editor

* Dispose UI in test_tuple_editor

* One more in test_data_frame_editor

* Remove additional store_exceptions_on_all_threads as they are orthogonal changes

* Experiment removing skips that were added due to test interactions

* Revert "Experiment removing skips that were added due to test interactions"

This reverts commit 7466c03.

* Try removing another workaround

* Revert "Try removing another workaround"

This reverts commit 090c75c.

* Revert inconsequential change to test_color_column

* Add close back just because it was there before - not sure if it is still needed.

* Add more TabularEditor tests (#874)

* Add more tests for Tabular Editor

* Minor wx TabularEditor fix

* Add brief explanation about Rows flag

* Add more ListStrEditor tests (#869)

* Add more tests for ListStrEditor

* Minor wx ListStrEditor fix

* Add gui.process_events to fix pyface2 errors

* Rename TestListStrEditor with adapter test to TestListStrAdapter

* Add docstrings and clarification comments

* Skip tests on Windows due to potential test interactions

* Fix clear_selection usage

* Fix bad TraitListEvent in ListStr and Tabular Editors (#875)

* Fix bad TraitListEvent in ListStr and Tabular Editors

* Remove test FIXMEs

* Fix unexpected format_func in RangeEditor bug (#900)

* Accept format_func, format_str and invalid parameters with RangeEditor

* Add a test to verify same behaviour

* Use kwargs in the signature

* Alternative fix to TabularAdapter crashes when column number reduces (#897)

Co-authored-by: Federico Miorelli <FedeMiorelli@users.noreply.github.com>

* Fix eerror when the columns number is reduced

* Workaround slot being called after the UI is disconnected

* destroy control (later) in dispose to prevent slots being called after dispose

* Revert "destroy control (later) in dispose to prevent slots being called after dispose"

This reverts commit 8bb6abd.

* Fix issue number in comment

* Update test name and comments

* Rework test comment

* Rewrite comment and run process_events so that bug appears independently of the workaround

* Add links from TabularEditor and TreeEditor to adapter docs (#917)

* Add links from TabularEditor and TreeEditor to adapter docs

* Fix tree node ref

* Fix Theme pickling (#915)

* Check toolkit before wx import + tests

* Address review comments

* Merge pull request #846 from enthought/maint-test-non-null-toolkit-layout-labels

TST: Add tests for layout and labels using any non-null toolkits
(cherry picked from commit 4398b3d)

Co-authored-by: Ieva <ievacerny@users.noreply.github.com>
Co-authored-by: Kit Choi <kitchoi@users.noreply.github.com>
Co-authored-by: Robert Kern <rkern@enthought.com>
Co-authored-by: Kit Yan Choi <kchoi@enthought.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants