You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by be...@apache.org on 2024/03/11 22:51:57 UTC

(superset) 01/01: chore: add unit test for `values_for_column`

This is an automated email from the ASF dual-hosted git repository.

beto pushed a commit to branch add_values_for_column_test
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 249c9201d11b599d7b3859d903d3707f976d8bc9
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Mon Mar 11 18:50:46 2024 -0400

    chore: add unit test for `values_for_column`
---
 tests/unit_tests/models/helpers_test.py | 72 +++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)

diff --git a/tests/unit_tests/models/helpers_test.py b/tests/unit_tests/models/helpers_test.py
new file mode 100644
index 0000000000..6d9597c0d6
--- /dev/null
+++ b/tests/unit_tests/models/helpers_test.py
@@ -0,0 +1,72 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# pylint: disable=import-outside-toplevel
+
+from contextlib import contextmanager
+
+from pytest_mock import MockerFixture
+from sqlalchemy import create_engine
+from sqlalchemy.orm.session import Session
+from sqlalchemy.pool import StaticPool
+
+
+def test_values_for_column(mocker: MockerFixture, session: Session) -> None:
+    """
+    Test the `values_for_column` method.
+
+    NULL values should be returned as `None`, not `np.nan`, since NaN cannot be
+    serialized to JSON.
+    """
+    from superset.connectors.sqla.models import SqlaTable, TableColumn
+    from superset.models.core import Database
+
+    SqlaTable.metadata.create_all(session.get_bind())
+
+    engine = create_engine(
+        "sqlite://",
+        connect_args={"check_same_thread": False},
+        poolclass=StaticPool,
+    )
+
+    database = Database(database_name="db", sqlalchemy_uri="sqlite://")
+
+    connection = engine.raw_connection()
+    connection.execute("CREATE TABLE t (c INTEGER)")
+    connection.execute("INSERT INTO t VALUES (1)")
+    connection.execute("INSERT INTO t VALUES (NULL)")
+    connection.commit()
+
+    # since we're using an in-memory SQLite database, make sure we always
+    # return the same engine where the table was created
+    @contextmanager
+    def mock_get_sqla_engine_with_context():
+        yield engine
+
+    mocker.patch.object(
+        database,
+        "get_sqla_engine_with_context",
+        new=mock_get_sqla_engine_with_context,
+    )
+
+    table = SqlaTable(
+        database=database,
+        schema=None,
+        table_name="t",
+        columns=[TableColumn(column_name="c")],
+    )
+    assert table.values_for_column("c") == [1, None]