mirror of https://github.com/apache/superset.git
Use test name for the custom macros testing: (#10695)
: Co-authored-by: bogdan kyryliuk <bogdankyryliuk@dropbox.com>
This commit is contained in:
parent
7abdf53944
commit
6ed36552e6
|
@ -334,22 +334,22 @@ class SupersetTestCase(TestCase):
|
||||||
if database:
|
if database:
|
||||||
db.session.delete(database)
|
db.session.delete(database)
|
||||||
|
|
||||||
def create_fake_presto_db(self):
|
def create_fake_db_for_macros(self):
|
||||||
self.login(username="admin")
|
self.login(username="admin")
|
||||||
database_name = "presto"
|
database_name = "db_for_macros_testing"
|
||||||
db_id = 200
|
db_id = 200
|
||||||
return self.get_or_create(
|
return self.get_or_create(
|
||||||
cls=models.Database,
|
cls=models.Database,
|
||||||
criteria={"database_name": database_name},
|
criteria={"database_name": database_name},
|
||||||
session=db.session,
|
session=db.session,
|
||||||
sqlalchemy_uri="presto://user@host:8080/hive",
|
sqlalchemy_uri="db_for_macros_testing://user@host:8080/hive",
|
||||||
id=db_id,
|
id=db_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
def delete_fake_presto_db(self):
|
def delete_fake_db_for_macros(self):
|
||||||
database = (
|
database = (
|
||||||
db.session.query(Database)
|
db.session.query(Database)
|
||||||
.filter(Database.database_name == "presto")
|
.filter(Database.database_name == "db_for_macros_testing")
|
||||||
.scalar()
|
.scalar()
|
||||||
)
|
)
|
||||||
if database:
|
if database:
|
||||||
|
|
|
@ -23,21 +23,18 @@ import html
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
from typing import Dict, List
|
||||||
from typing import Dict, List, Optional
|
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
import pytz
|
import pytz
|
||||||
import random
|
import random
|
||||||
import re
|
import re
|
||||||
import string
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest import mock, skipUnless
|
from unittest import mock, skipUnless
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import sqlalchemy as sqla
|
import sqlalchemy as sqla
|
||||||
|
|
||||||
from superset.utils.core import get_example_database
|
|
||||||
from tests.test_app import app # isort:skip
|
from tests.test_app import app # isort:skip
|
||||||
import superset.views.utils
|
import superset.views.utils
|
||||||
from superset import (
|
from superset import (
|
||||||
|
@ -49,7 +46,6 @@ from superset import (
|
||||||
is_feature_enabled,
|
is_feature_enabled,
|
||||||
)
|
)
|
||||||
from superset.connectors.sqla.models import SqlaTable
|
from superset.connectors.sqla.models import SqlaTable
|
||||||
from superset.datasets.dao import DatasetDAO
|
|
||||||
from superset.db_engine_specs.base import BaseEngineSpec
|
from superset.db_engine_specs.base import BaseEngineSpec
|
||||||
from superset.db_engine_specs.mssql import MssqlEngineSpec
|
from superset.db_engine_specs.mssql import MssqlEngineSpec
|
||||||
from superset.models import core as models
|
from superset.models import core as models
|
||||||
|
@ -702,7 +698,7 @@ class TestCore(SupersetTestCase):
|
||||||
"""Test macro defined in custom template processor works."""
|
"""Test macro defined in custom template processor works."""
|
||||||
mock_dt.utcnow = mock.Mock(return_value=datetime.datetime(1970, 1, 1))
|
mock_dt.utcnow = mock.Mock(return_value=datetime.datetime(1970, 1, 1))
|
||||||
db = mock.Mock()
|
db = mock.Mock()
|
||||||
db.backend = "presto"
|
db.backend = "db_for_macros_testing"
|
||||||
tp = jinja_context.get_template_processor(database=db)
|
tp = jinja_context.get_template_processor(database=db)
|
||||||
|
|
||||||
sql = "SELECT '$DATE()'"
|
sql = "SELECT '$DATE()'"
|
||||||
|
@ -717,7 +713,7 @@ class TestCore(SupersetTestCase):
|
||||||
"""Test macro passed as kwargs when getting template processor
|
"""Test macro passed as kwargs when getting template processor
|
||||||
works in custom template processor."""
|
works in custom template processor."""
|
||||||
db = mock.Mock()
|
db = mock.Mock()
|
||||||
db.backend = "presto"
|
db.backend = "db_for_macros_testing"
|
||||||
s = "$foo()"
|
s = "$foo()"
|
||||||
tp = jinja_context.get_template_processor(database=db, foo=lambda: "bar")
|
tp = jinja_context.get_template_processor(database=db, foo=lambda: "bar")
|
||||||
rendered = tp.process_template(s)
|
rendered = tp.process_template(s)
|
||||||
|
@ -727,7 +723,7 @@ class TestCore(SupersetTestCase):
|
||||||
"""Test macro passed as kwargs when processing template
|
"""Test macro passed as kwargs when processing template
|
||||||
works in custom template processor."""
|
works in custom template processor."""
|
||||||
db = mock.Mock()
|
db = mock.Mock()
|
||||||
db.backend = "presto"
|
db.backend = "db_for_macros_testing"
|
||||||
s = "$foo()"
|
s = "$foo()"
|
||||||
tp = jinja_context.get_template_processor(database=db)
|
tp = jinja_context.get_template_processor(database=db)
|
||||||
rendered = tp.process_template(s, foo=lambda: "bar")
|
rendered = tp.process_template(s, foo=lambda: "bar")
|
||||||
|
@ -736,7 +732,7 @@ class TestCore(SupersetTestCase):
|
||||||
def test_custom_template_processors_overwrite(self) -> None:
|
def test_custom_template_processors_overwrite(self) -> None:
|
||||||
"""Test template processor for presto gets overwritten by custom one."""
|
"""Test template processor for presto gets overwritten by custom one."""
|
||||||
db = mock.Mock()
|
db = mock.Mock()
|
||||||
db.backend = "presto"
|
db.backend = "db_for_macros_testing"
|
||||||
tp = jinja_context.get_template_processor(database=db)
|
tp = jinja_context.get_template_processor(database=db)
|
||||||
|
|
||||||
sql = "SELECT '{{ datetime(2017, 1, 1).isoformat() }}'"
|
sql = "SELECT '{{ datetime(2017, 1, 1).isoformat() }}'"
|
||||||
|
@ -751,11 +747,7 @@ class TestCore(SupersetTestCase):
|
||||||
"""Test custom template processor is ignored for a difference backend
|
"""Test custom template processor is ignored for a difference backend
|
||||||
database."""
|
database."""
|
||||||
maindb = utils.get_example_database()
|
maindb = utils.get_example_database()
|
||||||
sql = (
|
sql = "SELECT '$DATE()'"
|
||||||
"SELECT '$DATE()'"
|
|
||||||
if maindb.backend != "presto"
|
|
||||||
else f"SELECT '{datetime.date.today().isoformat()}'"
|
|
||||||
)
|
|
||||||
tp = jinja_context.get_template_processor(database=maindb)
|
tp = jinja_context.get_template_processor(database=maindb)
|
||||||
rendered = tp.process_template(sql)
|
rendered = tp.process_template(sql)
|
||||||
assert sql == rendered
|
assert sql == rendered
|
||||||
|
@ -774,7 +766,7 @@ class TestCore(SupersetTestCase):
|
||||||
}
|
}
|
||||||
sql_lab_mock.return_value = resp
|
sql_lab_mock.return_value = resp
|
||||||
|
|
||||||
dbobj = self.create_fake_presto_db()
|
dbobj = self.create_fake_db_for_macros()
|
||||||
json_payload = dict(database_id=dbobj.id, sql=sql)
|
json_payload = dict(database_id=dbobj.id, sql=sql)
|
||||||
self.get_json_resp(
|
self.get_json_resp(
|
||||||
"/superset/sql_json/", raise_on_error=False, json_=json_payload
|
"/superset/sql_json/", raise_on_error=False, json_=json_payload
|
||||||
|
@ -782,7 +774,7 @@ class TestCore(SupersetTestCase):
|
||||||
assert sql_lab_mock.called
|
assert sql_lab_mock.called
|
||||||
self.assertEqual(sql_lab_mock.call_args[0][1], "SELECT '1970-01-01' as test")
|
self.assertEqual(sql_lab_mock.call_args[0][1], "SELECT '1970-01-01' as test")
|
||||||
|
|
||||||
self.delete_fake_presto_db()
|
self.delete_fake_db_for_macros()
|
||||||
|
|
||||||
def test_fetch_datasource_metadata(self):
|
def test_fetch_datasource_metadata(self):
|
||||||
self.login(username="admin")
|
self.login(username="admin")
|
||||||
|
|
|
@ -95,11 +95,7 @@ class TestDatabaseModel(SupersetTestCase):
|
||||||
query_obj = dict(**base_query_obj, extras={})
|
query_obj = dict(**base_query_obj, extras={})
|
||||||
extra_cache_keys = table.get_extra_cache_keys(query_obj)
|
extra_cache_keys = table.get_extra_cache_keys(query_obj)
|
||||||
self.assertTrue(table.has_extra_cache_key_calls(query_obj))
|
self.assertTrue(table.has_extra_cache_key_calls(query_obj))
|
||||||
# TODO(bkyryliuk): make it work with presto
|
assert extra_cache_keys == ["abc"]
|
||||||
if get_example_database().backend == "presto":
|
|
||||||
assert extra_cache_keys == []
|
|
||||||
else:
|
|
||||||
assert extra_cache_keys == ["abc"]
|
|
||||||
|
|
||||||
# Table with Jinja callable disabled.
|
# Table with Jinja callable disabled.
|
||||||
table = SqlaTable(
|
table = SqlaTable(
|
||||||
|
@ -131,11 +127,7 @@ class TestDatabaseModel(SupersetTestCase):
|
||||||
)
|
)
|
||||||
extra_cache_keys = table.get_extra_cache_keys(query_obj)
|
extra_cache_keys = table.get_extra_cache_keys(query_obj)
|
||||||
self.assertTrue(table.has_extra_cache_key_calls(query_obj))
|
self.assertTrue(table.has_extra_cache_key_calls(query_obj))
|
||||||
# TODO(bkyryliuk): make it work with presto and hive
|
assert extra_cache_keys == ["abc"]
|
||||||
if get_example_database().backend == "presto":
|
|
||||||
assert extra_cache_keys == []
|
|
||||||
else:
|
|
||||||
assert extra_cache_keys == ["abc"]
|
|
||||||
|
|
||||||
def test_where_operators(self):
|
def test_where_operators(self):
|
||||||
class FilterTestCase(NamedTuple):
|
class FilterTestCase(NamedTuple):
|
||||||
|
|
|
@ -35,7 +35,7 @@ def DATE(
|
||||||
class CustomPrestoTemplateProcessor(PrestoTemplateProcessor):
|
class CustomPrestoTemplateProcessor(PrestoTemplateProcessor):
|
||||||
"""A custom presto template processor for test."""
|
"""A custom presto template processor for test."""
|
||||||
|
|
||||||
engine = "presto"
|
engine = "db_for_macros_testing"
|
||||||
|
|
||||||
def process_template(self, sql: str, **kwargs) -> str:
|
def process_template(self, sql: str, **kwargs) -> str:
|
||||||
"""Processes a sql template with $ style macro using regex."""
|
"""Processes a sql template with $ style macro using regex."""
|
||||||
|
|
Loading…
Reference in New Issue