-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move tests and assertions where they belong
- Loading branch information
Showing
3 changed files
with
124 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import unittest | ||
from unittest import mock | ||
|
||
from pyramid.config import Configurator | ||
|
||
from kinto.core import metrics | ||
|
||
|
||
class TestedClass: | ||
attribute = 3.14 | ||
|
||
def test_method(self): | ||
pass | ||
|
||
def _private_method(self): | ||
pass | ||
|
||
|
||
class WatchExecutionTimeTest(unittest.TestCase): | ||
def setUp(self): | ||
self.test_object = TestedClass() | ||
self.mocked = mock.MagicMock() | ||
metrics.watch_execution_time(self.mocked, self.test_object, prefix="test") | ||
|
||
def test_public_methods_generates_statsd_calls(self): | ||
self.test_object.test_method() | ||
self.mocked.timer.assert_called_with("test.testedclass.test_method") | ||
|
||
def test_private_methods_does_not_generates_statsd_calls(self): | ||
self.test_object._private_method() | ||
self.assertFalse(self.mocked().timer.called) | ||
|
||
|
||
class ListenerWithTimerTest(unittest.TestCase): | ||
def setUp(self): | ||
self.config = Configurator() | ||
self.func = lambda x: x # noqa: E731 | ||
|
||
def test_without_metrics_service(self): | ||
wrapped = metrics.listener_with_timer(self.config, "key", self.func) | ||
|
||
self.assertEqual(wrapped(42), 42) # does not raise | ||
|
||
def test_with_metrics_service(self): | ||
self.config.registry.registerUtility(mock.MagicMock(), metrics.IMetricsService) | ||
wrapped = metrics.listener_with_timer(self.config, "key", self.func) | ||
|
||
self.assertEqual(wrapped(42), 42) | ||
self.config.registry.metrics.timer.assert_called_with("key") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters