|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Unit tests for ``find_existing_for_import`` / |
| 18 | +``clear_soft_deleted_for_import``. |
| 19 | +
|
| 20 | +These pin the side-effect-free / side-effect split that addresses |
| 21 | +Richard's review on PR #39977: ``find`` returns soft-deleted matches |
| 22 | +as-is so the importer can decide overwrite/permission before |
| 23 | +``clear_soft_deleted_for_import`` performs the destructive op. |
| 24 | +""" |
| 25 | + |
| 26 | +from __future__ import annotations |
| 27 | + |
| 28 | +from collections.abc import Generator |
| 29 | + |
| 30 | +import pytest |
| 31 | +from sqlalchemy import Column, Integer, String |
| 32 | +from sqlalchemy.orm import declarative_base |
| 33 | +from sqlalchemy.orm.session import Session |
| 34 | +from sqlalchemy_utils import UUIDType |
| 35 | + |
| 36 | +from superset.commands.importers.v1.utils import ( |
| 37 | + clear_soft_deleted_for_import, |
| 38 | + find_existing_for_import, |
| 39 | +) |
| 40 | +from superset.models.helpers import SoftDeleteMixin |
| 41 | + |
| 42 | +_TestBase = declarative_base() |
| 43 | + |
| 44 | + |
| 45 | +class _ImportableSoftDeletable( # type: ignore[misc, valid-type] |
| 46 | + SoftDeleteMixin, _TestBase |
| 47 | +): |
| 48 | + __tablename__ = "_importable_soft_deletable_test" |
| 49 | + id = Column(Integer, primary_key=True) |
| 50 | + name = Column(String, nullable=False) |
| 51 | + uuid = Column(UUIDType(binary=False), unique=True) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def _synthetic_table(session: Session) -> Generator[None, None, None]: |
| 56 | + _ImportableSoftDeletable.metadata.create_all(session.get_bind()) |
| 57 | + yield |
| 58 | + _ImportableSoftDeletable.metadata.drop_all(session.get_bind()) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.usefixtures("_synthetic_table") |
| 62 | +def test_find_returns_none_when_no_match(app_context: None, session: Session) -> None: |
| 63 | + """No row with this UUID — function returns ``None``.""" |
| 64 | + result = find_existing_for_import( |
| 65 | + _ImportableSoftDeletable, |
| 66 | + "00000000-0000-0000-0000-000000000000", |
| 67 | + ) |
| 68 | + assert result is None |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.usefixtures("_synthetic_table") |
| 72 | +def test_find_returns_live_row_as_is( |
| 73 | + app_context: None, session: Session, mocker: object |
| 74 | +) -> None: |
| 75 | + """A live (not soft-deleted) row is returned as the existing row |
| 76 | + so the caller can treat it as an overwrite target.""" |
| 77 | + import uuid as uuid_lib |
| 78 | + |
| 79 | + obj_uuid = uuid_lib.uuid4() |
| 80 | + obj = _ImportableSoftDeletable(name="live", uuid=obj_uuid) |
| 81 | + session.add(obj) |
| 82 | + session.flush() |
| 83 | + |
| 84 | + result = find_existing_for_import(_ImportableSoftDeletable, str(obj_uuid)) |
| 85 | + assert result is not None |
| 86 | + assert result.deleted_at is None |
| 87 | + assert result.name == "live" |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.usefixtures("_synthetic_table") |
| 91 | +def test_find_returns_soft_deleted_row_as_is( |
| 92 | + app_context: None, session: Session |
| 93 | +) -> None: |
| 94 | + """A soft-deleted row is returned *with its ``deleted_at`` set*. |
| 95 | + Caller is responsible for deciding what to do with it — typically |
| 96 | + calling ``clear_soft_deleted_for_import`` after a permission |
| 97 | + check. The function does NOT hard-delete as a side effect. |
| 98 | + """ |
| 99 | + import uuid as uuid_lib |
| 100 | + |
| 101 | + obj_uuid = uuid_lib.uuid4() |
| 102 | + obj = _ImportableSoftDeletable(name="deleted_target", uuid=obj_uuid) |
| 103 | + session.add(obj) |
| 104 | + session.flush() |
| 105 | + obj.soft_delete() |
| 106 | + session.flush() |
| 107 | + |
| 108 | + result = find_existing_for_import(_ImportableSoftDeletable, str(obj_uuid)) |
| 109 | + assert result is not None |
| 110 | + assert result.deleted_at is not None |
| 111 | + assert result.name == "deleted_target" |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.usefixtures("_synthetic_table") |
| 115 | +def test_clear_hard_deletes_the_row(app_context: None, session: Session) -> None: |
| 116 | + """``clear_soft_deleted_for_import`` calls |
| 117 | + ``db.session.delete(existing)`` so the row is permanently removed |
| 118 | + (not just re-soft-deleted) and ORM ``after_delete`` listeners fire. |
| 119 | + A subsequent insert with the same UUID would not collide. |
| 120 | + """ |
| 121 | + import uuid as uuid_lib |
| 122 | + |
| 123 | + obj_uuid = uuid_lib.uuid4() |
| 124 | + obj = _ImportableSoftDeletable(name="to_clear", uuid=obj_uuid) |
| 125 | + session.add(obj) |
| 126 | + session.flush() |
| 127 | + obj.soft_delete() |
| 128 | + session.flush() |
| 129 | + obj_id = obj.id |
| 130 | + |
| 131 | + clear_soft_deleted_for_import(obj) |
| 132 | + |
| 133 | + # Row is gone — bypass-aware lookup returns None. |
| 134 | + found = find_existing_for_import(_ImportableSoftDeletable, str(obj_uuid)) |
| 135 | + assert found is None |
| 136 | + # And session.query confirms it's hard-deleted, not just soft. |
| 137 | + from superset.models.helpers import SKIP_VISIBILITY_FILTER_CLASSES |
| 138 | + |
| 139 | + raw = ( |
| 140 | + session.query(_ImportableSoftDeletable) |
| 141 | + .execution_options( |
| 142 | + **{SKIP_VISIBILITY_FILTER_CLASSES: {_ImportableSoftDeletable}} |
| 143 | + ) |
| 144 | + .filter_by(id=obj_id) |
| 145 | + .one_or_none() |
| 146 | + ) |
| 147 | + assert raw is None |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.usefixtures("_synthetic_table") |
| 151 | +def test_find_then_clear_is_the_intended_caller_sequence( |
| 152 | + app_context: None, session: Session |
| 153 | +) -> None: |
| 154 | + """The two functions compose as the documented contract: find |
| 155 | + first, decide based on the returned row, then clear if needed. |
| 156 | + Pinning this sequence ensures a refactor doesn't reverse the |
| 157 | + intent (find side-effects, clear pure).""" |
| 158 | + import uuid as uuid_lib |
| 159 | + |
| 160 | + obj_uuid = uuid_lib.uuid4() |
| 161 | + obj = _ImportableSoftDeletable(name="reimport_target", uuid=obj_uuid) |
| 162 | + session.add(obj) |
| 163 | + session.flush() |
| 164 | + obj.soft_delete() |
| 165 | + session.flush() |
| 166 | + |
| 167 | + # Step 1: find (pure). Caller now has the row to make decisions on. |
| 168 | + existing = find_existing_for_import(_ImportableSoftDeletable, str(obj_uuid)) |
| 169 | + assert existing is not None |
| 170 | + assert existing.deleted_at is not None |
| 171 | + |
| 172 | + # Step 2: caller decides to clear (in real code, after overwrite + |
| 173 | + # permission checks pass). |
| 174 | + clear_soft_deleted_for_import(existing) |
| 175 | + |
| 176 | + # Step 3: caller can now re-insert with the same UUID without |
| 177 | + # colliding. |
| 178 | + fresh = _ImportableSoftDeletable(name="fresh", uuid=obj_uuid) |
| 179 | + session.add(fresh) |
| 180 | + session.flush() |
| 181 | + assert fresh.id is not None |
| 182 | + assert fresh.deleted_at is None |
0 commit comments