mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
34: Added tests.unit_common with frequently used utility functions; split ro __setattr__, __delattr__ methods out of Proxy and into new ReadOnly base class; added corresponding unit tests
This commit is contained in:
@@ -67,7 +67,32 @@ class Plugin(object):
|
||||
)
|
||||
|
||||
|
||||
class Proxy(object):
|
||||
class ReadOnly(object):
|
||||
"""
|
||||
Base class for classes with read-only attributes.
|
||||
"""
|
||||
__slots__ = tuple()
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
"""
|
||||
This raises an AttributeError anytime an attempt is made to set an
|
||||
attribute.
|
||||
"""
|
||||
raise AttributeError('read-only: cannot set %s.%s' %
|
||||
(self.__class__.__name__, name)
|
||||
)
|
||||
|
||||
def __delattr__(self, name):
|
||||
"""
|
||||
This raises an AttributeError anytime an attempt is made to delete an
|
||||
attribute.
|
||||
"""
|
||||
raise AttributeError('read-only: cannot del %s.%s' %
|
||||
(self.__class__.__name__, name)
|
||||
)
|
||||
|
||||
|
||||
class Proxy(ReadOnly):
|
||||
"""
|
||||
Used to only export certain attributes into the dynamic API.
|
||||
|
||||
@@ -92,24 +117,6 @@ class Proxy(object):
|
||||
for name in self.__slots__:
|
||||
object.__setattr__(self, name, getattr(obj, name))
|
||||
|
||||
def __setattr__(self, name, value):
|
||||
"""
|
||||
Proxy instances are read-only. This raises an AttributeError
|
||||
anytime an attempt is made to set an attribute.
|
||||
"""
|
||||
raise AttributeError('cannot set %s.%s' %
|
||||
(self.__class__.__name__, name)
|
||||
)
|
||||
|
||||
def __delattr__(self, name):
|
||||
"""
|
||||
Proxy instances are read-only. This raises an AttributeError
|
||||
anytime an attempt is made to delete an attribute.
|
||||
"""
|
||||
raise AttributeError('cannot del %s.%s' %
|
||||
(self.__class__.__name__, name)
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return '%s(%r)' % (self.__class__.__name__, self.__obj)
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
Unit tests for `ipalib.plugable` module.
|
||||
"""
|
||||
|
||||
import unit_common as uc
|
||||
from ipalib import plugable, errors
|
||||
|
||||
|
||||
@@ -52,6 +53,23 @@ def test_Plugin():
|
||||
assert repr(p) == '%s.some_plugin()' % __name__
|
||||
|
||||
|
||||
def test_ReadOnly():
|
||||
obj = plugable.ReadOnly()
|
||||
names = ['not_an_attribute', 'an_attribute']
|
||||
for name in names:
|
||||
uc.no_set(obj, name)
|
||||
uc.no_del(obj, name)
|
||||
|
||||
class some_ro_class(plugable.ReadOnly):
|
||||
def __init__(self):
|
||||
object.__setattr__(self, 'an_attribute', 'Hello world!')
|
||||
obj = some_ro_class()
|
||||
for name in names:
|
||||
uc.no_set(obj, name)
|
||||
uc.no_del(obj, name)
|
||||
assert uc.read_only(obj, 'an_attribute') == 'Hello world!'
|
||||
|
||||
|
||||
def test_Proxy():
|
||||
class CommandProxy(plugable.Proxy):
|
||||
__slots__ = (
|
||||
|
||||
63
ipalib/tests/unit_common.py
Normal file
63
ipalib/tests/unit_common.py
Normal file
@@ -0,0 +1,63 @@
|
||||
# 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
|
||||
|
||||
"""
|
||||
Utility functions for unit tests.
|
||||
"""
|
||||
|
||||
|
||||
def no_set(obj, name):
|
||||
"""
|
||||
Tests that attribute cannot be set.
|
||||
"""
|
||||
raised = False
|
||||
try:
|
||||
setattr(obj, name, 'some_new_obj')
|
||||
except AttributeError:
|
||||
raised = True
|
||||
assert raised
|
||||
|
||||
|
||||
def no_del(obj, name):
|
||||
"""
|
||||
Tests that attribute cannot be deleted.
|
||||
"""
|
||||
raised = False
|
||||
try:
|
||||
delattr(obj, name)
|
||||
except AttributeError:
|
||||
raised = True
|
||||
assert raised
|
||||
|
||||
|
||||
def read_only(obj, name):
|
||||
"""
|
||||
Tests that attribute is read-only. Returns attribute.
|
||||
"""
|
||||
assert isinstance(obj, object)
|
||||
assert hasattr(obj, name)
|
||||
|
||||
# Test that it cannot be set:
|
||||
no_set(obj, name)
|
||||
|
||||
# Test that it cannot be deleted:
|
||||
no_del(obj, name)
|
||||
|
||||
# Return the attribute
|
||||
return getattr(obj, name)
|
||||
Reference in New Issue
Block a user