| 1 |  |  | # pylint: disable=unused-variable,redefined-outer-name,expression-not-assigned,singleton-comparison | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | import logging | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | from unittest.mock import Mock | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | import pytest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | from expecter import expect | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | import yorm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | from yorm import exceptions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | from yorm import utilities | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | log = logging.getLogger(__name__) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | @pytest.fixture | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | def model_class(tmpdir): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     tmpdir.chdir() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     @yorm.sync("data/{self.kind}/{self.key}.yml", auto_create=False) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     class Model: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         def __init__(self, kind, key): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             self.kind = kind | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             self.key = key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         def __eq__(self, other): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             return (self.kind, self.key) == (other.kind, other.key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     return Model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | @pytest.fixture | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | def instance(model_class): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     return model_class('foo', 'bar') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | def describe_create(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     def it_creates_files(model_class): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         instance = utilities.create(model_class, 'foo', 'bar') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         expect(instance.__mapper__.exists) == True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     def it_requires_files_to_not_yet_exist(model_class, instance): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         instance.__mapper__.create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         with expect.raises(exceptions.DuplicateMappingError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             utilities.create(model_class, 'foo', 'bar') | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     def it_requires_a_mapped_object(): | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         with expect.raises(TypeError): | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             utilities.create(Mock) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | def describe_find(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     def it_returns_object_when_found(model_class, instance): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         instance.__mapper__.create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         expect(utilities.find(model_class, 'foo', 'bar')) == instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     def it_returns_none_when_no_match(model_class): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         expect(utilities.find(model_class, 'not', 'here')) == None | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     def it_allows_objects_to_be_created(model_class): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         expect(utilities.find(model_class, 'new', 'one', create=True)) == \ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             model_class('new', 'one') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     def it_requires_a_mapped_object(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         with expect.raises(TypeError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             utilities.find(Mock) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | def describe_find_all(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     def it_is_not_yet_implemented(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         with expect.raises(NotImplementedError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             utilities.find_all(Mock) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | def describe_load(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     def it_marks_files_as_unmodified(instance): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         instance.__mapper__.create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         instance.__mapper__.modified = True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         utilities.load(instance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         expect(instance.__mapper__.modified) == False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     def it_requires_a_mapped_object(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         with expect.raises(TypeError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             utilities.load(Mock) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | def describe_save(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     def it_creates_files(instance): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         utilities.save(instance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         expect(instance.__mapper__.exists) == True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     def it_marks_files_as_modified(instance): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         instance.__mapper__.create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         instance.__mapper__.modified = False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         utilities.save(instance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         expect(instance.__mapper__.modified) == True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     def it_expects_the_file_to_not_be_deleted(instance): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         instance.__mapper__.delete() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         with expect.raises(exceptions.DeletedFileError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             utilities.save(instance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     def it_requires_a_mapped_object(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         with expect.raises(TypeError): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             utilities.save(Mock) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | def describe_delete(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     def it_deletes_files(instance): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         utilities.delete(instance) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         expect(instance.__mapper__.exists) == False | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         expect(instance.__mapper__.deleted) == True | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     def it_requires_a_mapped_object(): | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         with expect.raises(TypeError): | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 133 |  |  |             utilities.delete(Mock) | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |  |