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 2022/02/09 17:18:42 UTC

[superset] branch better_unit_tests created (now 5a05d34)

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

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


      at 5a05d34  WIP

This branch includes the following new commits:

     new 5a05d34  WIP

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[superset] 01/01: WIP

Posted by be...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 5a05d34e07abf7b5533938f8ca4b566b085794e1
Author: Beto Dealmeida <ro...@dealmeida.net>
AuthorDate: Wed Dec 15 16:24:56 2021 -0800

    WIP
---
 .../{conftest.py => annotation_layers/__init__.py} | 22 ---------
 tests/unit_tests/conftest.py                       | 52 +++++++++++++++++++++-
 .../{conftest.py => databases/__init__.py}         | 22 ---------
 .../{conftest.py => databases/api_test.py}         | 22 +++------
 tests/unit_tests/dataframe_test.py                 |  2 +-
 .../{conftest.py => db_engine_specs/__init__.py}   | 22 ---------
 .../unit_tests/{conftest.py => tasks/__init__.py}  | 22 ---------
 7 files changed, 56 insertions(+), 108 deletions(-)

diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/annotation_layers/__init__.py
similarity index 60%
copy from tests/unit_tests/conftest.py
copy to tests/unit_tests/annotation_layers/__init__.py
index 4700cd1..13a8339 100644
--- a/tests/unit_tests/conftest.py
+++ b/tests/unit_tests/annotation_layers/__init__.py
@@ -14,25 +14,3 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-
-import pytest
-
-from superset.app import SupersetApp
-from superset.initialization import SupersetAppInitializer
-
-
-@pytest.fixture
-def app_context():
-    """
-    A fixture for running the test inside an app context.
-    """
-    app = SupersetApp(__name__)
-
-    app.config.from_object("superset.config")
-    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
-
-    app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app)
-    app_initializer.init_app()
-
-    with app.app_context():
-        yield
diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py
index 4700cd1..3188b50 100644
--- a/tests/unit_tests/conftest.py
+++ b/tests/unit_tests/conftest.py
@@ -14,25 +14,73 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
+# pylint: disable=redefined-outer-name
+
+from typing import Iterator
 
 import pytest
+from flask.testing import FlaskClient
+from pytest_mock import MockFixture
+from sqlalchemy import create_engine
+from sqlalchemy.orm import sessionmaker
+from sqlalchemy.orm.session import Session
 
 from superset.app import SupersetApp
 from superset.initialization import SupersetAppInitializer
 
 
+@pytest.fixture()
+def session() -> Iterator[Session]:
+    """
+    Create an in-memory SQLite session to test models.
+    """
+    engine = create_engine("sqlite://")
+    Session_ = sessionmaker(bind=engine)  # pylint: disable=invalid-name
+    in_memory_session = Session_()
+
+    # flask calls session.remove()
+    in_memory_session.remove = lambda: None
+
+    yield in_memory_session
+
+
 @pytest.fixture
-def app_context():
+def app(mocker: MockFixture, session: Session) -> Iterator[None]:
     """
-    A fixture for running the test inside an app context.
+    A fixture that generates a Superset app.
     """
     app = SupersetApp(__name__)
 
     app.config.from_object("superset.config")
     app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
+    app.config["FAB_ADD_SECURITY_VIEWS"] = False
 
     app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app)
     app_initializer.init_app()
 
+    # patch session
+    mocker.patch(
+        "superset.security.SupersetSecurityManager.get_session",
+        return_value=session,
+    )
+    mocker.patch("superset.db.session", session)
+
+    yield app
+
+
+@pytest.fixture
+def app_context(app: SupersetApp) -> Iterator[None]:
+    """
+    A fixture that yields and application context.
+    """
     with app.app_context():
         yield
