def test_name_decorator(self): destination = _tomodule.Class obj = _frommodule.Class name_override = 'whatever' gorilla.name(name_override)(gorilla.get_attribute(obj, 'class_method')) gorilla.name(name_override)(gorilla.get_attribute( obj, 'static_method')) gorilla.name(name_override)(gorilla.get_attribute(obj.Inner, 'method')) gorilla.patches(destination)(obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, name_override, gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, name_override, gorilla.get_attribute(obj, 'static_method')), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value')), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE')), gorilla.Patch(destination.Inner, name_override, gorilla.get_attribute(obj.Inner, 'method')), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_create_patches_5(self): destination = _tomodule obj = _frommodule gorilla.name('function')(gorilla.get_attribute(obj, 'Class')) gorilla.name('dummy_1')(gorilla.get_attribute(obj, 'Parent')) gorilla.name('dummy_2')(gorilla.get_attribute(obj, 'Child')) patches = gorilla.create_patches(destination, obj) expected_patches = [ gorilla.Patch(destination, 'dummy_2', gorilla.get_attribute(obj, 'Child')), gorilla.Patch(destination, 'function', gorilla.get_attribute(obj, 'Class')), gorilla.Patch(destination, 'dummy_1', gorilla.get_attribute(obj, 'Parent')), gorilla.Patch(destination, 'function', gorilla.get_attribute(obj, 'function')), gorilla.Patch(destination, 'global_variable', gorilla.get_attribute(obj, 'global_variable')), gorilla.Patch(destination, 'whatever', gorilla.get_attribute(obj, 'unbound_class_method')), gorilla.Patch(destination, 'unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method'), settings=gorilla.Settings(allow_hit=True)), ] self.assertEqual(patches, expected_patches)
def test_filter_decorator(self): destination = _tomodule.Class obj = _frommodule.Class gorilla.filter(True)(gorilla.get_attribute(obj, '__init__')) gorilla.filter(False)(gorilla.get_attribute(obj, 'class_method')) gorilla.filter(False)(gorilla.get_attribute(obj.Inner, 'method')) gorilla.patches(destination)(obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, '__init__', gorilla.get_attribute(obj, '__init__')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method')), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value')), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE')), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_get_original_attribute(self): destination = _tomodule.Class name = 'method' target = gorilla.get_attribute(destination, name) obj = gorilla.get_attribute(_frommodule, 'unbound_method') settings = gorilla.Settings(allow_hit=True) patch = gorilla.Patch(destination, name, obj, settings=settings) gorilla.apply(patch) self.assertIs(_unfold(gorilla.get_original_attribute(destination, name)), target) gorilla.apply(patch) self.assertIs(_unfold(gorilla.get_original_attribute(destination, name)), target)
def test_create_patches_3(self): def filter(name, value): return 'method' in name destination = _tomodule obj = _frommodule patches = gorilla.create_patches(destination, obj, filter=filter, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'unbound_class_method', gorilla.get_attribute(obj, 'unbound_class_method')), gorilla.Patch(destination, 'unbound_method', gorilla.get_attribute(obj, 'unbound_method')), gorilla.Patch(destination, 'unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Class obj = _frommodule.Class patches = gorilla.create_patches(destination, obj, filter=filter, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Parent obj = _frommodule.Parent patches = gorilla.create_patches(destination, obj, filter=filter, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Child obj = _frommodule.Child patches = gorilla.create_patches(destination, obj, filter=filter, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches)
def test_get_original_attribute(self): destination = _tomodule.Class name = 'method' target = gorilla.get_attribute(destination, name) obj = gorilla.get_attribute(_frommodule, 'unbound_method') settings = gorilla.Settings(allow_hit=True) patch = gorilla.Patch(destination, name, obj, settings=settings) gorilla.apply(patch) self.assertIs( _unfold(gorilla.get_original_attribute(destination, name)), target) gorilla.apply(patch) self.assertIs( _unfold(gorilla.get_original_attribute(destination, name)), target)
def test_apply_patch_with_hit_3(self): settings = gorilla.Settings(allow_hit=True, store_hit=False) source_paths = [''] + _list_attribute_paths(_frommodule) target_paths = _list_attribute_paths(_tomodule) combinations = itertools.product(source_paths, target_paths) for source_path, target_path in combinations: self.setUp() destination_path, name = _split_attribute_path(target_path) destination = _get_attribute_from_path(_tomodule, destination_path) obj = _get_attribute_from_path(_frommodule, source_path) patch = gorilla.Patch(destination, name, obj, settings=settings) gorilla.apply(patch) self.assertIs( destination, _get_attribute_from_path(_tomodule, destination_path)) result = gorilla.get_attribute(destination, name) self.assertIs(result, obj) self.assertRaises(AttributeError, gorilla.get_original_attribute, destination, name) self.tearDown()
def _get_attribute_from_path(module, path): if path == '': return module obj = module for part in path.split('.'): obj = gorilla.get_attribute(obj, part) return obj
def test_create_patches_5(self): destination = _tomodule obj = _frommodule gorilla.name('function')(gorilla.get_attribute(obj, 'Class')) gorilla.name('dummy_1')(gorilla.get_attribute(obj, 'Parent')) gorilla.name('dummy_2')(gorilla.get_attribute(obj, 'Child')) patches = gorilla.create_patches(destination, obj) expected_patches = [ gorilla.Patch(destination, 'dummy_2', gorilla.get_attribute(obj, 'Child')), gorilla.Patch(destination, 'function', gorilla.get_attribute(obj, 'Class')), gorilla.Patch(destination, 'dummy_1', gorilla.get_attribute(obj, 'Parent')), gorilla.Patch(destination, 'function', gorilla.get_attribute(obj, 'function')), gorilla.Patch(destination, 'global_variable', gorilla.get_attribute(obj, 'global_variable')), gorilla.Patch(destination, 'whatever', gorilla.get_attribute(obj, 'unbound_class_method')), gorilla.Patch(destination, 'unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method'), settings=gorilla.Settings(allow_hit=True)), ] self.assertEqual(patches, expected_patches)
def test_patch_decorator_on_function(self): destination = _tomodule obj = gorilla.get_attribute(_frommodule, 'function') self.assertIs(gorilla.patch(destination)(obj), obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'function', obj), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_patch_decorator_on_function(self): destination = _tomodule obj = gorilla.get_attribute(_frommodule, 'function') self.assertIs(gorilla.patch(destination)(obj), obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'function', obj), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_name_decorator(self): destination = _tomodule.Class obj = _frommodule.Class name_override = 'whatever' gorilla.name(name_override)(gorilla.get_attribute(obj, 'class_method')) gorilla.name(name_override)(gorilla.get_attribute(obj, 'static_method')) gorilla.name(name_override)(gorilla.get_attribute(obj.Inner, 'method')) gorilla.patches(destination)(obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, name_override, gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, name_override, gorilla.get_attribute(obj, 'static_method')), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value')), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE')), gorilla.Patch(destination.Inner, name_override, gorilla.get_attribute(obj.Inner, 'method')), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_filter_decorator(self): destination = _tomodule.Class obj = _frommodule.Class gorilla.filter(True)(gorilla.get_attribute(obj, '__init__')) gorilla.filter(False)(gorilla.get_attribute(obj, 'class_method')) gorilla.filter(False)(gorilla.get_attribute(obj.Inner, 'method')) gorilla.patches(destination)(obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, '__init__', gorilla.get_attribute(obj, '__init__')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method')), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value')), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE')), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_patch_decorator(self): destination = _tomodule obj = gorilla.get_attribute(_frommodule, 'function') settings = gorilla.Settings(allow_hit=True, store_hit=True) self.assertIs(gorilla.patch(destination, settings=settings)(obj), obj) settings.allow_hit = False settings.store_hit = False decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'function', obj, gorilla.Settings(allow_hit=True, store_hit=True)), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_patch_decorator(self): destination = _tomodule obj = gorilla.get_attribute(_frommodule, 'function') settings = gorilla.Settings(allow_hit=True, store_hit=True) self.assertIs(gorilla.patch(destination, settings=settings)(obj), obj) settings.allow_hit = False settings.store_hit = False decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'function', obj, gorilla.Settings(allow_hit=True, store_hit=True)), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_patches_decorator(self): destination = _tomodule.Class obj = _frommodule.Class settings = gorilla.Settings(allow_hit=True, store_hit=True) self.assertIs( gorilla.patches(destination, settings=settings)(obj), obj) settings.allow_hit = False settings.store_hit = False decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination.Inner, 'method', gorilla.get_attribute(obj.Inner, 'method'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_create_patches_3(self): def filter(name, value): return 'method' in name destination = _tomodule obj = _frommodule patches = gorilla.create_patches(destination, obj, filter=filter, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'unbound_class_method', gorilla.get_attribute(obj, 'unbound_class_method')), gorilla.Patch(destination, 'unbound_method', gorilla.get_attribute(obj, 'unbound_method')), gorilla.Patch(destination, 'unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Class obj = _frommodule.Class patches = gorilla.create_patches(destination, obj, filter=filter, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Parent obj = _frommodule.Parent patches = gorilla.create_patches(destination, obj, filter=filter, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Child obj = _frommodule.Child patches = gorilla.create_patches(destination, obj, filter=filter, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches)
def test_create_patches_4(self): def filter(name, value): return 'method' in name destination = _tomodule obj = _frommodule patches = gorilla.create_patches(destination, obj, filter=filter) expected_patches = [ gorilla.Patch(destination, 'function', gorilla.get_attribute(obj, 'function')), gorilla.Patch(destination, 'whatever', gorilla.get_attribute(obj, 'unbound_class_method')), gorilla.Patch(destination, 'unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method'), settings=gorilla.Settings(allow_hit=True)) ] self.assertEqual(patches, expected_patches) destination = _tomodule.Class obj = _frommodule.Class patches = gorilla.create_patches(destination, obj, filter=filter) expected_patches = [ gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'whatever', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Parent obj = _frommodule.Parent patches = gorilla.create_patches(destination, obj, filter=filter) expected_patches = [ gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Child obj = _frommodule.Child patches = gorilla.create_patches(destination, obj, filter=filter) expected_patches = [ gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches)
def test_patches_decorator_on_class(self): destination = _tomodule.Class obj = _frommodule.Class self.assertIs(gorilla.patches(destination)(obj), obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method')), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value')), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE')), gorilla.Patch(destination.Inner, 'method', gorilla.get_attribute(obj.Inner, 'method')), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_patches_decorator(self): destination = _tomodule.Class obj = _frommodule.Class settings = gorilla.Settings(allow_hit=True, store_hit=True) self.assertIs(gorilla.patches(destination, settings=settings)(obj), obj) settings.allow_hit = False settings.store_hit = False decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), gorilla.Patch(destination.Inner, 'method', gorilla.get_attribute(obj.Inner, 'method'), settings=gorilla.Settings(allow_hit=True, store_hit=True)), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_create_patches_4(self): def filter(name, value): return 'method' in name destination = _tomodule obj = _frommodule patches = gorilla.create_patches(destination, obj, filter=filter) expected_patches = [ gorilla.Patch(destination, 'function', gorilla.get_attribute(obj, 'function')), gorilla.Patch(destination, 'whatever', gorilla.get_attribute(obj, 'unbound_class_method')), gorilla.Patch(destination, 'unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method'), settings=gorilla.Settings(allow_hit=True)) ] self.assertEqual(patches, expected_patches) destination = _tomodule.Class obj = _frommodule.Class patches = gorilla.create_patches(destination, obj, filter=filter) expected_patches = [ gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'whatever', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Parent obj = _frommodule.Parent patches = gorilla.create_patches(destination, obj, filter=filter) expected_patches = [ gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Child obj = _frommodule.Child patches = gorilla.create_patches(destination, obj, filter=filter) expected_patches = [ gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), ] self.assertEqual(patches, expected_patches)
def test_patches_decorator_on_class(self): destination = _tomodule.Class obj = _frommodule.Class self.assertIs(gorilla.patches(destination)(obj), obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method')), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value')), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE')), gorilla.Patch(destination.Inner, 'method', gorilla.get_attribute(obj.Inner, 'method')), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_find_patches_2(self): global _utils self.tearDown() _utils = importlib.import_module(_utils.__name__) patches = gorilla.find_patches([_utils]) self.setUp() expected_patches = [ gorilla.Patch( _tomodule.Class, 'STATIC_VALUE', gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE')), gorilla.Patch( _tomodule.Class, 'class_method', gorilla.get_attribute(_frommodule.Class, 'class_method')), gorilla.Patch(_tomodule.Class, 'whatever', gorilla.get_attribute(_frommodule.Class, 'method')), gorilla.Patch(_tomodule.Parent, 'value', gorilla.get_attribute(_frommodule.Class, 'value')), gorilla.Patch( _tomodule.Class.Inner, 'STATIC_VALUE', gorilla.get_attribute(_frommodule.Class.Inner, 'STATIC_VALUE')), gorilla.Patch( _tomodule.Class.Inner, 'method', gorilla.get_attribute(_frommodule.Class.Inner, 'method')), gorilla.Patch(_tomodule, 'function', gorilla.get_attribute(_frommodule, 'function')), gorilla.Patch(_tomodule.Parent, 'method', gorilla.get_attribute(_frommodule.Parent, 'method')), gorilla.Patch(_tomodule.Parent, 'method', gorilla.get_attribute(_frommodule.Parent, 'method')), gorilla.Patch(_tomodule, 'function0', gorilla.get_attribute(_subpackage, 'function')), gorilla.Patch(_tomodule, 'function1', gorilla.get_attribute(_module1, 'function')), gorilla.Patch( _tomodule.Class, 'unbound_class_method', gorilla.get_attribute(_module1, 'unbound_class_method')), gorilla.Patch(_tomodule.Class, 'unbound_method', gorilla.get_attribute(_module1, 'unbound_method')), gorilla.Patch( _tomodule.Class, 'unbound_static_method', gorilla.get_attribute(_module1, 'unbound_static_method')), gorilla.Patch(_tomodule.Class, 'method1', gorilla.get_attribute(_module1.Class, 'method')), gorilla.Patch(_tomodule.Class, 'value1', gorilla.get_attribute(_module1.Class, 'value')), gorilla.Patch( _tomodule.Class, 'class_method2', gorilla.get_attribute(_module2.Class, 'class_method')), gorilla.Patch( _tomodule.Class, 'static_method2', gorilla.get_attribute(_module2.Class, 'static_method')), ] self.assertEqual(patches, expected_patches) patches = gorilla.find_patches([_utils], recursive=False) expected_patches = [ gorilla.Patch( _tomodule.Class, 'STATIC_VALUE', gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE')), gorilla.Patch( _tomodule.Class, 'class_method', gorilla.get_attribute(_frommodule.Class, 'class_method')), gorilla.Patch(_tomodule.Class, 'whatever', gorilla.get_attribute(_frommodule.Class, 'method')), gorilla.Patch(_tomodule.Parent, 'value', gorilla.get_attribute(_frommodule.Class, 'value')), gorilla.Patch( _tomodule.Class.Inner, 'STATIC_VALUE', gorilla.get_attribute(_frommodule.Class.Inner, 'STATIC_VALUE')), gorilla.Patch( _tomodule.Class.Inner, 'method', gorilla.get_attribute(_frommodule.Class.Inner, 'method')), gorilla.Patch(_tomodule, 'function', gorilla.get_attribute(_frommodule, 'function')), gorilla.Patch(_tomodule.Parent, 'method', gorilla.get_attribute(_frommodule.Parent, 'method')), gorilla.Patch(_tomodule.Parent, 'method', gorilla.get_attribute(_frommodule.Parent, 'method')), ] self.assertEqual(patches, expected_patches)
def test__get_members_2(self): members = gorilla._get_members(_frommodule, traverse_bases=False) expected_members = [ ('Child', gorilla.get_attribute(_frommodule, 'Child')), ('Class', gorilla.get_attribute(_frommodule, 'Class')), ('Parent', gorilla.get_attribute(_frommodule, 'Parent')), ('function', gorilla.get_attribute(_frommodule, 'function')), ('global_variable', gorilla.get_attribute(_frommodule, 'global_variable')), ('unbound_class_method', gorilla.get_attribute(_frommodule, 'unbound_class_method')), ('unbound_method', gorilla.get_attribute(_frommodule, 'unbound_method')), ('unbound_static_method', gorilla.get_attribute(_frommodule, 'unbound_static_method')), ('child_value', gorilla.get_attribute(_frommodule.Child, 'child_value')), ('Inner', _frommodule.Class.Inner), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE')), ('class_method', gorilla.get_attribute(_frommodule.Class, 'class_method')), ('method', gorilla.get_attribute(_frommodule.Class, 'method')), ('static_method', gorilla.get_attribute(_frommodule.Class, 'static_method')), ('value', gorilla.get_attribute(_frommodule.Class, 'value')), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Parent, 'STATIC_VALUE')), ('from_value', gorilla.get_attribute(_frommodule.Parent, 'from_value')), ('instance_value', gorilla.get_attribute(_frommodule.Parent, 'instance_value')), ('method', gorilla.get_attribute(_frommodule.Parent, 'method')), ('parent_value', gorilla.get_attribute(_frommodule.Parent, 'parent_value')), ('to_value', gorilla.get_attribute(_frommodule.Parent, 'to_value')), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Class.Inner, 'STATIC_VALUE')), ('method', gorilla.get_attribute(_frommodule.Class.Inner, 'method')), ] self.assertEqual(members, expected_members) members = gorilla._get_members(_frommodule.Class, traverse_bases=False) expected_members = [ ('Inner', _frommodule.Class.Inner), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE')), ('class_method', gorilla.get_attribute(_frommodule.Class, 'class_method')), ('method', gorilla.get_attribute(_frommodule.Class, 'method')), ('static_method', gorilla.get_attribute(_frommodule.Class, 'static_method')), ('value', gorilla.get_attribute(_frommodule.Class, 'value')), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Class.Inner, 'STATIC_VALUE')), ('method', gorilla.get_attribute(_frommodule.Class.Inner, 'method')), ] self.assertEqual(members, expected_members) members = gorilla._get_members(_frommodule.Parent, traverse_bases=False) expected_members = [ ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Parent, 'STATIC_VALUE')), ('from_value', gorilla.get_attribute(_frommodule.Parent, 'from_value')), ('instance_value', gorilla.get_attribute(_frommodule.Parent, 'instance_value')), ('method', gorilla.get_attribute(_frommodule.Parent, 'method')), ('parent_value', gorilla.get_attribute(_frommodule.Parent, 'parent_value')), ('to_value', gorilla.get_attribute(_frommodule.Parent, 'to_value')), ] self.assertEqual(members, expected_members) members = gorilla._get_members(_frommodule.Child, traverse_bases=False) expected_members = [ ('child_value', gorilla.get_attribute(_frommodule.Child, 'child_value')), ] self.assertEqual(members, expected_members)
def test_settings_decorator_2(self): destination = _tomodule.Class obj = _frommodule.Class gorilla.settings(some_value=123)(gorilla.get_attribute(obj, 'method')) gorilla.settings(allow_hit=False)(gorilla.get_attribute( obj, 'static_method')) gorilla.settings(store_hit=True)(gorilla.get_attribute(obj, 'value')) gorilla.settings(allow_hit=False, store_hit=True)(gorilla.get_attribute( obj.Inner, 'method')) gorilla.patches(destination, settings=gorilla.Settings(allow_hit=True, store_hit=False))(obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE'), settings=gorilla.Settings(allow_hit=True, store_hit=False)), gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method'), settings=gorilla.Settings(allow_hit=True, store_hit=False)), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method'), settings=gorilla.Settings(allow_hit=True, some_value=123, store_hit=False)), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method'), settings=gorilla.Settings(store_hit=False)), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value'), settings=gorilla.Settings(allow_hit=True)), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE'), settings=gorilla.Settings(allow_hit=True, store_hit=False)), gorilla.Patch(destination.Inner, 'method', gorilla.get_attribute(obj.Inner, 'method'), settings=gorilla.Settings(allow_hit=False, store_hit=True)), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_get_attribute(self): self.assertIs(gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE'), _frommodule.Class.__dict__['STATIC_VALUE']) self.assertIs(gorilla.get_attribute(_frommodule.Class, '__init__'), _frommodule.Class.__dict__['__init__']) self.assertIs(gorilla.get_attribute(_frommodule.Class, 'value'), _frommodule.Class.__dict__['value']) self.assertIs(gorilla.get_attribute(_frommodule.Class, 'method'), _frommodule.Class.__dict__['method']) self.assertIs(gorilla.get_attribute(_frommodule.Class, 'class_method'), _frommodule.Class.__dict__['class_method']) self.assertIs(gorilla.get_attribute(_frommodule.Class, 'static_method'), _frommodule.Class.__dict__['static_method']) self.assertIs(gorilla.get_attribute(_frommodule.Parent, 'STATIC_VALUE'), _frommodule.Parent.__dict__['STATIC_VALUE']) self.assertIs(gorilla.get_attribute(_frommodule.Parent, '__init__'), _frommodule.Parent.__dict__['__init__']) self.assertIs(gorilla.get_attribute(_frommodule.Parent, 'method'), _frommodule.Parent.__dict__['method']) self.assertIs(gorilla.get_attribute(_frommodule.Child, 'STATIC_VALUE'), _frommodule.Parent.__dict__['STATIC_VALUE']) self.assertIs(gorilla.get_attribute(_frommodule.Child, '__init__'), _frommodule.Child.__dict__['__init__']) self.assertIs(gorilla.get_attribute(_frommodule.Child, 'method'), _frommodule.Parent.__dict__['method'])
def test_settings_decorator_2(self): destination = _tomodule.Class obj = _frommodule.Class gorilla.settings(some_value=123)(gorilla.get_attribute(obj, 'method')) gorilla.settings(allow_hit=False)(gorilla.get_attribute(obj, 'static_method')) gorilla.settings(store_hit=True)(gorilla.get_attribute(obj, 'value')) gorilla.settings(allow_hit=False, store_hit=True)(gorilla.get_attribute(obj.Inner, 'method')) gorilla.patches(destination, settings=gorilla.Settings(allow_hit=True, store_hit=False))(obj) decorator_data = gorilla.get_decorator_data(obj) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE'), settings=gorilla.Settings(allow_hit=True, store_hit=False)), gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method'), settings=gorilla.Settings(allow_hit=True, store_hit=False)), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method'), settings=gorilla.Settings(allow_hit=True, some_value=123, store_hit=False)), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method'), settings=gorilla.Settings(store_hit=False)), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value'), settings=gorilla.Settings(allow_hit=True)), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE'), settings=gorilla.Settings(allow_hit=True, store_hit=False)), gorilla.Patch(destination.Inner, 'method', gorilla.get_attribute(obj.Inner, 'method'), settings=gorilla.Settings(allow_hit=False, store_hit=True)), ] self.assertEqual(decorator_data.patches, expected_patches)
def test_find_patches_2(self): global _utils self.tearDown() _utils = importlib.import_module(_utils.__name__) patches = gorilla.find_patches([_utils]) self.setUp() expected_patches = [ gorilla.Patch(_tomodule.Class, 'STATIC_VALUE', gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE')), gorilla.Patch(_tomodule.Class, 'class_method', gorilla.get_attribute(_frommodule.Class, 'class_method')), gorilla.Patch(_tomodule.Class, 'whatever', gorilla.get_attribute(_frommodule.Class, 'method')), gorilla.Patch(_tomodule.Parent, 'value', gorilla.get_attribute(_frommodule.Class, 'value')), gorilla.Patch(_tomodule.Class.Inner, 'STATIC_VALUE', gorilla.get_attribute(_frommodule.Class.Inner, 'STATIC_VALUE')), gorilla.Patch(_tomodule.Class.Inner, 'method', gorilla.get_attribute(_frommodule.Class.Inner, 'method')), gorilla.Patch(_tomodule, 'function', gorilla.get_attribute(_frommodule, 'function')), gorilla.Patch(_tomodule.Parent, 'method', gorilla.get_attribute(_frommodule.Parent, 'method')), gorilla.Patch(_tomodule.Parent, 'method', gorilla.get_attribute(_frommodule.Parent, 'method')), gorilla.Patch(_tomodule, 'function0', gorilla.get_attribute(_subpackage, 'function')), gorilla.Patch(_tomodule, 'function1', gorilla.get_attribute(_module1, 'function')), gorilla.Patch(_tomodule.Class, 'unbound_class_method', gorilla.get_attribute(_module1, 'unbound_class_method')), gorilla.Patch(_tomodule.Class, 'unbound_method', gorilla.get_attribute(_module1, 'unbound_method')), gorilla.Patch(_tomodule.Class, 'unbound_static_method', gorilla.get_attribute(_module1, 'unbound_static_method')), gorilla.Patch(_tomodule.Class, 'method1', gorilla.get_attribute(_module1.Class, 'method')), gorilla.Patch(_tomodule.Class, 'value1', gorilla.get_attribute(_module1.Class, 'value')), gorilla.Patch(_tomodule.Class, 'class_method2', gorilla.get_attribute(_module2.Class, 'class_method')), gorilla.Patch(_tomodule.Class, 'static_method2', gorilla.get_attribute(_module2.Class, 'static_method')), ] self.assertEqual(patches, expected_patches) patches = gorilla.find_patches([_utils], recursive=False) expected_patches = [ gorilla.Patch(_tomodule.Class, 'STATIC_VALUE', gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE')), gorilla.Patch(_tomodule.Class, 'class_method', gorilla.get_attribute(_frommodule.Class, 'class_method')), gorilla.Patch(_tomodule.Class, 'whatever', gorilla.get_attribute(_frommodule.Class, 'method')), gorilla.Patch(_tomodule.Parent, 'value', gorilla.get_attribute(_frommodule.Class, 'value')), gorilla.Patch(_tomodule.Class.Inner, 'STATIC_VALUE', gorilla.get_attribute(_frommodule.Class.Inner, 'STATIC_VALUE')), gorilla.Patch(_tomodule.Class.Inner, 'method', gorilla.get_attribute(_frommodule.Class.Inner, 'method')), gorilla.Patch(_tomodule, 'function', gorilla.get_attribute(_frommodule, 'function')), gorilla.Patch(_tomodule.Parent, 'method', gorilla.get_attribute(_frommodule.Parent, 'method')), gorilla.Patch(_tomodule.Parent, 'method', gorilla.get_attribute(_frommodule.Parent, 'method')), ] self.assertEqual(patches, expected_patches)
def test__get_members_3(self): obj = _frommodule members = gorilla._get_members(obj, recursive=False) expected_members = [ ('Child', gorilla.get_attribute(obj, 'Child')), ('Class', gorilla.get_attribute(obj, 'Class')), ('Parent', gorilla.get_attribute(obj, 'Parent')), ('function', gorilla.get_attribute(obj, 'function')), ('global_variable', gorilla.get_attribute(obj, 'global_variable')), ('unbound_class_method', gorilla.get_attribute(obj, 'unbound_class_method')), ('unbound_method', gorilla.get_attribute(obj, 'unbound_method')), ('unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method')), ] self.assertEqual(members, expected_members) obj = _frommodule.Class members = gorilla._get_members(obj, recursive=False) expected_members = [ ('Inner', gorilla.get_attribute(obj, 'Inner')), ('STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), ('class_method', gorilla.get_attribute(obj, 'class_method')), ('method', gorilla.get_attribute(obj, 'method')), ('static_method', gorilla.get_attribute(obj, 'static_method')), ('value', gorilla.get_attribute(obj, 'value')), ] self.assertEqual(members, expected_members) obj = _frommodule.Parent members = gorilla._get_members(obj, recursive=False) expected_members = [ ('STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), ('from_value', gorilla.get_attribute(obj, 'from_value')), ('instance_value', gorilla.get_attribute(obj, 'instance_value')), ('method', gorilla.get_attribute(obj, 'method')), ('parent_value', gorilla.get_attribute(obj, 'parent_value')), ('to_value', gorilla.get_attribute(obj, 'to_value')), ] self.assertEqual(members, expected_members) obj = _frommodule.Child members = gorilla._get_members(obj, recursive=False) expected_members = [ ('STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), ('child_value', gorilla.get_attribute(obj, 'child_value')), ('from_value', gorilla.get_attribute(obj, 'from_value')), ('instance_value', gorilla.get_attribute(obj, 'instance_value')), ('method', gorilla.get_attribute(obj, 'method')), ('parent_value', gorilla.get_attribute(obj, 'parent_value')), ('to_value', gorilla.get_attribute(obj, 'to_value')), ] self.assertEqual(members, expected_members)
def test__get_members_2(self): members = gorilla._get_members(_frommodule, traverse_bases=False) expected_members = [ ('Child', gorilla.get_attribute(_frommodule, 'Child')), ('Class', gorilla.get_attribute(_frommodule, 'Class')), ('Parent', gorilla.get_attribute(_frommodule, 'Parent')), ('function', gorilla.get_attribute(_frommodule, 'function')), ('global_variable', gorilla.get_attribute(_frommodule, 'global_variable')), ('unbound_class_method', gorilla.get_attribute(_frommodule, 'unbound_class_method')), ('unbound_method', gorilla.get_attribute(_frommodule, 'unbound_method')), ('unbound_static_method', gorilla.get_attribute(_frommodule, 'unbound_static_method')), ('child_value', gorilla.get_attribute(_frommodule.Child, 'child_value')), ('Inner', _frommodule.Class.Inner), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE')), ('class_method', gorilla.get_attribute(_frommodule.Class, 'class_method')), ('method', gorilla.get_attribute(_frommodule.Class, 'method')), ('static_method', gorilla.get_attribute(_frommodule.Class, 'static_method')), ('value', gorilla.get_attribute(_frommodule.Class, 'value')), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Parent, 'STATIC_VALUE')), ('from_value', gorilla.get_attribute(_frommodule.Parent, 'from_value')), ('instance_value', gorilla.get_attribute(_frommodule.Parent, 'instance_value')), ('method', gorilla.get_attribute(_frommodule.Parent, 'method')), ('parent_value', gorilla.get_attribute(_frommodule.Parent, 'parent_value')), ('to_value', gorilla.get_attribute(_frommodule.Parent, 'to_value')), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Class.Inner, 'STATIC_VALUE')), ('method', gorilla.get_attribute(_frommodule.Class.Inner, 'method')), ] self.assertEqual(members, expected_members) members = gorilla._get_members(_frommodule.Class, traverse_bases=False) expected_members = [ ('Inner', _frommodule.Class.Inner), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE')), ('class_method', gorilla.get_attribute(_frommodule.Class, 'class_method')), ('method', gorilla.get_attribute(_frommodule.Class, 'method')), ('static_method', gorilla.get_attribute(_frommodule.Class, 'static_method')), ('value', gorilla.get_attribute(_frommodule.Class, 'value')), ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Class.Inner, 'STATIC_VALUE')), ('method', gorilla.get_attribute(_frommodule.Class.Inner, 'method')), ] self.assertEqual(members, expected_members) members = gorilla._get_members(_frommodule.Parent, traverse_bases=False) expected_members = [ ('STATIC_VALUE', gorilla.get_attribute(_frommodule.Parent, 'STATIC_VALUE')), ('from_value', gorilla.get_attribute(_frommodule.Parent, 'from_value')), ('instance_value', gorilla.get_attribute(_frommodule.Parent, 'instance_value')), ('method', gorilla.get_attribute(_frommodule.Parent, 'method')), ('parent_value', gorilla.get_attribute(_frommodule.Parent, 'parent_value')), ('to_value', gorilla.get_attribute(_frommodule.Parent, 'to_value')), ] self.assertEqual(members, expected_members) members = gorilla._get_members(_frommodule.Child, traverse_bases=False) expected_members = [ ('child_value', gorilla.get_attribute(_frommodule.Child, 'child_value')), ] self.assertEqual(members, expected_members)
def test_create_patches_1(self): destination = _tomodule obj = _frommodule patches = gorilla.create_patches(destination, obj, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'function', gorilla.get_attribute(obj, 'function')), gorilla.Patch(destination, 'global_variable', gorilla.get_attribute(obj, 'global_variable')), gorilla.Patch(destination, 'unbound_class_method', gorilla.get_attribute(obj, 'unbound_class_method')), gorilla.Patch(destination, 'unbound_method', gorilla.get_attribute(obj, 'unbound_method')), gorilla.Patch(destination, 'unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method')), gorilla.Patch(destination.Child, 'STATIC_VALUE', gorilla.get_attribute(obj.Child, 'STATIC_VALUE')), gorilla.Patch(destination.Child, 'child_value', gorilla.get_attribute(obj.Child, 'child_value')), gorilla.Patch(destination.Child, 'from_value', gorilla.get_attribute(obj.Child, 'from_value')), gorilla.Patch(destination.Child, 'instance_value', gorilla.get_attribute(obj.Child, 'instance_value')), gorilla.Patch(destination.Child, 'method', gorilla.get_attribute(obj.Child, 'method')), gorilla.Patch(destination.Child, 'parent_value', gorilla.get_attribute(obj.Child, 'parent_value')), gorilla.Patch(destination.Child, 'to_value', gorilla.get_attribute(obj.Child, 'to_value')), gorilla.Patch(destination.Class, 'STATIC_VALUE', gorilla.get_attribute(obj.Class, 'STATIC_VALUE')), gorilla.Patch(destination.Class, 'class_method', gorilla.get_attribute(obj.Class, 'class_method')), gorilla.Patch(destination.Class, 'method', gorilla.get_attribute(obj.Class, 'method')), gorilla.Patch(destination.Class, 'static_method', gorilla.get_attribute(obj.Class, 'static_method')), gorilla.Patch(destination.Class, 'value', gorilla.get_attribute(obj.Class, 'value')), gorilla.Patch(destination.Parent, 'STATIC_VALUE', gorilla.get_attribute(obj.Parent, 'STATIC_VALUE')), gorilla.Patch(destination.Parent, 'from_value', gorilla.get_attribute(obj.Parent, 'from_value')), gorilla.Patch(destination.Parent, 'instance_value', gorilla.get_attribute(obj.Parent, 'instance_value')), gorilla.Patch(destination.Parent, 'method', gorilla.get_attribute(obj.Parent, 'method')), gorilla.Patch(destination.Parent, 'parent_value', gorilla.get_attribute(obj.Parent, 'parent_value')), gorilla.Patch(destination.Parent, 'to_value', gorilla.get_attribute(obj.Parent, 'to_value')), gorilla.Patch(destination.Class.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Class.Inner, 'STATIC_VALUE')), gorilla.Patch(destination.Class.Inner, 'method', gorilla.get_attribute(obj.Class.Inner, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Class obj = _frommodule.Class patches = gorilla.create_patches(destination, obj, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method')), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value')), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE')), gorilla.Patch(destination.Inner, 'method', gorilla.get_attribute(obj.Inner, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Parent obj = _frommodule.Parent patches = gorilla.create_patches(destination, obj, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, 'from_value', gorilla.get_attribute(obj, 'from_value')), gorilla.Patch(destination, 'instance_value', gorilla.get_attribute(obj, 'instance_value')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'parent_value', gorilla.get_attribute(obj, 'parent_value')), gorilla.Patch(destination, 'to_value', gorilla.get_attribute(obj, 'to_value')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Child obj = _frommodule.Child patches = gorilla.create_patches(destination, obj, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, 'child_value', gorilla.get_attribute(obj, 'child_value')), gorilla.Patch(destination, 'from_value', gorilla.get_attribute(obj, 'from_value')), gorilla.Patch(destination, 'instance_value', gorilla.get_attribute(obj, 'instance_value')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'parent_value', gorilla.get_attribute(obj, 'parent_value')), gorilla.Patch(destination, 'to_value', gorilla.get_attribute(obj, 'to_value')), ] self.assertEqual(patches, expected_patches)
def test_apply_patch_no_hit(self): name = 'dummy' settings = gorilla.Settings() source_paths = [''] + _list_attribute_paths(_frommodule) target_paths = _list_attribute_paths(_tomodule) branch_count = 0 # Retrieve the destinations in two passes instead of directly using a # set in order to preserve the ordering. seen = set() destination_paths = [ _split_attribute_path(path)[0] for path in target_paths ] destination_paths = [ path for path in destination_paths if path not in seen and seen.add(path) is None ] combinations = itertools.product(source_paths, destination_paths) for source_path, destination_path in combinations: self.setUp() destination = _get_attribute_from_path(_tomodule, destination_path) obj = _get_attribute_from_path(_frommodule, source_path) patch = gorilla.Patch(destination, name, obj, settings=settings) gorilla.apply(patch) self.assertIs( destination, _get_attribute_from_path(_tomodule, destination_path)) result = gorilla.get_attribute(destination, name) self.assertIs(result, obj) if source_path == '': branch_count += 1 self.assertEqual(result.global_variable, "frommodule.global_variable") self.assertEqual( result.function(), "frommodule.function (frommodule.Class.STATIC_VALUE)") self.assertEqual(result.Class.STATIC_VALUE, "frommodule.Class.STATIC_VALUE") self.assertEqual(result.Class.Inner.STATIC_VALUE, "frommodule.Class.Inner.STATIC_VALUE") self.assertEqual(result.Parent.STATIC_VALUE, "frommodule.Parent.STATIC_VALUE") self.assertEqual(result.Child.STATIC_VALUE, "frommodule.Parent.STATIC_VALUE") elif source_path == 'global_variable': branch_count += 1 self.assertEqual(result, "frommodule.global_variable") elif source_path == 'function': branch_count += 1 self.assertEqual( result(), "frommodule.function (frommodule.Class.STATIC_VALUE)") elif source_path == 'unbound_method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination(), name)(), "frommodule.unbound_method (tomodule.%s.STATIC_VALUE, tomodule.%s.instance_value)" % ((_CLS_REFERENCES[destination_path], ) * 2)) elif source_path == 'unbound_class_method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination, name)(), "frommodule.unbound_class_method (tomodule.%s.STATIC_VALUE)" % (_CLS_REFERENCES[destination_path], )) elif source_path == 'unbound_static_method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination, name)(), "frommodule.unbound_static_method (frommodule.Class.STATIC_VALUE)" ) elif source_path == 'Class': branch_count += 1 self.assertEqual(result.STATIC_VALUE, "frommodule.Class.STATIC_VALUE") elif source_path == 'Class.Inner': branch_count += 1 self.assertEqual(result.STATIC_VALUE, "frommodule.Class.Inner.STATIC_VALUE") elif source_path == 'Class.value': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 instance = destination() self.assertEqual( getattr(instance, name), "frommodule.Class.value.getter (tomodule.%s.instance_value)" % (_CLS_REFERENCES[destination_path], )) setattr(instance, name, 'hello') self.assertEqual(getattr(instance, name), "frommodule.Class.value.getter (hello)") elif source_path == 'Class.method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination(), name)(), "frommodule.Class.method (tomodule.%s.STATIC_VALUE, tomodule.%s.instance_value)" % ((_CLS_REFERENCES[destination_path], ) * 2)) elif source_path == 'Class.class_method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination, name)(), "frommodule.Class.class_method (tomodule.%s.STATIC_VALUE)" % (_CLS_REFERENCES[destination_path], )) elif source_path == 'Class.static_method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination, name)(), "frommodule.Class.static_method (frommodule.Class.STATIC_VALUE)" ) elif source_path == 'Parent': branch_count += 1 self.assertEqual(result.__slots__, ('instance_value', 'parent_value', 'to_value', 'from_value')) self.assertEqual(result().parent_value, "frommodule.Parent.parent_value") self.assertEqual( result().method(), "frommodule.Parent.method (frommodule.Parent.instance_value)" ) elif source_path == 'Parent.method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination(), name)(), "frommodule.Parent.method (tomodule.%s.instance_value)" % (_CLS_REFERENCES[destination_path], )) elif source_path == 'Child': branch_count += 1 self.assertEqual(result.__slots__, ('child_value', )) self.assertEqual(result().parent_value, "frommodule.Parent.parent_value") self.assertEqual(result().child_value, "frommodule.Child.child_value") self.assertEqual( result().method(), "frommodule.Parent.method (frommodule.Parent.instance_value)" ) elif source_path == 'Child.method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination(), name)(), "frommodule.Parent.method (tomodule.%s.instance_value)" % (_CLS_REFERENCES[destination_path], )) self.tearDown() # Make sure that all test branches are covered. self.assertEqual(branch_count, 116)
def test_apply_patch_with_hit_2(self): settings = gorilla.Settings(allow_hit=True) branch_count = 0 source_paths = [''] + _list_attribute_paths(_frommodule) target_paths = _list_attribute_paths(_tomodule) combinations = itertools.product(source_paths, target_paths) for source_path, target_path in combinations: self.setUp() destination_path, name = _split_attribute_path(target_path) destination = _get_attribute_from_path(_tomodule, destination_path) target = gorilla.get_attribute(destination, name) obj = _get_attribute_from_path(_frommodule, source_path) patch = gorilla.Patch(destination, name, obj, settings=settings) gorilla.apply(patch) self.assertIs( destination, _get_attribute_from_path(_tomodule, destination_path)) result = gorilla.get_attribute(destination, name) self.assertIs(result, obj) # `gorilla.get_original_attribute` cannot be used here because it # could return a bounded method, which would not compare as # expected. original = gorilla.get_attribute(destination, '_gorilla_original_%s' % (name, )) self.assertIs(original, target) self.assertIsNot(original, result) if source_path == '': branch_count += 1 self.assertEqual(result.global_variable, "frommodule.global_variable") self.assertEqual( result.function(), "frommodule.function (frommodule.Class.STATIC_VALUE)") self.assertEqual(result.Class.STATIC_VALUE, "frommodule.Class.STATIC_VALUE") self.assertEqual(result.Class.Inner.STATIC_VALUE, "frommodule.Class.Inner.STATIC_VALUE") self.assertEqual(result.Parent.STATIC_VALUE, "frommodule.Parent.STATIC_VALUE") self.assertEqual(result.Child.STATIC_VALUE, "frommodule.Parent.STATIC_VALUE") elif source_path == 'global_variable': branch_count += 1 self.assertEqual(result, "frommodule.global_variable") if destination_path in _CLS_REFERENCES and name in ( 'STATIC_VALUE', ): branch_count += 1 self.assertEqual(destination.STATIC_VALUE, "frommodule.global_variable") if target_path == 'Class.STATIC_VALUE': branch_count += 1 self.assertEqual( destination.class_method(), "tomodule.Class.class_method (frommodule.global_variable)" ) self.assertEqual( destination.static_method(), "tomodule.Class.static_method (frommodule.global_variable)" ) elif source_path == 'function': branch_count += 1 self.assertEqual( result(), "frommodule.function (frommodule.Class.STATIC_VALUE)") elif source_path == 'unbound_method': branch_count += 1 if destination_path in _CLS_REFERENCES and name not in ( 'STATIC_VALUE', '__init__'): branch_count += 1 self.assertEqual( getattr(destination(), name)(), "frommodule.unbound_method (tomodule.%s.STATIC_VALUE, tomodule.%s.instance_value)" % ((_CLS_REFERENCES[destination_path], ) * 2)) elif source_path == 'unbound_class_method': branch_count += 1 if destination_path in _CLS_REFERENCES and name not in ( 'STATIC_VALUE', ): branch_count += 1 self.assertEqual( getattr(destination, name)(), "frommodule.unbound_class_method (tomodule.%s.STATIC_VALUE)" % (_CLS_REFERENCES[destination_path], )) elif source_path == 'unbound_static_method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination, name)(), "frommodule.unbound_static_method (frommodule.Class.STATIC_VALUE)" ) elif source_path == 'Class': branch_count += 1 self.assertEqual(result.STATIC_VALUE, "frommodule.Class.STATIC_VALUE") elif source_path == 'Class.Inner': branch_count += 1 self.assertEqual(result.STATIC_VALUE, "frommodule.Class.Inner.STATIC_VALUE") elif source_path == 'Class.value': branch_count += 1 if destination_path in _CLS_REFERENCES and name not in ( '__init__', ): branch_count += 1 instance = destination() self.assertEqual( getattr(instance, name), "frommodule.Class.value.getter (tomodule.%s.instance_value)" % (_CLS_REFERENCES[destination_path], )) setattr(instance, name, 'hello') self.assertEqual(getattr(instance, name), "frommodule.Class.value.getter (hello)") elif source_path == 'Class.method': branch_count += 1 if destination_path in _CLS_REFERENCES and name not in ( 'STATIC_VALUE', '__init__'): branch_count += 1 self.assertEqual( getattr(destination(), name)(), "frommodule.Class.method (tomodule.%s.STATIC_VALUE, tomodule.%s.instance_value)" % ((_CLS_REFERENCES[destination_path], ) * 2)) elif source_path == 'Class.class_method': branch_count += 1 if destination_path in _CLS_REFERENCES and name not in ( 'STATIC_VALUE', ): branch_count += 1 self.assertEqual( getattr(destination, name)(), "frommodule.Class.class_method (tomodule.%s.STATIC_VALUE)" % (_CLS_REFERENCES[destination_path], )) elif source_path == 'Class.static_method': branch_count += 1 if destination_path in _CLS_REFERENCES: branch_count += 1 self.assertEqual( getattr(destination, name)(), "frommodule.Class.static_method (frommodule.Class.STATIC_VALUE)" ) elif source_path == 'Parent': branch_count += 1 self.assertEqual(result.__slots__, ('instance_value', 'parent_value', 'to_value', 'from_value')) self.assertEqual(result().parent_value, "frommodule.Parent.parent_value") self.assertEqual( result().method(), "frommodule.Parent.method (frommodule.Parent.instance_value)" ) elif source_path == 'Parent.method': branch_count += 1 if destination_path in _CLS_REFERENCES and name not in ( '__init__', ): branch_count += 1 self.assertEqual( getattr(destination(), name)(), "frommodule.Parent.method (tomodule.%s.instance_value)" % (_CLS_REFERENCES[destination_path], )) elif source_path == 'Child': branch_count += 1 self.assertEqual(result.__slots__, ('child_value', )) self.assertEqual(result().parent_value, "frommodule.Parent.parent_value") self.assertEqual(result().child_value, "frommodule.Child.child_value") self.assertEqual( result().method(), "frommodule.Parent.method (frommodule.Parent.instance_value)" ) elif source_path == 'Child.method': branch_count += 1 if destination_path in _CLS_REFERENCES and name not in ( '__init__', ): branch_count += 1 self.assertEqual( getattr(destination(), name)(), "frommodule.Parent.method (tomodule.%s.instance_value)" % (_CLS_REFERENCES[destination_path], )) self.tearDown() # Make sure that all test branches are covered. self.assertEqual(branch_count, 427)
def test_create_patches_1(self): destination = _tomodule obj = _frommodule patches = gorilla.create_patches(destination, obj, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'function', gorilla.get_attribute(obj, 'function')), gorilla.Patch(destination, 'global_variable', gorilla.get_attribute(obj, 'global_variable')), gorilla.Patch(destination, 'unbound_class_method', gorilla.get_attribute(obj, 'unbound_class_method')), gorilla.Patch(destination, 'unbound_method', gorilla.get_attribute(obj, 'unbound_method')), gorilla.Patch(destination, 'unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method')), gorilla.Patch(destination.Child, 'STATIC_VALUE', gorilla.get_attribute(obj.Child, 'STATIC_VALUE')), gorilla.Patch(destination.Child, 'child_value', gorilla.get_attribute(obj.Child, 'child_value')), gorilla.Patch(destination.Child, 'from_value', gorilla.get_attribute(obj.Child, 'from_value')), gorilla.Patch(destination.Child, 'instance_value', gorilla.get_attribute(obj.Child, 'instance_value')), gorilla.Patch(destination.Child, 'method', gorilla.get_attribute(obj.Child, 'method')), gorilla.Patch(destination.Child, 'parent_value', gorilla.get_attribute(obj.Child, 'parent_value')), gorilla.Patch(destination.Child, 'to_value', gorilla.get_attribute(obj.Child, 'to_value')), gorilla.Patch(destination.Class, 'STATIC_VALUE', gorilla.get_attribute(obj.Class, 'STATIC_VALUE')), gorilla.Patch(destination.Class, 'class_method', gorilla.get_attribute(obj.Class, 'class_method')), gorilla.Patch(destination.Class, 'method', gorilla.get_attribute(obj.Class, 'method')), gorilla.Patch(destination.Class, 'static_method', gorilla.get_attribute(obj.Class, 'static_method')), gorilla.Patch(destination.Class, 'value', gorilla.get_attribute(obj.Class, 'value')), gorilla.Patch(destination.Parent, 'STATIC_VALUE', gorilla.get_attribute(obj.Parent, 'STATIC_VALUE')), gorilla.Patch(destination.Parent, 'from_value', gorilla.get_attribute(obj.Parent, 'from_value')), gorilla.Patch(destination.Parent, 'instance_value', gorilla.get_attribute(obj.Parent, 'instance_value')), gorilla.Patch(destination.Parent, 'method', gorilla.get_attribute(obj.Parent, 'method')), gorilla.Patch(destination.Parent, 'parent_value', gorilla.get_attribute(obj.Parent, 'parent_value')), gorilla.Patch(destination.Parent, 'to_value', gorilla.get_attribute(obj.Parent, 'to_value')), gorilla.Patch( destination.Class.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Class.Inner, 'STATIC_VALUE')), gorilla.Patch(destination.Class.Inner, 'method', gorilla.get_attribute(obj.Class.Inner, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Class obj = _frommodule.Class patches = gorilla.create_patches(destination, obj, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, 'class_method', gorilla.get_attribute(obj, 'class_method')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'static_method', gorilla.get_attribute(obj, 'static_method')), gorilla.Patch(destination, 'value', gorilla.get_attribute(obj, 'value')), gorilla.Patch(destination.Inner, 'STATIC_VALUE', gorilla.get_attribute(obj.Inner, 'STATIC_VALUE')), gorilla.Patch(destination.Inner, 'method', gorilla.get_attribute(obj.Inner, 'method')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Parent obj = _frommodule.Parent patches = gorilla.create_patches(destination, obj, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, 'from_value', gorilla.get_attribute(obj, 'from_value')), gorilla.Patch(destination, 'instance_value', gorilla.get_attribute(obj, 'instance_value')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'parent_value', gorilla.get_attribute(obj, 'parent_value')), gorilla.Patch(destination, 'to_value', gorilla.get_attribute(obj, 'to_value')), ] self.assertEqual(patches, expected_patches) destination = _tomodule.Child obj = _frommodule.Child patches = gorilla.create_patches(destination, obj, use_decorators=False) expected_patches = [ gorilla.Patch(destination, 'STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), gorilla.Patch(destination, 'child_value', gorilla.get_attribute(obj, 'child_value')), gorilla.Patch(destination, 'from_value', gorilla.get_attribute(obj, 'from_value')), gorilla.Patch(destination, 'instance_value', gorilla.get_attribute(obj, 'instance_value')), gorilla.Patch(destination, 'method', gorilla.get_attribute(obj, 'method')), gorilla.Patch(destination, 'parent_value', gorilla.get_attribute(obj, 'parent_value')), gorilla.Patch(destination, 'to_value', gorilla.get_attribute(obj, 'to_value')), ] self.assertEqual(patches, expected_patches)
def test_get_attribute(self): self.assertIs(gorilla.get_attribute(_frommodule.Class, 'STATIC_VALUE'), _frommodule.Class.__dict__['STATIC_VALUE']) self.assertIs(gorilla.get_attribute(_frommodule.Class, '__init__'), _frommodule.Class.__dict__['__init__']) self.assertIs(gorilla.get_attribute(_frommodule.Class, 'value'), _frommodule.Class.__dict__['value']) self.assertIs(gorilla.get_attribute(_frommodule.Class, 'method'), _frommodule.Class.__dict__['method']) self.assertIs(gorilla.get_attribute(_frommodule.Class, 'class_method'), _frommodule.Class.__dict__['class_method']) self.assertIs( gorilla.get_attribute(_frommodule.Class, 'static_method'), _frommodule.Class.__dict__['static_method']) self.assertIs( gorilla.get_attribute(_frommodule.Parent, 'STATIC_VALUE'), _frommodule.Parent.__dict__['STATIC_VALUE']) self.assertIs(gorilla.get_attribute(_frommodule.Parent, '__init__'), _frommodule.Parent.__dict__['__init__']) self.assertIs(gorilla.get_attribute(_frommodule.Parent, 'method'), _frommodule.Parent.__dict__['method']) self.assertIs(gorilla.get_attribute(_frommodule.Child, 'STATIC_VALUE'), _frommodule.Parent.__dict__['STATIC_VALUE']) self.assertIs(gorilla.get_attribute(_frommodule.Child, '__init__'), _frommodule.Child.__dict__['__init__']) self.assertIs(gorilla.get_attribute(_frommodule.Child, 'method'), _frommodule.Parent.__dict__['method'])
def test__get_members_3(self): obj = _frommodule members = gorilla._get_members(obj, recursive=False) expected_members = [ ('Child', gorilla.get_attribute(obj, 'Child')), ('Class', gorilla.get_attribute(obj, 'Class')), ('Parent', gorilla.get_attribute(obj, 'Parent')), ('function', gorilla.get_attribute(obj, 'function')), ('global_variable', gorilla.get_attribute(obj, 'global_variable')), ('unbound_class_method', gorilla.get_attribute(obj, 'unbound_class_method')), ('unbound_method', gorilla.get_attribute(obj, 'unbound_method')), ('unbound_static_method', gorilla.get_attribute(obj, 'unbound_static_method')), ] self.assertEqual(members, expected_members) obj = _frommodule.Class members = gorilla._get_members(obj, recursive=False) expected_members = [ ('Inner', gorilla.get_attribute(obj, 'Inner')), ('STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), ('class_method', gorilla.get_attribute(obj, 'class_method')), ('method', gorilla.get_attribute(obj, 'method')), ('static_method', gorilla.get_attribute(obj, 'static_method')), ('value', gorilla.get_attribute(obj, 'value')), ] self.assertEqual(members, expected_members) obj = _frommodule.Parent members = gorilla._get_members(obj, recursive=False) expected_members = [ ('STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), ('from_value', gorilla.get_attribute(obj, 'from_value')), ('instance_value', gorilla.get_attribute(obj, 'instance_value')), ('method', gorilla.get_attribute(obj, 'method')), ('parent_value', gorilla.get_attribute(obj, 'parent_value')), ('to_value', gorilla.get_attribute(obj, 'to_value')), ] self.assertEqual(members, expected_members) obj = _frommodule.Child members = gorilla._get_members(obj, recursive=False) expected_members = [ ('STATIC_VALUE', gorilla.get_attribute(obj, 'STATIC_VALUE')), ('child_value', gorilla.get_attribute(obj, 'child_value')), ('from_value', gorilla.get_attribute(obj, 'from_value')), ('instance_value', gorilla.get_attribute(obj, 'instance_value')), ('method', gorilla.get_attribute(obj, 'method')), ('parent_value', gorilla.get_attribute(obj, 'parent_value')), ('to_value', gorilla.get_attribute(obj, 'to_value')), ] self.assertEqual(members, expected_members)