Here are the examples of the python api mock.side_effect taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
9 Examples
3
Source : test_edges.py
with GNU Lesser General Public License v3.0
from BlueBrain
with GNU Lesser General Public License v3.0
from BlueBrain
def test_property_dtypes_fail(self):
a = pd.Series(
data=[dtype("int64"), dtype("float64")], index=["syn_weight", "efferent_surface_z"]
).sort_index()
b = pd.Series(
data=[dtype("int32"), dtype("float64")], index=["syn_weight", "efferent_surface_z"]
).sort_index()
with patch(
"bluepysnap.edges.EdgePopulation.property_dtypes", new_callable=PropertyMock
) as mock:
mock.side_effect = [a, b]
circuit = Circuit(str(TEST_DATA_DIR / "circuit_config.json"))
test_obj = test_module.Edges(circuit)
with pytest.raises(BluepySnapError):
test_obj.property_dtypes.sort_index()
def test_ids(self):
3
Source : testmock.py
with GNU General Public License v3.0
from guohuadeng
with GNU General Public License v3.0
from guohuadeng
def test_autospec_side_effect_exception(self):
# Test for issue 23661
def f():
pass
mock = create_autospec(f)
mock.side_effect = ValueError('Bazinga!')
self.assertRaisesRegex(ValueError, 'Bazinga!', mock)
@unittest.skipUnless('java' in sys.platform,
3
Source : testmock.py
with GNU General Public License v3.0
from guohuadeng
with GNU General Public License v3.0
from guohuadeng
def test_side_effect_iterator_default(self):
mock = Mock(return_value=2)
mock.side_effect = iter([1, DEFAULT])
self.assertEqual([mock(), mock()], [1, 2])
def test_assert_has_calls_any_order(self):
2
Source : testmock.py
with GNU General Public License v3.0
from guohuadeng
with GNU General Public License v3.0
from guohuadeng
def test_side_effect(self):
mock = Mock()
def effect(*args, **kwargs):
raise SystemError('kablooie')
mock.side_effect = effect
self.assertRaises(SystemError, mock, 1, 2, fish=3)
mock.assert_called_with(1, 2, fish=3)
results = [1, 2, 3]
def effect():
return results.pop()
mock.side_effect = effect
self.assertEqual([mock(), mock(), mock()], [3, 2, 1],
"side effect not used correctly")
mock = Mock(side_effect=sentinel.SideEffect)
self.assertEqual(mock.side_effect, sentinel.SideEffect,
"side effect in constructor not used")
def side_effect():
return DEFAULT
mock = Mock(side_effect=side_effect, return_value=sentinel.RETURN)
self.assertEqual(mock(), sentinel.RETURN)
def test_autospec_side_effect(self):
2
Source : testmock.py
with GNU General Public License v3.0
from guohuadeng
with GNU General Public License v3.0
from guohuadeng
def test_side_effect_setting_iterator(self):
mock = Mock()
mock.side_effect = iter([1, 2, 3])
self.assertEqual([mock(), mock(), mock()], [1, 2, 3])
self.assertRaises(StopIteration, mock)
side_effect = mock.side_effect
self.assertIsInstance(side_effect, type(iter([])))
mock.side_effect = ['a', 'b', 'c']
self.assertEqual([mock(), mock(), mock()], ['a', 'b', 'c'])
self.assertRaises(StopIteration, mock)
side_effect = mock.side_effect
self.assertIsInstance(side_effect, type(iter([])))
this_iter = Iter()
mock.side_effect = this_iter
self.assertEqual([mock(), mock(), mock(), mock()],
['this', 'is', 'an', 'iter'])
self.assertRaises(StopIteration, mock)
self.assertIs(mock.side_effect, this_iter)
def test_side_effect_iterator_exceptions(self):
0
Source : mock.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def mock_open(mock=None, read_data=''):
"""
A helper function to create a mock to replace the use of `open`. It works
for `open` called directly or used as a context manager.
The `mock` argument is the mock object to configure. If `None` (the
default) then a `MagicMock` will be created for you, with the API limited
to methods or attributes available on standard file handles.
`read_data` is a string for the `read`, `readline` and `readlines` of the
file handle to return. This is an empty string by default.
"""
_read_data = _to_stream(read_data)
_state = [_read_data, None]
def _readlines_side_effect(*args, **kwargs):
if handle.readlines.return_value is not None:
return handle.readlines.return_value
return _state[0].readlines(*args, **kwargs)
def _read_side_effect(*args, **kwargs):
if handle.read.return_value is not None:
return handle.read.return_value
return _state[0].read(*args, **kwargs)
def _readline_side_effect(*args, **kwargs):
for item in _iter_side_effect():
yield item
while True:
yield _state[0].readline(*args, **kwargs)
def _iter_side_effect():
if handle.readline.return_value is not None:
while True:
yield handle.readline.return_value
for line in _state[0]:
yield line
global file_spec
if file_spec is None:
# set on first use
if six.PY3:
import _io
file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))
else:
file_spec = file
if mock is None:
mock = MagicMock(name='open', spec=open)
handle = MagicMock(spec=file_spec)
handle.__enter__.return_value = handle
handle.write.return_value = None
handle.read.return_value = None
handle.readline.return_value = None
handle.readlines.return_value = None
handle.read.side_effect = _read_side_effect
_state[1] = _readline_side_effect()
handle.readline.side_effect = _state[1]
handle.readlines.side_effect = _readlines_side_effect
handle.__iter__.side_effect = _iter_side_effect
def reset_data(*args, **kwargs):
_state[0] = _to_stream(read_data)
if handle.readline.side_effect == _state[1]:
# Only reset the side effect if the user hasn't overridden it.
_state[1] = _readline_side_effect()
handle.readline.side_effect = _state[1]
return DEFAULT
mock.side_effect = reset_data
mock.return_value = handle
return mock
class PropertyMock(Mock):
0
Source : mock.py
with GNU General Public License v3.0
from guohuadeng
with GNU General Public License v3.0
from guohuadeng
def mock_open(mock=None, read_data=''):
"""
A helper function to create a mock to replace the use of `open`. It works
for `open` called directly or used as a context manager.
The `mock` argument is the mock object to configure. If `None` (the
default) then a `MagicMock` will be created for you, with the API limited
to methods or attributes available on standard file handles.
`read_data` is a string for the `read` methoddline`, and `readlines` of the
file handle to return. This is an empty string by default.
"""
def _readlines_side_effect(*args, **kwargs):
if handle.readlines.return_value is not None:
return handle.readlines.return_value
return list(_state[0])
def _read_side_effect(*args, **kwargs):
if handle.read.return_value is not None:
return handle.read.return_value
return type(read_data)().join(_state[0])
def _readline_side_effect():
if handle.readline.return_value is not None:
while True:
yield handle.readline.return_value
for line in _state[0]:
yield line
global file_spec
if file_spec is None:
# set on first use
if six.PY3:
import _io
file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO))))
else:
file_spec = file
if mock is None:
mock = MagicMock(name='open', spec=open)
handle = MagicMock(spec=file_spec)
handle.__enter__.return_value = handle
_state = [_iterate_read_data(read_data), None]
handle.write.return_value = None
handle.read.return_value = None
handle.readline.return_value = None
handle.readlines.return_value = None
handle.read.side_effect = _read_side_effect
_state[1] = _readline_side_effect()
handle.readline.side_effect = _state[1]
handle.readlines.side_effect = _readlines_side_effect
def reset_data(*args, **kwargs):
_state[0] = _iterate_read_data(read_data)
if handle.readline.side_effect == _state[1]:
# Only reset the side effect if the user hasn't overridden it.
_state[1] = _readline_side_effect()
handle.readline.side_effect = _state[1]
return DEFAULT
mock.side_effect = reset_data
mock.return_value = handle
return mock
class PropertyMock(Mock):
0
Source : testmock.py
with GNU General Public License v3.0
from guohuadeng
with GNU General Public License v3.0
from guohuadeng
def test_autospec_side_effect(self):
# Test for issue17826
results = [1, 2, 3]
def effect():
return results.pop()
def f():
pass
mock = create_autospec(f)
mock.side_effect = [1, 2, 3]
self.assertEqual([mock(), mock(), mock()], [1, 2, 3],
"side effect not used correctly in create_autospec")
# Test where side effect is a callable
results = [1, 2, 3]
mock = create_autospec(f)
mock.side_effect = effect
self.assertEqual([mock(), mock(), mock()], [3, 2, 1],
"callable side effect not used correctly")
def test_autospec_side_effect_exception(self):
0
Source : test_script.py
with Mozilla Public License 2.0
from mozilla-releng
with Mozilla Public License 2.0
from mozilla-releng
async def test_bouncer_locations(locations_context, mocker, info, updated_info, raises):
async def fake_get_locations_info(*args, **kwargs):
return info
@counted
async def toggled_get_locations_info(*args, **kwargs):
if toggled_get_locations_info.calls & 1:
return info
else:
return updated_info
locations_context.task["payload"]["bouncer_products"] = ["firefox-nightly-latest"]
locations_context.task["payload"]["product"] = "firefox"
mocker.patch.object(bscript, "check_product_names_match_nightly_locations", new=noop_sync)
mocker.patch.object(bscript, "check_version_matches_nightly_regex", new=noop_sync)
mocker.patch.object(bscript, "does_product_exist", new=return_false_async)
with pytest.raises(ScriptWorkerTaskException):
await bouncer_locations(locations_context)
mocker.patch.object(bscript, "does_product_exist", new=return_true_async)
mocker.patch.object(bscript, "get_locations_info", new=fake_get_locations_info)
mocker.patch.object(bscript, "check_location_path_matches_destination", new=noop_sync)
mocker.patch.object(bscript, "api_modify_location", new=noop_async)
if raises:
with pytest.raises(ScriptWorkerTaskException):
await bouncer_locations(locations_context)
else:
if updated_info != []:
mock = mocker.MagicMock()
mock.side_effect = toggled_get_locations_info
mocker.patch.object(bscript, "get_locations_info", new=mock)
await bouncer_locations(locations_context)
# async_main {{{1
@pytest.mark.asyncio