diff --git a/.github/workflows/superset-python-unittest.yml b/.github/workflows/superset-python-unittest.yml index 86ded831f..91406c545 100644 --- a/.github/workflows/superset-python-unittest.yml +++ b/.github/workflows/superset-python-unittest.yml @@ -1,5 +1,5 @@ -# Python MySQL unit tests -name: Python MySQL +# Python unit tests +name: Python on: push: diff --git a/tests/dashboards/api_tests.py b/tests/dashboards/api_tests.py index 232c68041..641154e1c 100644 --- a/tests/dashboards/api_tests.py +++ b/tests/dashboards/api_tests.py @@ -22,7 +22,6 @@ from io import BytesIO from typing import List, Optional from unittest.mock import patch from zipfile import is_zipfile, ZipFile -from tests.fixtures.birth_names_dashboard import load_birth_names_dashboard_with_slices import pytest import prison @@ -49,6 +48,7 @@ from tests.fixtures.importexport import ( dataset_metadata_config, ) from tests.utils.get_dashboards import get_dashboards_ids +from tests.fixtures.birth_names_dashboard import load_birth_names_dashboard_with_slices from tests.fixtures.world_bank_dashboard import load_world_bank_dashboard_with_slices DASHBOARDS_FIXTURE_COUNT = 10 @@ -223,6 +223,7 @@ class TestDashboardApi(SupersetTestCase, ApiOwnersTestCaseMixin): assert "can_write" in data["permissions"] assert len(data["permissions"]) == 2 + @pytest.mark.usefixtures("load_world_bank_dashboard_with_slices") def test_get_dashboard_not_found(self): """ Dashboard API: Test get dashboard not found diff --git a/tests/databases/api_tests.py b/tests/databases/api_tests.py index a72c7eb12..b1af44efa 100644 --- a/tests/databases/api_tests.py +++ b/tests/databases/api_tests.py @@ -21,8 +21,6 @@ import json from io import BytesIO from unittest import mock from zipfile import is_zipfile, ZipFile -from tests.fixtures.world_bank_dashboard import load_world_bank_dashboard_with_slices -from tests.fixtures.birth_names_dashboard import load_birth_names_dashboard_with_slices import prison import pytest @@ -36,8 +34,10 @@ from superset.models.core import Database from superset.models.reports import ReportSchedule, ReportScheduleType from superset.utils.core import get_example_database, get_main_database from tests.base_tests import SupersetTestCase +from tests.fixtures.birth_names_dashboard import load_birth_names_dashboard_with_slices from tests.fixtures.certificates import ssl_certificate from tests.fixtures.energy_dashboard import load_energy_table_with_slice +from tests.fixtures.world_bank_dashboard import load_world_bank_dashboard_with_slices from tests.fixtures.importexport import ( database_config, dataset_config, @@ -94,6 +94,28 @@ class TestDatabaseApi(SupersetTestCase): db.session.delete(database) db.session.commit() + @pytest.fixture() + def create_database_with_dataset(self): + with self.create_app().app_context(): + example_db = get_example_database() + self._database = self.insert_database( + "database_with_dataset", + example_db.sqlalchemy_uri_decrypted, + expose_in_sqllab=True, + ) + table = SqlaTable( + schema="main", table_name="ab_permission", database=self._database + ) + db.session.add(table) + db.session.commit() + yield self._database + + # rollback changes + db.session.delete(table) + db.session.delete(self._database) + db.session.commit() + self._database = None + def create_database_import(self): buf = BytesIO() with ZipFile(buf, "w") as bundle: @@ -528,15 +550,13 @@ class TestDatabaseApi(SupersetTestCase): rv = self.delete_assert_metric(uri, "delete") self.assertEqual(rv.status_code, 404) + @pytest.mark.usefixtures("create_database_with_dataset") def test_delete_database_with_datasets(self): """ Database API: Test delete fails because it has depending datasets """ - database_id = ( - db.session.query(Database).filter_by(database_name="examples").one() - ).id self.login(username="admin") - uri = f"api/v1/database/{database_id}" + uri = f"api/v1/database/{self._database.id}" rv = self.delete_assert_metric(uri, "delete") self.assertEqual(rv.status_code, 422) diff --git a/tests/datasets/api_tests.py b/tests/datasets/api_tests.py index 5bcaa9090..6b628d7d2 100644 --- a/tests/datasets/api_tests.py +++ b/tests/datasets/api_tests.py @@ -1119,6 +1119,7 @@ class TestDatasetApi(SupersetTestCase): {"VERSIONED_EXPORT": True}, clear=True, ) + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") def test_export_dataset_bundle(self): """ Dataset API: Test export dataset diff --git a/tests/reports/api_tests.py b/tests/reports/api_tests.py index 445320178..5f4922181 100644 --- a/tests/reports/api_tests.py +++ b/tests/reports/api_tests.py @@ -23,7 +23,6 @@ import pytest import prison from sqlalchemy.sql import func -import tests.test_app from superset import db from superset.models.core import Database from superset.models.slice import Slice @@ -36,8 +35,9 @@ from superset.models.reports import ( ReportRecipientType, ReportState, ) - +import tests.test_app from tests.base_tests import SupersetTestCase +from tests.fixtures.birth_names_dashboard import load_birth_names_dashboard_with_slices from tests.reports.utils import insert_report_schedule from superset.utils.core import get_example_database @@ -403,7 +403,7 @@ class TestReportSchedulesApi(SupersetTestCase): rv = self.client.get(uri) assert rv.status_code == 200 - @pytest.mark.usefixtures("create_report_schedules") + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") def test_create_report_schedule(self): """ ReportSchedule Api: Test create report schedule @@ -508,7 +508,7 @@ class TestReportSchedulesApi(SupersetTestCase): rv = self.client.post(uri, json=report_schedule_data) assert rv.status_code == 400 - @pytest.mark.usefixtures("create_report_schedules") + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") def test_create_report_schedule_chart_dash_validation(self): """ ReportSchedule Api: Test create report schedule chart and dashboard validation @@ -556,7 +556,7 @@ class TestReportSchedulesApi(SupersetTestCase): data = json.loads(rv.data.decode("utf-8")) assert data == {"message": {"database": "Database is required for alerts"}} - @pytest.mark.usefixtures("create_report_schedules") + @pytest.mark.usefixtures("load_birth_names_dashboard_with_slices") def test_create_report_schedule_relations_exist(self): """ ReportSchedule Api: Test create report schedule @@ -700,7 +700,9 @@ class TestReportSchedulesApi(SupersetTestCase): rv = self.client.put(uri, json=report_schedule_data) assert rv.status_code == 404 - @pytest.mark.usefixtures("create_report_schedules") + @pytest.mark.usefixtures( + "load_birth_names_dashboard_with_slices", "create_report_schedules" + ) def test_update_report_schedule_chart_dash_validation(self): """ ReportSchedule Api: Test update report schedule chart and dashboard validation @@ -727,7 +729,9 @@ class TestReportSchedulesApi(SupersetTestCase): data = json.loads(rv.data.decode("utf-8")) assert data == {"message": {"chart": "Choose a chart or dashboard not both"}} - @pytest.mark.usefixtures("create_report_schedules") + @pytest.mark.usefixtures( + "load_birth_names_dashboard_with_slices", "create_report_schedules" + ) def test_update_report_schedule_relations_exist(self): """ ReportSchedule Api: Test update report schedule relations exist