Skip to content

Commit

Permalink
Add rudimentary unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Basche authored and Tom Basche committed Dec 16, 2019
1 parent 627eb9e commit 916c43e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
8 changes: 5 additions & 3 deletions doghouse/datadog_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added tests/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions tests/test_datadog_client.py
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 916c43e

Please sign in to comment.