Skip to content

Commit

Permalink
Refactor to use unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Jan 21, 2021
1 parent 5759363 commit 47214aa
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 59 deletions.
9 changes: 4 additions & 5 deletions superset/sql_lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from celery.task.base import Task
from flask_babel import lazy_gettext as _
from sqlalchemy.orm import Session
from werkzeug.local import LocalProxy

from superset import app, results_backend, results_backend_use_msgpack, security_manager
from superset.dataframe import df_to_records
Expand All @@ -37,7 +38,6 @@
from superset.models.core import Database
from superset.models.sql_lab import Query
from superset.result_set import SupersetResultSet
from superset.security.manager import SupersetSecurityManager
from superset.sql_parse import CtasMethod, ParsedQuery
from superset.utils.celery import session_scope
from superset.utils.core import (
Expand All @@ -47,15 +47,14 @@
zlib_compress,
)
from superset.utils.dates import now_as_float
from superset.utils.decorators import guard, stats_timing
from superset.utils.decorators import stats_timing


# pylint: disable=unused-argument, redefined-outer-name
@guard("W}V8JTUVzx4ur~{CEhT?")
def dummy_sql_query_mutator(
sql: str,
user_name: str,
security_manager: SupersetSecurityManager,
user_name: Optional[str],
security_manager: LocalProxy,
database: Database,
) -> str:
"""A no-op version of SQL_QUERY_MUTATOR"""
Expand Down
60 changes: 6 additions & 54 deletions superset/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,13 @@
# under the License.
import time
import warnings
from base64 import b85encode
from hashlib import md5
from inspect import (
getmembers,
getsourcefile,
getsourcelines,
isclass,
isfunction,
isroutine,
signature,
)
from textwrap import indent
from typing import Any, Callable, Dict, Iterator, Union

from contextlib2 import contextmanager

from superset.stats_logger import BaseStatsLogger
from superset.utils.dates import now_as_float
from superset.utils.public_interfaces import compute_hash, get_warning_message


@contextmanager
Expand Down Expand Up @@ -84,52 +73,15 @@ def wrapped(*args: Any, **kwargs: Any) -> Any:
return decorate


def compute_hash(decorated: Callable[..., Any]) -> str:
if isfunction(decorated):
return compute_func_hash(decorated)

if isclass(decorated):
return compute_class_hash(decorated)

raise Exception(f"Invalid decorated object: {decorated}")


def compute_func_hash(function: Callable[..., Any]) -> str:
hashed = md5()
hashed.update(function.__name__.encode())
hashed.update(str(signature(function)).encode())
return b85encode(hashed.digest()).decode("utf-8")


def compute_class_hash(class_: Callable[..., Any]) -> str:
hashed = md5()
public_methods = {
method
for name, method in getmembers(class_, predicate=isroutine)
if not name.startswith("_") or name == "__init__"
}
for method in public_methods:
hashed.update(method.__name__.encode())
hashed.update(str(signature(method)).encode())
return b85encode(hashed.digest()).decode("utf-8")


def guard(given_hash: str) -> Callable[..., Any]:
"""
Decorate a public function or class to detect changes.
"""

def wrapper(decorated: Callable[..., Any]) -> Callable[..., Any]:
expected_hash = compute_hash(decorated)
if given_hash != expected_hash:
sourcefile = getsourcefile(decorated)
sourcelines = getsourcelines(decorated)
code = indent("".join(sourcelines[0]), " ")
lineno = sourcelines[1]
warnings.warn(
f"The decorated object `{decorated.__name__}` (in {sourcefile} "
f"line {lineno}) has a public interface which has currently been "
"modified. This MUST only be released in a new major version of "
"Superset according to SIP-57. To remove this warning message "
f"update the hash in the `guard` decorator to '{expected_hash}'."
f"\n\n{code}"
)
warnings.warn(get_warning_message(decorated, expected_hash))

def inner(*args: Any, **kwargs: Any) -> Any:
return decorated(*args, **kwargs)
Expand Down
73 changes: 73 additions & 0 deletions superset/utils/public_interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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 base64 import b85encode
from hashlib import md5
from inspect import (
getmembers,
getsourcefile,
getsourcelines,
isclass,
isfunction,
isroutine,
signature,
)
from textwrap import indent
from typing import Any, Callable


def compute_hash(decorated: Callable[..., Any]) -> str:
if isfunction(decorated):
return compute_func_hash(decorated)

if isclass(decorated):
return compute_class_hash(decorated)

raise Exception(f"Invalid decorated object: {decorated}")


def compute_func_hash(function: Callable[..., Any]) -> str:
hashed = md5()
hashed.update(function.__name__.encode())
hashed.update(str(signature(function)).encode())
return b85encode(hashed.digest()).decode("utf-8")


def compute_class_hash(class_: Callable[..., Any]) -> str:
hashed = md5()
public_methods = {
method
for name, method in getmembers(class_, predicate=isroutine)
if not name.startswith("_") or name == "__init__"
}
for method in public_methods:
hashed.update(method.__name__.encode())
hashed.update(str(signature(method)).encode())
return b85encode(hashed.digest()).decode("utf-8")


def get_warning_message(decorated: Callable[..., Any], expected_hash: str) -> str:
sourcefile = getsourcefile(decorated)
sourcelines = getsourcelines(decorated)
code = indent("".join(sourcelines[0]), " ")
lineno = sourcelines[1]
return (
f"The decorated object `{decorated.__name__}` (in {sourcefile} "
f"line {lineno}) has a public interface which has currently been "
"modified. This MUST only be released in a new major version of "
"Superset according to SIP-57. To remove this warning message "
f"update the associated hash to '{expected_hash}'.\n\n{code}"
)
39 changes: 39 additions & 0 deletions tests/utils/public_interfaces_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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.
# pylint: disable=no-self-use
from superset.sql_lab import dummy_sql_query_mutator
from superset.utils.public_interfaces import compute_hash, get_warning_message
from tests.base_tests import SupersetTestCase

# These are public interfaces exposed by Superset. Make sure
# to only change the interfaces and update the hashes in new
# major versions of Superset.
hashes = {
dummy_sql_query_mutator: "?1Y~;3l_|ss3^<`P;lWt",
}


class PublicInterfacesTest(SupersetTestCase):

"""Test that public interfaces have not been accidentally changed."""

def test_dummy_sql_query_mutator(self):
for interface, expected_hash in hashes.items():
current_hash = compute_hash(dummy_sql_query_mutator)
assert current_hash == expected_hash, get_warning_message(
dummy_sql_query_mutator, current_hash
)

0 comments on commit 47214aa

Please sign in to comment.