From fa35109bf2a416462e3c83f527aa82c20b67818d Mon Sep 17 00:00:00 2001 From: Daniel Vaz Gaspar Date: Wed, 30 Mar 2022 12:46:42 +0100 Subject: [PATCH] fix: add missing init on python pkg key_value (#19428) * fix: add missing init on python pkg key_value * fix lint issues * fix lint issues --- superset/dashboards/permalink/commands/create.py | 2 ++ superset/explore/permalink/commands/create.py | 2 ++ superset/key_value/commands/__init__.py | 16 ++++++++++++++++ superset/key_value/commands/create.py | 1 + superset/key_value/commands/update.py | 1 + superset/key_value/commands/upsert.py | 16 ++++++++-------- .../key_value/commands/update_test.py | 2 ++ .../key_value/commands/upsert_test.py | 3 +++ 8 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 superset/key_value/commands/__init__.py diff --git a/superset/dashboards/permalink/commands/create.py b/superset/dashboards/permalink/commands/create.py index 27ddf0534da8..4ffd41104ea0 100644 --- a/superset/dashboards/permalink/commands/create.py +++ b/superset/dashboards/permalink/commands/create.py @@ -53,6 +53,8 @@ def run(self) -> str: resource=self.resource, value=value, ).run() + if key.id is None: + raise DashboardPermalinkCreateFailedError("Unexpected missing key id") return encode_permalink_key(key=key.id, salt=self.salt) except SQLAlchemyError as ex: logger.exception("Error running create command") diff --git a/superset/explore/permalink/commands/create.py b/superset/explore/permalink/commands/create.py index 55fb0820cda0..c09ca3b37212 100644 --- a/superset/explore/permalink/commands/create.py +++ b/superset/explore/permalink/commands/create.py @@ -53,6 +53,8 @@ def run(self) -> str: value=value, ) key = command.run() + if key.id is None: + raise ExplorePermalinkCreateFailedError("Unexpected missing key id") return encode_permalink_key(key=key.id, salt=self.salt) except SQLAlchemyError as ex: logger.exception("Error running create command") diff --git a/superset/key_value/commands/__init__.py b/superset/key_value/commands/__init__.py new file mode 100644 index 000000000000..13a83393a912 --- /dev/null +++ b/superset/key_value/commands/__init__.py @@ -0,0 +1,16 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. diff --git a/superset/key_value/commands/create.py b/superset/key_value/commands/create.py index 613fabcdeb1f..5125ce7b01e2 100644 --- a/superset/key_value/commands/create.py +++ b/superset/key_value/commands/create.py @@ -39,6 +39,7 @@ class CreateKeyValueCommand(BaseCommand): key: Optional[Union[int, UUID]] expires_on: Optional[datetime] + # pylint: disable=too-many-arguments def __init__( self, resource: KeyValueResource, diff --git a/superset/key_value/commands/update.py b/superset/key_value/commands/update.py index 7333b48c5cc3..48fd8daa8a45 100644 --- a/superset/key_value/commands/update.py +++ b/superset/key_value/commands/update.py @@ -41,6 +41,7 @@ class UpdateKeyValueCommand(BaseCommand): key: Union[int, UUID] expires_on: Optional[datetime] + # pylint: disable=too-many-argumentsåå def __init__( self, resource: KeyValueResource, diff --git a/superset/key_value/commands/upsert.py b/superset/key_value/commands/upsert.py index aa495f7cc77c..8fd0bd240f2b 100644 --- a/superset/key_value/commands/upsert.py +++ b/superset/key_value/commands/upsert.py @@ -42,6 +42,7 @@ class UpsertKeyValueCommand(BaseCommand): key: Union[int, UUID] expires_on: Optional[datetime] + # pylint: disable=too-many-arguments def __init__( self, resource: KeyValueResource, @@ -96,11 +97,10 @@ def upsert(self) -> Optional[Key]: db.session.merge(entry) db.session.commit() return Key(entry.id, entry.uuid) - else: - return CreateKeyValueCommand( - resource=self.resource, - value=self.value, - actor=self.actor, - key=self.key, - expires_on=self.expires_on, - ).run() + return CreateKeyValueCommand( + resource=self.resource, + value=self.value, + actor=self.actor, + key=self.key, + expires_on=self.expires_on, + ).run() diff --git a/tests/integration_tests/key_value/commands/update_test.py b/tests/integration_tests/key_value/commands/update_test.py index 62a8126ba2ac..3b24ecdf0a30 100644 --- a/tests/integration_tests/key_value/commands/update_test.py +++ b/tests/integration_tests/key_value/commands/update_test.py @@ -53,6 +53,7 @@ def test_update_id_entry( key=ID_KEY, value=NEW_VALUE, ).run() + assert key is not None assert key.id == ID_KEY entry = db.session.query(KeyValueEntry).filter_by(id=ID_KEY).autoflush(False).one() assert pickle.loads(entry.value) == NEW_VALUE @@ -73,6 +74,7 @@ def test_update_uuid_entry( key=UUID_KEY, value=NEW_VALUE, ).run() + assert key is not None assert key.uuid == UUID_KEY entry = ( db.session.query(KeyValueEntry).filter_by(uuid=UUID_KEY).autoflush(False).one() diff --git a/tests/integration_tests/key_value/commands/upsert_test.py b/tests/integration_tests/key_value/commands/upsert_test.py index adb652e66a19..1970a1fc2c29 100644 --- a/tests/integration_tests/key_value/commands/upsert_test.py +++ b/tests/integration_tests/key_value/commands/upsert_test.py @@ -53,6 +53,7 @@ def test_upsert_id_entry( key=ID_KEY, value=NEW_VALUE, ).run() + assert key is not None assert key.id == ID_KEY entry = ( db.session.query(KeyValueEntry).filter_by(id=int(ID_KEY)).autoflush(False).one() @@ -75,6 +76,7 @@ def test_upsert_uuid_entry( key=UUID_KEY, value=NEW_VALUE, ).run() + assert key is not None assert key.uuid == UUID_KEY entry = ( db.session.query(KeyValueEntry).filter_by(uuid=UUID_KEY).autoflush(False).one() @@ -93,6 +95,7 @@ def test_upsert_missing_entry(app_context: AppContext, admin: User) -> None: key=456, value=NEW_VALUE, ).run() + assert key is not None assert key.id == 456 db.session.query(KeyValueEntry).filter_by(id=456).delete() db.session.commit()