From 1a2680d4a153719c38d7deebabab8b9f61fb698c Mon Sep 17 00:00:00 2001 From: Karol Kostrzewa Date: Wed, 6 Jan 2021 01:40:01 +0100 Subject: [PATCH] test: fix flaky Python unit tests (#12253) --- tests/databases/api_tests.py | 10 ++++++++++ tests/db_engine_specs/base_engine_spec_tests.py | 2 ++ tests/email_tests.py | 6 +++++- tests/queries/api_tests.py | 5 ++++- tests/queries/saved_queries/api_tests.py | 2 ++ tests/security_tests.py | 1 + 6 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/databases/api_tests.py b/tests/databases/api_tests.py index f28f138ea4..79d302b959 100644 --- a/tests/databases/api_tests.py +++ b/tests/databases/api_tests.py @@ -19,6 +19,7 @@ """Unit tests for Superset""" import json from io import BytesIO +from unittest import mock from zipfile import is_zipfile, ZipFile import prison @@ -343,6 +344,10 @@ class TestDatabaseApi(SupersetTestCase): "Invalid connection string", response["message"]["sqlalchemy_uri"][0], ) + @mock.patch( + "superset.views.core.app.config", + {**app.config, "PREVENT_UNSAFE_DB_CONNECTIONS": True}, + ) def test_create_database_fail_sqllite(self): """ Database API: Test create fail with sqllite @@ -493,6 +498,9 @@ class TestDatabaseApi(SupersetTestCase): "Invalid connection string", response["message"]["sqlalchemy_uri"][0], ) + db.session.delete(test_database) + db.session.commit() + def test_delete_database(self): """ Database API: Test delete @@ -830,6 +838,8 @@ class TestDatabaseApi(SupersetTestCase): } self.assertEqual(response, expected_response) + app.config["PREVENT_UNSAFE_DB_CONNECTIONS"] = False + @pytest.mark.usefixtures( "load_unicode_dashboard_with_position", "load_energy_table_with_slice" ) diff --git a/tests/db_engine_specs/base_engine_spec_tests.py b/tests/db_engine_specs/base_engine_spec_tests.py index 1b89f9a49f..5055265ca9 100644 --- a/tests/db_engine_specs/base_engine_spec_tests.py +++ b/tests/db_engine_specs/base_engine_spec_tests.py @@ -170,6 +170,8 @@ class TestDbEngineSpecs(TestDbEngineSpec): time_grain_addon = time_grains[-1] self.assertEqual("PTXM", time_grain_addon.duration) self.assertEqual("x seconds", time_grain_addon.label) + app.config["TIME_GRAIN_ADDONS"] = {} + app.config["TIME_GRAIN_ADDON_EXPRESSIONS"] = {} def test_engine_time_grain_validity(self): time_grains = set(builtin_time_grains.keys()) diff --git a/tests/email_tests.py b/tests/email_tests.py index 94337f6831..89b0c4b142 100644 --- a/tests/email_tests.py +++ b/tests/email_tests.py @@ -36,7 +36,7 @@ logger = logging.getLogger(__name__) class TestEmailSmtp(SupersetTestCase): def setUp(self): - app.config["smtp_ssl"] = False + app.config["SMTP_SSL"] = False @mock.patch("superset.utils.core.send_mime_email") def test_send_smtp(self, mock_send_mime): @@ -150,6 +150,8 @@ class TestEmailSmtp(SupersetTestCase): @mock.patch("smtplib.SMTP_SSL") @mock.patch("smtplib.SMTP") def test_send_mime_noauth(self, mock_smtp, mock_smtp_ssl): + smtp_user = app.config["SMTP_USER"] + smtp_password = app.config["SMTP_PASSWORD"] app.config["SMTP_USER"] = None app.config["SMTP_PASSWORD"] = None mock_smtp.return_value = mock.Mock() @@ -158,6 +160,8 @@ class TestEmailSmtp(SupersetTestCase): assert not mock_smtp_ssl.called mock_smtp.assert_called_with(app.config["SMTP_HOST"], app.config["SMTP_PORT"]) assert not mock_smtp.login.called + app.config["SMTP_USER"] = smtp_user + app.config["SMTP_PASSWORD"] = smtp_password @mock.patch("smtplib.SMTP_SSL") @mock.patch("smtplib.SMTP") diff --git a/tests/queries/api_tests.py b/tests/queries/api_tests.py index 54d100d5fa..a2fc02fe75 100644 --- a/tests/queries/api_tests.py +++ b/tests/queries/api_tests.py @@ -179,13 +179,16 @@ class TestQueryApi(SupersetTestCase): """ admin = self.get_user("admin") client_id = self.get_random_string() - self.insert_query(get_example_database().id, admin.id, client_id) + query = self.insert_query(get_example_database().id, admin.id, client_id) max_id = db.session.query(func.max(Query.id)).scalar() self.login(username="admin") uri = f"api/v1/query/{max_id + 1}" rv = self.client.get(uri) self.assertEqual(rv.status_code, 404) + db.session.delete(query) + db.session.commit() + def test_get_query_no_data_access(self): """ Query API: Test get query without data access diff --git a/tests/queries/saved_queries/api_tests.py b/tests/queries/saved_queries/api_tests.py index 82b82f9bd5..a506745694 100644 --- a/tests/queries/saved_queries/api_tests.py +++ b/tests/queries/saved_queries/api_tests.py @@ -534,6 +534,8 @@ class TestSavedQueryApi(SupersetTestCase): uri = f"api/v1/saved_query/{max_id + 1}" rv = self.client.get(uri) assert rv.status_code == 404 + db.session.delete(query) + db.session.commit() def test_create_saved_query(self): """ diff --git a/tests/security_tests.py b/tests/security_tests.py index 388770e9fb..d866527698 100644 --- a/tests/security_tests.py +++ b/tests/security_tests.py @@ -260,6 +260,7 @@ class TestRolePermission(SupersetTestCase): session.commit() def test_set_perm_druid_datasource(self): + self.create_druid_test_objects() session = db.session druid_cluster = ( session.query(DruidCluster).filter_by(cluster_name="druid_test").one()