mssql_tests.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. import unittest.mock as mock
  18. from sqlalchemy import column, table
  19. from sqlalchemy.dialects import mssql
  20. from sqlalchemy.sql import select
  21. from sqlalchemy.types import String, UnicodeText
  22. from superset.db_engine_specs.base import BaseEngineSpec
  23. from superset.db_engine_specs.mssql import MssqlEngineSpec
  24. from tests.db_engine_specs.base_tests import DbEngineSpecTestCase
  25. class MssqlEngineSpecTest(DbEngineSpecTestCase):
  26. def test_mssql_column_types(self):
  27. def assert_type(type_string, type_expected):
  28. type_assigned = MssqlEngineSpec.get_sqla_column_type(type_string)
  29. if type_expected is None:
  30. self.assertIsNone(type_assigned)
  31. else:
  32. self.assertIsInstance(type_assigned, type_expected)
  33. assert_type("INT", None)
  34. assert_type("STRING", String)
  35. assert_type("CHAR(10)", String)
  36. assert_type("VARCHAR(10)", String)
  37. assert_type("TEXT", String)
  38. assert_type("NCHAR(10)", UnicodeText)
  39. assert_type("NVARCHAR(10)", UnicodeText)
  40. assert_type("NTEXT", UnicodeText)
  41. def test_where_clause_n_prefix(self):
  42. dialect = mssql.dialect()
  43. spec = MssqlEngineSpec
  44. str_col = column("col", type_=spec.get_sqla_column_type("VARCHAR(10)"))
  45. unicode_col = column("unicode_col", type_=spec.get_sqla_column_type("NTEXT"))
  46. tbl = table("tbl")
  47. sel = (
  48. select([str_col, unicode_col])
  49. .select_from(tbl)
  50. .where(str_col == "abc")
  51. .where(unicode_col == "abc")
  52. )
  53. query = str(
  54. sel.compile(dialect=dialect, compile_kwargs={"literal_binds": True})
  55. )
  56. query_expected = (
  57. "SELECT col, unicode_col \n"
  58. "FROM tbl \n"
  59. "WHERE col = 'abc' AND unicode_col = N'abc'"
  60. )
  61. self.assertEqual(query, query_expected)
  62. def test_time_exp_mixd_case_col_1y(self):
  63. col = column("MixedCase")
  64. expr = MssqlEngineSpec.get_timestamp_expr(col, None, "P1Y")
  65. result = str(expr.compile(None, dialect=mssql.dialect()))
  66. self.assertEqual(result, "DATEADD(year, DATEDIFF(year, 0, [MixedCase]), 0)")
  67. def test_convert_dttm(self):
  68. dttm = self.get_dttm()
  69. self.assertEqual(
  70. MssqlEngineSpec.convert_dttm("DATE", dttm),
  71. "CONVERT(DATE, '2019-01-02', 23)",
  72. )
  73. self.assertEqual(
  74. MssqlEngineSpec.convert_dttm("DATETIME", dttm),
  75. "CONVERT(DATETIME, '2019-01-02T03:04:05.678', 126)",
  76. )
  77. self.assertEqual(
  78. MssqlEngineSpec.convert_dttm("SMALLDATETIME", dttm),
  79. "CONVERT(SMALLDATETIME, '2019-01-02 03:04:05', 20)",
  80. )
  81. @mock.patch.object(
  82. MssqlEngineSpec, "pyodbc_rows_to_tuples", return_value="converted"
  83. )
  84. def test_fetch_data(self, mock_pyodbc_rows_to_tuples):
  85. data = [(1, "foo")]
  86. with mock.patch.object(
  87. BaseEngineSpec, "fetch_data", return_value=data
  88. ) as mock_fetch:
  89. result = MssqlEngineSpec.fetch_data(None, 0)
  90. mock_pyodbc_rows_to_tuples.assert_called_once_with(data)
  91. self.assertEqual(result, "converted")