Skip to content

Commit

Permalink
Add kwargs to infer_arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike committed Aug 22, 2022
1 parent 6fb9313 commit 681db0c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
26 changes: 17 additions & 9 deletions src/hdx/api/configuration.py
Expand Up @@ -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!"
)
Expand Down
4 changes: 0 additions & 4 deletions tests/hdx/api/test_configuration.py
Expand Up @@ -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")
Expand Down
9 changes: 7 additions & 2 deletions tests/hdx/conftest.py
Expand Up @@ -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")
Expand Down
31 changes: 29 additions & 2 deletions tests/hdx/facades/test_infer_arguments.py
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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(
Expand Down

0 comments on commit 681db0c

Please sign in to comment.