+
+
+@pytest.fixture
+def client(app: SupersetApp) -> Iterator[FlaskClient]:
+    """
+    A fixture that yields a Flask test client.
+    """
+    with app.test_client() as client:
+        yield client
diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/databases/__init__.py
similarity index 60%
copy from tests/unit_tests/conftest.py
copy to tests/unit_tests/databases/__init__.py
index 4700cd1..13a8339 100644
--- a/tests/unit_tests/conftest.py
+++ b/tests/unit_tests/databases/__init__.py
@@ -14,25 +14,3 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-
-import pytest
-
-from superset.app import SupersetApp
-from superset.initialization import SupersetAppInitializer
-
-
-@pytest.fixture
-def app_context():
-    """
-    A fixture for running the test inside an app context.
-    """
-    app = SupersetApp(__name__)
-
-    app.config.from_object("superset.config")
-    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
-
-    app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app)
-    app_initializer.init_app()
-
-    with app.app_context():
-        yield
diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/databases/api_test.py
similarity index 61%
copy from tests/unit_tests/conftest.py
copy to tests/unit_tests/databases/api_test.py
index 4700cd1..e67bc76 100644
--- a/tests/unit_tests/conftest.py
+++ b/tests/unit_tests/databases/api_test.py
@@ -15,24 +15,12 @@
 # specific language governing permissions and limitations
 # under the License.
 
-import pytest
+from flask.testing import FlaskClient
 
-from superset.app import SupersetApp
-from superset.initialization import SupersetAppInitializer
 
-
-@pytest.fixture
-def app_context():
+def test_unauthenticated(client: FlaskClient) -> None:
     """
-    A fixture for running the test inside an app context.
+    Test that an error is returned when not authenticated.
     """
-    app = SupersetApp(__name__)
-
-    app.config.from_object("superset.config")
-    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
-
-    app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app)
-    app_initializer.init_app()
-
-    with app.app_context():
-        yield
+    response = client.get("api/v1/database/")
+    assert response.json == {"msg": "Missing Authorization Header"}
diff --git a/tests/unit_tests/dataframe_test.py b/tests/unit_tests/dataframe_test.py
index 3e986a5..9c6b923 100644
--- a/tests/unit_tests/dataframe_test.py
+++ b/tests/unit_tests/dataframe_test.py
@@ -19,7 +19,7 @@ from superset.dataframe import df_to_records
 from superset.typing import DbapiDescription
 
 
-def test_df_to_records(app_context: None) -> None:
+def test_df_to_records(app, client, app_context: None) -> None:
     from superset.db_engine_specs import BaseEngineSpec
     from superset.result_set import SupersetResultSet
 
diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/db_engine_specs/__init__.py
similarity index 60%
copy from tests/unit_tests/conftest.py
copy to tests/unit_tests/db_engine_specs/__init__.py
index 4700cd1..13a8339 100644
--- a/tests/unit_tests/conftest.py
+++ b/tests/unit_tests/db_engine_specs/__init__.py
@@ -14,25 +14,3 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-
-import pytest
-
-from superset.app import SupersetApp
-from superset.initialization import SupersetAppInitializer
-
-
-@pytest.fixture
-def app_context():
-    """
-    A fixture for running the test inside an app context.
-    """
-    app = SupersetApp(__name__)
-
-    app.config.from_object("superset.config")
-    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
-
-    app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app)
-    app_initializer.init_app()
-
-    with app.app_context():
-        yield
diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/tasks/__init__.py
similarity index 60%
copy from tests/unit_tests/conftest.py
copy to tests/unit_tests/tasks/__init__.py
index 4700cd1..13a8339 100644
--- a/tests/unit_tests/conftest.py
+++ b/tests/unit_tests/tasks/__init__.py
@@ -14,25 +14,3 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-
-import pytest
-
-from superset.app import SupersetApp
-from superset.initialization import SupersetAppInitializer
-
-
-@pytest.fixture
-def app_context():
-    """
-    A fixture for running the test inside an app context.
-    """
-    app = SupersetApp(__name__)
-
-    app.config.from_object("superset.config")
-    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
-
-    app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app)
-    app_initializer.init_app()
-
-    with app.app_context():
-        yield