def test_custom_kwargs_overrides_config(self): conf = {k: k for k in 'endpoint token custom'.split()} with mock.patch("dwave.cloud.client.load_config", lambda **kwargs: conf): with mock.patch("dwave.cloud.client.Client.__init__", return_value=None) as init: dwave.cloud.Client.from_config(custom='new-custom') init.assert_called_once_with( endpoint='endpoint', token='token', custom='new-custom')
def test_config_load_multiple_configfiles(self): """Test more specific config overrides less specific one, on a key by key basis.""" config_system = u""" [alpha] endpoint = alpha solver = DW_2000Q_1 """ config_user = u""" [alpha] solver = DW_2000Q_2 [beta] endpoint = beta """ with mock.patch("dwave.cloud.config.get_configfile_paths", lambda: ['config_system', 'config_user']): # test per-key override with mock.patch('dwave.cloud.config.open', create=True) as m: m.side_effect=[iterable_mock_open(config_system)(), iterable_mock_open(config_user)()] section = load_config(profile='alpha') self.assertEqual(section['endpoint'], 'alpha') self.assertEqual(section['solver'], 'DW_2000Q_2') # test per-section override (section addition) with mock.patch('dwave.cloud.config.open', create=True) as m: m.side_effect=[iterable_mock_open(config_system)(), iterable_mock_open(config_user)()] section = load_config(profile='beta') self.assertEqual(section['endpoint'], 'beta')
def test_custom_options(self): """Test custom options (request_timeout, polling_timeout, permissive_ssl) are propagated to Client.""" request_timeout = 15 polling_timeout = 180 with mock.patch("dwave.cloud.config.open", iterable_mock_open(config_body), create=True): with Client.from_config('config_file', profile='custom') as client: # check permissive_ssl and timeouts custom params passed-thru self.assertFalse(client.session.verify) self.assertEqual(client.request_timeout, request_timeout) self.assertEqual(client.polling_timeout, polling_timeout) # verify client uses those properly def mock_send(*args, **kwargs): self.assertEqual(kwargs.get('timeout'), request_timeout) response = requests.Response() response.status_code = 200 response._content = b'{}' return response with mock.patch("requests.adapters.HTTPAdapter.send", mock_send): client.get_solvers()
def test_file_read_error(self): """On config file read error, we should fail with `ConfigFileReadError`, but only if .dwrc actually exists on disk.""" with mock.patch("dwave.cloud.config.open", side_effect=OSError, create=True): with mock.patch("os.path.exists", lambda fn: True): self.assertRaises(ConfigFileReadError, legacy_load_config)
def test_only_file_key(self): """If give a name from the config file the proper URL should be loaded.""" with mock.patch("dwave.cloud.config.open", iterable_mock_open(config_body), create=True): with mock.patch("dwave.cloud.config.get_configfile_paths", lambda *x: ['file']): with Client.from_config(profile='alpha') as client: try: client.get_solver('arg-solver') except RequestEvent as event: self.assertTrue(event.url.startswith('http://file-alpha.url/')) return self.fail()
def test_legacy_config_load_fallback(self): conf = {k: k for k in 'endpoint token proxy solver'.split()} with mock.patch("dwave.cloud.client.load_config", return_value={}): with mock.patch("dwave.cloud.client.legacy_load_config", lambda **kwargs: conf): # test fallback works (legacy config is loaded) with dwave.cloud.Client.from_config(legacy_config_fallback=True) as client: self.assertEqual(client.endpoint, 'endpoint') self.assertEqual(client.token, 'token') self.assertEqual(client.default_solver, {"name__eq": "solver"}) self.assertEqual(client.session.proxies['http'], 'proxy') # test fallback is avoided (legacy config skipped) self.assertRaises( ValueError, dwave.cloud.Client.from_config, legacy_config_fallback=False)
def test_ping(self): config_file = 'dwave.conf' profile = 'profile' params = dict(num_reads=10) with mock.patch('dwave.cloud.cli.Client') as m: # mock returned solver client = m.from_config.return_value client.get_solver.return_value.nodes = [5, 7, 3] runner = CliRunner() with runner.isolated_filesystem(): touch(config_file) result = runner.invoke(cli, [ 'ping', '--config-file', config_file, '--profile', profile, '--sampling-params', json.dumps(params), '--request-timeout', '.5', '--polling-timeout', '30' ]) # proper arguments passed to Client.from_config? m.from_config.assert_called_with(config_file=config_file, profile=profile, solver=None, request_timeout=0.5, polling_timeout=30) # get solver called? client.get_solver.assert_called_with() # sampling method called on solver with correct params? solver = client.get_solver.return_value solver.sample_ising.assert_called_with({3: 0}, {}, **params) self.assertEqual(result.exit_code, 0)
def test_client_type(self): conf = {k: k for k in 'endpoint token'.split()} def mocked_load_config(**kwargs): kwargs.update(conf) return kwargs with mock.patch("dwave.cloud.client.load_config", mocked_load_config): with dwave.cloud.Client.from_config() as client: self.assertIsInstance(client, dwave.cloud.client.Client) with dwave.cloud.client.Client.from_config() as client: self.assertIsInstance(client, dwave.cloud.client.Client) with dwave.cloud.qpu.Client.from_config() as client: self.assertIsInstance(client, dwave.cloud.qpu.Client) with dwave.cloud.sw.Client.from_config() as client: self.assertIsInstance(client, dwave.cloud.sw.Client) with dwave.cloud.Client.from_config(client='qpu') as client: self.assertIsInstance(client, dwave.cloud.qpu.Client) with dwave.cloud.qpu.Client.from_config(client='base') as client: self.assertIsInstance(client, dwave.cloud.client.Client)
def test_eta_min_is_ignored_on_first_poll(self): "eta_min/earliest_estimated_completion should not be used anymore" with Client('endpoint', 'token') as client: now = datetime_in_future(0) eta_min, eta_max = datetime_in_future(10), datetime_in_future(30) client.session = mock.Mock() client.session.post = lambda path, _: choose_reply(path, { 'endpoint/problems/': '[%s]' % continue_reply( '1', 'abc123', eta_min=eta_min, eta_max=eta_max, now=now) }, date=now) client.session.get = lambda path: choose_reply(path, { 'endpoint/problems/?id=1': '[%s]' % complete_no_answer_reply('1', 'abc123'), 'endpoint/problems/1/': complete_reply('1', 'abc123') }, date=now) solver = Solver(client, solver_data('abc123')) def assert_no_delay(s): s and self.assertTrue( abs(s - client._POLL_BACKOFF_MIN) < client._POLL_BACKOFF_MIN / 10.0) with mock.patch('time.sleep', assert_no_delay): future = solver.sample_qubo({}) future.result()
def test_config_load_configfile_env(self): with mock.patch( "dwave.cloud.config.load_config_from_files", partial(self._load_config_from_files, provided=['myfile'])): with mock.patch.dict(os.environ, {'DWAVE_CONFIG_FILE': 'myfile'}): self._assert_config_valid( load_config(config_file=None, profile='alpha'))
def test_solver_name_from_config(self): solver_def = {"name__eq": "solver"} conf = {k: k for k in 'endpoint token solver'.split()} with mock.patch("dwave.cloud.client.load_config", lambda **kwargs: conf): with dwave.cloud.Client.from_config() as client: self.assertEqual(client.default_solver, solver_def)
def test_solvers(self): config_file = 'dwave.conf' profile = 'profile' solver = '{"qpu": true}' solvers = [solver_object('A'), solver_object('B')] with mock.patch('dwave.cloud.cli.Client') as m: # mock returned solvers client = m.from_config.return_value.__enter__.return_value client.get_solvers.return_value = solvers runner = CliRunner() with runner.isolated_filesystem(): touch(config_file) result = runner.invoke(cli, [ 'solvers', '--config-file', config_file, '--profile', profile, '--solver', solver ]) # proper arguments passed to Client.from_config? m.from_config.assert_called_with(config_file=config_file, profile=profile, solver=solver) # get solvers called? client.get_solvers.assert_called_with() # verify exit code and stdout printout self.assertEqual(result.exit_code, 0) self.assertEqual(result.output.count('Solver:'), 2) self.assertIn('Solver: A', result.output) self.assertIn('Solver: B', result.output)
def test_upload(self): config_file = 'dwave.conf' profile = 'profile' format = 'bq-zlib' problem_id = 'prob:lem:id' filename = 'filename' with mock.patch('dwave.cloud.cli.Client') as m: runner = CliRunner() with runner.isolated_filesystem(): touch(config_file) touch(filename) result = runner.invoke(cli, [ 'upload', '--config-file', config_file, '--profile', profile, '--format', format, '--problem-id', problem_id, filename ]) # proper arguments passed to Client.from_config? m.from_config.assert_called_with(config_file=config_file, profile=profile) # upload method called on client? c = m.from_config.return_value self.assertTrue(c.upload_problem_encoded.called) # verify problem_id args, kwargs = c.upload_problem_encoded.call_args self.assertEqual(kwargs.get('problem_id'), problem_id) self.assertEqual(result.exit_code, 0)
def test_immediate_polling_with_local_clock_unsynced(self): """First poll happens with minimal delay if local clock is way off from the remote/server clock.""" with Client('endpoint', 'token') as client: badnow = datetime_in_future(100) client.session = mock.Mock() client.session.post = lambda path, _: choose_reply( path, {'endpoint/problems/': '[%s]' % continue_reply('1', 'abc123')}, date=badnow) client.session.get = lambda path: choose_reply(path, { 'endpoint/problems/?id=1': '[%s]' % complete_no_answer_reply('1', 'abc123'), 'endpoint/problems/1/': complete_reply('1', 'abc123') }, date=badnow) solver = Solver(client, solver_data('abc123')) def assert_no_delay(s): s and self.assertTrue( abs(s - client._POLL_BACKOFF_MIN) < client._POLL_BACKOFF_MIN / 10.0) with mock.patch('time.sleep', assert_no_delay): future = solver.sample_qubo({}) future.result()
def test_config_load_multiple_explicit_configfiles(self): """Test more specific config overrides less specific one, on a key by key basis, in a list of explicitly given files.""" file1 = u""" [alpha] endpoint = alpha solver = DW_2000Q_1 """ file2 = u""" [alpha] solver = DW_2000Q_2 """ with mock.patch('dwave.cloud.config.open', create=True) as m: m.side_effect = [ iterable_mock_open(file1)(), iterable_mock_open(file2)() ] section = load_config(config_file=['file1', 'file2'], profile='alpha') m.assert_has_calls( [mock.call('file1', 'r'), mock.call('file2', 'r')]) self.assertEqual(section['endpoint'], 'alpha') self.assertEqual(section['solver'], 'DW_2000Q_2')
def test_immediate_polling_with_local_clock_unsynced(self): """First poll happens with minimal delay if local clock is way off from the remote/server clock.""" # each thread can have its instance of a session because # the mocked responses are stateless def create_mock_session(client): badnow = datetime_in_future(100) session = mock.Mock() session.post = lambda path, _: choose_reply( path, {'problems/': '[%s]' % continue_reply('1', 'abc123')}, date=badnow) session.get = lambda path: choose_reply(path, { 'problems/?id=1': '[%s]' % complete_no_answer_reply('1', 'abc123'), 'problems/1/': complete_reply('1', 'abc123') }, date=badnow) return session with mock.patch.object(Client, 'create_session', create_mock_session): with Client('endpoint', 'token') as client: solver = Solver(client, solver_data('abc123')) def assert_no_delay(s): s and self.assertTrue( abs(s - client._POLL_BACKOFF_MIN) < client._POLL_BACKOFF_MIN / 10.0) with mock.patch('time.sleep', assert_no_delay): future = solver.sample_qubo({}) future.result()
def test_sample(self): config_file = 'dwave.conf' profile = 'profile' biases = '[0]' couplings = '{(0, 4): 1}' num_reads = '10' with mock.patch('dwave.cloud.cli.Client') as m: runner = CliRunner() with runner.isolated_filesystem(): touch(config_file) result = runner.invoke(cli, [ 'sample', '--config-file', config_file, '--profile', profile, '-h', biases, '-j', couplings, '-n', num_reads ]) # proper arguments passed to Client.from_config? m.from_config.assert_called_with(config_file=config_file, profile=profile) # get solver called? c = m.from_config(config_file=config_file, profile=profile) c.get_solvers.assert_called_with() c.get_solver.assert_called_with() # sampling method called on solver? s = c.get_solver() s.sample_ising.assert_called_with([0], {(0, 4): 1}, num_reads=10) self.assertEqual(result.exit_code, 0)
def test_ping(self): config_file = 'dwave.conf' profile = 'profile' with mock.patch('dwave.cloud.cli.Client') as m: runner = CliRunner() with runner.isolated_filesystem(): touch(config_file) result = runner.invoke(cli, [ 'ping', '--config-file', config_file, '--profile', profile, '--request-timeout', '.5', '--polling-timeout', '30' ]) # proper arguments passed to Client.from_config? m.from_config.assert_called_with(config_file=config_file, profile=profile, request_timeout=0.5, polling_timeout=30) # get solver called? c = m.from_config(config_file=config_file, profile=profile) c.get_solvers.assert_called_with() c.get_solver.assert_called_with() # sampling method called on solver? s = c.get_solver() s.sample_ising.assert_called_with({0: 1}, {}) self.assertEqual(result.exit_code, 0)
def test_configure_inspect(self): runner = CliRunner() with runner.isolated_filesystem(): config_file = 'dwave.conf' with open(config_file, 'w') as f: f.write(''' [defaults] endpoint = 1 [a] endpoint = 2 [b] token = 3''') # test auto-detected case with mock.patch('dwave.cloud.config.get_configfile_paths', lambda **kw: [config_file]): result = runner.invoke(cli, ['config', 'inspect']) self.assertIn('endpoint = 2', result.output) # test explicit config result = runner.invoke( cli, ['config', 'inspect', '--config-file', config_file]) self.assertIn('endpoint = 2', result.output) # test explicit profile result = runner.invoke(cli, [ 'config', 'inspect', '--config-file', config_file, '--profile', 'b' ]) self.assertIn('endpoint = 1', result.output) self.assertIn('token = 3', result.output)
def test_config_load_env_override(self): with mock.patch( "dwave.cloud.config.load_config_from_files", partial(self._load_config_from_files, data=u"", provided=['myfile'])): with mock.patch.dict(os.environ, {'DWAVE_API_CLIENT': 'test'}): self.assertEqual( load_config(config_file='myfile')['client'], 'test') with mock.patch.dict(os.environ, {'DWAVE_API_ENDPOINT': 'test'}): self.assertEqual( load_config(config_file='myfile')['endpoint'], 'test') with mock.patch.dict(os.environ, {'DWAVE_API_TOKEN': 'test'}): self.assertEqual( load_config(config_file='myfile')['token'], 'test') with mock.patch.dict(os.environ, {'DWAVE_API_SOLVER': 'test'}): self.assertEqual( load_config(config_file='myfile')['solver'], 'test') with mock.patch.dict(os.environ, {'DWAVE_API_PROXY': 'test'}): self.assertEqual( load_config(config_file='myfile')['proxy'], 'test') with mock.patch.dict(os.environ, {'DWAVE_API_HEADERS': 'test'}): self.assertEqual( load_config(config_file='myfile')['headers'], 'test')
def test_config_load_configfile_arg_profile_default(self): """Check the right profile is loaded when `profile` specified only in [defaults] section. """ with mock.patch("dwave.cloud.config.load_config_from_files", partial(self._load_config_from_files, provided=['myfile'])): profile = load_config(config_file='myfile') self.assertEqual(profile['solver'], 'c4-sw_sample')
def test_solver_features_from_config(self): solver_def = {"qpu": True} conf = {k: k for k in 'endpoint token'.split()} conf.update(solver=json.dumps(solver_def)) with mock.patch("dwave.cloud.client.load_config", lambda **kwargs: conf): with dwave.cloud.Client.from_config() as client: self.assertEqual(client.default_solver, solver_def)
def test_iterable_mock_open(self): data = '1\n2\n3' namespaced_open = '{}.open'.format(__name__) # first, verify `mock.mock_open` fails for iteration with self.assertRaises(AttributeError): with mock.patch(namespaced_open, mock.mock_open(data), create=True): for line in open('filename'): pass # then verify our `iterable_mock_open` works lines = [] with mock.patch(namespaced_open, iterable_mock_open(data), create=True): for line in open('filename'): lines.append(line) self.assertEqual(''.join(lines), data)
def test_config_load_from_file(self): with mock.patch('dwave.cloud.config.open', iterable_mock_open(self.config_body), create=True): config = load_config_from_files(filenames=["filename"]) self.assertEqual(config.sections(), ['dw2000', 'software', 'alpha']) self.assertEqual(config['dw2000']['client'], 'qpu') self.assertEqual(config['software']['client'], 'sw')
def test_default(self): conf = {k: k for k in 'endpoint token'.split()} with mock.patch("dwave.cloud.client.load_config", lambda **kwargs: conf): with dwave.cloud.Client.from_config() as client: self.assertEqual(client.endpoint, 'endpoint') self.assertEqual(client.token, 'token') self.assertIsInstance(client, dwave.cloud.qpu.Client) self.assertNotIsInstance(client, dwave.cloud.sw.Client)
def test_config_load__profile_arg_nonexisting(self): """load_config should fail if the profile specified in kwargs or env in non-existing. """ with mock.patch("dwave.cloud.config.load_config_from_files", partial(self._load_config_from_files, provided=None)): self.assertRaises(ValueError, load_config, profile="nonexisting") with mock.patch.dict(os.environ, {'DWAVE_PROFILE': 'nonexisting'}): self.assertRaises(ValueError, load_config)
def test_headers_from_kwargs(self): headers_dict = {"key-1": "value-1", "key-2": "value-2"} headers_str = "key-2:value-2\nkey-1:value-1" conf = dict(token='token') def load_config(**kwargs): return merge(kwargs, conf, op=lambda a, b: a or b) # headers as dict with mock.patch("dwave.cloud.client.load_config", load_config): with dwave.cloud.Client.from_config( headers=headers_dict) as client: self.assertDictEqual(client.headers, headers_dict) # headers as str with mock.patch("dwave.cloud.client.load_config", load_config): with dwave.cloud.Client.from_config(headers=headers_str) as client: self.assertDictEqual(client.headers, headers_dict)
def test_config_file_detection_user(self): if sys.platform == 'win32': configpath = os.path.expanduser("~\\AppData\\Local\\dwavesystem\\dwave\\dwave.conf") elif sys.platform == 'darwin': configpath = os.path.expanduser("~/Library/Application Support/dwave/dwave.conf") else: configpath = os.path.expanduser("~/.config/dwave/dwave.conf") with mock.patch("os.path.exists", lambda path: path == configpath): self.assertEqual(get_configfile_paths(), [configpath])
def test_solver_features_kwargs_override_config(self): new_solver_def = {"software": True} conf = {k: k for k in 'endpoint token solver'.split()} def load_config(**kwargs): return merge(kwargs, conf, op=lambda a, b: a or b) with mock.patch("dwave.cloud.client.load_config", load_config): with dwave.cloud.Client.from_config(solver=new_solver_def) as client: self.assertEqual(client.default_solver, new_solver_def)
def test_solver_name_overrides_config_features(self): conf = {k: k for k in 'endpoint token solver'.split()} conf.update(solver=json.dumps({"software": True})) def load_config(**kwargs): return merge(kwargs, conf, op=lambda a, b: a or b) with mock.patch("dwave.cloud.client.load_config", load_config): with dwave.cloud.Client.from_config(solver='solver') as client: self.assertEqual(client.default_solver, {"name__eq": "solver"})