def test_put_file(self, _mkdir, shutil): path_in = '/tmp/test' unit = Mock(id='123', storage_path='/tmp/storage') storage = FileStorage() # test storage.put(unit, path_in) # validation _mkdir.assert_called_once_with(os.path.dirname(unit.storage_path)) shutil.copy.assert_called_once_with(path_in, unit.storage_path)
def test_put_file_with_location(self, _mkdir, shutil): path_in = '/tmp/test' location = '/a/b/' unit = Mock(id='123', storage_path='/tmp/storage') storage = FileStorage() # test storage.put(unit, path_in, location) # validation destination = os.path.join(unit.storage_path, location.lstrip('/')) _mkdir.assert_called_once_with(os.path.dirname(destination)) shutil.copy.assert_called_once_with(path_in, destination)
def test_put_file(self, config, shutil): path_in = '/tmp/test' storage_dir = '/tmp/storage' unit = Mock(id='0123456789', unit_type_id='ABC') config.get = lambda s, p: {'server': {'storage_dir': storage_dir}}[s][p] storage = FileStorage() # test storage.put(unit, path_in) # validation destination = os.path.join( os.path.join(storage_dir, 'content', 'units', unit.unit_type_id), unit.id[0:4], unit.id) shutil.copy.assert_called_once_with(path_in, destination) self.assertEqual(unit.storage_path, destination)
def test_put_file_with_location(self, _mkdir, shutil, tempfile, close, rename): path_in = '/tmp/test' location = '/a/b/' temp_destination = '/some/file/path' unit = Mock(id='123', storage_path='/tmp/storage') storage = FileStorage() tempfile.mkstemp.return_value = ('fd', temp_destination) # test storage.put(unit, path_in, location) # validation destination = os.path.join(unit.storage_path, location.lstrip('/')) _mkdir.assert_called_once_with(os.path.dirname(destination)) shutil.copy.assert_called_once_with(path_in, temp_destination) rename.assert_called_once_with(temp_destination, destination)
def test_put_file_correct_size(self, _mkdir, shutil, tempfile, close, rename): path_in = '/tmp/test' temp_destination = '/some/file/path' unit = Mock(id='123', storage_path='/tmp/storage') storage = FileStorage() tempfile.mkstemp.return_value = ('fd', temp_destination) # test storage.put(unit, path_in) # validation _mkdir.assert_called_once_with(os.path.dirname(unit.storage_path)) tempfile.mkstemp.assert_called_once_with(dir=os.path.dirname(unit.storage_path)) close.assert_called_once_with('fd') shutil.copy.assert_called_once_with(path_in, temp_destination) unit.verify_size.assert_called_once_with(temp_destination) rename.assert_called_once_with(temp_destination, unit.storage_path)
def test_put_file_correct_size(self, _mkdir, shutil, tempfile, close, rename): path_in = '/tmp/test' temp_destination = '/some/file/path' unit = Mock(id='123', storage_path='/tmp/storage') storage = FileStorage() tempfile.mkstemp.return_value = ('fd', temp_destination) # test storage.put(unit, path_in) # validation _mkdir.assert_called_once_with(os.path.dirname(unit.storage_path)) tempfile.mkstemp.assert_called_once_with( dir=os.path.dirname(unit.storage_path)) close.assert_called_once_with('fd') shutil.copy.assert_called_once_with(path_in, temp_destination) unit.verify_size.assert_called_once_with(temp_destination) rename.assert_called_once_with(temp_destination, unit.storage_path)
def test_put_file_no_verify_size(self, _mkdir, shutil, tempfile, close, remove, rename): path_in = '/tmp/test' temp_destination = '/some/file/path' unit = Mock(id='123', storage_path='/tmp/storage') storage = FileStorage() tempfile.mkstemp.return_value = ('fd', temp_destination) unit.verify_size.side_effect = AttributeError("object has no attribute 'verify_size'") # test storage.put(unit, path_in) # validation _mkdir.assert_called_once_with(os.path.dirname(unit.storage_path)) tempfile.mkstemp.assert_called_once_with(dir=os.path.dirname(unit.storage_path)) close.assert_called_once_with('fd') shutil.copy.assert_called_once_with(path_in, temp_destination) unit.verify_size.assert_called_once_with(temp_destination) self.assertFalse(remove.called) rename.assert_called_once_with(temp_destination, unit.storage_path)
def test_put_file_no_verify_size(self, _mkdir, shutil, tempfile, close, remove, rename): path_in = '/tmp/test' temp_destination = '/some/file/path' unit = Mock(id='123', storage_path='/tmp/storage') storage = FileStorage() tempfile.mkstemp.return_value = ('fd', temp_destination) unit.verify_size.side_effect = AttributeError( "object has no attribute 'verify_size'") # test storage.put(unit, path_in) # validation _mkdir.assert_called_once_with(os.path.dirname(unit.storage_path)) tempfile.mkstemp.assert_called_once_with( dir=os.path.dirname(unit.storage_path)) close.assert_called_once_with('fd') shutil.copy.assert_called_once_with(path_in, temp_destination) unit.verify_size.assert_called_once_with(temp_destination) self.assertFalse(remove.called) rename.assert_called_once_with(temp_destination, unit.storage_path)