Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AAfghahi committed Aug 24, 2021
1 parent 9826aa2 commit 95a7207
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 8 deletions.
4 changes: 3 additions & 1 deletion superset/models/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def copy_dashboard(
)


class Dashboard(Model, AuditMixinNullable, ImportExportMixin):
class Dashboard( # pylint: disable=too-many-instance-attributes
Model, AuditMixinNullable, ImportExportMixin
):
"""The dashboard object!"""

__tablename__ = "dashboards"
Expand Down
2 changes: 1 addition & 1 deletion superset/models/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
logger = logging.getLogger(__name__)


class Slice( # pylint: disable=too-many-public-methods
class Slice( # pylint: disable=too-many-public-methods, too-many-instance-attributes
Model, AuditMixinNullable, ImportExportMixin
):
"""A slice is essentially a report or a view on data"""
Expand Down
1 change: 0 additions & 1 deletion superset/reports/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def validate_chart_dashboard(
return

if creation_method == ReportCreationMethodType.DASHBOARDS and not dashboard_id:
print("I am here")
exceptions.append(DashboardNotSavedValidationError())
return

Expand Down
3 changes: 2 additions & 1 deletion superset/reports/commands/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ class ReportScheduleCreationMethodUniquenessValidationError(ValidationError):

def __init__(self) -> None:
super().__init__(
[_("Resource already has an attached report")], field_name="creation_method"
[_("Resource already has an attached report.")],
field_name="creation_method",
)


Expand Down
3 changes: 2 additions & 1 deletion superset/reports/dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ def validate_unique_creation_method(

if query is not None and chart_id is not None:
query = query.filter(ReportSchedule.chart_id == chart_id)
return query.one_or_none()
print(query)
return not db.session.query(query.exists()).scalar()

@staticmethod
def validate_update_uniqueness(
Expand Down
93 changes: 90 additions & 3 deletions tests/integration_tests/reports/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -784,15 +784,102 @@ def test_no_dashboard_report_schedule_schema(self):
uri = "api/v1/report/"
rv = self.client.post(uri, json=report_schedule_data)
data = json.loads(rv.data.decode("utf-8"))
print(rv)
assert rv.status_code == 422
print(data)
print(data["message"])
assert (
data["message"]["dashboard"]
== "Please save your dashboard first, then try creating a new email report."
)

@pytest.mark.usefixtures(
"load_birth_names_dashboard_with_slices", "create_report_schedules"
)
def test_create_multiple_creation_method_report_schedule_charts(self):
"""
ReportSchedule Api: Test create multiple reports with the same creation method
"""
self.login(username="admin")
chart = db.session.query(Slice).first()
dashboard = db.session.query(Dashboard).first()
example_db = get_example_database()
report_schedule_data = {
"type": ReportScheduleType.REPORT,
"name": "name4",
"description": "description",
"creation_method": ReportCreationMethodType.CHARTS,
"crontab": "0 9 * * *",
"working_timeout": 3600,
"chart": chart.id,
}
uri = "api/v1/report/"
rv = self.client.post(uri, json=report_schedule_data)
data = json.loads(rv.data.decode("utf-8"))
print(data)
assert rv.status_code == 201

# this second time it should receive an error because the chart has an attached report
# with the same creation method from the same user.
report_schedule_data = {
"type": ReportScheduleType.REPORT,
"name": "name5",
"description": "description",
"creation_method": ReportCreationMethodType.CHARTS,
"crontab": "0 9 * * *",
"working_timeout": 3600,
"chart": chart.id,
}
uri = "api/v1/report/"
rv = self.client.post(uri, json=report_schedule_data)
data = json.loads(rv.data.decode("utf-8"))
assert rv.status_code == 422
assert data["message"]["creation_method"] == [
"Resource already has an attached report."
]

@pytest.mark.usefixtures(
"load_birth_names_dashboard_with_slices", "create_report_schedules"
)
def test_create_multiple_creation_method_report_schedule_dashboards(self):
"""
ReportSchedule Api: Test create multiple reports with the same creation method
"""
self.login(username="admin")
chart = db.session.query(Slice).first()
dashboard = db.session.query(Dashboard).first()
example_db = get_example_database()
report_schedule_data = {
"type": ReportScheduleType.REPORT,
"name": "name4",
"description": "description",
"creation_method": ReportCreationMethodType.DASHBOARDS,
"crontab": "0 9 * * *",
"working_timeout": 3600,
"dashboard": dashboard.id,
}
uri = "api/v1/report/"
rv = self.client.post(uri, json=report_schedule_data)
data = json.loads(rv.data.decode("utf-8"))
print(data)
assert rv.status_code == 201

# this second time it should receive an error because the dashboard has an attached report
# with the same creation method from the same user.
report_schedule_data = {
"type": ReportScheduleType.REPORT,
"name": "name5",
"description": "description",
"creation_method": ReportCreationMethodType.DASHBOARDS,
"crontab": "0 9 * * *",
"working_timeout": 3600,
"dashboard": dashboard.id,
}
uri = "api/v1/report/"
rv = self.client.post(uri, json=report_schedule_data)
data = json.loads(rv.data.decode("utf-8"))
assert rv.status_code == 422
assert data["message"]["creation_method"] == [
"Resource already has an attached report."
]

@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices")
def test_create_report_schedule_chart_dash_validation(self):
"""
Expand Down

0 comments on commit 95a7207

Please sign in to comment.