Skip to content

Commit 1a2680d

Browse files
authored
test: fix flaky Python unit tests (#12253)
1 parent 1b908ab commit 1a2680d

File tree

6 files changed

+24
-2
lines changed

6 files changed

+24
-2
lines changed

tests/databases/api_tests.py

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""Unit tests for Superset"""
2020
import json
2121
from io import BytesIO
22+
from unittest import mock
2223
from zipfile import is_zipfile, ZipFile
2324

2425
import prison
@@ -343,6 +344,10 @@ def test_create_database_uri_validate(self):
343344
"Invalid connection string", response["message"]["sqlalchemy_uri"][0],
344345
)
345346

347+
@mock.patch(
348+
"superset.views.core.app.config",
349+
{**app.config, "PREVENT_UNSAFE_DB_CONNECTIONS": True},
350+
)
346351
def test_create_database_fail_sqllite(self):
347352
"""
348353
Database API: Test create fail with sqllite
@@ -493,6 +498,9 @@ def test_update_database_uri_validate(self):
493498
"Invalid connection string", response["message"]["sqlalchemy_uri"][0],
494499
)
495500

501+
db.session.delete(test_database)
502+
db.session.commit()
503+
496504
def test_delete_database(self):
497505
"""
498506
Database API: Test delete
@@ -830,6 +838,8 @@ def test_test_connection_unsafe_uri(self):
830838
}
831839
self.assertEqual(response, expected_response)
832840

841+
app.config["PREVENT_UNSAFE_DB_CONNECTIONS"] = False
842+
833843
@pytest.mark.usefixtures(
834844
"load_unicode_dashboard_with_position", "load_energy_table_with_slice"
835845
)

tests/db_engine_specs/base_engine_spec_tests.py

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def test_time_grain_addons(self):
170170
time_grain_addon = time_grains[-1]
171171
self.assertEqual("PTXM", time_grain_addon.duration)
172172
self.assertEqual("x seconds", time_grain_addon.label)
173+
app.config["TIME_GRAIN_ADDONS"] = {}
174+
app.config["TIME_GRAIN_ADDON_EXPRESSIONS"] = {}
173175

174176
def test_engine_time_grain_validity(self):
175177
time_grains = set(builtin_time_grains.keys())

tests/email_tests.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
class TestEmailSmtp(SupersetTestCase):
3838
def setUp(self):
39-
app.config["smtp_ssl"] = False
39+
app.config["SMTP_SSL"] = False
4040

4141
@mock.patch("superset.utils.core.send_mime_email")
4242
def test_send_smtp(self, mock_send_mime):
@@ -150,6 +150,8 @@ def test_send_mime_ssl(self, mock_smtp, mock_smtp_ssl):
150150
@mock.patch("smtplib.SMTP_SSL")
151151
@mock.patch("smtplib.SMTP")
152152
def test_send_mime_noauth(self, mock_smtp, mock_smtp_ssl):
153+
smtp_user = app.config["SMTP_USER"]
154+
smtp_password = app.config["SMTP_PASSWORD"]
153155
app.config["SMTP_USER"] = None
154156
app.config["SMTP_PASSWORD"] = None
155157
mock_smtp.return_value = mock.Mock()
@@ -158,6 +160,8 @@ def test_send_mime_noauth(self, mock_smtp, mock_smtp_ssl):
158160
assert not mock_smtp_ssl.called
159161
mock_smtp.assert_called_with(app.config["SMTP_HOST"], app.config["SMTP_PORT"])
160162
assert not mock_smtp.login.called
163+
app.config["SMTP_USER"] = smtp_user
164+
app.config["SMTP_PASSWORD"] = smtp_password
161165

162166
@mock.patch("smtplib.SMTP_SSL")
163167
@mock.patch("smtplib.SMTP")

tests/queries/api_tests.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,16 @@ def test_get_query_not_found(self):
179179
"""
180180
admin = self.get_user("admin")
181181
client_id = self.get_random_string()
182-
self.insert_query(get_example_database().id, admin.id, client_id)
182+
query = self.insert_query(get_example_database().id, admin.id, client_id)
183183
max_id = db.session.query(func.max(Query.id)).scalar()
184184
self.login(username="admin")
185185
uri = f"api/v1/query/{max_id + 1}"
186186
rv = self.client.get(uri)
187187
self.assertEqual(rv.status_code, 404)
188188

189+
db.session.delete(query)
190+
db.session.commit()
191+
189192
def test_get_query_no_data_access(self):
190193
"""
191194
Query API: Test get query without data access

tests/queries/saved_queries/api_tests.py

+2
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ def test_get_saved_query_not_found(self):
534534
uri = f"api/v1/saved_query/{max_id + 1}"
535535
rv = self.client.get(uri)
536536
assert rv.status_code == 404
537+
db.session.delete(query)
538+
db.session.commit()
537539

538540
def test_create_saved_query(self):
539541
"""

tests/security_tests.py

+1
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ def test_set_perm_sqla_table(self):
260260
session.commit()
261261

262262
def test_set_perm_druid_datasource(self):
263+
self.create_druid_test_objects()
263264
session = db.session
264265
druid_cluster = (
265266
session.query(DruidCluster).filter_by(cluster_name="druid_test").one()

0 commit comments

Comments
 (0)