def test_conf_file_loads_well(self, monkeypatch, tmpdir): """Test that the load_config function loads a configuration from a TOML file correctly.""" filename = tmpdir.join("config.toml") with open(filename, "w") as f: f.write(TEST_FILE) with monkeypatch.context() as m: m.setattr(os, "getcwd", lambda: tmpdir) configuration = conf.load_config() assert configuration == EXPECTED_CONFIG
def test_get_api_section_safely_error(self, monkeypatch, tmpdir, caplog): """Test that the get_api_section_safely function raises an error and logs correctly if there is no api section in the configuration file.""" filename = tmpdir.join("config.toml") empty_file = "" with open(filename, "w") as f: f.write(empty_file) with monkeypatch.context() as m: with pytest.raises(conf.ConfigurationError, match=""): m.setattr(os, "getcwd", lambda: tmpdir) configuration = conf.load_config() assert "does not contain an \"api\" section" in caplog.text
def test_environment_variables_take_precedence_over_conf_file( self, monkeypatch, tmpdir): """Test that the data in environment variables take precedence over data in a configuration file.""" with open(tmpdir.join("test_config.toml"), "w") as f: f.write(TEST_FILE) with monkeypatch.context() as m: m.setattr(os, "getcwd", lambda: tmpdir) m.setenv("SF_API_AUTHENTICATION_TOKEN", "SomeAuth") m.setenv("SF_API_HOSTNAME", "SomeHost") m.setenv("SF_API_USE_SSL", "False") m.setenv("SF_API_DEBUG", "True") m.setenv("SF_API_PORT", "56") configuration = conf.load_config() assert configuration == OTHER_EXPECTED_CONFIG
def __init__( self, token: str = None, host: str = None, port: int = None, use_ssl: bool = None, verbose: bool = True, ): default_config = load_config() self._token = token or default_config["api"]["authentication_token"] self._host = host or default_config["api"]["hostname"] self._port = port or default_config["api"]["port"] self._use_ssl = use_ssl or default_config["api"]["use_ssl"] self._verbose = verbose self._base_url = "http{}://{}:{}".format("s" if self.use_ssl else "", self.host, self.port) self._headers = {"Authorization": self.token} self.log = create_logger(__name__)
def test_keywords_take_precedence_over_everything(self, monkeypatch, tmpdir): """Test that the keyword arguments passed to load_config take precedence over data in environment variables or data in a configuration file.""" with open(tmpdir.join("test_config.toml"), "w") as f: f.write(TEST_FILE) with monkeypatch.context() as m: m.setenv("SF_API_AUTHENTICATION_TOKEN", "NotOurAuth") m.setenv("SF_API_HOSTNAME", "NotOurHost") m.setenv("SF_API_USE_SSL", "True") m.setenv("SF_API_DEBUG", "False") m.setenv("SF_API_PORT", "42") m.setattr(os, "getcwd", lambda: tmpdir) configuration = conf.load_config(authentication_token="SomeAuth", hostname="SomeHost", use_ssl=False, port=56) assert configuration == OTHER_EXPECTED_CONFIG
def test_not_found_warning(self, caplog): """Test that a warning is raised if no configuration file found.""" conf.load_config(filename="NotAFileName") assert "No Strawberry Fields configuration file found." in caplog.text