Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Authors: # Jason Gerard DeRose <jderose@redhat.com> # # Copyright (C) 2008 Red Hat # see file 'COPYING' for use and warranty information # # 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/>.
Common utility functions and classes for unit tests. """
""" Exception raised when an *expected* exception is *not* raised during a unit test. """
return self.msg % self.expected.__name__
""" Assert ``val1`` and ``val2`` are the same type and of equal value. """
""" Assert ``val1`` and ``val2`` are the same type and of non-equal value. """
""" Perform a fuzzy (non-strict) equality tests.
`Fuzzy` instances will likely be used when comparing nesting data-structures using `assert_deepequal()`.
By default a `Fuzzy` instance is equal to everything. For example, all of these evaluate to ``True``:
>>> Fuzzy() == False True >>> 7 == Fuzzy() # Order doesn't matter True >>> Fuzzy() == u'Hello False, Lucky 7!' True
The first optional argument *regex* is a regular expression pattern to match. For example, you could match a phone number like this:
>>> phone = Fuzzy('^\d{3}-\d{3}-\d{4}$') >>> u'123-456-7890' == phone True
Use of a regular expression by default implies the ``unicode`` type, so comparing with an ``str`` instance will evaluate to ``False``:
>>> phone.type <type 'unicode'> >>> '123-456-7890' == phone False
The *type* kwarg allows you to specify a type constraint, so you can force the above to work on ``str`` instances instead:
>>> '123-456-7890' == Fuzzy('^\d{3}-\d{3}-\d{4}$', type=str) True
You can also use the *type* constraint on its own without the *regex*, for example:
>>> 42 == Fuzzy(type=int) True >>> 42.0 == Fuzzy(type=int) False >>> 42.0 == Fuzzy(type=(int, float)) True
Finally the *test* kwarg is an optional callable that will be called to perform the loose equality test. For example:
>>> 42 == Fuzzy(test=lambda other: other > 42) False >>> 43 == Fuzzy(test=lambda other: other > 42) True
You can use *type* and *test* together. For example:
>>> 43 == Fuzzy(type=float, test=lambda other: other > 42) False >>> 42.5 == Fuzzy(type=float, test=lambda other: other > 42) True
The *regex*, *type*, and *test* kwargs are all availabel via attributes on the `Fuzzy` instance:
>>> fuzzy = Fuzzy('.+', type=str, test=lambda other: True) >>> fuzzy.regex '.+' >>> fuzzy.type <type 'str'> >>> fuzzy.test # doctest:+ELLIPSIS <function <lambda> at 0x...>
To aid debugging, `Fuzzy.__repr__()` revealse these kwargs as well:
>>> fuzzy # doctest:+ELLIPSIS Fuzzy('.+', <type 'str'>, <function <lambda> at 0x...>) """
""" Initialize.
:param regex: A regular expression pattern to match, e.g. ``u'^\d+foo'``
:param type: A type or tuple of types to test using ``isinstance()``, e.g. ``(int, float)``
:param test: A callable used to perform equality test, e.g. ``lambda other: other >= 18`` """ else:
self.__class__.__name__, self.regex, self.type, self.test )
%s expected = %r got = %r path = %r"""
%s type(expected) = %r type(got) = %r expected = %r got = %r path = %r"""
%s len(expected) = %r len(got) = %r expected = %r got = %r path = %r"""
%s missing keys = %r extra keys = %r expected = %r got = %r path = %r"""
""" Recursively check for type and equality.
If a value in expected is callable then it will used as a callback to test for equality on the got value. The callback is passed the got value and returns True if equal, False otherwise.
If the tests fails, it will raise an ``AssertionError`` with detailed information, including the path to the offending value. For example:
>>> expected = [u'Hello', dict(world=u'how are you?')] >>> got = [u'Hello', dict(world='how are you?')] >>> expected == got True >>> assert_deepequal(expected, got, doc='Testing my nested data') Traceback (most recent call last): ... AssertionError: assert_deepequal: type(expected) is not type(got). Testing my nested data type(expected) = <type 'unicode'> type(got) = <type 'str'> expected = u'how are you?' got = 'how are you?' path = (0, 'world')
Note that lists and tuples are considered equivalent, and the order of their elements does not matter. """ TYPE % (doc, type(expected), type(got), expected, got, stack) ) LEN % (doc, len(expected), len(got), expected, got, stack) ) doc, sorted(missing), sorted(extra), expected, got, stack ) ) raise AssertionError( VALUE % (doc, expected, got, stack) ) VALUE % (doc, expected, got, stack) )
""" Tests that the expected exception is raised; raises ExceptionNotRaised if test fails. """
""" Works like getattr but for dictionary interface. Use this in combination with raises() to test that, for example, KeyError is raised. """
""" Works like setattr but for dictionary interface. Use this in combination with raises() to test that, for example, TypeError is raised. """
""" Works like delattr but for dictionary interface. Use this in combination with raises() to test that, for example, TypeError is raised. """
""" Tests that attribute cannot be set. """
""" Tests that attribute cannot be deleted. """
""" Tests that attribute is read-only. Returns attribute. """ # Test that it cannot be set:
# Test that it cannot be deleted:
# Return the attribute
return type(prop) is property
raise NotImplementedError( self.__class__.__name__, 'get_subcls()' )
""" nose tear-down fixture. """
""" Tests a standard TypeError raised with `errors.raise_TypeError`. """ e = raises(TypeError, callback, *args, **kw) assert e.value is value assert e.type is type_ assert e.name == name assert type(e.name) is str assert str(e) == ipalib.errors.TYPE_ERROR % (name, type_, value) return e
""" Returns (api, home) tuple.
This function returns a tuple containing an `ipalib.plugable.API` instance and a `TempHome` instance. """ api.env[key] = value
""" Returns (api, home) tuple.
This function returns a tuple containing an `ipalib.plugable.API` instance and a `TempHome` instance. """
""" Create a testing api and register ``self.plugin``.
This method returns an (api, home) tuple.
:param plugins: Additional \*plugins to register. :param kw: Additional \**kw args to pass to `create_test_api`. """
""" nose tear-down fixture. """
self.translation_singular = u'The singular translation' self.translation_plural = u'The plural translation'
assert type(singular) is str assert type(plural) is str assert type(n) is int assert self.__called is False self.__called = True self.singular = singular self.plural = plural self.n = n if n == 1: return self.translation_singular return self.translation_plural
raise AssertionError( 'extra call: %s, %r, %r' % (name_, args_, kw_) ) raise AssertionError( 'call %d should be to method %r; got %r' % (i, name, name_) ) raise AssertionError( 'call %d to %r should have args %r; got %r' % (i, name, args, args_) ) raise AssertionError( 'call %d to %r should have kw %r, got %r' % (i, name, kw, kw_) )
|