Skip to content

Commit

Permalink
fix: API tests, possible to run independently
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar committed Feb 11, 2021
1 parent 42c4fac commit 8b0ad73
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
3 changes: 2 additions & 1 deletion tests/dashboards/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
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
Expand All @@ -49,6 +48,7 @@
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
Expand Down Expand Up @@ -223,6 +223,7 @@ def test_info_security_database(self):
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
Expand Down
32 changes: 26 additions & 6 deletions tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
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
Expand All @@ -36,8 +34,10 @@
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,
Expand Down Expand Up @@ -94,6 +94,28 @@ def create_database_with_report(self):
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:
Expand Down Expand Up @@ -528,15 +550,13 @@ def test_delete_database_not_found(self):
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)

Expand Down
1 change: 1 addition & 0 deletions tests/datasets/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,7 @@ def test_export_dataset_gamma(self):
{"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
Expand Down
18 changes: 11 additions & 7 deletions tests/reports/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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
Expand All @@ -36,8 +35,9 @@
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

Expand Down Expand Up @@ -403,7 +403,7 @@ def test_get_related_report_schedule(self):
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
Expand Down Expand Up @@ -508,7 +508,7 @@ def test_create_report_schedule_schema(self):
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
Expand Down Expand Up @@ -556,7 +556,7 @@ def test_create_report_schedule_chart_db_validation(self):
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
Expand Down Expand Up @@ -700,7 +700,9 @@ def test_update_report_schedule_not_found(self):
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
Expand All @@ -727,7 +729,9 @@ def test_update_report_schedule_chart_dash_validation(self):
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
Expand Down

0 comments on commit 8b0ad73

Please sign in to comment.