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

Tests: Test composite synthetic uniqueness constraints #147

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions backlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ From dialect split-off.
- https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.Connection.execution_options.params.stream_results
- https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.Result.yield_per
- https://github.com/jamescasbon/vertica-sqlalchemy
- pandas: `index=True`
- @cratedb: test_table_kwargs_unknown => ColumnUnknownException[Column bazqux unknown]
3 changes: 3 additions & 0 deletions src/sqlalchemy_cratedb/support/polyfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def check_uniqueness_factory(sa_entity, *attribute_names):

This is used by CrateDB's MLflow adapter.

TODO: Maybe add to some helper function?
TODO: Maybe enable through a dialect parameter `crate_polyfill_unique` or such.
TODO: Maybe derive from the model definition itself?
__table_args__ = (sa.UniqueConstraint("name", "user_id", name="unique_name_user"),)
"""

# Synthesize a canonical "name" for the constraint,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_support_polyfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class FooBar(Base):
name = sa.Column(sa.String)

# Add synthetic UNIQUE constraint on `name` column.
listen(FooBar, "before_insert", check_uniqueness_factory(FooBar, "name"))
listen(FooBar, "before_insert", check_uniqueness_factory(FooBar, "id", "name"))

Base.metadata.drop_all(engine, checkfirst=True)
Base.metadata.create_all(engine, checkfirst=True)
Expand All @@ -86,11 +86,11 @@ class FooBar(Base):
session.execute(sa.text("REFRESH TABLE foobar"))

# Insert second record, violating the uniqueness constraint.
bar_item = FooBar(id="bar", name="foo")
bar_item = FooBar(id="foo", name="foo")
session.add(bar_item)
with pytest.raises(IntegrityError) as ex:
session.commit()
assert ex.match("DuplicateKeyException in table 'foobar' on constraint 'name'")
assert ex.match("DuplicateKeyException in table 'foobar' on constraint 'id-name'")


@pytest.mark.skipif(SA_VERSION < SA_1_4, reason="Feature not supported on SQLAlchemy 1.3 and earlier")
Expand Down