def test_05_duration_calculation_success(): """Tests a duration calculation for a good case. tests: R-PROCESSOR-DURATION-004 tests: R-PROCESSOR-DURATION-005""" m = MagicMock() duration = Duration(m.callback) merge_properties = [ dict(timestamp=1337, atom="cat/package-3.2.1", atom_base="cat/package", atom_version="3.2.1", count_n="11", count_m="23"), dict(timestamp=3677820, atom="cat/package-3.2.1", atom_base="cat/package", atom_version="3.2.1", count_n="11", count_m="23") ] duration.callbacks["merge_begin"](merge_properties[0]) duration.callbacks["merge_end"](merge_properties[1]) assert m.method_calls == [ call.callback( merge_properties[1], merge_properties[1]["timestamp"] - merge_properties[0]["timestamp"]) ]
async def test_UpdateInfoThread_call_callback_gets_value_from_value_getter( info_updater, mocker): mocks = Mock( value_getter=AsyncMock(return_value='The Value'), callback=Mock(), sleep_mock=AsyncMock(), ) mocker.patch('asyncio.sleep', mocks.sleep_mock) info_updater._cache.clear() await info_updater._call_callback( cache_key=('id', 'key'), value_getter=mocks.value_getter, callback=mocks.callback, ) assert mocks.mock_calls == [ call.callback(Ellipsis), call.sleep_mock(info_updater._delay_between_updates), call.value_getter(), call.callback('The Value'), ] assert info_updater._cache[('id', 'key')] == 'The Value'
def test_should_call_on_start_and_on_finish_before_and_after_the_callback(self, callbacks): # given mock = Mock() on_start = mock.on_start callback = mock.callback on_finish = mock.on_finish # when async_callback = AsyncCallback(callback) async_callback.trigger(on_start, on_finish) # then assert mock.mock_calls == [call.on_start(), call.callback(), call.on_finish()]
async def test_UpdateInfoThread_call_callback_handles_RequestError( info_updater, mocker): mocks = Mock( value_getter=AsyncMock(side_effect=errors.RequestError('Nah')), callback=Mock(), error_callback=Mock(), sleep_mock=AsyncMock(), ) info_updater._error_callback = mocks.error_callback mocker.patch('asyncio.sleep', mocks.sleep_mock) info_updater._cache.clear() await info_updater._call_callback( callback=mocks.callback, value_getter=mocks.value_getter, cache_key=('id', 'key'), ) assert mocks.mock_calls == [ call.callback(Ellipsis), call.sleep_mock(info_updater._delay_between_updates), call.value_getter(), call.callback(''), call.error_callback(errors.RequestError('Nah')), ] assert info_updater._cache == {}
async def test_UpdateInfoThread_call_callback_gets_value_from_cache( info_updater, mocker): mocks = Mock( value_getter=AsyncMock(return_value='The Value'), callback=Mock(), sleep_mock=AsyncMock(), ) mocker.patch('asyncio.sleep', mocks.sleep_mock) info_updater._cache[('id', 'key')] = 'The Cached Value' await info_updater._call_callback( callback=mocks.callback, value_getter=mocks.value_getter, cache_key=('id', 'key'), ) assert mocks.mock_calls == [ call.callback('The Cached Value'), ] assert info_updater._cache[('id', 'key')] == 'The Cached Value'