Skip to content

Commit

Permalink
Fix ord-schema tests (#716)
Browse files Browse the repository at this point in the history
* skip tests if rdkit cartridge is not available

* message

* skip

* fix notebook

* postgresql version

* apt

* apt-get

* notebook

* conda-forge
  • Loading branch information
skearnes committed Apr 12, 2024
1 parent 451b1d5 commit 9c2607e
Show file tree
Hide file tree
Showing 8 changed files with 857 additions and 936 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ jobs:
shell: bash -l {0}
run: |
# NOTE(skearnes): conda is only used for postgres (not python).
conda install -c rdkit rdkit-postgresql==2020.03.3.0
# NOTE(skearnes): rdkit-postgresql may not be available for ARM.
conda install -c rdkit rdkit-postgresql==2020.03.3.0 || conda install -c conda-forge postgresql
initdb
- uses: actions/setup-python@v4
with:
Expand Down

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 7 additions & 5 deletions ord_schema/orm/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from sqlalchemy import cast, delete, func, select, text, update
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.engine import Engine
from sqlalchemy.exc import OperationalError
from sqlalchemy.exc import OperationalError, NotSupportedError
from sqlalchemy.orm import Session

from ord_schema.logging import get_logger
Expand Down Expand Up @@ -55,14 +55,16 @@ def prepare_database(engine: Engine) -> bool:
with engine.begin() as connection:
connection.execute(text("CREATE SCHEMA IF NOT EXISTS ord"))
connection.execute(text("CREATE SCHEMA IF NOT EXISTS rdkit"))
try:
try:
with engine.begin() as connection:
# NOTE(skearnes): The RDKit PostgreSQL extension works best in the public schema.
connection.execute(text("CREATE EXTENSION IF NOT EXISTS rdkit"))
rdkit_cartridge = True
except OperationalError:
rdkit_cartridge = True
except (OperationalError, NotSupportedError):
with engine.begin() as connection:
logger.warning("RDKit PostgreSQL cartridge is not installed; structure search will be disabled")
connection.execute(text("CREATE EXTENSION IF NOT EXISTS btree_gist"))
rdkit_cartridge = False
rdkit_cartridge = False
with patch.dict(os.environ, {"ORD_POSTGRES_RDKIT": "1" if rdkit_cartridge else "0"}):
Base.metadata.create_all(engine)
return rdkit_cartridge
Expand Down
43 changes: 25 additions & 18 deletions ord_schema/orm/rdkit_mappers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,37 @@
"""Tests for ord_schema.orm.rdkit_mappers."""
import pytest
from sqlalchemy import select
from sqlalchemy.exc import ProgrammingError
from ord_schema.orm.mappers import Mappers
from ord_schema.orm.rdkit_mappers import FingerprintType, RDKitMol


def test_tanimoto_operator(test_session):
query = (
select(Mappers.Reaction)
.join(Mappers.ReactionInput)
.join(Mappers.Compound)
.join(RDKitMol)
.where(RDKitMol.morgan_bfp % FingerprintType.MORGAN_BFP("c1ccccc1CCC(O)C"))
)
results = test_session.execute(query)
assert len(results.fetchall()) == 20
try:
query = (
select(Mappers.Reaction)
.join(Mappers.ReactionInput)
.join(Mappers.Compound)
.join(RDKitMol)
.where(RDKitMol.morgan_bfp % FingerprintType.MORGAN_BFP("c1ccccc1CCC(O)C"))
)
results = test_session.execute(query)
assert len(results.fetchall()) == 20
except ProgrammingError as error:
pytest.skip(f"RDKit cartridge is required: {error}")


@pytest.mark.parametrize("fp_type", list(FingerprintType))
def test_tanimoto(test_session, fp_type):
query = (
select(Mappers.Reaction)
.join(Mappers.ReactionInput)
.join(Mappers.Compound)
.join(RDKitMol)
.where(RDKitMol.tanimoto("c1ccccc1CCC(O)C", fp_type=fp_type) > 0.5)
)
results = test_session.execute(query)
assert len(results.fetchall()) == 20
try:
query = (
select(Mappers.Reaction)
.join(Mappers.ReactionInput)
.join(Mappers.Compound)
.join(RDKitMol)
.where(RDKitMol.tanimoto("c1ccccc1CCC(O)C", fp_type=fp_type) > 0.5)
)
results = test_session.execute(query)
assert len(results.fetchall()) == 20
except ProgrammingError as error:
pytest.skip(f"RDKit cartridge is required: {error}")
4 changes: 3 additions & 1 deletion ord_schema/orm/scripts/add_datasets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os

import docopt
import pytest
from sqlalchemy import create_engine
from testing.postgresql import Postgresql

Expand All @@ -26,7 +27,8 @@
def test_main():
with Postgresql() as postgres:
engine = create_engine(postgres.url(), future=True)
assert prepare_database(engine) # Requires RDKit.
if not prepare_database(engine):
pytest.skip("RDKit cartridge is required")
argv = [
"--url",
postgres.url(),
Expand Down

0 comments on commit 9c2607e

Please sign in to comment.