mirror of https://github.com/apache/superset.git
tests(engine_specs): full postgres engine coverage (#9682)
This commit is contained in:
parent
f13ba2561c
commit
527bee5051
|
@ -20,11 +20,11 @@ from typing import Any, List, Optional, Tuple, TYPE_CHECKING
|
||||||
from pytz import _FixedOffset # type: ignore
|
from pytz import _FixedOffset # type: ignore
|
||||||
from sqlalchemy.dialects.postgresql.base import PGInspector
|
from sqlalchemy.dialects.postgresql.base import PGInspector
|
||||||
|
|
||||||
from superset.db_engine_specs.base import BaseEngineSpec, LimitMethod
|
from superset.db_engine_specs.base import BaseEngineSpec
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
# prevent circular imports
|
# pylint: disable=unused-import
|
||||||
from superset.models.core import Database # pylint: disable=unused-import
|
from superset.models.core import Database # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
# Replace psycopg2.tz.FixedOffsetTimezone with pytz, which is serializable by PyArrow
|
# Replace psycopg2.tz.FixedOffsetTimezone with pytz, which is serializable by PyArrow
|
||||||
|
@ -55,9 +55,7 @@ class PostgresBaseEngineSpec(BaseEngineSpec):
|
||||||
cursor.tzinfo_factory = FixedOffsetTimezone
|
cursor.tzinfo_factory = FixedOffsetTimezone
|
||||||
if not cursor.description:
|
if not cursor.description:
|
||||||
return []
|
return []
|
||||||
if cls.limit_method == LimitMethod.FETCH_MANY:
|
return super().fetch_data(cursor, limit)
|
||||||
return cursor.fetchmany(limit)
|
|
||||||
return cursor.fetchall()
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def epoch_to_dttm(cls) -> str:
|
def epoch_to_dttm(cls) -> str:
|
||||||
|
|
|
@ -25,6 +25,10 @@ from tests.db_engine_specs.base_tests import DbEngineSpecTestCase
|
||||||
|
|
||||||
class PostgresTests(DbEngineSpecTestCase):
|
class PostgresTests(DbEngineSpecTestCase):
|
||||||
def test_get_table_names(self):
|
def test_get_table_names(self):
|
||||||
|
"""
|
||||||
|
DB Eng Specs (postgres): Test get table names
|
||||||
|
"""
|
||||||
|
|
||||||
""" Make sure postgres doesn't try to remove schema name from table name
|
""" Make sure postgres doesn't try to remove schema name from table name
|
||||||
ie. when try_remove_schema_from_table_name == False. """
|
ie. when try_remove_schema_from_table_name == False. """
|
||||||
inspector = mock.Mock()
|
inspector = mock.Mock()
|
||||||
|
@ -38,24 +42,36 @@ class PostgresTests(DbEngineSpecTestCase):
|
||||||
self.assertListEqual(pg_result_expected, pg_result)
|
self.assertListEqual(pg_result_expected, pg_result)
|
||||||
|
|
||||||
def test_time_exp_literal_no_grain(self):
|
def test_time_exp_literal_no_grain(self):
|
||||||
|
"""
|
||||||
|
DB Eng Specs (postgres): Test no grain literal column
|
||||||
|
"""
|
||||||
col = literal_column("COALESCE(a, b)")
|
col = literal_column("COALESCE(a, b)")
|
||||||
expr = PostgresEngineSpec.get_timestamp_expr(col, None, None)
|
expr = PostgresEngineSpec.get_timestamp_expr(col, None, None)
|
||||||
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
||||||
self.assertEqual(result, "COALESCE(a, b)")
|
self.assertEqual(result, "COALESCE(a, b)")
|
||||||
|
|
||||||
def test_time_exp_literal_1y_grain(self):
|
def test_time_exp_literal_1y_grain(self):
|
||||||
|
"""
|
||||||
|
DB Eng Specs (postgres): Test grain literal column 1 YEAR
|
||||||
|
"""
|
||||||
col = literal_column("COALESCE(a, b)")
|
col = literal_column("COALESCE(a, b)")
|
||||||
expr = PostgresEngineSpec.get_timestamp_expr(col, None, "P1Y")
|
expr = PostgresEngineSpec.get_timestamp_expr(col, None, "P1Y")
|
||||||
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
||||||
self.assertEqual(result, "DATE_TRUNC('year', COALESCE(a, b))")
|
self.assertEqual(result, "DATE_TRUNC('year', COALESCE(a, b))")
|
||||||
|
|
||||||
def test_time_ex_lowr_col_no_grain(self):
|
def test_time_ex_lowr_col_no_grain(self):
|
||||||
|
"""
|
||||||
|
DB Eng Specs (postgres): Test no grain expr lower case
|
||||||
|
"""
|
||||||
col = column("lower_case")
|
col = column("lower_case")
|
||||||
expr = PostgresEngineSpec.get_timestamp_expr(col, None, None)
|
expr = PostgresEngineSpec.get_timestamp_expr(col, None, None)
|
||||||
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
||||||
self.assertEqual(result, "lower_case")
|
self.assertEqual(result, "lower_case")
|
||||||
|
|
||||||
def test_time_exp_lowr_col_sec_1y(self):
|
def test_time_exp_lowr_col_sec_1y(self):
|
||||||
|
"""
|
||||||
|
DB Eng Specs (postgres): Test grain expr lower case 1 YEAR
|
||||||
|
"""
|
||||||
col = column("lower_case")
|
col = column("lower_case")
|
||||||
expr = PostgresEngineSpec.get_timestamp_expr(col, "epoch_s", "P1Y")
|
expr = PostgresEngineSpec.get_timestamp_expr(col, "epoch_s", "P1Y")
|
||||||
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
||||||
|
@ -66,12 +82,18 @@ class PostgresTests(DbEngineSpecTestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_time_exp_mixd_case_col_1y(self):
|
def test_time_exp_mixd_case_col_1y(self):
|
||||||
|
"""
|
||||||
|
DB Eng Specs (postgres): Test grain expr mixed case 1 YEAR
|
||||||
|
"""
|
||||||
col = column("MixedCase")
|
col = column("MixedCase")
|
||||||
expr = PostgresEngineSpec.get_timestamp_expr(col, None, "P1Y")
|
expr = PostgresEngineSpec.get_timestamp_expr(col, None, "P1Y")
|
||||||
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
result = str(expr.compile(None, dialect=postgresql.dialect()))
|
||||||
self.assertEqual(result, "DATE_TRUNC('year', \"MixedCase\")")
|
self.assertEqual(result, "DATE_TRUNC('year', \"MixedCase\")")
|
||||||
|
|
||||||
def test_convert_dttm(self):
|
def test_convert_dttm(self):
|
||||||
|
"""
|
||||||
|
DB Eng Specs (postgres): Test conversion to date time
|
||||||
|
"""
|
||||||
dttm = self.get_dttm()
|
dttm = self.get_dttm()
|
||||||
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
@ -83,3 +105,15 @@ class PostgresTests(DbEngineSpecTestCase):
|
||||||
PostgresEngineSpec.convert_dttm("TIMESTAMP", dttm),
|
PostgresEngineSpec.convert_dttm("TIMESTAMP", dttm),
|
||||||
"TO_TIMESTAMP('2019-01-02 03:04:05.678900', 'YYYY-MM-DD HH24:MI:SS.US')",
|
"TO_TIMESTAMP('2019-01-02 03:04:05.678900', 'YYYY-MM-DD HH24:MI:SS.US')",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.assertEqual(PostgresEngineSpec.convert_dttm("DATETIME", dttm), None)
|
||||||
|
|
||||||
|
def test_empty_dbapi_cursor_description(self):
|
||||||
|
"""
|
||||||
|
DB Eng Specs (postgres): Test empty cursor description (no columns)
|
||||||
|
"""
|
||||||
|
cursor = mock.Mock()
|
||||||
|
# empty description mean no columns, this mocks the following SQL: "SELECT"
|
||||||
|
cursor.description = []
|
||||||
|
results = PostgresEngineSpec.fetch_data(cursor, 1000)
|
||||||
|
self.assertEqual(results, [])
|
||||||
|
|
Loading…
Reference in New Issue