コード例 #1
0
ファイル: test_filedepot.py プロジェクト: cruxlog/Kotti
    def test_migrate_between_storages(self, db_session, root, no_filedepots):
        from kotti.filedepot import configure_filedepot
        from kotti.filedepot import migrate_storage
        from kotti.resources import Node
        from depot.fields.sqlalchemy import _SQLAMutationTracker
        from sqlalchemy import event
        import os
        import tempfile
        import shutil

        event.listen(db_session, 'before_commit',
                     _SQLAMutationTracker._session_committed)

        tmp_location = tempfile.mkdtemp()

        settings = {
            'kotti.depot.0.backend': 'kotti.filedepot.DBFileStorage',
            'kotti.depot.0.name': 'dbfiles',
            'kotti.depot.1.backend': 'depot.io.local.LocalFileStorage',
            'kotti.depot.1.name': 'localfs',
            'kotti.depot.1.storage_path': tmp_location,
        }

        configure_filedepot(settings)
        self._create_content(db_session, root)

        assert db_session.query(DBStoredFile).count() == 4

        migrate_storage('dbfiles', 'localfs')

        folders = os.listdir(tmp_location)
        assert len(folders) == 4

        db_session.flush()

        # here we need a transaction.commit(), but that would mess with the
        # rest of the tests; we'll just trigger the event handler manually
        _SQLAMutationTracker._session_committed(db_session)

        root = db_session.query(Node).filter_by(parent=None).one()
        f1 = root['file1.jpg']
        assert f1.data.file_id in folders
        assert f1.data.file.read() == 'f1...'

        f2 = root['file2.png']
        assert f2.data.file_id in folders
        assert f2.data.file.read() == 'f2...'

        i1 = root['image1.jpg']
        assert i1.data.file_id in folders
        assert i1.data.file.read() == 'i1...'

        i2 = root['image2.png']
        assert i2.data.file_id in folders
        assert i2.data.file.read() == 'i2...'

        assert db_session.query(DBStoredFile).count() == 0

        shutil.rmtree(tmp_location)
コード例 #2
0
    def test_migrate_between_storages(self, db_session, root, no_filedepots,
                                      image_asset, image_asset2):
        from kotti.filedepot import configure_filedepot
        from kotti.filedepot import migrate_storage
        from kotti.resources import Node
        from depot.fields.sqlalchemy import _SQLAMutationTracker
        from sqlalchemy import event
        import os
        import tempfile
        import shutil

        event.listen(db_session,
                     'before_commit',
                     _SQLAMutationTracker._session_committed)

        tmp_location = tempfile.mkdtemp()

        settings = {
            'kotti.depot.0.backend': 'kotti.filedepot.DBFileStorage',
            'kotti.depot.0.name': 'dbfiles',

            'kotti.depot.1.backend': 'depot.io.local.LocalFileStorage',
            'kotti.depot.1.name': 'localfs',
            'kotti.depot.1.storage_path': tmp_location,
        }

        configure_filedepot(settings)
        image1 = image_asset.read()
        image2 = image_asset2.read()
        self._create_content(db_session, root, image1, image2)

        assert db_session.query(DBStoredFile).count() == 8

        migrate_storage('dbfiles', 'localfs')

        folders = os.listdir(tmp_location)
        assert len(folders) == 8

        db_session.flush()

        # here we need a transaction.commit(), but that would mess with the
        # rest of the tests; we'll just trigger the event handler manually
        _SQLAMutationTracker._session_committed(db_session)

        root = db_session.query(Node).filter_by(parent=None).one()
        f1 = root['file1.jpg']
        assert f1.data.file_id in folders
        assert f1.data.file.read() == 'f1...'

        f2 = root['file2.png']
        assert f2.data.file_id in folders
        assert f2.data.file.read() == 'f2...'

        i1 = root['image1.jpg']
        assert i1.data.file_id in folders
        i1data = i1.data.file.read()
        assert i1data == image2

        i2 = root['image2.png']
        assert i2.data.file_id in folders
        i2data = i2.data.file.read()
        assert i2data == image1

        assert db_session.query(DBStoredFile).count() == 0

        shutil.rmtree(tmp_location)
コード例 #3
0
    def test_migrate_between_storages(
        self, db_session, root, no_filedepots, image_asset, image_asset2
    ):
        from kotti.filedepot import configure_filedepot
        from kotti.filedepot import migrate_storage
        from kotti.resources import Node
        from depot.fields.sqlalchemy import _SQLAMutationTracker
        from sqlalchemy import event
        import os
        import tempfile
        import shutil

        event.listen(
            db_session, "before_commit", _SQLAMutationTracker._session_committed
        )

        tmp_location = tempfile.mkdtemp()

        settings = {
            "kotti.depot.0.backend": "kotti.filedepot.DBFileStorage",
            "kotti.depot.0.name": "dbfiles",
            "kotti.depot.1.backend": "depot.io.local.LocalFileStorage",
            "kotti.depot.1.name": "localfs",
            "kotti.depot.1.storage_path": tmp_location,
        }

        configure_filedepot(settings)
        image1 = image_asset.read()
        image2 = image_asset2.read()
        self._create_content(db_session, root, image1, image2)

        assert db_session.query(DBStoredFile).count() == 4

        migrate_storage("dbfiles", "localfs")

        folders = os.listdir(tmp_location)
        assert len(folders) == 4

        db_session.flush()

        # here we need a transaction.commit(), but that would mess with the
        # rest of the tests; we'll just trigger the event handler manually
        _SQLAMutationTracker._session_committed(db_session)

        root = db_session.query(Node).filter_by(parent=None).one()
        f1 = root["file1.jpg"]
        assert f1.data.file_id in folders
        assert f1.data.file.read() == b"f1..."

        f2 = root["file2.png"]
        assert f2.data.file_id in folders
        assert f2.data.file.read() == b"f2..."

        i1 = root["image1.jpg"]
        assert i1.data.file_id in folders
        i1data = i1.data.file.read()
        assert i1data == image2

        i2 = root["image2.png"]
        assert i2.data.file_id in folders
        i2data = i2.data.file.read()
        assert i2data == image1

        assert db_session.query(DBStoredFile).count() == 0

        shutil.rmtree(tmp_location)