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_explicit_only(self): """Specify information only through function arguments.""" with Client.from_config(endpoint=self.endpoint, token='arg-token') as client: try: client.get_solver('arg-solver') except RequestEvent as event: self.assertTrue(event.url.startswith(self.endpoint)) return self.fail()
def test_env_args_set(self): """With arguments and environment variables, the environment variables should be ignored.""" with mock.patch.dict(os.environ, {'DW_INTERNAL__HTTPLINK': 'http://env.url', 'DW_INTERNAL__TOKEN': 'env-token'}): with Client.from_config(endpoint=self.endpoint, token='args-token') as client: try: client.get_solver('arg-solver') except RequestEvent as event: self.assertTrue(event.url.startswith(self.endpoint)) return self.fail()
def test_only_file(self): """With no arguments or environment variables, the default connection from the config file should be used.""" with mock.patch("dwave.cloud.config.open", iterable_mock_open(config_body), create=True): with Client.from_config('config_file') as client: try: client.get_solver('arg-solver') except RequestEvent as event: self.assertTrue(event.url.startswith('http://file-prod.url/')) return self.fail()
def test_explicit_with_file(self): """With arguments and a config file, the config file should be ignored.""" with mock.patch("dwave.cloud.config.open", iterable_mock_open(config_body), create=True): with Client.from_config(endpoint=self.endpoint, token='arg-token') as client: try: client.get_solver('arg-solver') except RequestEvent as event: self.assertTrue(event.url.startswith(self.endpoint)) return self.fail()
def test_explicit_only(self): """Specify information only through function arguments.""" with Client.from_config(endpoint='arg-url', token='arg-token') as client: client.session.get = GetEvent.handle try: client.get_solver('arg-solver') except GetEvent as event: self.assertTrue(event.url.startswith('arg-url')) return self.fail()
def test_env_with_file_set(self): """With environment variables and a config file, the config file should be ignored.""" with mock.patch("dwave.cloud.config.open", iterable_mock_open(legacy_config_body), create=True): with mock.patch.dict(os.environ, {'DW_INTERNAL__HTTPLINK': 'http://env.url', 'DW_INTERNAL__TOKEN': 'env-token'}): with Client.from_config(config_file=False, legacy_config_fallback=True) as client: try: client.get_solver('arg-solver') except RequestEvent as event: self.assertTrue(event.url.startswith('http://env.url/')) return self.fail()
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 setUpClass(cls): try: with Client.from_config() as client: solvers = client.get_solvers(qpu=True) if not solvers: raise unittest.SkipTest("no qpu found") # select a QPU with less than 100% yield for solver in solvers: if solver.num_active_qubits < solver.num_qubits: cls.qpu = DWaveSampler(solver=solver.id) return raise unittest.SkipTest("no qpu with less than 100% yield found") except (ValueError, ConfigFileError, SolverNotFoundError): raise unittest.SkipTest("no qpu available")
def test_nonexisting_file(self): """With no values set, we should get an error when trying to create Client.""" with self.assertRaises(ConfigFileReadError): with Client.from_config(config_file='nonexisting', legacy_config_fallback=False) as client: pass