async def test_asyncmemoize_clear_cache(): def a(*args, **kwargs): args_str = ', '.join(args) kwargs_str = ', '.join(f'{k}={v}' for k, v in kwargs.items()) return f'a({args_str}, {kwargs_str})' def b(*args, **kwargs): args_str = ', '.join(args) kwargs_str = ', '.join(f'{k}={v}' for k, v in kwargs.items()) return f'b({args_str}, {kwargs_str})' a_mock = AsyncMock(side_effect=a) b_mock = AsyncMock(side_effect=b) a_memoized = utils.asyncmemoize(a_mock) b_memoized = utils.asyncmemoize(b_mock) mocks = Mock() mocks.attach_mock(a_mock, 'a') mocks.attach_mock(b_mock, 'b') for _ in range(3): assert await a_memoized('foo', bar='baz') == 'a(foo, bar=baz)' assert mocks.mock_calls == [ call.a('foo', bar='baz'), ] for _ in range(3): assert await b_memoized('this', that='what') == 'b(this, that=what)' assert mocks.mock_calls == [ call.a('foo', bar='baz'), call.b('this', that='what'), ] a_memoized.clear_cache() for _ in range(3): assert await a_memoized('foo', bar='baz') == 'a(foo, bar=baz)' assert mocks.mock_calls == [ call.a('foo', bar='baz'), call.b('this', that='what'), call.a('foo', bar='baz'), ] for _ in range(3): assert await b_memoized('this', that='what') == 'b(this, that=what)' assert mocks.mock_calls == [ call.a('foo', bar='baz'), call.b('this', that='what'), call.a('foo', bar='baz'), ] b_memoized.clear_cache() for _ in range(3): assert await b_memoized('this', that='what') == 'b(this, that=what)' assert mocks.mock_calls == [ call.a('foo', bar='baz'), call.b('this', that='what'), call.a('foo', bar='baz'), call.b('this', that='what'), ]
async def test_asyncmemoize_caches_exceptions(): def a(exccls, *args, **kwargs): args_str = ', '.join(args) kwargs_str = ', '.join(f'{k}={v}' for k, v in kwargs.items()) raise exccls(f'a({args_str}, {kwargs_str})') def b(exccls, *args, **kwargs): args_str = ', '.join(args) kwargs_str = ', '.join(f'{k}={v}' for k, v in kwargs.items()) raise exccls(f'b({args_str}, {kwargs_str})') a_mock = AsyncMock(side_effect=a) b_mock = AsyncMock(side_effect=b) a_memoized = utils.asyncmemoize(a_mock) b_memoized = utils.asyncmemoize(b_mock) mocks = Mock() mocks.attach_mock(a_mock, 'a') mocks.attach_mock(b_mock, 'b') for _ in range(3): with pytest.raises(RuntimeError, match=r'a\(foo, bar=baz\)'): await a_memoized(RuntimeError, 'foo', bar='baz') assert mocks.mock_calls == [ call.a(RuntimeError, 'foo', bar='baz'), ] for _ in range(3): with pytest.raises(TypeError, match=r'a\(this, that=what\)'): await a_memoized(TypeError, 'this', that='what') assert mocks.mock_calls == [ call.a(RuntimeError, 'foo', bar='baz'), call.a(TypeError, 'this', that='what'), ] for _ in range(3): with pytest.raises(BaseException, match=r'b\(this, that=what\)'): await b_memoized(BaseException, 'this', that='what') assert mocks.mock_calls == [ call.a(RuntimeError, 'foo', bar='baz'), call.a(TypeError, 'this', that='what'), call.b(BaseException, 'this', that='what'), ] for _ in range(3): with pytest.raises(ValueError, match=r'a\(x, y, z, \)'): await a_memoized(ValueError, 'x', 'y', 'z') assert mocks.mock_calls == [ call.a(RuntimeError, 'foo', bar='baz'), call.a(TypeError, 'this', that='what'), call.b(BaseException, 'this', that='what'), call.a(ValueError, 'x', 'y', 'z'), ]
def test_emit_to_multiple_callbacks(args, kwargs): s = Signal('foo', 'bar') cb = Mock() s.register('foo', cb.a) s.register('foo', cb.b) s.register('foo', cb.c) s.register('bar', cb.d) s.register('bar', cb.e) s.register('bar', cb.f) s.emit('foo', *args, **kwargs) assert cb.mock_calls == [ call.a(*args, **kwargs), call.b(*args, **kwargs), call.c(*args, **kwargs), ] s.emit('bar') assert cb.mock_calls == [ call.a(*args, **kwargs), call.b(*args, **kwargs), call.c(*args, **kwargs), call.d(), call.e(), call.f(), ]
def test_run_an(self, mock_readcsv, mock_writecsv, mock_convert_prices, mock_convert_prices_an): '''tests run method for roman conversion''' mock_parent = Mock() mock_parent.attach_mock(mock_readcsv, 'a') mock_parent.attach_mock(mock_writecsv, 'b') mock_parent.attach_mock(mock_convert_prices, 'c') mock_parent.attach_mock(mock_convert_prices_an, 'd') obj1 = collections.OrderedDict([('a', 'A1'), ('b', 'B1'), ('c', 'C1')]) obj2 = collections.OrderedDict([('a', 'A2'), ('b', 'B2'), ('c', 'C2')]) objects1 = [obj1, obj2] mock_readcsv.return_value = objects1 run('source', 'dest', 'roman') mock_convert_prices.assert_not_called() mock_parent.assert_has_calls( [call.a('source'), call.d(objects1), call.b('dest', objects1)])