Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
services:

bingo:
build:
dockerfile: docker/Dockerfile.bingo
context: .
args:
- POSTGRES_VERSION=17.6
- BINGO_VERSION=1.34.0
image: antonsiomchen/cheminfo-db:postgres16.13-bingo1.40.0
restart: always
# set shared memory limit when using docker compose
shm_size: 1024mb
Expand All @@ -24,16 +19,13 @@ services:
# volumes:
# - db_data:/var/lib/postgresql/data
rdkit:
build:
dockerfile: docker/Dockerfile.rdkit
context: .
# args:
# - rdkit_git_ref=Release_2025_03
# - boost_dev_version=1.81
image: antonsiomchen/cheminfo-db:postgres18.3-rdkit2025.09.6
restart: always
ports:
- 5432:5432
environment:
POSTGRES_USER: postgres
POSTGRES_DB: postgres
POSTGRES_PASSWORD: example


Expand Down
30 changes: 27 additions & 3 deletions src/molalchemy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,44 @@
from loguru import logger

from molalchemy._version import __version__
from molalchemy.bingo.index import BingoBinaryMolIndex, BingoMolIndex
from molalchemy.bingo.types import BingoBinaryMol, BingoMol
from molalchemy.bingo.index import (
BingoBinaryMolIndex,
BingoBinaryRxnIndex,
BingoMolIndex,
BingoRxnIndex,
)
from molalchemy.bingo.types import (
BingoBinaryMol,
BingoBinaryReaction,
BingoMol,
BingoReaction,
)
from molalchemy.rdkit.index import RdkitIndex
from molalchemy.rdkit.types import RdkitBitFingerprint, RdkitMol, RdkitSparseFingerprint
from molalchemy.rdkit.types import (
RdkitBitFingerprint,
RdkitMol,
RdkitQMol,
RdkitReaction,
RdkitSparseFingerprint,
RdkitXQMol,
)

logger.disable("molalchemy")
__all__ = [
"BingoBinaryMol",
"BingoBinaryMolIndex",
"BingoBinaryReaction",
"BingoBinaryRxnIndex",
"BingoMol",
"BingoMolIndex",
"BingoReaction",
"BingoRxnIndex",
"RdkitBitFingerprint",
"RdkitIndex",
"RdkitMol",
"RdkitQMol",
"RdkitReaction",
"RdkitSparseFingerprint",
"RdkitXQMol",
"__version__",
]
10 changes: 10 additions & 0 deletions src/molalchemy/rdkit/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,20 @@ def process(value, return_type):


class RdkitQMol(RdkitBaseType):
cache_ok = True

def __repr__(self):
return "RdkitQMol()"

def get_col_spec(self, **kwargs: Any) -> str:
return "qmol"


class RdkitXQMol(RdkitBaseType):
cache_ok = True

def __repr__(self):
return "RdkitXQMol()"

def get_col_spec(self, **kwargs: Any) -> str:
return "xqmol"
192 changes: 134 additions & 58 deletions tests/test_alembic_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,104 +2,180 @@

from unittest.mock import Mock, patch

import pytest

from molalchemy.alembic_helpers import (
add_rdkit_extension,
drop_rdkit_extension,
render_item,
)
from molalchemy.bingo.types import BingoMol
from molalchemy.rdkit.types import RdkitMol


class TestRenderItem:
"""Test cases for the render_item function."""
from molalchemy.bingo.types import (
BingoBinaryMol,
BingoBinaryReaction,
BingoMol,
BingoReaction,
)
from molalchemy.rdkit.types import (
RdkitBitFingerprint,
RdkitMol,
RdkitQMol,
RdkitReaction,
RdkitSparseFingerprint,
RdkitXQMol,
)

def test_render_rdkit_type(self):
"""Test rendering of RDKit types adds correct import and returns repr."""
# Create a mock autogen_context with an imports set
# All type variants with expected module, class name, and constructor repr
ALL_TYPES = [
# RDKit types
(
RdkitMol(),
"molalchemy.rdkit.types",
"RdkitMol",
"RdkitMol(return_type='smiles')",
),
(
RdkitMol(return_type="mol"),
"molalchemy.rdkit.types",
"RdkitMol",
"RdkitMol(return_type='mol')",
),
(
RdkitMol(return_type="bytes"),
"molalchemy.rdkit.types",
"RdkitMol",
"RdkitMol(return_type='bytes')",
),
(
RdkitBitFingerprint(),
"molalchemy.rdkit.types",
"RdkitBitFingerprint",
"RdkitBitFingerprint()",
),
(
RdkitSparseFingerprint(),
"molalchemy.rdkit.types",
"RdkitSparseFingerprint",
"RdkitSparseFingerprint()",
),
(
RdkitReaction(),
"molalchemy.rdkit.types",
"RdkitReaction",
"RdkitReaction(return_type='smiles')",
),
(
RdkitReaction(return_type="mol"),
"molalchemy.rdkit.types",
"RdkitReaction",
"RdkitReaction(return_type='mol')",
),
(RdkitQMol(), "molalchemy.rdkit.types", "RdkitQMol", "RdkitQMol()"),
(RdkitXQMol(), "molalchemy.rdkit.types", "RdkitXQMol", "RdkitXQMol()"),
# Bingo types
(BingoMol(), "molalchemy.bingo.types", "BingoMol", "BingoMol()"),
(
BingoBinaryMol(),
"molalchemy.bingo.types",
"BingoBinaryMol",
"BingoBinaryMol(preserve_pos=False, return_type='smiles')",
),
(
BingoBinaryMol(preserve_pos=True, return_type="molfile"),
"molalchemy.bingo.types",
"BingoBinaryMol",
"BingoBinaryMol(preserve_pos=True, return_type='molfile')",
),
(BingoReaction(), "molalchemy.bingo.types", "BingoReaction", "BingoReaction()"),
(
BingoBinaryReaction(),
"molalchemy.bingo.types",
"BingoBinaryReaction",
"BingoBinaryReaction()",
),
]


