Skip to content

Commit

Permalink
Merge pull request #17 from MatBarba/mbarba/no_datadir
Browse files Browse the repository at this point in the history
Allow test db without schema/data from dump_dir
  • Loading branch information
JAlvarezJarreta authored Jul 12, 2024
2 parents 40fd264 + 5c368a5 commit bef0981
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
5 changes: 3 additions & 2 deletions src/ensembl/utils/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ def _db_factory(src: StrPath, name: Optional[str] = None) -> UnitTestDB:
"""Returns a unit test database.
Args:
src: Directory path where the test database schema and content files are located.
src: Directory path where the test database schema and content files are located, if any.
name: Name to give to the new database. See `UnitTestDB` for more information.
"""
src_path = Path(src)
if not src_path.is_absolute():
src_path = data_dir / src_path
db_key = name if name else src_path.name
return created.setdefault(db_key, UnitTestDB(server_url, dump_dir=src_path, name=name))
dump_dir: Path | None = src_path if src_path.exists() else None
return created.setdefault(db_key, UnitTestDB(server_url, dump_dir=dump_dir, name=name))

yield _db_factory
# Drop all unit test databases unless the user has requested to keep them
Expand Down
51 changes: 23 additions & 28 deletions tests/plugin/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,13 @@
"""

from contextlib import nullcontext as does_not_raise
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, ContextManager
from unittest.mock import patch

import pytest
from pytest import FixtureRequest, param, raises

from ensembl.utils import StrPath
from ensembl.utils.database import StrURL


@dataclass
class MockTestDB:
"""Mocks `UnitTestDB` class by just storing the three arguments provided."""

server_url: StrURL
dump_dir: StrPath
name: str

def drop(self) -> None:
"""Mocks `UnitTestDB.drop()` method."""
from ensembl.utils.database import UnitTestDB


@pytest.mark.dependency(name="test_data_dir")
Expand Down Expand Up @@ -80,27 +65,37 @@ def test_assert_files(


@pytest.mark.parametrize(
"dump_dir, db_name",
"dump_dir, make_absolute, db_name, expected_tables",
[
(Path("dump_dir"), "dump_dir"),
(Path("dump_dir").resolve(), "test_db"),
param(Path("dump_dir"), False, "relative_dump_db", ["gibberish"], id="Relative dump_dir"),
param(Path("dump_dir"), True, "absolute_dump_db", ["gibberish"], id="Absolute dump_dir"),
param(Path("not_a_dir"), False, "no_dump_db", [], id="Non-existent dump_dir"),
],
)
@patch("ensembl.utils.plugin.UnitTestDB", new=MockTestDB)
def test_db_factory(request: FixtureRequest, db_factory: Callable, dump_dir: Path, db_name: str) -> None:
def test_db_factory(
request: FixtureRequest,
db_factory: Callable[[Path, str], UnitTestDB],
data_dir: Path,
dump_dir: Path,
make_absolute: bool,
db_name: str,
expected_tables: list[str],
) -> None:
"""Tests the `db_factory` fixture.
Args:
request: Fixture that provides information of the requesting test function.
db_factory: Fixture that provides a unit test database factory.
data_dir: Directory where this specific test data are stored.
dump_dir: Directory path where the test database schema and content files are located.
make_absolute: change the dump_dir from relative to absolute (based on `data_dir`).
db_name: Name to give to the new database.
expected_tables: List of tables that should be loaded in the test database.
"""
test_db = db_factory(dump_dir, db_name)
assert test_db.server_url == request.config.getoption("server")
assert test_db.name == db_name
if dump_dir.is_absolute():
assert test_db.dump_dir == dump_dir
else:
assert test_db.dump_dir.stem == str(dump_dir)
if make_absolute:
dump_dir = Path(data_dir, dump_dir).absolute()
test_db: UnitTestDB = db_factory(dump_dir, db_name)
assert test_db.dbc.url.startswith(request.config.getoption("server"))
assert Path(test_db.dbc.db_name).stem.endswith(db_name)
assert set(test_db.dbc.tables.keys()) == set(expected_tables)
6 changes: 6 additions & 0 deletions tests/plugin/test_plugin/dump_dir/gibberish.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1 grp1 11
2 grp1 12
3 grp2 21
4 grp2 22
5 grp2 23
6 grp3 31
7 changes: 7 additions & 0 deletions tests/plugin/test_plugin/dump_dir/table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE `gibberish` (
`id` INTEGER NOT NULL,
`grp` VARCHAR(20) DEFAULT "",
`value` INT DEFAULT NULL,
PRIMARY KEY (`id`, `grp`)
);
CREATE INDEX `id_idx` ON `gibberish` (`id`);

0 comments on commit bef0981

Please sign in to comment.