/usr/lib/python3/dist-packages/autopilot/tests/unit/test_utilities.py is in python3-autopilot-tests 1.4+14.04.20140416-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2013 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from mock import Mock, patch
import re
import six
from testtools import skipIf, TestCase
from testtools.matchers import (
Equals,
IsInstance,
LessThan,
MatchesRegex,
Not,
raises,
Raises,
)
import time
from autopilot.utilities import (
_raise_on_unknown_kwargs,
cached_result,
compatible_repr,
deprecated,
sleep,
)
class ElapsedTimeCounter(object):
"""A simple utility to count the amount of real time that passes."""
def __enter__(self):
self._start_time = time.time()
return self
def __exit__(self, *args):
pass
@property
def elapsed_time(self):
return time.time() - self._start_time
class MockableSleepTests(TestCase):
def test_mocked_sleep_contextmanager(self):
with ElapsedTimeCounter() as time_counter:
with sleep.mocked():
sleep(10)
self.assertThat(time_counter.elapsed_time, LessThan(2))
def test_mocked_sleep_methods(self):
with ElapsedTimeCounter() as time_counter:
sleep.enable_mock()
self.addCleanup(sleep.disable_mock)
sleep(10)
self.assertThat(time_counter.elapsed_time, LessThan(2))
def test_total_time_slept_starts_at_zero(self):
with sleep.mocked() as sleep_counter:
self.assertThat(sleep_counter.total_time_slept(), Equals(0.0))
def test_total_time_slept_accumulates(self):
with sleep.mocked() as sleep_counter:
sleep(1)
self.assertThat(sleep_counter.total_time_slept(), Equals(1.0))
sleep(0.5)
self.assertThat(sleep_counter.total_time_slept(), Equals(1.5))
sleep(0.5)
self.assertThat(sleep_counter.total_time_slept(), Equals(2.0))
def test_unmocked_sleep_calls_real_time_sleep_function(self):
with patch('autopilot.utilities.time') as patched_time:
sleep(1.0)
patched_time.sleep.assert_called_once_with(1.0)
class CompatibleReprTests(TestCase):
@skipIf(six.PY3, "Applicable to python 2 only")
def test_py2_unicode_is_returned_as_bytes(self):
repr_fn = compatible_repr(lambda: u"unicode")
result = repr_fn()
self.assertThat(result, IsInstance(six.binary_type))
self.assertThat(result, Equals(b'unicode'))
@skipIf(six.PY3, "Applicable to python 2 only")
def test_py2_bytes_are_untouched(self):
repr_fn = compatible_repr(lambda: b"bytes")
result = repr_fn()
self.assertThat(result, IsInstance(six.binary_type))
self.assertThat(result, Equals(b'bytes'))
@skipIf(six.PY2, "Applicable to python 3 only")
def test_py3_unicode_is_untouched(self):
repr_fn = compatible_repr(lambda: u"unicode")
result = repr_fn()
self.assertThat(result, IsInstance(six.text_type))
self.assertThat(result, Equals(u'unicode'))
@skipIf(six.PY2, "Applicable to python 3 only.")
def test_py3_bytes_are_returned_as_unicode(self):
repr_fn = compatible_repr(lambda: b"bytes")
result = repr_fn()
self.assertThat(result, IsInstance(six.text_type))
self.assertThat(result, Equals(u'bytes'))
class UnknownKWArgsTests(TestCase):
def test_raise_if_not_empty_raises_on_nonempty_dict(self):
populated_dict = dict(testing=True)
self.assertThat(
lambda: _raise_on_unknown_kwargs(populated_dict),
raises(ValueError("Unknown keyword arguments: 'testing'."))
)
def test_raise_if_not_empty_does_not_raise_on_empty(self):
empty_dict = dict()
self.assertThat(
lambda: _raise_on_unknown_kwargs(empty_dict),
Not(Raises())
)
class DeprecatedDecoratorTests(TestCase):
def test_deprecated_logs_warning(self):
@deprecated('Testing')
def not_testing():
pass
with patch('autopilot.utilities.logger') as patched_log:
not_testing()
self.assertThat(
patched_log.warning.call_args[0][0],
MatchesRegex(
"WARNING: in file \".*.py\", line \d+ in "
"test_deprecated_logs_warning\nThis "
"function is deprecated. Please use 'Testing' instead.\n",
re.DOTALL
)
)
class CachedResultTests(TestCase):
def get_wrapped_mock_pair(self):
inner = Mock()
# Mock() under python 2 does not support __name__. When we drop py2
# support we can obviously delete this hack:
if six.PY2:
inner.__name__ = ""
return inner, cached_result(inner)
def test_can_be_used_as_decorator(self):
@cached_result
def foo():
pass
def test_adds_reset_cache_callable_to_function(self):
@cached_result
def foo():
pass
self.assertTrue(hasattr(foo, 'reset_cache'))
def test_retains_docstring(self):
@cached_result
def foo():
"""xxXX super docstring XXxx"""
pass
self.assertThat(foo.__doc__, Equals("xxXX super docstring XXxx"))
def test_call_passes_through_once(self):
inner, wrapped = self.get_wrapped_mock_pair()
wrapped()
inner.assert_called_once_with()
def test_call_passes_through_only_once(self):
inner, wrapped = self.get_wrapped_mock_pair()
wrapped()
wrapped()
inner.assert_called_once_with()
def test_first_call_returns_actual_result(self):
inner, wrapped = self.get_wrapped_mock_pair()
self.assertThat(
wrapped(),
Equals(inner.return_value)
)
def test_subsequent_calls_return_actual_results(self):
inner, wrapped = self.get_wrapped_mock_pair()
wrapped()
self.assertThat(
wrapped(),
Equals(inner.return_value)
)
def test_can_pass_hashable_arguments(self):
inner, wrapped = self.get_wrapped_mock_pair()
wrapped(1, True, 2.0, "Hello", tuple(), )
inner.assert_called_once_with(1, True, 2.0, "Hello", tuple())
def test_passing_kwargs_raises_TypeError(self):
inner, wrapped = self.get_wrapped_mock_pair()
self.assertThat(
lambda: wrapped(foo='bar'),
raises(TypeError)
)
def test_passing_unhashable_args_raises_TypeError(self):
inner, wrapped = self.get_wrapped_mock_pair()
self.assertThat(
lambda: wrapped([]),
raises(TypeError)
)
def test_resetting_cache_works(self):
inner, wrapped = self.get_wrapped_mock_pair()
wrapped()
wrapped.reset_cache()
wrapped()
self.assertThat(inner.call_count, Equals(2))
|