From 513bdc746a7f096904c48bc947afff6b3ce95ae7 Mon Sep 17 00:00:00 2001 From: Anton Siomchen <41703271+asiomchen@users.noreply.github.com> Date: Sun, 1 Mar 2026 18:21:44 +0100 Subject: [PATCH 1/2] Refactor Docker Compose configuration and improve alembic support --- docker-compose.yaml | 16 +--- src/molalchemy/__init__.py | 13 ++- src/molalchemy/alembic_helpers.py | 1 + src/molalchemy/rdkit/types.py | 10 +++ tests/test_alembic_helpers.py | 134 +++++++++++++++++------------- 5 files changed, 101 insertions(+), 73 deletions(-) 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..d8328de 100644 --- a/src/molalchemy/__init__.py +++ b/src/molalchemy/__init__.py @@ -1,20 +1,27 @@ 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/alembic_helpers.py b/src/molalchemy/alembic_helpers.py index 6d66d96..5df23a8 100644 --- a/src/molalchemy/alembic_helpers.py +++ b/src/molalchemy/alembic_helpers.py @@ -34,6 +34,7 @@ def drop_rdkit_extension(): op.execute("DROP EXTENSION IF EXISTS rdkit;") + def render_item(obj_type, obj, autogen_context): logger.debug(f"Rendering item: {obj_type}, {obj}") if obj_type == "type": 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..cdfc8d9 100644 --- a/tests/test_alembic_helpers.py +++ b/tests/test_alembic_helpers.py @@ -2,69 +2,100 @@ 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() - - # Call render_item - result = render_item("type", rdkit_mol, autogen_context) + result = render_item("type", instance, 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}) # noqa: S307 + 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() + result = render_item("type", instance, autogen_context) + assert result == expected_repr - # Call render_item - result = render_item("type", bingo_mol, autogen_context) - - # 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 +103,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() - - # Call render_item with "type" but unknown object - result = render_item("type", unknown_type, autogen_context) + result = render_item("type", Mock(), 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 +113,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) + result = render_item("type", RdkitMol(), autogen_context) - # Should still work and return repr - assert result == repr(rdkit_mol) - - # 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: @@ -116,3 +133,4 @@ def test_drop_rdkit_extension(self, mock_op): """Test that drop_rdkit_extension executes correct SQL.""" drop_rdkit_extension() mock_op.execute.assert_called_once_with("DROP EXTENSION IF EXISTS rdkit;") + From afd58288b4e56437b08eee17bee3a29e0ebb2f24 Mon Sep 17 00:00:00 2001 From: Anton Siomchen <41703271+asiomchen@users.noreply.github.com> Date: Sun, 1 Mar 2026 18:50:03 +0100 Subject: [PATCH 2/2] run ruff --- src/molalchemy/__init__.py | 23 ++++++-- src/molalchemy/alembic_helpers.py | 1 - tests/test_alembic_helpers.py | 88 +++++++++++++++++++++++++------ 3 files changed, 93 insertions(+), 19 deletions(-) diff --git a/src/molalchemy/__init__.py b/src/molalchemy/__init__.py index d8328de..dd63387 100644 --- a/src/molalchemy/__init__.py +++ b/src/molalchemy/__init__.py @@ -1,10 +1,27 @@ from loguru import logger from molalchemy._version import __version__ -from molalchemy.bingo.index import BingoBinaryMolIndex, BingoBinaryRxnIndex, BingoMolIndex, BingoRxnIndex -from molalchemy.bingo.types import BingoBinaryMol, BingoBinaryReaction, BingoMol, BingoReaction +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, RdkitQMol, RdkitReaction, RdkitSparseFingerprint, RdkitXQMol +from molalchemy.rdkit.types import ( + RdkitBitFingerprint, + RdkitMol, + RdkitQMol, + RdkitReaction, + RdkitSparseFingerprint, + RdkitXQMol, +) logger.disable("molalchemy") __all__ = [ diff --git a/src/molalchemy/alembic_helpers.py b/src/molalchemy/alembic_helpers.py index 5df23a8..6d66d96 100644 --- a/src/molalchemy/alembic_helpers.py +++ b/src/molalchemy/alembic_helpers.py @@ -34,7 +34,6 @@ def drop_rdkit_extension(): op.execute("DROP EXTENSION IF EXISTS rdkit;") - def render_item(obj_type, obj, autogen_context): logger.debug(f"Rendering item: {obj_type}, {obj}") if obj_type == "type": diff --git a/tests/test_alembic_helpers.py b/tests/test_alembic_helpers.py index cdfc8d9..068cfa1 100644 --- a/tests/test_alembic_helpers.py +++ b/tests/test_alembic_helpers.py @@ -9,7 +9,12 @@ drop_rdkit_extension, render_item, ) -from molalchemy.bingo.types import BingoBinaryMol, BingoBinaryReaction, BingoMol, BingoReaction +from molalchemy.bingo.types import ( + BingoBinaryMol, + BingoBinaryReaction, + BingoMol, + BingoReaction, +) from molalchemy.rdkit.types import ( RdkitBitFingerprint, RdkitMol, @@ -22,21 +27,71 @@ # 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')"), + ( + 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')"), + ( + 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()"), + ( + BingoBinaryReaction(), + "molalchemy.bingo.types", + "BingoBinaryReaction", + "BingoBinaryReaction()", + ), ] @@ -48,7 +103,9 @@ class TestRenderAllTypes: ALL_TYPES, ids=[t[3] for t in ALL_TYPES], ) - def test_render_all_types(self, instance, expected_module, class_name, expected_repr): + 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() @@ -69,7 +126,7 @@ def test_repr_roundtrip(self, instance, expected_module, class_name, expected_re # 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}) # noqa: S307 + reconstructed = eval(repr(instance), {class_name: cls}) assert repr(reconstructed) == repr(instance) @pytest.mark.parametrize( @@ -77,7 +134,9 @@ def test_repr_roundtrip(self, instance, expected_module, class_name, expected_re 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): + 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() @@ -133,4 +192,3 @@ def test_drop_rdkit_extension(self, mock_op): """Test that drop_rdkit_extension executes correct SQL.""" drop_rdkit_extension() mock_op.execute.assert_called_once_with("DROP EXTENSION IF EXISTS rdkit;") -