From f6ac2df6bd7ceddd0f3eb968198cc3ebd1388087 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 7 Oct 2008 21:59:47 -0600 Subject: Moved tstutil.py into base of tests so it can be used by all test subpackages more easily --- tests/test_ipalib/test_backend.py | 2 +- tests/test_ipalib/test_cli.py | 2 +- tests/test_ipalib/test_config.py | 2 +- tests/test_ipalib/test_crud.py | 2 +- tests/test_ipalib/test_errors.py | 2 +- tests/test_ipalib/test_frontend.py | 4 +- tests/test_ipalib/test_ipa_types.py | 2 +- tests/test_ipalib/test_plugable.py | 6 +- tests/test_ipalib/test_tstutil.py | 148 ------------------------------------ tests/test_ipalib/test_util.py | 2 +- tests/test_ipalib/tstutil.py | 147 ----------------------------------- 11 files changed, 12 insertions(+), 307 deletions(-) delete mode 100644 tests/test_ipalib/test_tstutil.py delete mode 100644 tests/test_ipalib/tstutil.py (limited to 'tests/test_ipalib') diff --git a/tests/test_ipalib/test_backend.py b/tests/test_ipalib/test_backend.py index ddf665404..07c5eb009 100644 --- a/tests/test_ipalib/test_backend.py +++ b/tests/test_ipalib/test_backend.py @@ -22,7 +22,7 @@ Test the `ipalib.backend` module. """ from ipalib import backend, plugable, errors -from tstutil import ClassChecker +from tests.tstutil import ClassChecker class test_Backend(ClassChecker): diff --git a/tests/test_ipalib/test_cli.py b/tests/test_ipalib/test_cli.py index 4095af6de..c7b37126c 100644 --- a/tests/test_ipalib/test_cli.py +++ b/tests/test_ipalib/test_cli.py @@ -21,7 +21,7 @@ Test the `ipalib.cli` module. """ -from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker +from tests.tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker from ipalib import cli, plugable diff --git a/tests/test_ipalib/test_config.py b/tests/test_ipalib/test_config.py index 242ce16b8..a1329fa4c 100644 --- a/tests/test_ipalib/test_config.py +++ b/tests/test_ipalib/test_config.py @@ -23,7 +23,7 @@ Test the `ipalib.config` module. import types -from tstutil import raises +from tests.tstutil import raises from ipalib import config diff --git a/tests/test_ipalib/test_crud.py b/tests/test_ipalib/test_crud.py index fbfbfab1a..e2dd60aab 100644 --- a/tests/test_ipalib/test_crud.py +++ b/tests/test_ipalib/test_crud.py @@ -21,7 +21,7 @@ Test the `ipalib.crud` module. """ -from tstutil import read_only, raises, ClassChecker +from tests.tstutil import read_only, raises, ClassChecker from ipalib import crud, frontend, plugable, config def get_api(): diff --git a/tests/test_ipalib/test_errors.py b/tests/test_ipalib/test_errors.py index 7c81cf626..bb729776b 100644 --- a/tests/test_ipalib/test_errors.py +++ b/tests/test_ipalib/test_errors.py @@ -21,7 +21,7 @@ Test the `ipalib.errors` module. """ -from tstutil import raises, ClassChecker +from tests.tstutil import raises, ClassChecker from ipalib import errors diff --git a/tests/test_ipalib/test_frontend.py b/tests/test_ipalib/test_frontend.py index d99c7bd9d..320e20789 100644 --- a/tests/test_ipalib/test_frontend.py +++ b/tests/test_ipalib/test_frontend.py @@ -21,8 +21,8 @@ Test the `ipalib.frontend` module. """ -from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker -from tstutil import check_TypeError +from tests.tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker +from tests.tstutil import check_TypeError from ipalib import frontend, backend, plugable, errors, ipa_types, config diff --git a/tests/test_ipalib/test_ipa_types.py b/tests/test_ipalib/test_ipa_types.py index 81261a357..0b8b728e0 100644 --- a/tests/test_ipalib/test_ipa_types.py +++ b/tests/test_ipalib/test_ipa_types.py @@ -21,7 +21,7 @@ Test the `ipalib.ipa_types` module. """ -from tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker +from tests.tstutil import raises, getitem, no_set, no_del, read_only, ClassChecker from ipalib import ipa_types, errors, plugable diff --git a/tests/test_ipalib/test_plugable.py b/tests/test_ipalib/test_plugable.py index 759ec3c41..bfd7259f6 100644 --- a/tests/test_ipalib/test_plugable.py +++ b/tests/test_ipalib/test_plugable.py @@ -21,9 +21,9 @@ Test the `ipalib.plugable` module. """ -from tstutil import raises, no_set, no_del, read_only -from tstutil import getitem, setitem, delitem -from tstutil import ClassChecker +from tests.tstutil import raises, no_set, no_del, read_only +from tests.tstutil import getitem, setitem, delitem +from tests.tstutil import ClassChecker from ipalib import plugable, errors diff --git a/tests/test_ipalib/test_tstutil.py b/tests/test_ipalib/test_tstutil.py deleted file mode 100644 index c441ddb8e..000000000 --- a/tests/test_ipalib/test_tstutil.py +++ /dev/null @@ -1,148 +0,0 @@ -# Authors: -# Jason Gerard DeRose -# -# 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; version 2 only -# -# 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, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -""" -Test the `tests.tstutil` module. -""" - -import tstutil - - -class Prop(object): - def __init__(self, *ops): - self.__ops = frozenset(ops) - self.__prop = 'prop value' - - def __get_prop(self): - if 'get' not in self.__ops: - raise AttributeError('get prop') - return self.__prop - - def __set_prop(self, value): - if 'set' not in self.__ops: - raise AttributeError('set prop') - self.__prop = value - - def __del_prop(self): - if 'del' not in self.__ops: - raise AttributeError('del prop') - self.__prop = None - - prop = property(__get_prop, __set_prop, __del_prop) - - -def test_yes_raised(): - f = tstutil.raises - - class SomeError(Exception): - pass - - class AnotherError(Exception): - pass - - def callback1(): - 'raises correct exception' - raise SomeError() - - def callback2(): - 'raises wrong exception' - raise AnotherError() - - def callback3(): - 'raises no exception' - - f(SomeError, callback1) - - raised = False - try: - f(SomeError, callback2) - except AnotherError: - raised = True - assert raised - - raised = False - try: - f(SomeError, callback3) - except tstutil.ExceptionNotRaised: - raised = True - assert raised - - -def test_no_set(): - # Tests that it works when prop cannot be set: - tstutil.no_set(Prop('get', 'del'), 'prop') - - # Tests that ExceptionNotRaised is raised when prop *can* be set: - raised = False - try: - tstutil.no_set(Prop('set'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised - - -def test_no_del(): - # Tests that it works when prop cannot be deleted: - tstutil.no_del(Prop('get', 'set'), 'prop') - - # Tests that ExceptionNotRaised is raised when prop *can* be set: - raised = False - try: - tstutil.no_del(Prop('del'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised - - -def test_read_only(): - # Test that it works when prop is read only: - assert tstutil.read_only(Prop('get'), 'prop') == 'prop value' - - # Test that ExceptionNotRaised is raised when prop can be set: - raised = False - try: - tstutil.read_only(Prop('get', 'set'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised - - # Test that ExceptionNotRaised is raised when prop can be deleted: - raised = False - try: - tstutil.read_only(Prop('get', 'del'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised - - # Test that ExceptionNotRaised is raised when prop can be both set and - # deleted: - raised = False - try: - tstutil.read_only(Prop('get', 'del'), 'prop') - except tstutil.ExceptionNotRaised: - raised = True - assert raised - - # Test that AttributeError is raised when prop can't be read: - raised = False - try: - tstutil.read_only(Prop(), 'prop') - except AttributeError: - raised = True - assert raised diff --git a/tests/test_ipalib/test_util.py b/tests/test_ipalib/test_util.py index 0423ad621..5b8b13f79 100644 --- a/tests/test_ipalib/test_util.py +++ b/tests/test_ipalib/test_util.py @@ -21,7 +21,7 @@ Test the `ipalib.util` module. """ -from tstutil import raises +from tests.tstutil import raises from ipalib import util diff --git a/tests/test_ipalib/tstutil.py b/tests/test_ipalib/tstutil.py deleted file mode 100644 index 743716a08..000000000 --- a/tests/test_ipalib/tstutil.py +++ /dev/null @@ -1,147 +0,0 @@ -# Authors: -# Jason Gerard DeRose -# -# 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; version 2 only -# -# 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, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - -""" -Utility functions for the unit tests. -""" - -import inspect -from ipalib import errors - -class ExceptionNotRaised(Exception): - """ - Exception raised when an *expected* exception is *not* raised during a - unit test. - """ - msg = 'expected %s' - - def __init__(self, expected): - self.expected = expected - - def __str__(self): - return self.msg % self.expected.__name__ - - -def raises(exception, callback, *args, **kw): - """ - Tests that the expected exception is raised; raises ExceptionNotRaised - if test fails. - """ - raised = False - try: - callback(*args, **kw) - except exception, e: - raised = True - if not raised: - raise ExceptionNotRaised(exception) - return e - - -def getitem(obj, key): - """ - Works like getattr but for dictionary interface. Use this in combination - with raises() to test that, for example, KeyError is raised. - """ - return obj[key] - - -def setitem(obj, key, value): - """ - Works like setattr but for dictionary interface. Use this in combination - with raises() to test that, for example, TypeError is raised. - """ - obj[key] = value - - -def delitem(obj, key): - """ - Works like delattr but for dictionary interface. Use this in combination - with raises() to test that, for example, TypeError is raised. - """ - del obj[key] - - -def no_set(obj, name, value='some_new_obj'): - """ - Tests that attribute cannot be set. - """ - raises(AttributeError, setattr, obj, name, value) - - -def no_del(obj, name): - """ - Tests that attribute cannot be deleted. - """ - raises(AttributeError, delattr, obj, name) - - -def read_only(obj, name, value='some_new_obj'): - """ - Tests that attribute is read-only. Returns attribute. - """ - # Test that it cannot be set: - no_set(obj, name, value) - - # Test that it cannot be deleted: - no_del(obj, name) - - # Return the attribute - return getattr(obj, name) - - -def is_prop(prop): - return type(prop) is property - - -class ClassChecker(object): - __cls = None - __subcls = None - - def __get_cls(self): - if self.__cls is None: - self.__cls = self._cls - assert inspect.isclass(self.__cls) - return self.__cls - cls = property(__get_cls) - - def __get_subcls(self): - if self.__subcls is None: - self.__subcls = self.get_subcls() - assert inspect.isclass(self.__subcls) - return self.__subcls - subcls = property(__get_subcls) - - def get_subcls(self): - raise NotImplementedError( - self.__class__.__name__, - 'get_subcls()' - ) - - -def check_TypeError(value, type_, name, callback, *args, **kw): - """ - 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) == errors.TYPE_FORMAT % (name, type_, value) - return e -- cgit