Skip to content

Commit

Permalink
Use test name for the custom macros testing: (apache#10695)
Browse files Browse the repository at this point in the history
:

Co-authored-by: bogdan kyryliuk <[email protected]>
  • Loading branch information
bkyryliuk and bogdan-dbx authored Aug 27, 2020
1 parent 7abdf53 commit 6ed3655
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 32 deletions.
10 changes: 5 additions & 5 deletions tests/base_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,22 +334,22 @@ def delete_fake_db(self):
if database:
db.session.delete(database)

def create_fake_presto_db(self):
def create_fake_db_for_macros(self):
self.login(username="admin")
database_name = "presto"
database_name = "db_for_macros_testing"
db_id = 200
return self.get_or_create(
cls=models.Database,
criteria={"database_name": database_name},
session=db.session,
sqlalchemy_uri="presto://user@host:8080/hive",
sqlalchemy_uri="db_for_macros_testing://user@host:8080/hive",
id=db_id,
)

def delete_fake_presto_db(self):
def delete_fake_db_for_macros(self):
database = (
db.session.query(Database)
.filter(Database.database_name == "presto")
.filter(Database.database_name == "db_for_macros_testing")
.scalar()
)
if database:
Expand Down
24 changes: 8 additions & 16 deletions tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@
import io
import json
import logging
import os
from typing import Dict, List, Optional
from typing import Dict, List
from urllib.parse import quote

import pytz
import random
import re
import string
import unittest
from unittest import mock, skipUnless

import pandas as pd
import sqlalchemy as sqla

from superset.utils.core import get_example_database
from tests.test_app import app # isort:skip
import superset.views.utils
from superset import (
Expand All @@ -49,7 +46,6 @@
is_feature_enabled,
)
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.mssql import MssqlEngineSpec
from superset.models import core as models
Expand Down Expand Up @@ -702,7 +698,7 @@ def test_custom_process_template(self, mock_dt) -> None:
"""Test macro defined in custom template processor works."""
mock_dt.utcnow = mock.Mock(return_value=datetime.datetime(1970, 1, 1))
db = mock.Mock()
db.backend = "presto"
db.backend = "db_for_macros_testing"
tp = jinja_context.get_template_processor(database=db)

sql = "SELECT '$DATE()'"
Expand All @@ -717,7 +713,7 @@ def test_custom_get_template_kwarg(self):
"""Test macro passed as kwargs when getting template processor
works in custom template processor."""
db = mock.Mock()
db.backend = "presto"
db.backend = "db_for_macros_testing"
s = "$foo()"
tp = jinja_context.get_template_processor(database=db, foo=lambda: "bar")
rendered = tp.process_template(s)
Expand All @@ -727,7 +723,7 @@ def test_custom_template_kwarg(self) -> None:
"""Test macro passed as kwargs when processing template
works in custom template processor."""
db = mock.Mock()
db.backend = "presto"
db.backend = "db_for_macros_testing"
s = "$foo()"
tp = jinja_context.get_template_processor(database=db)
rendered = tp.process_template(s, foo=lambda: "bar")
Expand All @@ -736,7 +732,7 @@ def test_custom_template_kwarg(self) -> None:
def test_custom_template_processors_overwrite(self) -> None:
"""Test template processor for presto gets overwritten by custom one."""
db = mock.Mock()
db.backend = "presto"
db.backend = "db_for_macros_testing"
tp = jinja_context.get_template_processor(database=db)

sql = "SELECT '{{ datetime(2017, 1, 1).isoformat() }}'"
Expand All @@ -751,11 +747,7 @@ def test_custom_template_processors_ignored(self) -> None:
"""Test custom template processor is ignored for a difference backend
database."""
maindb = utils.get_example_database()
sql = (
"SELECT '$DATE()'"
if maindb.backend != "presto"
else f"SELECT '{datetime.date.today().isoformat()}'"
)
sql = "SELECT '$DATE()'"
tp = jinja_context.get_template_processor(database=maindb)
rendered = tp.process_template(sql)
assert sql == rendered
Expand All @@ -774,15 +766,15 @@ def test_custom_templated_sql_json(self, sql_lab_mock, mock_dt) -> None:
}
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)
self.get_json_resp(
"/superset/sql_json/", raise_on_error=False, json_=json_payload
)
assert sql_lab_mock.called
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):
self.login(username="admin")
Expand Down
12 changes: 2 additions & 10 deletions tests/sqla_models_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ def test_extra_cache_keys(self, flask_g):
query_obj = dict(**base_query_obj, extras={})
extra_cache_keys = table.get_extra_cache_keys(query_obj)
self.assertTrue(table.has_extra_cache_key_calls(query_obj))
# TODO(bkyryliuk): make it work with presto
if get_example_database().backend == "presto":
assert extra_cache_keys == []
else:
assert extra_cache_keys == ["abc"]
assert extra_cache_keys == ["abc"]

# Table with Jinja callable disabled.
table = SqlaTable(
Expand Down Expand Up @@ -131,11 +127,7 @@ def test_extra_cache_keys(self, flask_g):
)
extra_cache_keys = table.get_extra_cache_keys(query_obj)
self.assertTrue(table.has_extra_cache_key_calls(query_obj))
# TODO(bkyryliuk): make it work with presto and hive
if get_example_database().backend == "presto":
assert extra_cache_keys == []
else:
assert extra_cache_keys == ["abc"]
assert extra_cache_keys == ["abc"]

def test_where_operators(self):
class FilterTestCase(NamedTuple):
Expand Down
2 changes: 1 addition & 1 deletion tests/superset_test_custom_template_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def DATE(
class CustomPrestoTemplateProcessor(PrestoTemplateProcessor):
"""A custom presto template processor for test."""

engine = "presto"
engine = "db_for_macros_testing"

def process_template(self, sql: str, **kwargs) -> str:
"""Processes a sql template with $ style macro using regex."""
Expand Down

0 comments on commit 6ed3655

Please sign in to comment.