Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Oct 5, 2023
1 parent 05d8596 commit fc7fe44
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
17 changes: 17 additions & 0 deletions src/tribler/core/upgrade/tribler_db/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from tribler.core.components.database.db.tribler_database import TriblerDatabase
from tribler.core.upgrade.tribler_db.migration_chain import TriblerDatabaseMigrationChain
from tribler.core.utilities.path_util import Path
from tribler.core.utilities.simpledefs import STATEDIR_DB_DIR


# pylint: disable=redefined-outer-name


@pytest.fixture
def migration_chain(tmpdir):
db_file_name = Path(tmpdir) / STATEDIR_DB_DIR / 'tribler.db'
db_file_name.parent.mkdir()
TriblerDatabase(filename=str(db_file_name))
return TriblerDatabaseMigrationChain(state_dir=Path(tmpdir), chain=[])
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from tribler.core.upgrade.tribler_db.migration_chain import TriblerDatabaseMigrationChain
from tribler.core.upgrade.tribler_db.scheme_migrations.scheme_migration_0 import scheme_migration_0
from tribler.core.utilities.pony_utils import db_session


@db_session
def test_scheme_migration_0(migration_chain: TriblerDatabaseMigrationChain):
""" Test that the scheme_migration_0 changes the database version to 1. """
migration_chain.db.version = 0
migration_chain.migrations = [scheme_migration_0]

assert migration_chain.execute()
assert migration_chain.db.version == 1
17 changes: 5 additions & 12 deletions src/tribler/core/upgrade/tribler_db/tests/test_migration_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,21 @@

import pytest

from tribler.core.components.database.db.tribler_database import TriblerDatabase
from tribler.core.upgrade.tribler_db.decorator import migration
from tribler.core.upgrade.tribler_db.migration_chain import TriblerDatabaseMigrationChain
from tribler.core.utilities.path_util import Path
from tribler.core.utilities.pony_utils import db_session
from tribler.core.utilities.simpledefs import STATEDIR_DB_DIR


@pytest.fixture
def migration_chain(tmpdir):
db_file_name = Path(tmpdir) / STATEDIR_DB_DIR / 'tribler.db'
db_file_name.parent.mkdir()
TriblerDatabase(filename=str(db_file_name))
return TriblerDatabaseMigrationChain(state_dir=Path(tmpdir), chain=[])


def test_db_does_not_exist(tmpdir):
""" Test that the migration chain does not execute if the database does not exist."""
tribler_db_migration = TriblerDatabaseMigrationChain(state_dir=Path(tmpdir))
assert not tribler_db_migration.execute()


@db_session
def test_db_execute(migration_chain: TriblerDatabaseMigrationChain):
# create a migration
""" Test that the migration chain executes all the migrations step by step."""
migration3_call = Mock()
migration_chain.db.version = 0

Expand All @@ -42,7 +33,7 @@ def migration3(*_, **__):
migration3_call()

@migration(execute_only_if_version=99, set_after_successful_execution_version=100)
def migration99(*_, **__):
def migration99(*_, **__): # this migration should be skipped
...

migration_chain.migrations = [
Expand Down Expand Up @@ -80,6 +71,8 @@ def migration99(*_, **__):

@db_session
def test_db_execute_no_annotation(migration_chain: TriblerDatabaseMigrationChain):
""" Test that the migration chain raises the NotImplementedError if the migration does not have the annotation."""

def migration_without_annotation(*_, **__):
...

Expand Down

0 comments on commit fc7fe44

Please sign in to comment.