test: fix flaky Python unit tests (#12253)

This commit is contained in:
Karol Kostrzewa 2021-01-06 01:40:01 +01:00 committed by GitHub
parent 1b908ab9f1
commit 1a2680d4a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 2 deletions

View File

@ -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"
)

View File

@ -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())

View File

@ -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")

View File

@ -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

View File

@ -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):
"""

View File

@ -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()