def test_save_tokens_no_config_found(self, monkeypatch, tmpdir, tokens, new_dir): """Tests that the save_tokens method correctly defaults to the user config directory when no configuration file exists.""" config_file_name = "config.toml" mock_config = qml.Configuration(config_file_name) if new_dir: # Case when the target directory doesn't exist directory = tmpdir.join("new_dir") else: directory = tmpdir filepath = directory.join(config_file_name) mock_config._user_config_dir = directory # Only the filename is in the filepath: just like when no config file # was found mock_config._filepath = config_file_name monkeypatch.setattr(qml, "default_config", mock_config) HQSDevice(2, machine=DUMMY_MACHINE).save_tokens(*tokens) with open(filepath) as f: configuration_file = toml.load(f) assert configuration_file["honeywell"]["global"][ "access_token"] == tokens[0] if len(tokens) > 1: assert configuration_file["honeywell"]["global"][ "refresh_token"] == tokens[1]
def test_api_key_not_found_error(self, monkeypatch, tmpdir): """Tests that an error is thrown with the device is created without a valid API token.""" monkeypatch.setenv("AQT_TOKEN", "") monkeypatch.setenv("PENNYLANE_CONF", "") monkeypatch.setattr("os.curdir", tmpdir.join("folder_without_a_config_file")) monkeypatch.setattr( "pennylane.default_config", qml.Configuration("config.toml") ) # force loading of config with pytest.raises(ValueError, match="No valid api key for AQT platform found"): dev = qml.device("aqt.sim", 2)
def test_user_not_found_error(self, monkeypatch, tmpdir): """Tests that an error is thrown with the device is created without a valid API token.""" monkeypatch.setenv("HQS_USER", "") monkeypatch.setenv("PENNYLANE_CONF", "") monkeypatch.setattr("os.curdir", tmpdir.join("folder_without_a_config_file")) monkeypatch.setattr( "pennylane.default_config", qml.Configuration("config.toml")) # force loading of config with pytest.raises(ValueError, match="No username for HQS platform found"): HQSDevice(2, machine=DUMMY_MACHINE)._login()
def test_device_gets_local_config(self, monkeypatch, tmpdir): """Tests that the device successfully reads a config from the local directory.""" monkeypatch.setenv("PENNYLANE_CONF", "") monkeypatch.setenv("HQS_USER", "") tmpdir.join("config.toml").write(test_config) monkeypatch.setattr("os.curdir", tmpdir) monkeypatch.setattr( "pennylane.default_config", qml.Configuration("config.toml")) # force loading of config dev = qml.device("honeywell.hqs", wires=2, machine=DUMMY_MACHINE) assert dev.shots == 99 assert dev._user == DUMMY_EMAIL
def test_user_not_found_error_login(self, monkeypatch, tmpdir): """Tests that an error is thrown with the device if no user email was specified before a login.""" monkeypatch.setenv("HQS_USER", "") monkeypatch.setenv("PENNYLANE_CONF", "") monkeypatch.setattr("os.curdir", tmpdir.join("folder_without_a_config_file")) monkeypatch.setattr( "pennylane.default_config", qml.Configuration("config.toml")) # force loading of config with pytest.raises(ValueError, match="No username for HQS platform found"): qml.device("honeywell.hqs", wires=2, machine=DUMMY_MACHINE)._login()
def test_device_gets_local_config(self, monkeypatch, tmpdir): """Tests that the device successfully reads a config from the local directory.""" monkeypatch.setenv("PENNYLANE_CONF", "") monkeypatch.setenv("AQT_TOKEN", "") tmpdir.join("config.toml").write(test_config) monkeypatch.setattr("os.curdir", tmpdir) monkeypatch.setattr( "pennylane.default_config", qml.Configuration("config.toml") ) # force loading of config dev = qml.device("aqt.sim", wires=2) assert dev.shots == 99 assert API_HEADER_KEY in dev.header.keys() assert dev.header[API_HEADER_KEY] == SOME_API_KEY
def test_device_gets_api_key_pennylane_conf_env_var(self, monkeypatch, tmpdir): """Tests that the device gets an api key via the PENNYLANE_CONF environment variable.""" monkeypatch.setenv("AQT_TOKEN", "") filepath = tmpdir.join("config.toml") filepath.write(test_config) monkeypatch.setenv("PENNYLANE_CONF", str(tmpdir)) monkeypatch.setattr("os.curdir", tmpdir.join("folder_without_a_config_file")) monkeypatch.setattr( "pennylane.default_config", qml.Configuration("config.toml") ) # force loading of config dev = qml.device("aqt.sim", wires=2) assert API_HEADER_KEY in dev.header.keys() assert dev.header[API_HEADER_KEY] == SOME_API_KEY
def test_device_gets_email_pennylane_conf_env_var(self, monkeypatch, tmpdir): """Tests that the device gets an email via the PENNYLANE_CONF environment variable.""" monkeypatch.setenv("HQS_USER", "") filepath = tmpdir.join("config.toml") filepath.write(test_config) monkeypatch.setenv("PENNYLANE_CONF", str(tmpdir)) monkeypatch.setattr("os.curdir", tmpdir.join("folder_without_a_config_file")) monkeypatch.setattr( "pennylane.default_config", qml.Configuration("config.toml")) # force loading of config dev = qml.device("honeywell.hqs", wires=2, machine=DUMMY_MACHINE) assert dev._user == DUMMY_EMAIL
def test_device_gets_api_key_default_config_directory(self, monkeypatch, tmpdir): """Tests that the device gets an api key that is stored in the default config directory.""" monkeypatch.setenv("AQT_TOKEN", "") monkeypatch.setenv("PENNYLANE_CONF", "") config_dir = tmpdir.mkdir("pennylane") # fake default config directory config_dir.join("config.toml").write(test_config) monkeypatch.setenv( "XDG_CONFIG_HOME", os.path.expanduser(tmpdir) ) # HACK: only works on linux monkeypatch.setattr("os.curdir", tmpdir.join("folder_without_a_config_file")) c = qml.Configuration("config.toml") monkeypatch.setattr("pennylane.default_config", c) # force loading of config dev = qml.device("aqt.sim", wires=2) assert API_HEADER_KEY in dev.header.keys() assert dev.header[API_HEADER_KEY] == SOME_API_KEY
def test_device_gets_email_default_config_directory( self, monkeypatch, tmpdir): """Tests that the device gets a user email that is stored in the default config directory.""" monkeypatch.setenv("HQS_USER", "") monkeypatch.setenv("PENNYLANE_CONF", "") config_dir = tmpdir.mkdir("pennylane") # fake default config directory config_dir.join("config.toml").write(test_config) monkeypatch.setenv( "XDG_CONFIG_HOME", os.path.expanduser(tmpdir)) # HACK: only works on linux monkeypatch.setattr("os.curdir", tmpdir.join("folder_without_a_config_file")) c = qml.Configuration("config.toml") monkeypatch.setattr("pennylane.default_config", c) # force loading of config dev = qml.device("honeywell.hqs", wires=2, machine=DUMMY_MACHINE) assert dev._user == DUMMY_EMAIL
def test_save_tokens(self, monkeypatch, tmpdir, tokens, new_dir): """Tests that the save_tokens method correctly saves to the PennyLane configuration file.""" mock_config = qml.Configuration("config.toml") if new_dir: # Case when the target directory doesn't exist filepath = tmpdir.join("new_dir").join("config.toml") else: filepath = tmpdir.join("config.toml") mock_config._filepath = filepath monkeypatch.setattr(qml, "default_config", mock_config) HQSDevice(2, machine=DUMMY_MACHINE).save_tokens(*tokens) with open(filepath) as f: configuration_file = toml.load(f) assert configuration_file["honeywell"]["global"][ "access_token"] == tokens[0] if len(tokens) > 1: assert configuration_file["honeywell"]["global"][ "refresh_token"] == tokens[1]