def test_clear_requestor(self): client = self.client with client_ctx(Resources, client): res = Resources() res.clear(provider=False, requestor=True) client.clear_dir.assert_called_with(DirectoryType.RECEIVED)
def test_show_all(self): with client_ctx(Settings, self.client): settings = Settings() result = settings.show(False, False, False) assert isinstance(result, dict) assert len(result) == len(self.client_config_dict) result = settings.show(True, True, True) assert isinstance(result, dict) assert len(result) == len(self.client_config_dict) result = settings.show(True, False, False) assert isinstance(result, dict) assert len(result) == len(Settings.basic_settings) result = settings.show(True, True, False) assert isinstance(result, dict) assert len(result) >= len(Settings.settings) - len( Settings.requestor_settings) result = settings.show(True, False, True) assert isinstance(result, dict) assert len(result) == len(Settings.basic_settings) + len( Settings.requestor_settings)
def test_create(self) -> None: client = self.client definition = TaskDefinition() definition.name = "The greatest task ever" def_str = json.dumps(definition.to_dict()) with client_ctx(Tasks, client): tasks = Tasks() # pylint: disable=no-member tasks._Tasks__create_from_json(def_str) # type: ignore # pylint: enable=no-member client._call.assert_called_once_with( 'comp.task.create', definition.to_dict(), ) client._call.reset_mock() patched_open = "golem.interface.client.tasks.open" with patch(patched_open, mock_open(read_data='{"name": "Golem task"}')): client._call.return_value = ('task_id', None) tasks.create("foo") task_def = json.loads('{"name": "Golem task"}') client._call.assert_called_once_with( 'comp.task.create', task_def, force=False, )
def test_subtasks_error(self): with client_ctx(Tasks, self.client): self.client.get_subtasks = Mock(return_value=None) tasks = Tasks() result = tasks.subtasks('task_id', None) self.assertEqual(result, 'No subtasks') self.client.get_subtasks.assert_called_once_with('task_id')
def test_template(self) -> None: tasks = Tasks() with patch('sys.stdout', io.StringIO()) as mock_io: tasks.template(None) output = mock_io.getvalue() self.assertIn("bid", output) self.assertIn("0.0", output) self.assertIn('"subtask_timeout": "0:00:00"', output) self.assertEqual(json.loads(output), TaskDefinition().to_dict()) temp = self.temp_file_name("test_template") tasks.template(temp) with open(temp) as f: content = f.read() self.assertEqual(content, output) with client_ctx(Tasks, self.client): Tasks.client.get_task.return_value = TaskDefinition().to_dict() tasks.dump('id', temp) with open(temp) as f: content_dump = f.read() self.assertEqual(content, content_dump)
def test_incomes(self): with client_ctx(incomes, self.client): result = incomes(None, None) self._assert_type_and_length(result, self.n_incomes) self.assertEqual(result.data[1][0], ['node_1', 'sent', '1.00000000 GNT'])
def test_incomes_awaiting(self): with client_ctx(incomes, self.client): result = incomes(None, "awaiting") self._assert_type_and_length(result, 1) self.assertEqual(result.data[1][0], ['node_3', 'awaiting', '3.00000000 GNT'])
def test_show_error(self): with client_ctx(Subtasks, self.client): self.client.get_subtask.return_value = None, 'error' subtasks = Subtasks() result = subtasks.show('subtask_id') self.assertEqual(result, 'error') self.client.get_subtask.assert_called_with('subtask_id')
def test_block_error(self): with client_ctx(Network, self.client): self.client.block_node.return_value = False, 'error_msg' network = Network() result = network.block('node_id') self.assertEqual(result, 'error_msg') self.client.block_node.assert_called_once_with('node_id')
def test_show(self): with client_ctx(Environments, self.client): result_1 = Environments().show(sort=None) assert isinstance(result_1, CommandResult) assert result_1.type == CommandResult.TABULAR assert result_1.data == (Environments.table_headers, [ ['env 2', 'False', 'False', '2000', '17', 'description 2'], ['env 1', 'True', 'True', '1000', '1777', 'description 1'], ]) result_2 = Environments().show(sort='name') assert result_2.data assert result_1.data != result_2.data self.client.get_environments.return_value = None result_3 = Environments().show(sort=None) result_4 = Environments().show(sort='name') assert isinstance(result_3, CommandResult) assert isinstance(result_4, CommandResult) assert result_3.data[0] == Environments.table_headers assert result_4.data[0] == Environments.table_headers assert not result_3.data[1] assert not result_4.data[1]
def test(self): node = dict(node_name='node1', key='deadbeef') client = Mock() client.__getattribute__ = assert_client_method client.get_node.return_value = node client.get_computing_trust.return_value = .01 client.get_requesting_trust.return_value = .02 client.get_payment_address.return_value = 'f0f0f0ababab' client.get_balance.return_value = 3 * denoms.ether, 2 * denoms.ether, denoms.ether with client_ctx(account, client): result = account() assert result == { 'node_name': 'node1', 'provider_reputation': 1, 'requestor_reputation': 2, 'Golem_ID': 'deadbeef', 'finances': { 'available_balance': '2.000000 GNT', 'eth_address': 'f0f0f0ababab', 'eth_balance': '1.000000 ETH', 'reserved_balance': '1.000000 GNT', 'total_balance': '3.000000 GNT' }, }
def test_clear_all(self): client = self.client with client_ctx(Resources, client): res = Resources() res.clear(provider=True, requestor=True) assert len(client.clear_dir.mock_calls) == 1
def test_dht(self): with client_ctx(Network, self.client): net = Network() result_1 = net.dht(None, full=False) result_2 = net.dht(None, full=True) self.__assert_peer_result(result_1, result_2)
def test_connect(self): with client_ctx(Network, self.client): with self.assertRaises(CommandException): Network().connect('266.266.0.1', '25000') assert not self.client.connect.called assert Network().connect('127.0.0.1', '25000') is None assert self.client.connect.called
def test_show(self, html2text: MagicMock): with client_ctx(Terms, self.client): terms = Terms() result = terms.show() self.client.show_terms.assert_called_once() html2text.assert_called_once_with( self.client.show_terms.return_value) self.assertEqual(result, html2text.return_value)
def test_show_ok(self): with client_ctx(Subtasks, self.client): subtask_dict = {'subtask_id': 'subtask_id'} self.client.get_subtask.return_value = subtask_dict, None subtasks = Subtasks() result = subtasks.show('subtask_id') self.assertEqual(result, subtask_dict) self.client.get_subtask.assert_called_with('subtask_id')
def test_unlock_unlocked(self, mock_pass: Mock): client = Mock() client.is_account_unlocked.return_value = True with client_ctx(Account, client): result = Account().unlock() assert result == "Account already unlocked" mock_pass.assert_not_called()
def test_accept(self, stdin): stdin.readline.return_value = 'y' with client_ctx(Terms, self.client): terms = Terms() terms.accept() self.client.accept_terms.assert_called_once_with( enable_monitor=True, enable_talkback=True, )
def test_incomes_confirmed(self): with client_ctx(incomes, self.client): result = incomes(None, "confirmed") self._assert_type_and_length(result, 2) self.assertEqual(result.data[1][0], ['node_2', 'confirmed', '2.00000000 GNT']) self.assertEqual(result.data[1][1], ['node_5', 'confirmed', '5.00000000 GNT'])
def test_incomes(self): with client_ctx(incomes, self.client): result = incomes(None) assert isinstance(result, CommandResult) assert result.type == CommandResult.TABULAR assert len(result.data[1]) == self.n_incomes assert result.data[1][0] == [ u'6e6f64655f31', u'waiting', u'0.000000 GNT', u'deadbeef01' ]
def test_unsupport(self): client = self.client with client_ctx(Tasks, client): tasks = Tasks() unsupport = tasks.unsupport(0) assert isinstance(unsupport, CommandResult) assert unsupport.data[1][0] == ['app_version', 3, '0.8.1'] assert unsupport.data[1][1] == ['max_price', 2, 7] assert unsupport.data[1][2] == ['environment_missing', 1, None]
def test_show_result_is_none(self): client = Mock() client.get_tasks.return_value = None with client_ctx(Tasks, client): tasks = Tasks() one_task = tasks.show("non existing task_id", None, current=True) self.assertIsNone(one_task)
def test_clear_none(self): client = self.client with client_ctx(Resources, client): res = Resources() with self.assertRaises(CommandException): res.clear(False, False) assert not client.clear_dir.called
def test_restart(self): client = self.client with client_ctx(Subtasks, client): subtasks = Subtasks() subtasks.restart('valid') client.restart_subtask.assert_called_with('valid') subtasks.restart('invalid') client.restart_subtask.assert_called_with('invalid')
def test_performance_multiplier_set(self): anInt = fake.random_int( min=MinPerformanceMultiplier.MIN, max=MinPerformanceMultiplier.MAX, ) with client_ctx(Environments, self.client): Environments().perf_mult_set(multiplier=anInt) self.client._call.assert_called_once_with( 'performance.multiplier.update', anInt, )
def test_subtasks_ok(self): client = self.client with client_ctx(Tasks, client): tasks = Tasks() subtasks = tasks.subtasks('745c1d01', None) assert isinstance(subtasks, CommandResult) assert subtasks.data[1][0] == [ 'node_1', 'subtask_1', '0:00:09', 'waiting', '1.00 %' ]
def test_deposit_payments(self): with client_ctx(payments, self.client): result = deposit_payments(None, None) self._assert_type_and_length(result, self.n_deposit_payments) self.assertEqual(result.data[1][3], [ 'deadbeaf4', 'sent', '4.40000000 GNT', '4.00000000 ETH', ])
def test_deposit_payments_awaiting(self): with client_ctx(payments, self.client): result = deposit_payments(None, 'awaiting') self._assert_type_and_length(result, 1) self.assertEqual(result.data[1][0], [ 'deadbeaf3', 'awaiting', '3.30000000 GNT', '3.00000000 ETH', ])
def test_restart_error(self): with client_ctx(Tasks, self.client): self.client._call.return_value = None, 'error' tasks = Tasks() with self.assertRaises(CommandException): tasks.restart('task_id') self.client._call.assert_called_once_with( 'comp.task.restart', 'task_id', force=False, )
def test_restart_success(self): with client_ctx(Tasks, self.client): self.client._call.return_value = 'new_task_id', None tasks = Tasks() result = tasks.restart('task_id') self.assertEqual(result, 'new_task_id') self.client._call.assert_called_once_with( 'comp.task.restart', 'task_id', force=False, )