villebro commented on a change in pull request #17882: URL: https://github.com/apache/superset/pull/17882#discussion_r780321962
########## File path: superset/key_value/commands/update.py ########## @@ -16,30 +16,25 @@ # under the License. import logging from abc import ABC, abstractmethod -from typing import Optional -from flask_appbuilder.models.sqla import Model -from flask_appbuilder.security.sqla.models import User from sqlalchemy.exc import SQLAlchemyError from superset.commands.base import BaseCommand from superset.key_value.commands.exceptions import KeyValueUpdateFailedError +from superset.key_value.commands.parameters import CommandParameters logger = logging.getLogger(__name__) class UpdateKeyValueCommand(BaseCommand, ABC): def __init__( - self, actor: User, resource_id: int, key: str, value: str, + self, cmd_params: CommandParameters, ): - self._actor = actor - self._resource_id = resource_id - self._key = key - self._value = value + self._parameters = cmd_params Review comment: Could we have the same variable name here? Either `self._cmd_params = cmd_params` or rename in the sig to get `self._parameters = parameters` ########## File path: superset/charts/form_data/commands/get.py ########## @@ -0,0 +1,43 @@ +# 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. +from typing import Optional + +from flask import current_app as app + +from superset.charts.form_data.utils import check_access +from superset.extensions import cache_manager +from superset.key_value.commands.entry import Entry +from superset.key_value.commands.get import GetKeyValueCommand +from superset.key_value.commands.parameters import CommandParameters +from superset.key_value.utils import cache_key + + +class GetFormDataCommand(GetKeyValueCommand): + def get(self, cmd_params: CommandParameters) -> Optional[str]: + resource_id = cmd_params["resource_id"] + key = cmd_params["key"] + config = app.config["CHART_FORM_DATA_CACHE_CONFIG"] Review comment: nit: Should we move this to a class or object property so we don't have to check this every time on the get? Feels like this should be referenced only once. ########## File path: tests/integration_tests/charts/form_data/api_tests.py ########## @@ -0,0 +1,173 @@ +# 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. +import json + +import pytest +from flask_appbuilder.security.sqla.models import User +from sqlalchemy.orm import Session + +from superset.extensions import cache_manager +from superset.key_value.commands.entry import Entry +from superset.key_value.utils import cache_key +from superset.models.slice import Slice +from tests.integration_tests.base_tests import login +from tests.integration_tests.fixtures.world_bank_dashboard import ( + load_world_bank_dashboard_with_slices, + load_world_bank_data, +) +from tests.integration_tests.test_app import app + +key = "test-key" +value = "test" + + +@pytest.fixture +def client(): + with app.test_client() as client: + with app.app_context(): + yield client + + +@pytest.fixture +def chart_id(load_world_bank_dashboard_with_slices) -> int: + with app.app_context() as ctx: + session: Session = ctx.app.appbuilder.get_session + chart = session.query(Slice).filter_by(slice_name="World's Population").one() + return chart.id + + +@pytest.fixture +def admin_id() -> int: + with app.app_context() as ctx: + session: Session = ctx.app.appbuilder.get_session + admin = session.query(User).filter_by(username="admin").one() + return admin.id Review comment: Same here ########## File path: superset/key_value/commands/parameters.py ########## @@ -0,0 +1,28 @@ +# 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. +from typing import Dict + +from flask_appbuilder.security.sqla.models import User +from typing_extensions import TypedDict + + +class CommandParameters(TypedDict, total=False): + actor: User + resource_id: int + key: str + value: str + query_params: Dict[str, str] Review comment: As we don't need to serialize/deserialize these to/from JSON, could we rather use a `@dataclass` here? https://docs.python.org/3/library/dataclasses.html This will make accessing properties more pythonic; `cmd_params.actor` vs `cmd_params["actor"]`. ########## File path: tests/integration_tests/charts/form_data/api_tests.py ########## @@ -0,0 +1,173 @@ +# 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. +import json + +import pytest +from flask_appbuilder.security.sqla.models import User +from sqlalchemy.orm import Session + +from superset.extensions import cache_manager +from superset.key_value.commands.entry import Entry +from superset.key_value.utils import cache_key +from superset.models.slice import Slice +from tests.integration_tests.base_tests import login +from tests.integration_tests.fixtures.world_bank_dashboard import ( + load_world_bank_dashboard_with_slices, + load_world_bank_data, +) +from tests.integration_tests.test_app import app + +key = "test-key" +value = "test" + + +@pytest.fixture +def client(): + with app.test_client() as client: + with app.app_context(): + yield client + + +@pytest.fixture +def chart_id(load_world_bank_dashboard_with_slices) -> int: + with app.app_context() as ctx: + session: Session = ctx.app.appbuilder.get_session + chart = session.query(Slice).filter_by(slice_name="World's Population").one() + return chart.id Review comment: Don' we need to make sure the world bank data is available by adding the decorator here? ``` @pytest.mark.usefixtures("load_world_bank_dashboard_with_slices") @pytest.fixture def chart_id(load_world_bank_dashboard_with_slices) -> int: ... ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org