async def test_non_tail(self): """ Called with a non-tail transaction. """ # For mocking GetTrytesCommand call self.adapter.seed_response( 'getTrytes', { # Tx with ID=1 'trytes': [self.three_tx_bundle[1].as_tryte_string()] }) # For mocking FindTransactionObjectsCommand call self.adapter.seed_response( 'findTransactions', {'hashes': [tx.hash for tx in self.three_tx_bundle]}) self.adapter.seed_response( 'getTrytes', {'trytes': [tx.as_tryte_string() for tx in self.three_tx_bundle]}) with mock.patch( 'iota.commands.extended.get_latest_inclusion.GetLatestInclusionCommand.__call__', MagicMock(return_value=async_return({ 'states': { self.three_tx_bundle.tail_transaction.hash: True } }))) as mocked_glis: with mock.patch( 'iota.commands.extended.get_bundles.GetBundlesCommand.__call__', MagicMock(return_value=async_return( {'bundles': [ self.three_tx_bundle, ]}))) as mocked_get_bundles: response = await get_bundles_from_transaction_hashes( adapter=self.adapter, transaction_hashes=[self.three_tx_bundle[1].hash], inclusion_states=True, ) self.assertListEqual( response, [ self.three_tx_bundle, ], ) self.assertTrue(response[0].is_confirmed) mocked_glis.assert_called_once_with( hashes=[self.three_tx_bundle.tail_transaction.hash]) mocked_get_bundles.assert_called_once_with( transactions=[self.three_tx_bundle.tail_transaction.hash])
async def test_happy_path_no_inclusion(self): """ A bundle is successfully fetched without inclusion states. """ self.adapter.seed_response( 'getTrytes', { 'trytes': self.single_bundle.as_tryte_strings() } ) with mock.patch( 'iota.commands.core.get_inclusion_states.GetInclusionStatesCommand.__call__', MagicMock(return_value=async_return({'states': { self.single_bundle.tail_transaction.hash: True }})) ) as mocked_glis: with mock.patch( 'iota.commands.extended.get_bundles.GetBundlesCommand.__call__', MagicMock(return_value=async_return({'bundles': [self.single_bundle]})) ) as mocked_get_bundles: response = await get_bundles_from_transaction_hashes( adapter=self.adapter, transaction_hashes=[self.single_bundle.tail_transaction.hash], inclusion_states=False, ) self.assertListEqual( response, [self.single_bundle], ) self.assertFalse( mocked_glis.called ) mocked_get_bundles.assert_called_once_with( transactions=[self.single_bundle.tail_transaction.hash] ) self.assertFalse( response[0].is_confirmed )
def test_happy_path_multiple_bundle(self): """ Test if command returns the correct bundle if underlying `get_bundles` returns multiple bundles. """ # Call the command with a tail hash. # Let's mock away GetBundlesCommand, and we don't do # BroadcastTransactionsCommand either. # Note that GetBundlesCommand returns multiple bundles! with patch( 'iota.commands.extended.get_bundles.GetBundlesCommand.__call__', MagicMock(return_value=[self.trytes, self.trytes_dummy ])) as mocked_get_bundles: with patch( 'iota.commands.core.BroadcastTransactionsCommand.__call__', MagicMock(return_value=[])) as mocked_broadcast: response = self.command(tail_hash=self.tail) # Expect only the first bundle self.assertEqual(response['trytes'], self.trytes)
async def test_wireup_async(self): """ Verify that the command is wired up correctly. (async) The API method indeed calls the appropiate command. """ with patch('iota.commands.core.get_tips.GetTipsCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = AsyncIota(self.adapter) response = await api.get_tips() self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. The API method indeed calls the appropiate command. """ with patch( 'iota.commands.core.add_neighbors.AddNeighborsCommand.__call__', MagicMock(return_value='You found me!')) as mocked_command: api = StrictIota(self.adapter) response = api.add_neighbors('test_uri') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. The API method indeed calls the appropiate command. """ with patch('iota.commands.core.get_tips.GetTipsCommand.__call__', MagicMock(return_value='You found me!')) as mocked_command: api = Iota(self.adapter) # Don't need to call with proper args here. response = api.get_tips() self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. (sync) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.core.remove_neighbors.RemoveNeighborsCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = Iota(self.adapter) response = api.remove_neighbors('uris') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
async def test_wireup_async(self): """ Verify that the command is wired up correctly. (async) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.core.attach_to_tangle.AttachToTangleCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = AsyncIota(self.adapter) response = await api.attach_to_tangle('trunk', 'branch', 'trytes') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. (sync) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.core.interrupt_attaching_to_tangle.InterruptAttachingToTangleCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = Iota(self.adapter) response = api.interrupt_attaching_to_tangle() self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. The API method indeed calls the appropiate command. """ with patch( 'iota.commands.extended.broadcast_and_store.BroadcastAndStoreCommand.__call__', MagicMock(return_value='You found me!')) as mocked_command: api = Iota(self.adapter) # Don't need to call with proper args here. response = api.broadcast_and_store('trytes') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
async def test_wireup_async(self): """ Verify that the command is wired up correctly. (async) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.core.were_addresses_spent_from.WereAddressesSpentFromCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = AsyncIota(self.adapter) response = await api.were_addresses_spent_from('addresses') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. (sync) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.core.get_missing_transactions.GetMissingTransactionsCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = Iota(self.adapter) response = api.get_missing_transactions() self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
async def test_wireup_async(self): """ Verify that the command is wired up correctly. (async) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.extended.send_trytes.SendTrytesCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = AsyncIota(self.adapter) # Don't need to call with proper args here. response = await api.send_trytes('trytes') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. (sync) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.extended.find_transaction_objects.FindTransactionObjectsCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = Iota(self.adapter) # Don't need to call with proper args here. response = api.find_transaction_objects('bundle') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. The API method indeed calls the appropiate command. """ with patch( 'iota.multisig.commands.prepare_multisig_transfer.PrepareMultisigTransferCommand.__call__', MagicMock(return_value='You found me!')) as mocked_command: api = MultisigIota(self.adapter) # Don't need to call with proper args here. response = api.prepare_multisig_transfer('transfer', 'multisig_input') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. (sync) The API method indeed calls the appropiate command. """ with patch( 'iota.multisig.commands.create_multisig_address.CreateMultisigAddressCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = MultisigIota(self.adapter) # Don't need to call with proper args here. response = api.create_multisig_address('digests') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
async def test_wireup_async(self): """ Verify that the command is wired up correctly. (async) The API method indeed calls the appropiate command. """ with patch( 'iota.multisig.commands.get_private_keys.GetPrivateKeysCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = AsyncMultisigIota(self.adapter) # Don't need to call with proper args here. response = await api.get_private_keys() self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. (sync) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.core.get_inclusion_states.GetInclusionStatesCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = Iota(self.adapter) # Don't need to call with proper args here. response = api.get_inclusion_states('transactions') self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. (sync) The API method indeed calls the appropiate command. """ with patch( 'iota.commands.extended.get_account_data.GetAccountDataCommand.__call__', MagicMock(return_value=async_return( 'You found me!'))) as mocked_command: api = Iota(self.adapter) # Don't need to call with proper args here. response = api.get_account_data() self.assertTrue(mocked_command.called) self.assertEqual(response, 'You found me!')
def test_wireup(self): """ Verify that the command is wired up correctly. (sync) The API method indeed calls the appropiate command. """ with patch('iota.commands.core.check_consistency.CheckConsistencyCommand.__call__', MagicMock(return_value=async_return('You found me!')) ) as mocked_command: api = Iota(self.adapter) response = api.check_consistency('tails') self.assertTrue(mocked_command.called) self.assertEqual( response, 'You found me!' )
def test_wireup(self): """ Verify that the command is wired up correctly. The API method indeed calls the appropiate command. """ with patch('iota.commands.core.attach_to_tangle.AttachToTangleCommand.__call__', MagicMock(return_value='You found me!') ) as mocked_command: api = Iota(self.adapter) # Don't need to call with proper args here. response = api.attach_to_tangle('trunk', 'branch', 'trytes') self.assertTrue(mocked_command.called) self.assertEqual( response, 'You found me!' )
async def test_multiple_tail_transactions(self): """ Multiple tail transactions are requested. """ self.adapter.seed_response( 'getTrytes', { 'trytes': [ self.single_bundle.tail_transaction.as_tryte_string(), self.three_tx_bundle.tail_transaction.as_tryte_string(), ] } ) with mock.patch( 'iota.commands.core.get_inclusion_states.GetInclusionStatesCommand.__call__', MagicMock(return_value=async_return({'states': { self.single_bundle.tail_transaction.hash: True, self.three_tx_bundle.tail_transaction.hash: True }})) ) as mocked_glis: with mock.patch( 'iota.commands.extended.get_bundles.GetBundlesCommand.__call__', MagicMock(return_value=async_return({ 'bundles': [ self.single_bundle, self.three_tx_bundle, ] })) ) as mocked_get_bundles: response = await get_bundles_from_transaction_hashes( adapter=self.adapter, transaction_hashes=[ self.single_bundle.tail_transaction.hash, self.three_tx_bundle.tail_transaction.hash ], inclusion_states=True, ) self.assertListEqual( response, [ self.single_bundle, self.three_tx_bundle, ], ) # Check if it was called only once mocked_glis.assert_called_once() # Get the keyword arguments from that call _, _, mocked_glis_kwargs = mocked_glis.mock_calls[0] # 'transactions' keyword's value should be a list of hashes it was called # with. Due to the set -> list conversion in the src code, we can't # be sure of the order of the elements, so we check by value. self.assertCountEqual( mocked_glis_kwargs.get('transactions'), [ self.three_tx_bundle.tail_transaction.hash, self.single_bundle.tail_transaction.hash, ] ) mocked_get_bundles.assert_called_once_with( transactions=[ self.single_bundle.tail_transaction.hash, self.three_tx_bundle.tail_transaction.hash, ] ) self.assertTrue( response[0].is_confirmed ) self.assertTrue( response[1].is_confirmed )
async def test_ordered_by_timestamp(self): """ Returned bundles are sorted by tail transaction timestamp. """ self.adapter.seed_response( 'getTrytes', { 'trytes': [ self.three_tx_bundle.tail_transaction.as_tryte_string(), self.single_bundle.tail_transaction.as_tryte_string(), ] }) with mock.patch( 'iota.commands.extended.get_latest_inclusion.GetLatestInclusionCommand.__call__', MagicMock(return_value=async_return({ 'states': { self.three_tx_bundle.tail_transaction.hash: True, self.single_bundle.tail_transaction.hash: True, } }))) as mocked_glis: with mock.patch( 'iota.commands.extended.get_bundles.GetBundlesCommand.__call__', MagicMock(return_value=async_return({ 'bundles': [ self.three_tx_bundle, self.single_bundle, ] }))) as mocked_get_bundles: response = await get_bundles_from_transaction_hashes( adapter=self.adapter, # three_tx_bundle is the first now, which should be newer # than single_bundle transaction_hashes=[ self.three_tx_bundle.tail_transaction.hash, self.single_bundle.tail_transaction.hash ], inclusion_states=True, ) self.assertListEqual( response, [ # Response is sorted in ascending order based on timestamp! # (single_bundle is older than three_tx_bundle) self.single_bundle, self.three_tx_bundle, ], ) # Check if it was called only once mocked_glis.assert_called_once() # Get the keyword arguments from that call _, _, mocked_glis_kwargs = mocked_glis.mock_calls[0] # 'hashes' keyword's value should be a list of hashes it was called # with. Due to the set -> list conversion in the src code, we can't # be sure of the order of the elements, so we check by value. self.assertCountEqual(mocked_glis_kwargs.get('hashes'), [ self.three_tx_bundle.tail_transaction.hash, self.single_bundle.tail_transaction.hash, ]) mocked_get_bundles.assert_called_once_with(transactions=[ self.three_tx_bundle.tail_transaction.hash, self.single_bundle.tail_transaction.hash, ]) self.assertTrue(response[0].is_confirmed) self.assertTrue(response[1].is_confirmed)