def test_pre_rename_file_signal(self): resource = DummyFileResource() signal_fired = False def signal_handler(sender, instance, old_name, new_name, options, **kwargs): nonlocal signal_fired signal_fired = True assert sender is DummyFileResource assert instance is resource assert instance.basename == 'milky-way-nasa' assert instance.extension == 'jpg' assert old_name == 'milky-way-nasa.jpg' assert new_name == 'new name.png' # extra parameters passed to `rename_file` assert options == { 'key1': 'value1', 'key2': 'value2' } signals.pre_rename_file.connect(signal_handler) with open(NASA_FILEPATH, 'rb') as fp: resource.attach_file(fp) assert signal_fired is False resource.rename_file('new name.png', key1='value1', key2='value2') assert signal_fired is True resource.delete_file() signals.pre_rename_file.disconnect(signal_handler)
def test_update_checksum_signal(self): resource = DummyFileResource() signal_fired = False resource.checksum = '' def signal_handler(sender, instance, checksum, **kwargs): nonlocal signal_fired signal_fired = True assert sender is DummyFileResource assert instance is resource assert checksum == '5d8ec227d0d8794d4d99dfbbdb9ad3b479c16952ad4ef69252644d9c404543a5' signals.checksum_update.connect(signal_handler) assert signal_fired is False assert resource.update_checksum() is True assert signal_fired is True signals.checksum_update.disconnect(signal_handler)
def test_post_delete_file_signal(self): resource = DummyFileResource() signal_fired = False def signal_handler(sender, instance, options, response, **kwargs): nonlocal signal_fired signal_fired = True assert sender is DummyFileResource assert instance is resource # extra parameters passed to `rename_file` assert options == { 'key1': 'value1', 'key2': 'value2' } # result of `_delete_file` method assert response == { 'success': True, } signals.post_delete_file.connect(signal_handler) with open(NASA_FILEPATH, 'rb') as fp: resource.attach_file(fp) assert signal_fired is False resource.delete_file(key1='value1', key2='value2') assert signal_fired is True signals.post_delete_file.disconnect(signal_handler)
def test_rename_to_same_name(self): resource = DummyFileResource() signal_fired = False def signal_handler(sender, **kwargs): nonlocal signal_fired signal_fired = True signals.pre_rename_file.connect(signal_handler) signals.post_rename_file.connect(signal_handler) with open(NASA_FILEPATH, 'rb') as fp: resource.attach_file(fp) assert signal_fired is False resource.rename_file(os.path.basename(NASA_FILEPATH)) assert signal_fired is False resource.delete_file() signals.pre_rename_file.disconnect(signal_handler) signals.post_rename_file.disconnect(signal_handler)
def test_post_attach_file_signal(self): resource = DummyFileResource() signal_fired = False def signal_handler(sender, instance, file, options, response, **kwargs): nonlocal signal_fired signal_fired = True assert sender is DummyFileResource assert instance is resource # ensure instance filled assert instance.basename == 'milky-way-nasa' assert instance.extension == 'jpg' assert instance.size == 28 assert instance.checksum == '485291fa0ee50c016982abbfa943957bcd231aae0492ccbaa22c58e3997b35e0' # ensure file type assert isinstance(file, File) # ensure modified file assert file.size == 28 # extra parameters passed to `attach_file` assert options == { 'key1': 'value1', 'key2': 'value2' } # result of `_attach_file` method assert response == { 'success': True, } signals.post_attach_file.connect(signal_handler) with open(NASA_FILEPATH, 'rb') as fp: assert signal_fired is False resource.attach_file(fp, key1='value1', key2='value2') assert signal_fired is True resource.delete_file() signals.post_attach_file.disconnect(signal_handler)
def test_pre_delete_file_signal(self): resource = DummyFileResource() signal_fired = False def signal_handler(sender, instance, **kwargs): nonlocal signal_fired signal_fired = True assert sender is DummyFileResource assert instance is resource signals.pre_delete_file.connect(signal_handler) with open(NASA_FILEPATH, 'rb') as fp: resource.attach_file(fp) assert signal_fired is False resource.delete_file() assert signal_fired is True signals.pre_delete_file.disconnect(signal_handler)
def test_pre_attach_file_signal(self): resource = DummyFileResource() signal_fired = False def signal_handler(sender, instance, file, options, **kwargs): nonlocal signal_fired signal_fired = True assert sender is DummyFileResource assert instance is resource # ensure instance not filled yet assert instance.basename == '' assert instance.extension == '' assert instance.size == 0 assert instance.checksum == '' # ensure file type assert isinstance(file, File) # ensure original file assert file.size == 9711423 # extra parameters passed to `attach_file` assert options == { 'key1': 'value1', 'key2': 'value2' } signals.pre_attach_file.connect(signal_handler) with open(NASA_FILEPATH, 'rb') as fp: assert signal_fired is False resource.attach_file(fp, key1='value1', key2='value2') assert signal_fired is True resource.delete_file() signals.pre_attach_file.disconnect(signal_handler)
def test_prepare_file(self, storage): obj = DummyFileResource() with open(NASA_FILEPATH, 'rb') as fp: file = File(fp) assert obj._prepare_file(file) is file