diff --git a/doghouse/datadog_client.py b/doghouse/datadog_client.py index a4d3a6b..611fa43 100644 --- a/doghouse/datadog_client.py +++ b/doghouse/datadog_client.py @@ -22,12 +22,14 @@ class DatadogClient: default_config_file = f"{home}/{config_dir}/{config_file}" def __init__(self): - config = self.__load_config() + config = self._load_config() if not config: config = self._create_config_file() - initialize(api_key=config["api_key"], app_key=config["app_key"]) + self.api_key = config["api_key"] + self.app_key = config["app_key"] + initialize(api_key=self.api_key, app_key=self.app_key) - def __load_config(self) -> Optional[dict]: + def _load_config(self) -> Optional[dict]: config_file = self.default_config_file if not os.path.exists(config_file): return None diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_datadog_client.py b/tests/test_datadog_client.py new file mode 100644 index 0000000..84e26d4 --- /dev/null +++ b/tests/test_datadog_client.py @@ -0,0 +1,26 @@ +from unittest.mock import patch + +from doghouse.datadog_client import DatadogClient + +config = { + 'app_key': 'test_app_key', + 'api_key': 'test_api_key' +} + + +@patch('doghouse.datadog_client.yaml.safe_load', return_value=config) +def test_load_yaml(mocked_yaml): + d = DatadogClient() + mocked_yaml.assert_called() + assert d.api_key == 'test_api_key' + assert d.app_key == 'test_app_key' + + +@patch('doghouse.datadog_client.yaml.dump') +@patch('doghouse.datadog_client.DatadogClient._load_config', return_value=None) +@patch('builtins.input', return_value="api_or_app_key") +@patch('doghouse.datadog_client.DatadogClient.default_config_file', return_value=".") +def test_no_config_file(input_func, load_config, mocked_yaml_create, mocked_file): + d = DatadogClient() + assert d.api_key == 'api_or_app_key' + assert d.app_key == 'api_or_app_key'