diff --git a/docker-compose.yaml b/docker-compose.yaml index dd95ffe..f1484e7 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -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 @@ -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 diff --git a/src/molalchemy/__init__.py b/src/molalchemy/__init__.py index 66cd9b5..dd63387 100644 --- a/src/molalchemy/__init__.py +++ b/src/molalchemy/__init__.py @@ -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__", ] diff --git a/src/molalchemy/rdkit/types.py b/src/molalchemy/rdkit/types.py index 06551c9..b277f6d 100644 --- a/src/molalchemy/rdkit/types.py +++ b/src/molalchemy/rdkit/types.py @@ -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" diff --git a/tests/test_alembic_helpers.py b/tests/test_alembic_helpers.py index 5d92d45..068cfa1 100644 --- a/tests/test_alembic_helpers.py +++ b/tests/test_alembic_helpers.py @@ -2,69 +2,159 @@ 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): @@ -72,16 +162,9 @@ def test_render_unknown_type(self): 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): @@ -89,17 +172,10 @@ def test_render_item_with_existing_imports(self): 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: