From 95d21182288469d81cdb73204dd39cc2dde7efc5 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Thu, 14 Nov 2024 10:13:37 +0000 Subject: [PATCH 1/3] Fix extracts but typing nested tuples caused by hash decorator Co-authored-by: Audrey Roy Greenfeld --- sqlite_minutils/db.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sqlite_minutils/db.py b/sqlite_minutils/db.py index 19d9a1e..ab46c86 100644 --- a/sqlite_minutils/db.py +++ b/sqlite_minutils/db.py @@ -3652,10 +3652,13 @@ def jsonify_if_needed(value): def resolve_extracts( - extracts: Optional[Union[Dict[str, str], List[str], Tuple[str]]] + extracts: Optional[Union[Dict[str, str], List[str], Tuple[str], Tuple[Tuple[str]]]] ) -> dict: if extracts is None: extracts = {} + if isinstance(extracts, (list, tuple)) and len(extracts) and any(isinstance(x, (list, tuple)) for x in extracts): + # Handles nested lists/tuples + extracts = dict(extracts) if isinstance(extracts, (list, tuple)): extracts = {item: item for item in extracts} return extracts From 03bceede99b1e14999a1a6f44948590204e9db84 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Thu, 14 Nov 2024 11:05:08 +0000 Subject: [PATCH 2/3] Remove unsupported argument option The ForeignKeyType and ForeignKeyIndicator do not include `dict` as a supported type. Co-Authored-By: Audrey Roy Greenfeld --- tests/test_create.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_create.py b/tests/test_create.py index c8dcef6..11b7d46 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -239,8 +239,6 @@ def test_create_table_column_order(fresh_db, use_table_factory): ((("one_id", "one", "id", "five"), ("two_id", "two", "id")), AssertionError), # Likewise a bad column: ((("one_id", "one", "id2"),), AlterError), - # Or a list of dicts - (({"one_id": "one"},), AssertionError), ), ) @pytest.mark.parametrize("use_table_factory", [True, False]) From 48667fabdd0f2691fd54de2ea2bca12c55032d12 Mon Sep 17 00:00:00 2001 From: Daniel Roy Greenfeld Date: Thu, 14 Nov 2024 11:59:51 +0000 Subject: [PATCH 3/3] Use transactions to constrain and rollback transform error Co-authored-by: Audrey Roy Greenfeld --- tests/test_transform.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/test_transform.py b/tests/test_transform.py index 4d7e411..93bed9c 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -1,5 +1,6 @@ from sqlite_minutils.db import ForeignKey from sqlite_minutils.utils import OperationalError +from sqlite3 import IntegrityError import pytest @@ -384,10 +385,16 @@ def test_transform_verify_foreign_keys(fresh_db): fresh_db["books"].insert( {"id": 1, "title": "Book", "author_id": 3}, pk="id", foreign_keys={"author_id"} ) - # Renaming the id column on authors should break everything - with pytest.raises(OperationalError) as e: + # Renaming the id column on authors should break everything with an IntegrityError + # The old error didn't match the Sqlite docs. + # We use a transaction to constrain and rollback the error. + fresh_db.begin() + try: fresh_db["authors"].transform(rename={"id": "id2"}) - assert e.value.args[0] == 'foreign key mismatch - "books" referencing "authors"' + fresh_db.commit() + except IntegrityError: + fresh_db.rollback() + # This should have rolled us back assert ( fresh_db["authors"].schema