Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLAlchemy: Add Python-based test module for integration tests #88

Closed
amotl opened this issue Dec 21, 2022 · 2 comments
Closed

SQLAlchemy: Add Python-based test module for integration tests #88

amotl opened this issue Dec 21, 2022 · 2 comments

Comments

@amotl
Copy link
Member

amotl commented Dec 21, 2022

At crate/crate-python#464 (comment), we observed that

We currently have no place where Python-based SQLAlchemy integration tests are conducted. Currently, there are only Python-based unit tests exercising the CrateDB dialect using corresponding mocks to work around needing a real database. On the other hand, there are integration test cases with SQLAlchemy and CrateDB, but they are only written as doctests.

In order to conduct special test scenarios which don't fit having them written as a doctest, and for improved capabilities to run specific test cases selectively, it may make sense to add a module which fills the gap on running Python-based SQLAlchemy integration test cases against a real database.

@amotl
Copy link
Member Author

amotl commented Dec 22, 2022

At crate/crate-python#490 (review) ff., we discovered that it would be indeed good to have a spot to run Python-based SQLAlchemy integration tests.

I will stage a corresponding PR where some of @hammerhead's fixes can then be picked into.

Specifically, this is 1429658e. Thank you again!

@amotl amotl transferred this issue from crate/crate-python Jun 16, 2024
@amotl
Copy link
Member Author

amotl commented Jun 17, 2024

Integration tests can be written in Python now. Example:

def test_float_vector_integration():
"""
An integration test for `FLOAT_VECTOR` and `KNN_SEARCH`.
"""
np = pytest.importorskip("numpy")
engine = sa.create_engine(f"crate://")
session = sessionmaker(bind=engine)()
Base = declarative_base()
# Define DDL.
class SearchIndex(Base):
__tablename__ = 'search'
name = sa.Column(sa.String, primary_key=True)
embedding = sa.Column(FloatVector(3))
Base.metadata.drop_all(engine, checkfirst=True)
Base.metadata.create_all(engine, checkfirst=True)
# Insert record.
foo_item = SearchIndex(name="foo", embedding=[42.42, 43.43, 44.44])
session.add(foo_item)
session.commit()
session.execute(sa.text("REFRESH TABLE search"))
# Query record.
query = session.query(SearchIndex.embedding) \
.filter(knn_match(SearchIndex.embedding, [42.42, 43.43, 41.41], 3))
result = query.first()
# Compare outcome.
assert np.array_equal(result.embedding, np.array([42.42, 43.43, 44.44], np.float32))

@amotl amotl closed this as completed Jun 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant