from __future__ import unicode_literals, division, absolute_import
import os


class TestMigrate(object):

    config = """
        tasks:
          test:
            mock:
              - {title: 'foobar'}
            accept_all: yes
    """

    def setup(self):
        import logging
        logging.critical('TestMigrate.setup()')
        db_filename = os.path.join(self.base_path, 'upgrade_test.sqlite')
        # in case running on windows, needs double \\
        filename = db_filename.replace('\\', '\\\\')
        self.database_uri = 'sqlite:///%s' % filename
        super(TestMigrate, self).setup()

    # This fails on windows when it tries to delete upgrade_test.sqlite
    # WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'upgrade_test.sqlite'
    #@with_filecopy('db-r1042.sqlite', 'upgrade_test.sqlite')
    def test_upgrade(self, execute_task):
        # TODO: for some reason this will fail
        return

        task = execute_task('test')
        assert task.accepted

from __future__ import unicode_literals, division, absolute_import

import pytest

from .conftest import MockManager

class TestMigrate(object):

    config = """
        tasks:
          test:
            mock:
              - {title: 'foobar'}
            accept_all: yes
    """

    @pytest.mark.filecopy('db-r1042.sqlite', '__tmp__/upgrade_test.sqlite')
    def test_upgrade(self, request, config, caplog, tmpdir, filecopy):
        db_filename = tmpdir.join('upgrade_test.sqlite')
        # in case running on windows, needs double \\
        filename = db_filename.strpath.replace('\\', '\\\\')
        database_uri = 'sqlite:///%s' % filename
        # This will raise an error if the upgrade wasn't successful
        mockmanager = MockManager(config, request.cls.__name__, db_uri=database_uri)
        try:
            mockmanager.shutdown()
        finally:
            mockmanager.__del__()
        # TODO: verify we actually loaded the old config, and didn't just create a new one or something

fix and enable test_migrate