mu-editor / mu

A small, simple editor for beginner Python programmers. Written in Python and Qt5.
http://codewith.mu
GNU General Public License v3.0
1.39k stars 433 forks source link

test_Window_connect_zoom fails with Python 3.12 #2449

Open hrnciar opened 1 year ago

hrnciar commented 1 year ago

What were you trying to do?

To build mu with Python 3.12

What steps did you take to trigger the issue?

While working on https://github.com/mu-editor/mu/pull/2448, I managed to fix AttributeError: 'called_once_with' is not a valid assertion. Use a spec for the mock if 'called_once_with' is meant to be an attribute.. Did you mean: 'assert_called_once_with'? coming from test_Window_connect_zoom, but it uncovered another problem.

What did you expect to happen?

The tests pass.

What actually happened?

The test now fails with:

=================================== FAILURES ===================================
___________________________ test_Window_connect_zoom ___________________________

    def test_Window_connect_zoom():
        """
        Ensure the zoom in/out signals are connected to the passed in widget's
        zoomIn and zoomOut handlers.
        """
        w = mu.interface.main.Window()
        w._zoom_in = mock.MagicMock()
        w._zoom_in.connect = mock.MagicMock()
        w._zoom_out = mock.MagicMock()
        w._zoom_out.connect = mock.MagicMock()
        widget = mock.MagicMock()
        widget.zoomIn = mock.MagicMock()
        widget.zoomOut = mock.MagicMock()
        w.connect_zoom(widget)
>       w._zoom_in.connect.assert_called_once_with(widget.zoomIn)

tests/interface/test_main.py:489:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/lib64/python3.11/unittest/mock.py:951: in assert_called_once_with
    return self.assert_called_with(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <MagicMock name='mock.connect' id='139722360538576'>
args = (<MagicMock name='mock.zoomIn' id='139722360614608'>,), kwargs = {}
expected = call(<MagicMock name='mock.zoomIn' id='139722360614608'>)
actual = call(<MagicMock name='mock.set_zoom' id='139722366300624'>)
_error_message = <function NonCallableMock.assert_called_with.<locals>._error_message at 0x7f13a62accc0>
cause = None

    def assert_called_with(self, /, *args, **kwargs):
        """assert that the last call was made with the specified arguments.

        Raises an AssertionError if the args and keyword args passed in are
        different to the last call to the mock."""
        if self.call_args is None:
            expected = self._format_mock_call_signature(args, kwargs)
            actual = 'not called.'
            error_message = ('expected call not found.\nExpected: %s\nActual: %s'
                    % (expected, actual))
            raise AssertionError(error_message)

        def _error_message():
            msg = self._format_mock_failure_message(args, kwargs)
            return msg
        expected = self._call_matcher(_Call((args, kwargs), two=True))
        actual = self._call_matcher(self.call_args)
        if actual != expected:
            cause = expected if isinstance(expected, Exception) else None
>           raise AssertionError(_error_message()) from cause
E           AssertionError: expected call not found.
E           Expected: connect(<MagicMock name='mock.zoomIn' id='139722360614608'>)
E           Actual: connect(<MagicMock name='mock.set_zoom' id='139722366300624'>)

I've tried to patch it somehow, but I get mock.set_zoom every time instead of mock.zoomIn.

diff --git a/tests/interface/test_main.py b/tests/interface/test_main.py
index d80e97b..eab27a6 100644
--- a/tests/interface/test_main.py
+++ b/tests/interface/test_main.py
@@ -478,16 +478,20 @@ def test_Window_connect_zoom():
     zoomIn and zoomOut handlers.
     """
     w = mu.interface.main.Window()
+    w._set_zoom = mock.MagicMock()
+    w._set_zoom.connect = mock.MagicMock()
     w._zoom_in = mock.MagicMock()
     w._zoom_in.connect = mock.MagicMock()
     w._zoom_out = mock.MagicMock()
     w._zoom_out.connect = mock.MagicMock()
     widget = mock.MagicMock()
+    widget.set_zoom = mock.MagicMock()
     widget.zoomIn = mock.MagicMock()
     widget.zoomOut = mock.MagicMock()
     w.connect_zoom(widget)
-    assert w._zoom_in.connect.called_once_with(widget.zoomIn)
-    assert w._zoom_out.connect.called_once_with(widget.zoomOut)
+    w._zoom_in.connect.assert_called_once_with(widget.set_zoom)
+    w._zoom_in.connect.assert_called_once_with(widget.zoomIn)
+    w._zoom_out.connect.assert_called_once_with(widget.zoomOut)

Operating System Version

Fedora rawhide x86_64

Mu Version

1.2.0

Other Info

No response

Editor Log

No response