Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mock_config_helper and mock_metrics test utils #23

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions shared/utils/test_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .mock_config_helper import mock_config_helper
from .mock_metrics import mock_metrics
39 changes: 39 additions & 0 deletions shared/utils/test_utils/mock_config_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from shared.config import ConfigHelper


def mock_config_helper(mocker, configs={}, file_configs={}):
"""
Generic utility for mocking two functions on `ConfigHelper`:
- `get()`, which takes a config key and returns its value
- `load_filename_from_path()`, which takes a config key, treats its value as
a file path, and returns the contents of the file.

These functions underpin the non-method APIs of the config module.

Example:
configs = {"github.client_id": "testvalue"}
file_configs = {"github.integration.pem": "--------BEGIN RSA PRIVATE KEY-----..."}
mock_config_helper(mocker, configs, file_configs)

assert "testvalue" == get_config("github", "client_id")
assert "BEGIN RSA" in load_file_from_path_at_config("github", "integration", "pem")
"""
orig_get = ConfigHelper.get
orig_load_file = ConfigHelper.load_filename_from_path

def mock_get(obj, *args, **kwargs):
conf_key = ".".join(args)
if conf_key in configs:
return configs.get(conf_key)
else:
return orig_get(obj, *args, **kwargs)

def mock_load_file(obj, *args, **kwargs):
conf_key = ".".join(args)
if conf_key in file_configs:
return file_configs.get(conf_key)
else:
return orig_load_file(obj, *args, **kwargs)

mocker.patch.object(ConfigHelper, "get", mock_get)
mocker.patch.object(ConfigHelper, "load_filename_from_path", mock_load_file)
20 changes: 20 additions & 0 deletions shared/utils/test_utils/mock_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from collections import defaultdict

from shared.metrics import metrics as orig_metrics


class MockMetrics:
def __init__(self, mocker):
self.data = defaultdict(int)
mocker.patch.object(orig_metrics, "incr", self._incr)
mocker.patch.object(orig_metrics, "decr", self._decr)

def _incr(self, stat, count=1, rate=1):
self.data[stat] += count

def _decr(self, stat, count=1, rate=1):
self.data[stat] -= count


def mock_metrics(mocker):
return MockMetrics(mocker)
Empty file.
12 changes: 12 additions & 0 deletions tests/unit/utils/test_utils/test_mock_config_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from shared.config import get_config, load_file_from_path_at_config
from shared.utils.test_utils import mock_config_helper


class TestMockConfigHelper(object):
def test_mock_config_helper_get(self, mocker):
mock_config_helper(mocker, configs={"foo.bar": "baz"})
assert get_config("foo", "bar", default="not baz") == "baz"

def test_mock_config_helper_load_file(self, mocker):
mock_config_helper(mocker, file_configs={"foo.bar": "baz"})
assert load_file_from_path_at_config("foo", "bar") == "baz"
13 changes: 13 additions & 0 deletions tests/unit/utils/test_utils/test_mock_metrics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from shared.metrics import metrics
from shared.utils.test_utils import mock_metrics


class TestMockMetrics(object):
def test_mock_metrics_incr_decr(self, mocker):
m = mock_metrics(mocker)

metrics.incr("foo", count=33)
assert m.data["foo"] == 33

metrics.decr("foo", count=15)
assert m.data["foo"] == 33 - 15