postgres_tests.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from unittest import mock
  18. from sqlalchemy import column, literal_column
  19. from sqlalchemy.dialects import postgresql
  20. from superset.db_engine_specs.postgres import PostgresEngineSpec
  21. from tests.db_engine_specs.base_tests import DbEngineSpecTestCase
  22. class PostgresTests(DbEngineSpecTestCase):
  23. def test_get_table_names(self):
  24. """ Make sure postgres doesn't try to remove schema name from table name
  25. ie. when try_remove_schema_from_table_name == False. """
  26. inspector = mock.Mock()
  27. inspector.get_table_names = mock.Mock(return_value=["schema.table", "table_2"])
  28. inspector.get_foreign_table_names = mock.Mock(return_value=["table_3"])
  29. pg_result_expected = ["schema.table", "table_2", "table_3"]
  30. pg_result = PostgresEngineSpec.get_table_names(
  31. database=mock.ANY, schema="schema", inspector=inspector
  32. )
  33. self.assertListEqual(pg_result_expected, pg_result)
  34. def test_time_exp_literal_no_grain(self):
  35. col = literal_column("COALESCE(a, b)")
  36. expr = PostgresEngineSpec.get_timestamp_expr(col, None, None)
  37. result = str(expr.compile(None, dialect=postgresql.dialect()))
  38. self.assertEqual(result, "COALESCE(a, b)")
  39. def test_time_exp_literal_1y_grain(self):
  40. col = literal_column("COALESCE(a, b)")
  41. expr = PostgresEngineSpec.get_timestamp_expr(col, None, "P1Y")
  42. result = str(expr.compile(None, dialect=postgresql.dialect()))
  43. self.assertEqual(result, "DATE_TRUNC('year', COALESCE(a, b))")
  44. def test_time_ex_lowr_col_no_grain(self):
  45. col = column("lower_case")
  46. expr = PostgresEngineSpec.get_timestamp_expr(col, None, None)
  47. result = str(expr.compile(None, dialect=postgresql.dialect()))
  48. self.assertEqual(result, "lower_case")
  49. def test_time_exp_lowr_col_sec_1y(self):
  50. col = column("lower_case")
  51. expr = PostgresEngineSpec.get_timestamp_expr(col, "epoch_s", "P1Y")
  52. result = str(expr.compile(None, dialect=postgresql.dialect()))
  53. self.assertEqual(
  54. result,
  55. "DATE_TRUNC('year', "
  56. "(timestamp 'epoch' + lower_case * interval '1 second'))",
  57. )
  58. def test_time_exp_mixd_case_col_1y(self):
  59. col = column("MixedCase")
  60. expr = PostgresEngineSpec.get_timestamp_expr(col, None, "P1Y")
  61. result = str(expr.compile(None, dialect=postgresql.dialect()))
  62. self.assertEqual(result, "DATE_TRUNC('year', \"MixedCase\")")
  63. def test_convert_dttm(self):
  64. dttm = self.get_dttm()
  65. self.assertEqual(
  66. PostgresEngineSpec.convert_dttm("DATE", dttm),
  67. "TO_DATE('2019-01-02', 'YYYY-MM-DD')",
  68. )
  69. self.assertEqual(
  70. PostgresEngineSpec.convert_dttm("TIMESTAMP", dttm),
  71. "TO_TIMESTAMP('2019-01-02 03:04:05.678900', 'YYYY-MM-DD HH24:MI:SS.US')",
  72. )