diff options
author | Tres Seaver <tseaver@palladion.com> | 2012-06-07 01:32:20 +0000 |
---|---|---|
committer | Tres Seaver <tseaver@palladion.com> | 2012-06-07 01:32:20 +0000 |
commit | ca74f8c929e56d3adf9e363d4311af9e639557ef (patch) | |
tree | 3e4480b88bad995fd8c8eac48b65b52bd92a01c5 | |
parent | 56ed6e2e928b7fa6f4c4bb3cb1e95f77324b6acc (diff) | |
download | zope-location-ca74f8c929e56d3adf9e363d4311af9e639557ef.tar.gz |
Add missing tests files.
-rw-r--r-- | src/zope/location/tests/test_location.py | 352 | ||||
-rw-r--r-- | src/zope/location/tests/test_pickling.py | 61 | ||||
-rw-r--r-- | src/zope/location/tests/test_traversing.py | 307 |
3 files changed, 720 insertions, 0 deletions
diff --git a/src/zope/location/tests/test_location.py b/src/zope/location/tests/test_location.py new file mode 100644 index 0000000..88b6248 --- /dev/null +++ b/src/zope/location/tests/test_location.py @@ -0,0 +1,352 @@ +############################################################################## +# +# Copyright (c) 2012 Zope Foundation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +import unittest + + +class ConformsToILocation(object): + + def test_class_conforms_to_ILocation(self): + from zope.interface.verify import verifyClass + from zope.location.interfaces import ILocation + verifyClass(ILocation, self._getTargetClass()) + + def test_instance_conforms_to_ILocation(self): + from zope.interface.verify import verifyObject + from zope.location.interfaces import ILocation + verifyObject(ILocation, self._makeOne()) + + +class LocationTests(unittest.TestCase, ConformsToILocation): + + def _getTargetClass(self): + from zope.location.location import Location + return Location + + def _makeOne(self): + return self._getTargetClass()() + + def test_ctor(self): + loc = self._makeOne() + self.assertEqual(loc.__parent__, None) + self.assertEqual(loc.__name__, None) + + +class Test_locate(unittest.TestCase): + + def _callFUT(self, obj, *args, **kw): + from zope.location.location import locate + return locate(obj, *args, **kw) + + def test_wo_name(self): + class Dummy(object): + pass + parent = Dummy() + dummy = Dummy() + self._callFUT(dummy, parent) + self.assertTrue(dummy.__parent__ is parent) + self.assertEqual(dummy.__name__, None) + + def test_w_name(self): + class Dummy(object): + pass + parent = Dummy() + dummy = Dummy() + self._callFUT(dummy, parent, 'name') + self.assertTrue(dummy.__parent__ is parent) + self.assertEqual(dummy.__name__, 'name') + + +class Test_located(unittest.TestCase): + + def _callFUT(self, obj, *args, **kw): + from zope.location.location import located + return located(obj, *args, **kw) + + def test_wo_name_obj_implements_ILocation(self): + from zope.interface import implementer + from zope.location.interfaces import ILocation + @implementer(ILocation) + class Dummy(object): + __parent__ = None + __name__ = object() + parent = Dummy() + dummy = Dummy() + self._callFUT(dummy, parent) + self.assertTrue(dummy.__parent__ is parent) + self.assertEqual(dummy.__name__, None) + + def test_w_name_adaptable_to_ILocation(self): + from zope.interface.interface import adapter_hooks + from zope.location.interfaces import ILocation + _hooked = [] + def _hook(iface, obj): + _hooked.append((iface, obj)) + return obj + class Dummy(object): + pass + parent = Dummy() + dummy = Dummy() + before = adapter_hooks[:] + adapter_hooks.insert(0, _hook) + try: + self._callFUT(dummy, parent, 'name') + finally: + adapter_hooks[:] = before + self.assertTrue(dummy.__parent__ is parent) + self.assertEqual(dummy.__name__, 'name') + self.assertEqual(len(_hooked), 1) + self.assertEqual(_hooked[0], (ILocation, dummy)) + + def test_wo_name_not_adaptable_to_ILocation(self): + class Dummy(object): + __parent__ = None + __name__ = 'before' + parent = Dummy() + dummy = Dummy() + self.assertRaises(TypeError, self._callFUT, dummy, parent, 'name') + self.assertEqual(dummy.__parent__, None) + self.assertEqual(dummy.__name__, 'before') + + +class Test_LocationIterator(unittest.TestCase): + + def _callFUT(self, obj): + from zope.location.location import LocationIterator + return LocationIterator(obj) + + def test_w_None(self): + self.assertEqual(list(self._callFUT(None)), []) + + def test_w_non_location_object(self): + island = object() + self.assertEqual(list(self._callFUT(island)), [island]) + + def test_w_isolated_location_object(self): + class Dummy(object): + __parent__ = None + __name__ = 'before' + island = Dummy() + self.assertEqual(list(self._callFUT(island)), [island]) + + def test_w_nested_location_object(self): + class Dummy(object): + __parent__ = None + __name__ = 'before' + parent = Dummy() + child = Dummy() + child.__parent__ = parent + grand = Dummy() + grand.__parent__ = child + self.assertEqual(list(self._callFUT(grand)), [grand, child, parent]) + + +class Test_inside(unittest.TestCase): + + def _callFUT(self, i1, i2): + from zope.location.location import inside + return inside(i1, i2) + + def test_w_non_location_objects(self): + island = object() + atoll = object() + self.assertTrue(self._callFUT(island, island)) + self.assertFalse(self._callFUT(island, atoll)) + self.assertFalse(self._callFUT(atoll, island)) + self.assertTrue(self._callFUT(atoll, atoll)) + + def test_w_isolated_location_objects(self): + class Dummy(object): + __parent__ = None + __name__ = 'before' + island = Dummy() + atoll = Dummy() + self.assertTrue(self._callFUT(island, island)) + self.assertFalse(self._callFUT(island, atoll)) + self.assertFalse(self._callFUT(atoll, island)) + self.assertTrue(self._callFUT(atoll, atoll)) + + def test_w_nested_location_object(self): + class Dummy(object): + __parent__ = None + __name__ = 'before' + parent = Dummy() + child = Dummy() + child.__parent__ = parent + grand = Dummy() + grand.__parent__ = child + self.assertTrue(self._callFUT(child, parent)) + self.assertFalse(self._callFUT(parent, child)) + self.assertTrue(self._callFUT(child, child)) + self.assertTrue(self._callFUT(grand, parent)) + self.assertFalse(self._callFUT(parent, grand)) + self.assertTrue(self._callFUT(grand, child)) + self.assertFalse(self._callFUT(child, grand)) + self.assertTrue(self._callFUT(grand, grand)) + + +class ClassAndInstanceDescrTests(unittest.TestCase): + + def _getTargetClass(self): + from zope.location.location import ClassAndInstanceDescr + return ClassAndInstanceDescr + + def _makeOne(self, _inst, _class): + return self._getTargetClass()(_inst, _class) + + def _makeScaffold(self): + _inst_called = [] + def _inst(*args, **kw): + _inst_called.append((args, kw)) + return 'INST' + _class_called = [] + def _class(*args, **kw): + _class_called.append((args, kw)) + return 'CLASS' + class Foo(object): + descr = self._makeOne(_inst, _class) + return Foo, _class_called, _inst_called + + def test_fetched_from_class(self): + Foo, _class_called, _inst_called = self._makeScaffold() + self.assertEqual(Foo.descr, 'CLASS') + self.assertEqual(_class_called, [((Foo,),{})]) + self.assertEqual(_inst_called, []) + + def test_fetched_from_instance(self): + Foo, _class_called, _inst_called = self._makeScaffold() + foo = Foo() + self.assertEqual(foo.descr, 'INST') + self.assertEqual(_class_called, []) + self.assertEqual(_inst_called, [((foo,),{})]) + + +_MARKER = object() + + +class LocationProxyTests(unittest.TestCase, ConformsToILocation): + + def _getTargetClass(self): + from zope.location.location import LocationProxy + return LocationProxy + + def _makeOne(self, obj=None, container=_MARKER, name=_MARKER): + if obj is None: + obj = object() + if container is _MARKER: + if name is _MARKER: + return self._getTargetClass()(obj) + return self._getTargetClass()(obj, name=name) + if name is _MARKER: + return self._getTargetClass()(obj, container) + return self._getTargetClass()(obj, container, name) + + def test_ctor_defaults(self): + dummy = object() # can't setattr + proxy = self._makeOne(dummy) + self.assertEqual(proxy.__parent__, None) + self.assertEqual(proxy.__name__, None) + + def test_ctor_explicit(self): + dummy = object() # can't setattr + parent = object() + proxy = self._makeOne(dummy, parent, 'name') + self.assertTrue(proxy.__parent__ is parent) + self.assertEqual(proxy.__name__, 'name') + + def test___doc___from_derived_class(self): + klass = self._getTargetClass() + class Derived(klass): + """DERIVED""" + self.assertEqual(Derived.__doc__, 'DERIVED') + + def test___doc___from_target_class(self): + klass = self._getTargetClass() + class Derived(klass): + """DERIVED""" + class Context(object): + """CONTEXT""" + proxy = self._makeOne(Context()) + self.assertEqual(proxy.__doc__, 'CONTEXT') + + def test___doc___from_target_instance(self): + klass = self._getTargetClass() + class Derived(klass): + """DERIVED""" + class Context(object): + """CONTEXT""" + context = Context() + context.__doc__ = 'INSTANCE' + proxy = self._makeOne(context) + self.assertEqual(proxy.__doc__, 'INSTANCE') + + def test___reduce__(self): + proxy = self._makeOne() + self.assertRaises(TypeError, proxy.__reduce__) + + def test___reduce_ex__(self): + proxy = self._makeOne() + self.assertRaises(TypeError, proxy.__reduce_ex__, 1) + + def test__providedBy___class(self): + from zope.interface import Interface + from zope.interface import implementer + from zope.interface import providedBy + from zope.interface import provider + class IProxyFactory(Interface): + pass + class IProxy(Interface): + pass + @provider(IProxyFactory) + @implementer(IProxy) + class Foo(self._getTargetClass()): + pass + self.assertEqual(list(providedBy(Foo)), [IProxyFactory]) + + def test__providedBy___instance(self): + from zope.interface import Interface + from zope.interface import implementer + from zope.interface import providedBy + from zope.interface import provider + from zope.location.interfaces import ILocation + class IProxyFactory(Interface): + pass + class IProxy(Interface): + pass + class IContextFactory(Interface): + pass + class IContext(Interface): + pass + @provider(IProxyFactory) + @implementer(IProxy) + class Proxy(self._getTargetClass()): + pass + @provider(IContextFactory) + @implementer(IContext) + class Context(object): + pass + context = Context() + proxy = Proxy(context) + self.assertEqual(list(providedBy(proxy)), [IContext, IProxy, ILocation]) + + +def test_suite(): + return unittest.TestSuite(( + unittest.makeSuite(LocationTests), + unittest.makeSuite(Test_locate), + unittest.makeSuite(Test_located), + unittest.makeSuite(Test_inside), + unittest.makeSuite(Test_LocationIterator), + unittest.makeSuite(ClassAndInstanceDescrTests), + unittest.makeSuite(LocationProxyTests), + )) diff --git a/src/zope/location/tests/test_pickling.py b/src/zope/location/tests/test_pickling.py new file mode 100644 index 0000000..10b46ec --- /dev/null +++ b/src/zope/location/tests/test_pickling.py @@ -0,0 +1,61 @@ +############################################################################## +# +# Copyright (c) 2012 Zope Foundation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +import unittest + + +class LocationCopyHookTests(unittest.TestCase): + + def _getTargetClass(self): + from zope.location.pickling import LocationCopyHook + return LocationCopyHook + + def _makeOne(self, obj=None): + if obj is None: + obj = object() + return self._getTargetClass()(obj) + + def test_class_conforms_to_ICopyHook(self): + from zope.interface.verify import verifyClass + from zope.copy.interfaces import ICopyHook + verifyClass(ICopyHook, self._getTargetClass()) + + def test_instance_conforms_to_ICopyHook(self): + from zope.interface.verify import verifyObject + from zope.copy.interfaces import ICopyHook + verifyObject(ICopyHook, self._makeOne()) + + def test___call___w_context_inside_toplevel(self): + from zope.copy.interfaces import ResumeCopy + class Dummy(object): + __parent__ = __name__ = None + top_level = Dummy() + context = Dummy() + context.__parent__ = top_level + hook = self._makeOne(context) + self.assertRaises(ResumeCopy, hook, top_level, object()) + + def test___call___w_context_outside_toplevel(self): + class Dummy(object): + __parent__ = __name__ = None + top_level = Dummy() + context = Dummy() + hook = self._makeOne(context) + self.assertTrue(hook(top_level, object()) is context) + + + +def test_suite(): + return unittest.TestSuite(( + unittest.makeSuite(LocationCopyHookTests), + )) diff --git a/src/zope/location/tests/test_traversing.py b/src/zope/location/tests/test_traversing.py new file mode 100644 index 0000000..71a119d --- /dev/null +++ b/src/zope/location/tests/test_traversing.py @@ -0,0 +1,307 @@ +############################################################################## +# +# Copyright (c) 2012 Zope Foundation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +import unittest + + +class ConformsToILocationInfo(object): + + def test_class_conforms_to_ILocationInfo(self): + from zope.interface.verify import verifyClass + from zope.location.interfaces import ILocationInfo + verifyClass(ILocationInfo, self._getTargetClass()) + + def test_instance_conforms_to_ILocationInfo(self): + from zope.interface.verify import verifyObject + from zope.location.interfaces import ILocationInfo + verifyObject(ILocationInfo, self._makeOne()) + + +class LocationPhysicallyLocatableTests( + unittest.TestCase, ConformsToILocationInfo): + + def _getTargetClass(self): + from zope.location.traversing import LocationPhysicallyLocatable + return LocationPhysicallyLocatable + + def _makeOne(self, obj=None): + if obj is None: + obj = object() + return self._getTargetClass()(obj) + + def test_getRoot_not_location_aware(self): + proxy = self._makeOne(object()) + self.assertRaises(AttributeError, proxy.getRoot) + + def test_getRoot_location_but_no_IRoot(self): + class Dummy(object): + __parent__ = None + proxy = self._makeOne(Dummy()) + self.assertRaises(TypeError, proxy.getRoot) + + def test_getRoot_wo_cycle(self): + from zope.interface import directlyProvides + from zope.location.interfaces import IRoot + class Dummy(object): + __parent__ = None + one = Dummy() + directlyProvides(one, IRoot) + two = Dummy() + two.__parent__ = one + three = Dummy() + three.__parent__ = two + proxy = self._makeOne(three) + self.assertTrue(proxy.getRoot() is one) + + def test_getRoot_w_cycle(self): + class Dummy(object): + __parent__ = None + one = Dummy() + two = Dummy() + two.__parent__ = one + three = Dummy() + three.__parent__ = two + one.__parent__ = three + proxy = self._makeOne(two) + self.assertRaises(TypeError, proxy.getRoot) + + def test_getPath_not_location_aware(self): + proxy = self._makeOne(object()) + self.assertRaises(AttributeError, proxy.getPath) + + def test_getPath_location_but_no_IRoot(self): + class Dummy(object): + __parent__ = __name__ = None + proxy = self._makeOne(Dummy()) + self.assertRaises(TypeError, proxy.getPath) + + def test_getPath_at_root(self): + from zope.interface import directlyProvides + from zope.location.interfaces import IRoot + class Dummy(object): + __parent__ = __name__ = None + one = Dummy() + directlyProvides(one, IRoot) + proxy = self._makeOne(one) + self.assertEqual(proxy.getPath(), '/') + + def test_getPath_wo_cycle(self): + from zope.interface import directlyProvides + from zope.location.interfaces import IRoot + class Dummy(object): + __parent__ = __name__ = None + one = Dummy() + directlyProvides(one, IRoot) + two = Dummy() + two.__parent__ = one + two.__name__ = 'two' + three = Dummy() + three.__parent__ = two + three.__name__ = 'three' + proxy = self._makeOne(three) + self.assertEqual(proxy.getPath(), '/two/three') + + def test_getPath_w_cycle(self): + class Dummy(object): + __parent__ = __name__ = None + one = Dummy() + two = Dummy() + two.__parent__ = one + two.__name__ = 'two' + three = Dummy() + three.__parent__ = two + three.__name__ = 'three' + one.__parent__ = three + proxy = self._makeOne(two) + self.assertRaises(TypeError, proxy.getPath) + + def test_getParent_not_location_aware(self): + proxy = self._makeOne(object()) + self.assertRaises(TypeError, proxy.getParent) + + def test_getParent_location_but_no_IRoot(self): + class Dummy(object): + __parent__ = __name__ = None + proxy = self._makeOne(Dummy()) + self.assertRaises(TypeError, proxy.getParent) + + def test_getParent_at_root(self): + from zope.interface import directlyProvides + from zope.location.interfaces import IRoot + class Dummy(object): + __parent__ = __name__ = None + one = Dummy() + directlyProvides(one, IRoot) + proxy = self._makeOne(one) + self.assertRaises(TypeError, proxy.getParent) + + def test_getParent_wo_cycle(self): + from zope.interface import directlyProvides + from zope.location.interfaces import IRoot + class Dummy(object): + __parent__ = __name__ = None + one = Dummy() + directlyProvides(one, IRoot) + two = Dummy() + two.__parent__ = one + two.__name__ = 'two' + three = Dummy() + three.__parent__ = two + three.__name__ = 'three' + proxy = self._makeOne(three) + self.assertTrue(proxy.getParent() is two) + + def test_getParents_not_location_aware(self): + proxy = self._makeOne(object()) + self.assertRaises(TypeError, proxy.getParents) + + def test_getParents_location_but_no_IRoot(self): + class Dummy(object): + __parent__ = __name__ = None + proxy = self._makeOne(Dummy()) + self.assertRaises(TypeError, proxy.getParents) + + def test_getParents_at_root(self): + from zope.interface import directlyProvides + from zope.location.interfaces import IRoot + class Dummy(object): + __parent__ = __name__ = None + one = Dummy() + directlyProvides(one, IRoot) + proxy = self._makeOne(one) + self.assertRaises(TypeError, proxy.getParents) + + def test_getParents_wo_cycle(self): + from zope.interface import directlyProvides + from zope.location.interfaces import IRoot + class Dummy(object): + __parent__ = __name__ = None + one = Dummy() + directlyProvides(one, IRoot) + two = Dummy() + two.__parent__ = one + two.__name__ = 'two' + three = Dummy() + three.__parent__ = two + three.__name__ = 'three' + proxy = self._makeOne(three) + self.assertEqual(proxy.getParents(), [two, one]) + + def test_getName_not_location_aware(self): + proxy = self._makeOne(object()) + self.assertRaises(AttributeError, proxy.getName) + + def test_getName_location(self): + class Dummy(object): + __name__ = None + proxy = self._makeOne(Dummy()) + self.assertEqual(proxy.getName(), None) + + def test_getName_location_w_name(self): + class Dummy(object): + __name__ = 'name' + proxy = self._makeOne(Dummy()) + self.assertEqual(proxy.getName(), 'name') + + def test_getNearestSite_context_is_site(self): + from zope.component.interfaces import ISite + from zope.interface import directlyProvides + class Dummy(object): + pass + context = Dummy() + directlyProvides(context, ISite) + proxy = self._makeOne(context) + self.assertTrue(proxy.getNearestSite() is context) + + def test_getNearestSite_ancestor_is_site(self): + from zope.component.interfaces import ISite + from zope.interface import directlyProvides + from zope.location.interfaces import IRoot + class Dummy(object): + pass + one = Dummy() + directlyProvides(one, (ISite, IRoot)) + two = Dummy() + two.__parent__ = one + two.__name__ = 'two' + three = Dummy() + three.__parent__ = two + three.__name__ = 'three' + proxy = self._makeOne(three) + self.assertTrue(proxy.getNearestSite() is one) + + def test_getNearestSite_no_site(self): + from zope.interface import directlyProvides + from zope.location.interfaces import IRoot + class Dummy(object): + __parent__ = __name__ = None + one = Dummy() + directlyProvides(one, IRoot) + two = Dummy() + two.__parent__ = one + two.__name__ = 'two' + three = Dummy() + three.__parent__ = two + three.__name__ = 'three' + proxy = self._makeOne(three) + self.assertTrue(proxy.getNearestSite() is one) + + +class RootPhysicallyLocatableTests( + unittest.TestCase, ConformsToILocationInfo): + + def _getTargetClass(self): + from zope.location.traversing import RootPhysicallyLocatable + return RootPhysicallyLocatable + + def _makeOne(self, obj=None): + if obj is None: + obj = object() + return self._getTargetClass()(obj) + + def test_getRoot(self): + context = object() + proxy = self._makeOne(context) + self.assertTrue(proxy.getRoot() is context) + + def test_getPath(self): + context = object() + proxy = self._makeOne(context) + self.assertEqual(proxy.getPath(), '/') + + def test_getParent(self): + context = object() + proxy = self._makeOne(context) + self.assertEqual(proxy.getParent(), None) + + def test_getParents(self): + context = object() + proxy = self._makeOne(context) + self.assertEqual(proxy.getParents(), []) + + def test_getName(self): + context = object() + proxy = self._makeOne(context) + self.assertEqual(proxy.getName(), '') + + def test_getNearestSite(self): + context = object() + proxy = self._makeOne(context) + self.assertTrue(proxy.getNearestSite() is context) + + +def test_suite(): + return unittest.TestSuite(( + unittest.makeSuite(LocationPhysicallyLocatableTests), + unittest.makeSuite(RootPhysicallyLocatableTests), + )) |