def test_local_cache(self): """ Installing a cache only for a single instance of :py:class:`AddressGenerator`. """ mock_generate_address = Mock(return_value=self.addy) with patch( 'cornode.crypto.addresses.AddressGenerator._generate_address', mock_generate_address, ): generator1 = AddressGenerator(Seed.random()) # Install the cache locally. generator1.cache = MemoryAddressCache() addy1 = generator1.get_addresses(42) mock_generate_address.assert_called_once() # The second time we try to generate the same address, it is # fetched from the cache. addy2 = generator1.get_addresses(42) mock_generate_address.assert_called_once() self.assertEqual(addy2, addy1) # Create a new instance to verify it has its own cache. generator2 = AddressGenerator(generator1.seed) # The generator has its own cache instance, so even though the # resulting address is the same, it is not fetched from cache. addy3 = generator2.get_addresses(42) self.assertEqual(mock_generate_address.call_count, 2) self.assertEqual(addy3, addy1)
def test_no_transactions(self): """ There are no transactions for the specified seed. """ # To speed up the test, we will mock the address generator. # :py:class:`cornode.crypto.addresses.AddressGenerator` already has # its own test case, so this does not impact the stability of the # codebase. # noinspection PyUnusedLocal def create_generator(ag, start, step=1): for addy in [self.addy1][start::step]: yield addy self.adapter.seed_response( 'findTransactions', { 'duration': 1, 'hashes': [], }, ) with patch( 'cornode.crypto.addresses.AddressGenerator.create_iterator', create_generator, ): response = self.command(seed=Seed.random()) self.assertDictEqual(response, {'bundles': []})
def test_global_cache(self): """ Installing a cache that affects all :py:class:`AddressGenerator` instances. """ # Install the cache globally. AddressGenerator.cache = MemoryAddressCache() mock_generate_address = Mock(return_value=self.addy) with patch( 'cornode.crypto.addresses.AddressGenerator._generate_address', mock_generate_address, ): generator1 = AddressGenerator(Seed.random()) addy1 = generator1.get_addresses(42) mock_generate_address.assert_called_once() # The second time we try to generate the same address, it is # fetched from the cache. addy2 = generator1.get_addresses(42) mock_generate_address.assert_called_once() self.assertEqual(addy2, addy1) # Create a new AddressGenerator and verify it uses the same # cache. generator2 = AddressGenerator(generator1.seed) # Cache is global, so the cached address is returned again. addy3 = generator2.get_addresses(42) mock_generate_address.assert_called_once() self.assertEqual(addy3, addy1)
def test_stop_threshold_zero(self): """ ``stop`` provided, ``threshold`` is 0. """ # Note that the first address has a zero balance. self.adapter.seed_response('getBalances', { 'balances': [0, 1], }) # To keep the unit test nice and speedy, we will mock the address # generator. We already have plenty of unit tests for that # functionality, so we can get away with mocking it here. mock_address_generator = Mock(return_value=[self.addy0, self.addy1]) with patch( 'cornode.crypto.addresses.AddressGenerator.get_addresses', mock_address_generator, ): response = self.command( seed=Seed.random(), stop=2, threshold=0, ) self.assertEqual(response['totalBalance'], 1) self.assertEqual(len(response['inputs']), 1) # Address 0 was skipped because it has a zero balance. input0 = response['inputs'][0] self.assertIsInstance(input0, Address) self.assertEqual(input0, self.addy1) self.assertEqual(input0.balance, 1) self.assertEqual(input0.key_index, 1)
def test_no_stop_threshold_not_met(self): """ No ``stop`` provided, balance does not meet ``threshold``. """ self.adapter.seed_response('getBalances', { 'balances': [42, 29, 0], }) # To keep the unit test nice and speedy, we will mock the address # generator. We already have plenty of unit tests for that # functionality, so we can get away with mocking it here. # noinspection PyUnusedLocal def mock_address_generator(ag, start, step=1): for addy in [self.addy0, self.addy1, self.addy2][start::step]: yield addy # When ``stop`` is None, the command uses a generator internally. with patch( 'cornode.crypto.addresses.AddressGenerator.create_iterator', mock_address_generator, ): with self.assertRaises(BadApiResponse): self.command( seed=Seed.random(), threshold=72, )
def test_cache_miss_seed(self): """ Cached addresses are keyed by seed. """ AddressGenerator.cache = MemoryAddressCache() mock_generate_address = Mock(return_value=self.addy) with patch( 'cornode.crypto.addresses.AddressGenerator._generate_address', mock_generate_address, ): generator1 = AddressGenerator(Seed.random()) generator1.get_addresses(42) mock_generate_address.assert_called_once() generator2 = AddressGenerator(Seed.random()) generator2.get_addresses(42) self.assertEqual(mock_generate_address.call_count, 2)
def test_start(self): """ Using ``start`` to offset the key range. """ self.adapter.seed_response('getBalances', { 'balances': [86], }) # ``getInputs`` uses ``findTransactions`` to identify unused # addresses. # noinspection SpellCheckingInspection self.adapter.seed_response( 'findTransactions', { 'hashes': [ TransactionHash( b'TESTVALUE9DONTUSEINPRODUCTION99999YFXGOD' b'GISBJAX9PDJIRDMDV9DCRDCAEG9FN9KECCBDDFZ9H'), ], }) self.adapter.seed_response('findTransactions', { 'hashes': [], }) # To keep the unit test nice and speedy, we will mock the address # generator. We already have plenty of unit tests for that # functionality, so we can get away with mocking it here. # noinspection PyUnusedLocal def mock_address_generator(ag, start, step=1): # If ``start`` has the wrong value, return garbage to make the # test asplode. for addy in [None, self.addy1, self.addy2][start::step]: yield addy # When ``stop`` is None, the command uses a generator internally. with patch( 'cornode.crypto.addresses.AddressGenerator.create_iterator', mock_address_generator, ): response = self.command( seed=Seed.random(), start=1, ) self.assertEqual(response['totalBalance'], 86) self.assertEqual(len(response['inputs']), 1) input0 = response['inputs'][0] self.assertIsInstance(input0, Address) self.assertEqual(input0, self.addy1) self.assertEqual(input0.balance, 86) self.assertEqual(input0.key_index, 1)
def __init__(self, adapter, seed=None, testnet=False): # type: (AdapterSpec, Optional[TrytesCompatible], bool) -> None """ :param seed: Seed used to generate new addresses. If not provided, a random one will be generated. Note: This value is never transferred to the node/network. """ super(cornode, self).__init__(adapter, testnet) self.seed = Seed(seed) if seed else Seed.random()
def test_thread_safety(self): """ Address cache is thread-safe, eliminating invalid cache misses when multiple threads attempt to access the cache concurrently. """ AddressGenerator.cache = MemoryAddressCache() seed = Seed.random() generated = [] def get_address(): generator = AddressGenerator(seed) generated.extend(generator.get_addresses(0)) # noinspection PyUnusedLocal def mock_generate_address(address_generator, key_iterator): # type: (AddressGenerator, KeyIterator) -> Address # Insert a teensy delay, to make it more likely that multiple # threads hit the cache concurrently. sleep(0.01) # Note that in this test, the address generator always returns a # new instance. return Address(self.addy, key_index=key_iterator.current) with patch( 'cornode.crypto.addresses.AddressGenerator._generate_address', mock_generate_address, ): threads = [Thread(target=get_address) for _ in range(100)] for t in threads: t.start() for t in threads: t.join() # Quick sanity check. self.assertEqual(len(generated), len(threads)) # If the cache is operating in a thread-safe manner, then it will # always return the exact same instance, given the same seed and # key index. expected = generated[0] for actual in generated[1:]: # Compare `id` values instead of using ``self.assertIs`` because # the failure message is a bit easier to understand. self.assertEqual(id(actual), id(expected))
def test_happy_path(self): """ Loading account data for an account. """ # noinspection PyUnusedLocal def mock_iter_used_addresses(adapter, seed, start): """ Mocks the ``iter_used_addresses`` function, so that we can simulate its functionality without actually connecting to the Tangle. References: - :py:func:`cornode.commands.extended.utils.iter_used_addresses` """ yield self.addy1, [self.hash1] yield self.addy2, [self.hash2] mock_get_balances = Mock(return_value={'balances': [42, 0]}) # Not particularly realistic, but good enough to prove that the # mocked function was invoked correctly. bundles = [Bundle(), Bundle()] mock_get_bundles_from_transaction_hashes = Mock(return_value=bundles) with patch( 'cornode.commands.extended.get_account_data.iter_used_addresses', mock_iter_used_addresses, ): with patch( 'cornode.commands.extended.get_account_data.get_bundles_from_transaction_hashes', mock_get_bundles_from_transaction_hashes, ): with patch( 'cornode.commands.core.get_balances.GetBalancesCommand._execute', mock_get_balances, ): response = self.command(seed=Seed.random()) self.assertDictEqual( response, { 'addresses': [self.addy1, self.addy2], 'balance': 42, 'bundles': bundles, }, )
def test_no_transactions(self): """ Loading account data for a seed that hasn't been used yet. """ with patch( 'cornode.commands.extended.get_account_data.iter_used_addresses', Mock(return_value=[]), ): response = self.command(seed=Seed.random()) self.assertDictEqual( response, { 'addresses': [], 'balance': 0, 'bundles': [], }, )
def test_stop_no_threshold(self): """ ``stop`` provided, no ``threshold``. """ self.adapter.seed_response('getBalances', { 'balances': [42, 29], }) # To keep the unit test nice and speedy, we will mock the address # generator. We already have plenty of unit tests for that # functionality, so we can get away with mocking it here. mock_address_generator = Mock(return_value=[self.addy0, self.addy1]) with patch( 'cornode.crypto.addresses.AddressGenerator.get_addresses', mock_address_generator, ): response = self.command( seed=Seed.random(), start=0, stop=2, ) self.assertEqual(response['totalBalance'], 71) self.assertEqual(len(response['inputs']), 2) input0 = response['inputs'][0] self.assertIsInstance(input0, Address) self.assertEqual(input0, self.addy0) self.assertEqual(input0.balance, 42) self.assertEqual(input0.key_index, 0) input1 = response['inputs'][1] self.assertIsInstance(input1, Address) self.assertEqual(input1, self.addy1) self.assertEqual(input1.balance, 29) self.assertEqual(input1.key_index, 1)
def test_stop_threshold_not_met(self): """ ``stop`` provided, balance does not meet ``threshold``. """ self.adapter.seed_response('getBalances', { 'balances': [42, 29], }) # To keep the unit test nice and speedy, we will mock the address # generator. We already have plenty of unit tests for that # functionality, so we can get away with mocking it here. mock_address_generator = Mock(return_value=[self.addy0, self.addy1]) with patch( 'cornode.crypto.addresses.AddressGenerator.get_addresses', mock_address_generator, ): with self.assertRaises(BadApiResponse): self.command( seed=Seed.random(), stop=2, threshold=72, )
def test_happy_path(self): """ Sending a transfer successfully. """ # noinspection SpellCheckingInspection transaction1 =\ TransactionTrytes( b'GYPRVHBEZOOFXSHQBLCYW9ICTCISLHDBNMMVYD9JJHQMPQCTIQAQTJNNNJ9IDXLRCC' b'OYOXYPCLR9PBEY9ORZIEPPDNTI9CQWYZUOTAVBXPSBOFEQAPFLWXSWUIUSJMSJIIIZ' b'WIKIRH9GCOEVZFKNXEVCUCIIWZQCQEUVRZOCMEL9AMGXJNMLJCIA9UWGRPPHCEOPTS' b'VPKPPPCMQXYBHMSODTWUOABPKWFFFQJHCBVYXLHEWPD9YUDFTGNCYAKQKVEZYRBQRB' b'XIAUX9SVEDUKGMTWQIYXRGSWYRK9SRONVGTW9YGHSZRIXWGPCCUCDRMAXBPDFVHSRY' b'WHGB9DQSQFQKSNICGPIPTRZINYRXQAFSWSEWIFRMSBMGTNYPRWFSOIIWWT9IDSELM9' b'JUOOWFNCCSHUSMGNROBFJX9JQ9XT9PKEGQYQAWAFPRVRRVQPUQBHLSNTEFCDKBWRCD' b'X9EYOBB9KPMTLNNQLADBDLZPRVBCKVCYQEOLARJYAGTBFR9QLPKZBOYWZQOVKCVYRG' b'YI9ZEFIQRKYXLJBZJDBJDJVQZCGYQMROVHNDBLGNLQODPUXFNTADDVYNZJUVPGB9LV' b'PJIYLAPBOEHPMR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HVTHEQUMHO999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999999999999999999999999999999999999' b'999999999999RKWEEVD99A99999999A99999999NFDPEEZCWVYLKZGSLCQNOFUSENI' b'XRHWWTZFBXMPSQHEDFWZULBZFEOMNLRNIDQKDNNIELAOXOVMYEI9PGTKORV9IKTJZQ' b'UBQAWTKBKZ9NEZHBFIMCLV9TTNJNQZUIJDFPTTCTKBJRHAITVSKUCUEMD9M9SQJ999' b'999TKORV9IKTJZQUBQAWTKBKZ9NEZHBFIMCLV9TTNJNQZUIJDFPTTCTKBJRHAITVSK' b'UCUEMD9M9SQJ999999999999999999999999999999999999999999999999999999' b'999999999999999999999999999999999' ) mock_prepare_transfer = Mock(return_value={ 'trytes': [transaction1], }) mock_send_trytes = Mock(return_value={ 'trytes': [transaction1], }) with patch( 'cornode.commands.extended.prepare_transfer.PrepareTransferCommand._execute', mock_prepare_transfer, ): with patch( 'cornode.commands.extended.send_trytes.SendTrytesCommand._execute', mock_send_trytes, ): response = self.command( depth = 100, minWeightMagnitude = 18, seed = Seed.random(), transfers = [ ProposedTransaction( address = Address( b'9999999999999999999999999999999999999999' b'99999999999999999999999999999999999999999' ), value = 0, ), ], ) bundle = response['bundle'] # type: Bundle self.assertEqual(len(bundle), 1) self.assertEqual(bundle[0].as_tryte_string(), transaction1)
def test_get_inclusion_states(self): """ Fetching inclusion states with transactions. """ # noinspection PyUnusedLocal def create_generator(ag, start, step=1): for addy in [self.addy1][start::step]: yield addy # The first address received cornode. self.adapter.seed_response( 'findTransactions', { 'duration': 42, 'hashes': [ 'TESTVALUEFIVE9DONTUSEINPRODUCTION99999VH' 'YHRHJETGYCAFZGABTEUBWCWAS9WF99UHBHRHLIOFJ', ], }, ) # For this test, we have to generate a real TryteString. transaction_trytes =\ TryteString( b'KMYUMNEUAYODAQSNGWTAERRRHNZBZCOLMVVOBTVWLOFYCJKYMGRAMH9RQ9MTZOSZMH' b'QNZFHFEJEDFQ99HSUNVOTULDJGXEDULS9ZHABVDZODJUMCNWVCPNSCUVKVYWCEXBHW' b'RBZBSWFPQLWZWMUPGQIGAEGOVE9DDXBVCIPKQYCFZFBELTSMVFSIXLPTACTKAFMCTK' b'CPYD9BWDJMLKWAOBDSJNQYAHS9GFIQKZCROLFZJVUEIVXVNBRRLEIWTYVHURUXHSCG' b'DKEIEGPOCXKCYWIBUG9ABYCALYJVFLBNGMS9ARHGTQXBZFLENXCJVKHPVKD9KSAEOL' b'FFVAJCNKLDVHOCDARWUNKARDYMVKFKRSMUTYOUXSBFFYTKRREBDJZTLVUROQFCBXQN' b'SXDDYTZTEBRSXOBMLXHJKSJAVOOVCXATOWNQDWHT9CCUAAJUJKDOQLMAEZACSNFKXZ' b'IGWDQEUEFRZYAOSDNVMSXWYLVDAUXZSHNHAIBEMNPFUGORYUETNJK9UCEMSUJYBBDK' b'BHIPKEINQCGOVYCPKUPJMUCUVZOJSIWYRFMFXYUVSMOUALAQBWIMXBUBXSAETGKJRP' b'AHVAXHQJDMEVSRFYEXUSIEBKMGYCUKFD9JPGUV9AIYUVCRUURKMYUHMVE9OJCYYWTQ' b'WUWFMTBZYFXASHHVCMSWXKBRQFHHQVEQMEULJRWZKLWFFSGGKEHUZZFNDNITSRAUH9' b'PQK9OGLYMVBSHXQLLZHOBBIM9KVUWDLHZRDKQQVLQXGWYXEEVQPDZUO9PVXMALOMRQ' b'VCTHGIZLILSCFKTBRESYZGBZKHXEODNDJZ9GK9ROWYXNGFHZCCBHHZEYEOGWXRGSUD' b'SUZFUAUBXVXZHCUVJSYBWTCYCEDYKZNGWFZ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BJYSOMPNMEAJGEHICJI' b'PVXRKCYX9RZVT9TDZIMXGZJAIYJRGIVMSOICSUINRBQILMJOUQYXCYNJ9WGGJFHYTU' b'LWOIPUXXFNTIFNOJRZFSQQNAWBQZOLHHLVGHEPWTKKQEVIPVWZUN9ZBICZ9DZZBVII' b'BF9EPHARZJUFJGBQXQFQIBUECAWRSEKYJNYKNSVBCOWTFBZ9NAHFSAMRBPEYGPRGKW' b'WTWACZOAPEOECUO9OTMGABJVAIICIPXGSXACVINSYEQFTRCQPCEJXZCY9XZWVWVJRZ' b'CYEYNFUUBKPWCHICGJZXKE9GSUDXZYUAPLHAKAHYHDXNPHENTERYMMBQOPSQIDENXK' b'LKCEYCPVTZQLEEJVYJZV9BWU999999999999999999999999999FFL999999999999' b'9999999999999RJQGVD99999999999A99999999USGBXHGJUEWAUAKNPPRHJXDDMQV' b'YDSYZJSDWFYLOQVFGBOSLE9KHFDLDYHUYTXVSFAFCOCLQUHJXTEIQRNBTLHEGJFGVF' b'DJCE9IKAOCSYHLCLWPVVNWNESKLYAJG9FGGZOFXCEYOTWLVIJUHGY9QCU9FMZJY999' b'9999HYBUYQKKRNAVDPVGYBTVDZ9SVQBLCCVLJTPEQWWOIG9CQZIFQKCROH9YHUCNJT' b'SYPBVZVBNESX999999D9TARGPQTNIYRZURQGVHCAWEDRBJIIEJIUZYENVE9LLJQMXH' b'GSUUYUCPSOWBCXVFDCHHAZUDC9LUODYWO' ) self.adapter.seed_response( 'getTrytes', { 'duration': 99, 'trytes': [binary_type(transaction_trytes)], }, ) transaction = Transaction.from_tryte_string(transaction_trytes) mock_get_bundles = Mock(return_value={ 'bundles': [Bundle([transaction])], }) mock_get_latest_inclusion = Mock(return_value={ 'states': { transaction.hash: True, }, }) with patch( 'cornode.crypto.addresses.AddressGenerator.create_iterator', create_generator, ): with patch( 'cornode.commands.extended.get_bundles.GetBundlesCommand._execute', mock_get_bundles, ): with patch( 'cornode.commands.extended.get_latest_inclusion.GetLatestInclusionCommand._execute', mock_get_latest_inclusion, ): response = self.command( seed = Seed.random(), inclusionStates = True, # To keep the test focused, only retrieve a single # transaction. start = 0, stop = 1, ) bundle = response['bundles'][0] # type: Bundle self.assertTrue(bundle[0].is_confirmed)
def test_stop(self): """ Scanning the Tangle for all transfers, with stop index. """ # noinspection PyUnusedLocal def create_generator(ag, start, step=1): # Inject an invalid value into the generator, to ensure it is # skipped. for addy in [self.addy1, None][start::step]: yield addy # The first address received cornode. self.adapter.seed_response( 'findTransactions', { 'duration': 42, 'hashes': [ 'TESTVALUEFIVE9DONTUSEINPRODUCTION99999VH' 'YHRHJETGYCAFZGABTEUBWCWAS9WF99UHBHRHLIOFJ', ], }, ) self.adapter.seed_response( 'getTrytes', { 'duration': 99, 'trytes': [''], }, ) bundle = Bundle([ Transaction( address = self.addy1, timestamp = 1483033814, # These values are not relevant to the test. hash_ = None, signature_message_fragment = None, value = 42, tag = Tag(b''), current_index = 0, last_index = 0, bundle_hash = None, trunk_transaction_hash = None, branch_transaction_hash = None, nonce = None, ) ]) mock_get_bundles = Mock(return_value={ 'bundles': [bundle], }) with patch( 'cornode.crypto.addresses.AddressGenerator.create_iterator', create_generator, ): with patch( 'cornode.commands.extended.get_bundles.GetBundlesCommand._execute', mock_get_bundles, ): response = self.command(seed=Seed.random(), stop=1) self.assertDictEqual( response, { 'bundles': [bundle], }, )
def test_no_stop_no_threshold(self): """ No ``stop`` provided, no ``threshold``. """ self.adapter.seed_response('getBalances', { 'balances': [42, 29], }) # ``getInputs`` uses ``findTransactions`` to identify unused # addresses. # noinspection SpellCheckingInspection self.adapter.seed_response( 'findTransactions', { 'hashes': [ TransactionHash( b'TESTVALUE9DONTUSEINPRODUCTION99999WBL9KD' b'EIZDMEDFPEYDIIA9LEMEUCC9MFPBY9TEVCUGSEGGN'), ], }) # noinspection SpellCheckingInspection self.adapter.seed_response( 'findTransactions', { 'hashes': [ TransactionHash( b'TESTVALUE9DONTUSEINPRODUCTION99999YFXGOD' b'GISBJAX9PDJIRDMDV9DCRDCAEG9FN9KECCBDDFZ9H'), ], }) self.adapter.seed_response('findTransactions', { 'hashes': [], }) # To keep the unit test nice and speedy, we will mock the address # generator. We already have plenty of unit tests for that # functionality, so we can get away with mocking it here. # noinspection PyUnusedLocal def mock_address_generator(ag, start, step=1): for addy in [self.addy0, self.addy1, self.addy2][start::step]: yield addy # When ``stop`` is None, the command uses a generator internally. with patch( 'cornode.crypto.addresses.AddressGenerator.create_iterator', mock_address_generator, ): response = self.command(seed=Seed.random(), ) self.assertEqual(response['totalBalance'], 71) self.assertEqual(len(response['inputs']), 2) input0 = response['inputs'][0] self.assertIsInstance(input0, Address) self.assertEqual(input0, self.addy0) self.assertEqual(input0.balance, 42) self.assertEqual(input0.key_index, 0) input1 = response['inputs'][1] self.assertIsInstance(input1, Address) self.assertEqual(input1, self.addy1) self.assertEqual(input1.balance, 29) self.assertEqual(input1.key_index, 1)
def test_full_scan(self): """ Scanning the Tangle for all transfers. """ # To speed up the test, we will mock the address generator. # :py:class:`cornode.crypto.addresses.AddressGenerator` already has # its own test case, so this does not impact the stability of the # codebase. # noinspection PyUnusedLocal def create_generator(ag, start, step=1): for addy in [self.addy1, self.addy2][start::step]: yield addy # The first address received cornode. self.adapter.seed_response( 'findTransactions', { 'duration': 42, 'hashes': [ 'TESTVALUEFIVE9DONTUSEINPRODUCTION99999VH' 'YHRHJETGYCAFZGABTEUBWCWAS9WF99UHBHRHLIOFJ', ], }, ) # The second address is unused. self.adapter.seed_response( 'findTransactions', { 'duration': 1, 'hashes': [], }, ) self.adapter.seed_response( 'getTrytes', { 'duration': 99, # Thankfully, we do not have to seed a realistic response for # ``getTrytes``, as we will be mocking the ``getBundles`` # command that uses on it. 'trytes': [''], }, ) bundle = Bundle([ Transaction( address = self.addy1, timestamp = 1483033814, # These values are not relevant to the test. hash_ = None, signature_message_fragment = None, value = 42, tag = Tag(b''), current_index = 0, last_index = 0, bundle_hash = None, trunk_transaction_hash = None, branch_transaction_hash = None, nonce = None, ) ]) mock_get_bundles = Mock(return_value={ 'bundles': [bundle], }) with patch( 'cornode.crypto.addresses.AddressGenerator.create_iterator', create_generator, ): with patch( 'cornode.commands.extended.get_bundles.GetBundlesCommand._execute', mock_get_bundles, ): response = self.command(seed=Seed.random()) self.assertDictEqual( response, { 'bundles': [bundle], }, )