diff --git a/src/hdx/api/configuration.py b/src/hdx/api/configuration.py index 0e0a01f..cb1743f 100755 --- a/src/hdx/api/configuration.py +++ b/src/hdx/api/configuration.py @@ -206,24 +206,32 @@ def __init__(self, **kwargs: Any) -> None: hdx_site = kwargs.get("hdx_site") if not hdx_site: hdx_site = self.data.get("hdx_site") - if not hdx_site: - hdx_site = "stage" + if not hdx_site: + hdx_site = "stage" self.hdx_site = f"hdx_{hdx_site}_site" if self.hdx_site not in self.data: raise ConfigurationError( f"{self.hdx_site} not defined in configuration!" ) - self.hdx_read_only = kwargs.get( - "hdx_read_only", self.data.get("hdx_read_only", False) - ) + hdx_read_only = kwargs.get("hdx_read_only") + if hdx_read_only is None: + hdx_read_only = self.data.get("hdx_read_only") + if hdx_read_only is None: + hdx_read_only = False + self.hdx_read_only = hdx_read_only logger.info(f"Read only access to HDX: {str(self.hdx_read_only)}") hdx_key_site = f"hdx_key_{hdx_site}" - hdx_key = kwargs.get(hdx_key_site, self.data.get(hdx_key_site)) + hdx_key = kwargs.get(hdx_key_site) + if not hdx_key: + hdx_key = self.data.get(hdx_key_site) + if not hdx_key: + hdx_key = kwargs.get("hdx_key") + if not hdx_key: + hdx_key = self.data.get("hdx_key") if hdx_key: self.hdx_key = hdx_key - else: - self.hdx_key = kwargs.get("hdx_key", self.data.get("hdx_key")) - if not self.hdx_key and not self.hdx_read_only: + return + if not self.hdx_read_only: raise ConfigurationError( "No HDX API key supplied as a parameter or in configuration!" ) diff --git a/tests/hdx/api/test_configuration.py b/tests/hdx/api/test_configuration.py index 2e10ff0..5148a95 100755 --- a/tests/hdx/api/test_configuration.py +++ b/tests/hdx/api/test_configuration.py @@ -19,10 +19,6 @@ def hdx_base_config_yaml(self, configfolder): def hdx_base_config_json(self, configfolder): return join(configfolder, "hdx_base_config.json") - @pytest.fixture(scope="class") - def hdx_config_json(self, configfolder): - return join(configfolder, "hdx_config.json") - @pytest.fixture(scope="class") def hdx_missing_site_config_json(self, configfolder): return join(configfolder, "hdx_missing_site_config.json") diff --git a/tests/hdx/conftest.py b/tests/hdx/conftest.py index 5b69d24..626559d 100755 --- a/tests/hdx/conftest.py +++ b/tests/hdx/conftest.py @@ -19,8 +19,13 @@ def configfolder(fixturesfolder): @pytest.fixture(scope="session") -def hdx_config_yaml(): - return join("tests", "fixtures", "config", "hdx_config.yml") +def hdx_config_yaml(configfolder): + return join(configfolder, "hdx_config.yml") + + +@pytest.fixture(scope="session") +def hdx_config_json(configfolder): + return join(configfolder, "hdx_config.json") @pytest.fixture(scope="session") diff --git a/tests/hdx/facades/test_infer_arguments.py b/tests/hdx/facades/test_infer_arguments.py index 5834c25..87334d1 100755 --- a/tests/hdx/facades/test_infer_arguments.py +++ b/tests/hdx/facades/test_infer_arguments.py @@ -11,7 +11,13 @@ class TestInferArguments: - def test_facade(self, monkeypatch, hdx_config_yaml, project_config_yaml): + def test_facade( + self, + monkeypatch, + hdx_config_yaml, + hdx_config_json, + project_config_yaml, + ): UserAgent.clear_global() mydata = "hello" monkeypatch.setattr( @@ -52,6 +58,23 @@ def test_facade(self, monkeypatch, hdx_config_yaml, project_config_yaml): facade(my_testfnia, hdx_site="stage", hdx_key="123") assert testresult.actual_result == mydata + UserAgent.clear_global() + monkeypatch.setattr( + sys, + "argv", + [ + "test", + "--mydata", + mydata, + "--hdx-site", + "prod", + "--user-agent", + "test", + ], + ) + with pytest.raises(FileNotFoundError): + facade(my_testfnia, hdx_config_yaml="NOT EXIST") + UserAgent.clear_global() monkeypatch.setattr( sys, @@ -67,7 +90,11 @@ def test_facade(self, monkeypatch, hdx_config_yaml, project_config_yaml): ], ) with pytest.raises(ConfigurationError): - facade(my_testfnia) + facade( + my_testfnia, + hdx_config_yaml=hdx_config_yaml, + hdx_config_json=hdx_config_json, + ) UserAgent.clear_global() monkeypatch.setattr(