def test_not_async__nested_call(): mock = asyncmock.AsyncMock() mock.my_method.not_async = True mock.my_method("foo", 123, bar="eek") mock.my_method.assert_called_with("foo", 123, bar="eek")
async def test_async__return_value(): mock = asyncmock.AsyncMock(return_value="hi") actual = await mock("foo", 123, bar="eek") assert actual == "hi" mock.assert_called_with("foo", 123, bar="eek")
async def test_async__with_side_effect(): mock = asyncmock.AsyncMock(side_effect=KeyError) with pytest.raises(KeyError): await mock("foo", 123, bar="eek") mock.assert_called_with("foo", 123, bar="eek")
def test_configure(self, monkeypatch, event_loop): mock_factories = asyncmock.Mock() mock_factories.message_sender_factory.available = ["foo"] mock_factories.message_sender_factory.create.return_value = ( mock_sender) = asyncmock.AsyncMock() mock_factories.message_receiver_factory.available = ["bar"] mock_factories.message_receiver_factory.create.return_value = ( mock_receiver) = asyncmock.AsyncMock() monkeypatch.setattr(cli, "factory", mock_factories) cli.configure(loop=event_loop) mock_factories.message_sender_factory.create.assert_called_with("foo") mock_sender.configure.assert_called() mock_factories.message_receiver_factory.create.assert_called_with( "bar") mock_receiver.configure.assert_called()
def test_send(self, monkeypatch, event_loop): mock_factories = asyncmock.Mock() mock_factories.get_sender.return_value = mock_sender = asyncmock.AsyncMock( ) monkeypatch.setattr(cli, "factory", mock_factories) cli.send("foo", "bar", loop=event_loop) mock_factories.get_sender.assert_called_with("bar") mock_sender.send.assert_called_with(data="foo")
def test_receiver(self, monkeypatch, event_loop): mock_factories = asyncmock.Mock() mock_factories.get_receiver.return_value = mock_receiver = asyncmock.AsyncMock( ) mock_receiver.new_message.bind.not_async = True monkeypatch.setattr(cli, "factory", mock_factories) cli.receiver("foo", loop=event_loop) mock_factories.get_receiver.assert_called_with("foo") mock_receiver.listen.assert_called() mock_receiver.new_message.bind.assert_called_with(cli.on_new_message)
def test_send__queue_not_found(self, monkeypatch, event_loop): mock_factories = asyncmock.Mock() mock_factories.get_sender.return_value = mock_sender = asyncmock.AsyncMock( ) mock_sender.send.side_effect = QueueNotFound monkeypatch.setattr(cli, "factory", mock_factories) actual = cli.send("foo", "bar", loop=event_loop) assert actual == -2 mock_factories.get_sender.assert_called_with("bar") mock_sender.send.assert_called_with(data="foo")
def test_receiver__queue_not_found(self, monkeypatch, event_loop): mock_factories = asyncmock.Mock() mock_factories.get_receiver.return_value = mock_receiver = asyncmock.AsyncMock( ) mock_receiver.listen.side_effect = QueueNotFound mock_receiver.new_message.bind.not_async = True monkeypatch.setattr(cli, "factory", mock_factories) actual = cli.receiver("foo", loop=event_loop) assert actual == -2 mock_factories.get_receiver.assert_called_with("foo") mock_receiver.listen.assert_called()
def servicex_ds(mocker): 'Create a mock ServiceXDataset' # Just pattern it off the real one. x = asyncmock.AsyncMock(spec=ServiceXDataset) # We need to return a loaded file. import uproot f_in = uproot.open('tests/sample_servicex_output.root') try: r = f_in[f_in.keys()[0]] data = r.arrays() # type: ignore finally: f_in._context.source.close() x.get_data_awkward_async.return_value = data return x
async def test_async(): mock = asyncmock.AsyncMock() await mock("foo", 123, bar="eek") mock.assert_called_with("foo", 123, bar="eek")
def test_not_async(): mock = asyncmock.AsyncMock(not_async=True) mock("foo", 123, bar="eek") mock.assert_called_with("foo", 123, bar="eek")
async def test_async_context_manager(): mock = asyncmock.AsyncMock() async with mock as x: assert x is mock