class TestRenderAllTypes:
"""Test render_item for all type variants."""

@pytest.mark.parametrize(
("instance", "expected_module", "class_name", "expected_repr"),
ALL_TYPES,
ids=[t[3] for t in ALL_TYPES],
)
def test_render_all_types(
self, instance, expected_module, class_name, expected_repr
):
"""Test that render_item produces correct import and repr for every type."""
autogen_context = Mock()
autogen_context.imports = set()

# Create an RDKit type instance
rdkit_mol = RdkitMol()
result = render_item("type", instance, autogen_context)

# Call render_item
result = render_item("type", rdkit_mol, autogen_context)

# Check that the correct import was added
expected_import = "from molalchemy.rdkit.types import RdkitMol"
expected_import = f"from {expected_module} import {class_name}"
assert expected_import in autogen_context.imports

# Check that the repr of the object was returned
assert result == repr(rdkit_mol)

def test_render_bingo_type(self):
"""Test rendering of Bingo types adds correct import and returns repr."""
# Create a mock autogen_context with an imports set
assert result == expected_repr

@pytest.mark.parametrize(
("instance", "expected_module", "class_name", "expected_repr"),
ALL_TYPES,
ids=[t[3] for t in ALL_TYPES],
)
def test_repr_roundtrip(self, instance, expected_module, class_name, expected_repr):
"""Test that eval(repr(instance)) reconstructs the same type."""
# Import the class into local namespace for eval
mod = __import__(expected_module, fromlist=[class_name])
cls = getattr(mod, class_name)
reconstructed = eval(repr(instance), {class_name: cls})
assert repr(reconstructed) == repr(instance)

@pytest.mark.parametrize(
("instance", "expected_module", "class_name", "expected_repr"),
ALL_TYPES,
ids=[t[3] for t in ALL_TYPES],
)
def test_rendered_type_is_valid_constructor(
self, instance, expected_module, class_name, expected_repr
):
"""Test that the rendered string is exactly the constructor call."""
autogen_context = Mock()
autogen_context.imports = set()

# Create a Bingo type instance
bingo_mol = BingoMol()

# Call render_item
result = render_item("type", bingo_mol, autogen_context)
result = render_item("type", instance, autogen_context)
assert result == expected_repr

# Check that the correct import was added
expected_import = "from molalchemy.bingo.types import BingoMol"
assert expected_import in autogen_context.imports

# Check that the repr of the object was returned
assert result == repr(bingo_mol)
class TestRenderNonTypes:
"""Test render_item for non-type objects."""

def test_render_non_type_object(self):
"""Test rendering of non-type objects returns False."""
autogen_context = Mock()
autogen_context.imports = set()

# Test with different object types
result = render_item("column", Mock(), autogen_context)
assert result is False

result = render_item("table", Mock(), autogen_context)
assert result is False

# Ensure no imports were added
assert render_item("column", Mock(), autogen_context) is False
assert render_item("table", Mock(), autogen_context) is False
assert len(autogen_context.imports) == 0

def test_render_unknown_type(self):
"""Test rendering of unknown type objects returns False."""
autogen_context = Mock()
autogen_context.imports = set()

# Create a mock object that doesn't inherit from base types
unknown_type = Mock()
result = render_item("type", Mock(), autogen_context)

# Call render_item with "type" but unknown object
result = render_item("type", unknown_type, autogen_context)

# Should return False for unknown types
assert result is False

# Ensure no imports were added
assert len(autogen_context.imports) == 0

def test_render_item_with_existing_imports(self):
"""Test that render_item doesn't duplicate imports."""
autogen_context = Mock()
autogen_context.imports = {"from molalchemy.rdkit.types import RdkitMol"}

rdkit_mol = RdkitMol()

# Call render_item
result = render_item("type", rdkit_mol, autogen_context)

# Should still work and return repr
assert result == repr(rdkit_mol)
result = render_item("type", RdkitMol(), autogen_context)

# Should have only one import (set prevents duplicates)
assert result == repr(RdkitMol())
assert len(autogen_context.imports) == 1
assert "from molalchemy.rdkit.types import RdkitMol" in autogen_context.imports


class TestExtensionFunctions:
Expand Down
Loading