I have a mocked object that I would like to check its calls using mock_calls, where it is called with numpy arrays. But the problem is that it raises ValueError, as shown in the following simple toy example.
>>> mocked_model_called_with_np_array = mock.Mock()
>>> mocked_model_called_with_np_array(np.array([1, 2]))
>>> mocked_model_called_with_np_array.mock_calls
[call(array([1, 2]))]
Now I set the expected calls:
>>> expected_call_with_numpy = [mock.call(np.array([1, 2]))]
Now if I check it as shown below, it raises error:
>>> assert expected_call_with_numpy == mocked_model_called_with_np_array.mock_calls
---------------------------------------------------------------------------
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-61-9806e62badf5> in <module>
----> 1 assert expected_call_with_numpy == mocked_model_called_with_np_array.mock_calls
c:\..\python\python36\lib\unittest\mock.py in __eq__(self, other)
2053
2054 # this order is important for ANY to work!
-> 2055 return (other_args, other_kwargs) == (self_args, self_kwargs)
2056
2057
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
My search on stackoverflow and the found solutions:
HERE it is suggested to use np.testing.assert_array_equal while you have numpy arrays, but this also does not solve my problems as shown below.
>>> np.testing.assert_array_equal(expected_call_with_numpy, mocked_model_called_with_np_array.mock_calls)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-57-4a0373c94354> in <module>
----> 1 np.testing.assert_array_equal(expected_call_with_numpy, mocked_model_called_with_np_array.mock_calls)
c:\...\python\python36\lib\site-packages\numpy\testing\utils.py in assert_array_equal(x, y, err_msg, verbose)
852 __tracebackhide__ = True # Hide traceback for py.test
853 assert_array_compare(operator.__eq__, x, y, err_msg=err_msg,
--> 854 verbose=verbose, header='Arrays are not equal')
855
856
c:\...\python\python36\lib\site-packages\numpy\testing\utils.py in assert_array_compare(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf)
776 names=('x', 'y'), precision=precision)
777 if not cond:
--> 778 raise AssertionError(msg)
779 except ValueError:
780 import traceback
AssertionError:
Arrays are not equal
(mismatch 100.0%)
x: array([['', (array([1, 2]),), {}]], dtype=object)
y: array([['', (array([1, 2]),), {}]], dtype=object)
Note that the arrays are the same, but it produces error!
Can anyone comment on how to use mock_calls for a mockec object called with a numpy array and then check if the mock_calls produce the expected calls? e.g., something like below
assert expected_call_with_numpy == mocked_model_called_with_np_array.mock_calls