diff options
author | Jason Gerard DeRose <jderose@redhat.com> | 2008-07-28 04:34:25 +0000 |
---|---|---|
committer | Jason Gerard DeRose <jderose@redhat.com> | 2008-07-28 04:34:25 +0000 |
commit | bc1675dc3853748064dbf1485bf58bce0e344add (patch) | |
tree | 1403fd2ae76c21b49f3ee16f91d72c79aac2d76e /ipalib/tests/test_plugable.py | |
parent | 8b64314359950801f1b3220f655261bcee2ead85 (diff) | |
download | freeipa-bc1675dc3853748064dbf1485bf58bce0e344add.tar.gz freeipa-bc1675dc3853748064dbf1485bf58bce0e344add.tar.xz freeipa-bc1675dc3853748064dbf1485bf58bce0e344add.zip |
30: Added plugable module with more generic implementation of Registrar; added corresponding unit tests
Diffstat (limited to 'ipalib/tests/test_plugable.py')
-rw-r--r-- | ipalib/tests/test_plugable.py | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/ipalib/tests/test_plugable.py b/ipalib/tests/test_plugable.py new file mode 100644 index 000000000..1ba021134 --- /dev/null +++ b/ipalib/tests/test_plugable.py @@ -0,0 +1,115 @@ +# 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; 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 + +""" +Unit tests for `ipalib.plugable` module. +""" + +from ipalib import plugable, exceptions + + +def test_Registrar(): + class Base1(object): + pass + class Base2(object): + pass + class Base3(object): + pass + class plugin1(Base1): + pass + class plugin2(Base2): + pass + class plugin3(Base3): + pass + + # Test creation of Registrar: + r = plugable.Registrar(Base1, Base2) + assert sorted(r) == ['Base1', 'Base2'] + + # Check that TypeError is raised trying to register something that isn't + # a class: + raised = False + try: + r(plugin1()) + except TypeError: + raised = True + assert raised + + # Check that SubclassError is raised trying to register a class that is + # not a subclass of an allowed base: + raised = False + try: + r(plugin3) + except exceptions.SubclassError: + raised = True + assert raised + + # Check that registration works + r(plugin1) + sub_d = r['Base1'] + assert len(sub_d) == 1 + assert sub_d['plugin1'] is plugin1 + # Check that a copy is returned + assert sub_d is not r['Base1'] + assert sub_d == r['Base1'] + + # Check that DuplicateError is raised trying to register exact class + # again: + raised = False + try: + r(plugin1) + except exceptions.DuplicateError: + raised = True + assert raised + + # Check that OverrideError is raised trying to register class with same + # name and same base: + orig1 = plugin1 + class base1_extended(Base1): + pass + class plugin1(base1_extended): + pass + raised = False + try: + r(plugin1) + except exceptions.OverrideError: + raised = True + assert raised + + # Check that overriding works + r(plugin1, override=True) + sub_d = r['Base1'] + assert len(sub_d) == 1 + assert sub_d['plugin1'] is plugin1 + assert sub_d['plugin1'] is not orig1 + + # Check that MissingOverrideError is raised trying to override a name + # not yet registerd: + raised = False + try: + r(plugin2, override=True) + except exceptions.MissingOverrideError: + raised = True + assert raised + + # Check that additional plugin can be registered: + r(plugin2) + sub_d = r['Base2'] + assert len(sub_d) == 1 + assert sub_d['plugin2'] is plugin2 |