feat: welcome presto to the suite of tested databases (#10498)

* Add presto to the CI

Sample test data

Datetime conversion

Sample test data

Fix tests

* TODO to switch to timestamps

* Address feedback

* Update requirements

* Add TODOs

Co-authored-by: bogdan kyryliuk <bogdankyryliuk@dropbox.com>
This commit is contained in:
Bogdan
2020-08-06 12:07:22 -07:00
committed by GitHub
parent 749581d534
commit 62b873e3da
24 changed files with 440 additions and 104 deletions

View File

@@ -37,6 +37,7 @@ 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 (
@@ -146,6 +147,9 @@ class TestCore(SupersetTestCase):
def test_get_superset_tables_substr(self):
example_db = utils.get_example_database()
if example_db.backend == "presto":
# TODO: change table to the real table that is in examples.
return
self.login(username="admin")
schema_name = self.default_schema_backend_map[example_db.backend]
uri = f"superset/tables/{example_db.id}/{schema_name}/ab_role/"
@@ -631,13 +635,17 @@ class TestCore(SupersetTestCase):
def test_extra_table_metadata(self):
self.login("admin")
dbid = utils.get_example_database().id
example_db = utils.get_example_database()
schema = "default" if example_db.backend == "presto" else "superset"
self.get_json_resp(
f"/superset/extra_table_metadata/{dbid}/birth_names/superset/"
f"/superset/extra_table_metadata/{example_db.id}/birth_names/{schema}/"
)
def test_process_template(self):
maindb = utils.get_example_database()
if maindb.backend == "presto":
# TODO: make it work for presto
return
sql = "SELECT '{{ datetime(2017, 1, 1).isoformat() }}'"
tp = jinja_context.get_template_processor(database=maindb)
rendered = tp.process_template(sql)
@@ -645,6 +653,9 @@ class TestCore(SupersetTestCase):
def test_get_template_kwarg(self):
maindb = utils.get_example_database()
if maindb.backend == "presto":
# TODO: make it work for presto
return
s = "{{ foo }}"
tp = jinja_context.get_template_processor(database=maindb, foo="bar")
rendered = tp.process_template(s)
@@ -652,12 +663,18 @@ class TestCore(SupersetTestCase):
def test_template_kwarg(self):
maindb = utils.get_example_database()
if maindb.backend == "presto":
# TODO: make it work for presto
return
s = "{{ foo }}"
tp = jinja_context.get_template_processor(database=maindb)
rendered = tp.process_template(s, foo="bar")
self.assertEqual("bar", rendered)
def test_templated_sql_json(self):
if utils.get_example_database().backend == "presto":
# TODO: make it work for presto
return
self.login("admin")
sql = "SELECT '{{ datetime(2017, 1, 1).isoformat() }}' as test"
data = self.run_sql(sql, "fdaklj3ws")
@@ -717,10 +734,14 @@ class TestCore(SupersetTestCase):
"""Test custom template processor is ignored for a difference backend
database."""
maindb = utils.get_example_database()
sql = "SELECT '$DATE()'"
sql = (
"SELECT '$DATE()'"
if maindb.backend != "presto"
else f"SELECT '{datetime.date.today().isoformat()}'"
)
tp = jinja_context.get_template_processor(database=maindb)
rendered = tp.process_template(sql)
self.assertEqual(sql, rendered)
assert sql == rendered
@mock.patch("tests.superset_test_custom_template_processors.datetime")
@mock.patch("superset.sql_lab.get_sql_results")
@@ -904,7 +925,7 @@ class TestCore(SupersetTestCase):
explore_db_id = utils.get_example_database().id
upload_db = utils.get_or_create_db(
"csv_explore_db", app.config["SQLALCHEMY_DATABASE_URI"]
"csv_explore_db", app.config["SQLALCHEMY_EXAMPLES_URI"]
)
upload_db_id = upload_db.id
extra = upload_db.get_extra()
@@ -914,7 +935,7 @@ class TestCore(SupersetTestCase):
self.login(username="admin")
self.enable_csv_upload(DatasetDAO.get_database_by_id(upload_db_id))
table_name = "".join(random.choice(string.ascii_uppercase) for _ in range(5))
table_name = "".join(random.choice(string.ascii_lowercase) for _ in range(5))
f = "testCSV.csv"
self.create_sample_csvfile(f, ["a,b", "john,1", "paul,2"])
@@ -932,13 +953,14 @@ class TestCore(SupersetTestCase):
def test_import_csv(self):
self.login(username="admin")
examples_db = utils.get_example_database()
table_name = "".join(random.choice(string.ascii_lowercase) for _ in range(5))
f1 = "testCSV.csv"
self.create_sample_csvfile(f1, ["a,b", "john,1", "paul,2"])
f2 = "testCSV2.csv"
self.create_sample_csvfile(f2, ["b,c,d", "john,1,x", "paul,2,"])
self.enable_csv_upload(utils.get_example_database())
self.enable_csv_upload(examples_db)
try:
success_msg_f1 = f'CSV file "{f1}" uploaded to table "{table_name}"'
@@ -981,13 +1003,14 @@ class TestCore(SupersetTestCase):
extra={"null_values": '["", "john"]', "if_exists": "replace"},
)
# make sure that john and empty string are replaced with None
data = db.session.execute(f"SELECT * from {table_name}").fetchall()
engine = examples_db.get_sqla_engine()
data = engine.execute(f"SELECT * from {table_name}").fetchall()
assert data == [(None, 1, "x"), ("paul", 2, None)]
# default null values
self.upload_csv(f2, table_name, extra={"if_exists": "replace"})
# make sure that john and empty string are replaced with None
data = db.session.execute(f"SELECT * from {table_name}").fetchall()
data = engine.execute(f"SELECT * from {table_name}").fetchall()
assert data == [("john", 1, "x"), ("paul", 2, None)]
finally:
@@ -1022,7 +1045,12 @@ class TestCore(SupersetTestCase):
self.assertIn(success_msg_f1, resp)
# make sure that john and empty string are replaced with None
data = db.session.execute(f"SELECT * from {table_name}").fetchall()
data = (
utils.get_example_database()
.get_sqla_engine()
.execute(f"SELECT * from {table_name}")
.fetchall()
)
assert data == [(0, "john", 1), (1, "paul", 2)]
finally:
os.remove(f1)