| 1 |  |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | @pytest.fixture | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | def test_infra(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     from so_magic.data.backend.backend import MyDecorator, CommandRegistrator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     return type('TestInfra', (object,), dict(MyDecorator=MyDecorator, CommandRegistrator=CommandRegistrator)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | def test_type_class_properties(test_infra): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     assert not hasattr(test_infra.CommandRegistrator, 'registry') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     assert not hasattr(test_infra.CommandRegistrator, 'state') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     assert not hasattr(test_infra.MyDecorator, '_commands_hash') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | @pytest.fixture | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | def test_objects(test_infra): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     class A(test_infra.MyDecorator): pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     class Ab(test_infra.MyDecorator): pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     class B(metaclass=test_infra.MyDecorator): pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     DynamicClass = test_infra.MyDecorator('DynamicClass', (object,), {}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     class ParentClass: pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     class DefinedClass(ParentClass, metaclass=test_infra.MyDecorator): pass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     inst1 = DynamicClass() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     inst2 = DefinedClass() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     inst3 = B() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     return type('TestObjects', (object,), { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         'type_classes': type('TypeClasses', (object,), {'A': A, 'Ab': Ab, '__iter__': lambda self: iter([A, Ab])})(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         'normal_classes': type('NormalClasses', (object,), {'B': B, 'DynamicClass': DynamicClass, 'DefinedClass': DefinedClass, '__iter__': lambda self: iter([B, DynamicClass, DefinedClass])})(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         'instances': type('Instances', (object,), {'DynamicClass': inst1, 'DefinedClass': inst2, 'B': inst3, '__iter__': lambda self: iter([inst1, inst2, inst3])}), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         'MyDecorator': test_infra.MyDecorator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'have_magic_decorator': [type(type(test_objects.instances.DynamicClass)), test_objects.MyDecorator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                                  type(test_objects.normal_classes.DynamicClass)] + | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                                 list(iter(test_objects.type_classes)) + | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                                 list(iter(test_objects.normal_classes)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | def test_decorating(test_objects): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     assert type(test_objects.MyDecorator) == type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     assert type(test_objects.type_classes.A) == type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     assert type(test_objects.normal_classes.B) == test_objects.MyDecorator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     assert type(test_objects.normal_classes.DynamicClass) == test_objects.MyDecorator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     assert type(test_objects.instances.DynamicClass) == test_objects.normal_classes.DynamicClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     assert type(test_objects.instances.DefinedClass) == test_objects.normal_classes.DefinedClass | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     assert type(test_objects.instances.B) == test_objects.normal_classes.B | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     assert not hasattr(test_objects.instances.B, 'magic_decorator') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     assert not hasattr(test_objects.instances.DefinedClass, 'magic_decorator') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     assert all([hasattr(x, 'magic_decorator') for x in test_objects.have_magic_decorator]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     assert id(test_objects.type_classes.A.magic_decorator) != id(test_objects.type_classes.Ab.magic_decorator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     assert len(set([id(o.magic_decorator) for o in test_objects.have_magic_decorator])) == 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | @pytest.fixture | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | def classes(test_infra): | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     class ADecoratorClass(metaclass=test_infra.CommandRegistrator): pass | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     class BDecoratorClass(metaclass=test_infra.CommandRegistrator): pass | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     @ADecoratorClass.func_decorator() | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     def aa(x): | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         return str(x) + 'aa' | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     @BDecoratorClass.func_decorator() | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     def bb(x): | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         return str(x) + 'bb' | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     value = 1 | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     return [{'c': c, 'test_data': (value, f'{value}{decorated_func_name}')} | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             for c, decorated_func_name in zip([ADecoratorClass, BDecoratorClass], ['aa', 'bb'])] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | def test_decorator(classes): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     func_names = ('aa', 'bb') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     assert all(func_name in test_data['c'].registry for func_name, test_data in zip(func_names, classes)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     assert all(func_name not in test_data['c'].registry for func_name, test_data in zip(func_names, reversed(classes))) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     assert all(test_data['c'].registry[func_name](test_data['test_data'][0]) == test_data['test_data'][1] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |                for func_name, test_data in zip(func_names, classes)) | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |