Пример #1
0
def test_sequential_version_numbers(tmp_path):
    """ Test that the version received by each migration
    function is sequantially incremented according to the
    version returned by the previous migration.
    Sequence of events:
    - The first migration runs and returns v16 as the
      version it upgraded the database to.
    - The next migration should receive the old_version
      as v16 returned previously.
    - the above goes on for subsequent migrations.
    """
    db_path = tmp_path / Path('v19_log.db')
    upgrade_manager = UpgradeManager(db_filename=db_path)

    old_db_filename = tmp_path / Path('v16_log.db')
    storage = None

    upgrade_functions = [Mock(), Mock(), Mock()]

    upgrade_functions[0].return_value = 17
    upgrade_functions[1].return_value = 18
    upgrade_functions[2].return_value = 19

    with patch('raiden.storage.sqlite.RAIDEN_DB_VERSION', new=16):
        storage = setup_storage(old_db_filename)
        storage.update_version()

    with ExitStack() as stack:
        stack.enter_context(
            patch(
                'raiden.utils.upgrades.UPGRADES_LIST',
                new=upgrade_functions,
            ))
        stack.enter_context(
            patch(
                'raiden.utils.upgrades.RAIDEN_DB_VERSION',
                new=19,
            ))
        older_db_file = stack.enter_context(
            patch('raiden.utils.upgrades.older_db_file'))
        older_db_file.return_value = str(old_db_filename)

        upgrade_manager.run()

        upgrade_functions[0].assert_called_once_with(ANY, 16, 19)
        upgrade_functions[1].assert_called_once_with(ANY, 17, 19)
        upgrade_functions[2].assert_called_once_with(ANY, 18, 19)

        assert get_db_version(str(db_path)) == 19
Пример #2
0
def test_sequential_version_numbers(tmp_path, monkeypatch):
    """ Test that the version received by each migration
    function is sequentially incremented according to the
    version returned by the previous migration.
    Sequence of events:
    - The first migration runs and returns v16 as the
      version it upgraded the database to.
    - The next migration should receive the old_version
      as v16 returned previously.
    - the above goes on for subsequent migrations.
    """
    db_path = tmp_path / Path("v19_log.db")

    old_db_filename = tmp_path / Path("v16_log.db")

    upgrade_functions = []
    for i in range(16, 19):
        mock = Mock()
        mock.return_value = i + 1
        upgrade_functions.append(UpgradeRecord(from_version=i, function=mock))

    with patch("raiden.storage.sqlite.RAIDEN_DB_VERSION", new=16):
        storage = setup_storage(old_db_filename)
        storage.update_version()
        storage.conn.close()

    with monkeypatch.context() as m:

        def latest_db_file(paths):  # pylint: disable=unused-argument
            return old_db_filename

        m.setattr(raiden.utils.upgrades, "UPGRADES_LIST", upgrade_functions)
        m.setattr(raiden.utils.upgrades, "RAIDEN_DB_VERSION", 19)
        m.setattr(raiden.utils.upgrades, "latest_db_file", latest_db_file)

        UpgradeManager(db_filename=db_path).run()

        upgrade_functions[0].function.assert_called_once_with(
            old_version=16, current_version=19, storage=ANY
        )
        upgrade_functions[1].function.assert_called_once_with(
            old_version=17, current_version=19, storage=ANY
        )
        upgrade_functions[2].function.assert_called_once_with(
            old_version=18, current_version=19, storage=ANY
        )

        assert get_db_version(db_path) == 19