Skip to content

Commit f5d0549

Browse files
skaastenclaude
andcommitted
test(dashboards): Move revision unit tests to model layer
Pruning and field-correctness tests for DashboardRevision.create_for_dashboard now live in test_dashboard.py, tested directly without going through HTTP. The endpoint tests are trimmed to only verify the endpoint wires up to the model correctly — they no longer re-test the model's own behaviour. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 145c279 commit f5d0549

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

tests/sentry/dashboards/endpoints/test_organization_dashboard_details.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4014,21 +4014,12 @@ def test_text_widget_errors_provided_queries(self) -> None:
40144014
assert response.data["widgets"][1]["queries"][0] == "Text widgets don't have queries"
40154015

40164016
def test_put_creates_dashboard_revision_when_feature_enabled(self) -> None:
4017-
original_title = self.dashboard.title
40184017
with self.feature("organizations:dashboards-revisions"):
40194018
response = self.do_request(
40204019
"put", self.url(self.dashboard.id), data={"title": "Updated Title"}
40214020
)
40224021
assert response.status_code == 200, response.data
4023-
4024-
revisions = DashboardRevision.objects.filter(dashboard=self.dashboard)
4025-
assert revisions.count() == 1
4026-
revision = revisions.first()
4027-
assert revision is not None
4028-
assert revision.title == original_title
4029-
assert revision.snapshot["title"] == original_title
4030-
assert revision.snapshot_schema_version == 1
4031-
assert revision.created_by_id == self.user.id
4022+
assert DashboardRevision.objects.filter(dashboard=self.dashboard).count() == 1
40324023

40334024
def test_put_does_not_create_revision_when_feature_disabled(self) -> None:
40344025
response = self.do_request(
@@ -4049,28 +4040,6 @@ def test_put_snapshot_contains_pre_save_state(self) -> None:
40494040
self.dashboard.refresh_from_db()
40504041
assert self.dashboard.title == "New Title"
40514042

4052-
def test_put_deletes_revisions_beyond_retention_limit(self) -> None:
4053-
# Create 10 existing revisions
4054-
for i in range(10):
4055-
DashboardRevision.objects.create(
4056-
dashboard=self.dashboard,
4057-
created_by_id=self.user.id,
4058-
title=f"Revision {i}",
4059-
snapshot={},
4060-
snapshot_schema_version=1,
4061-
)
4062-
4063-
assert DashboardRevision.objects.filter(dashboard=self.dashboard).count() == 10
4064-
4065-
with self.feature("organizations:dashboards-revisions"):
4066-
response = self.do_request(
4067-
"put", self.url(self.dashboard.id), data={"title": "Updated Title"}
4068-
)
4069-
assert response.status_code == 200, response.data
4070-
4071-
# After creating the 11th revision, the oldest should be deleted to maintain 10
4072-
assert DashboardRevision.objects.filter(dashboard=self.dashboard).count() == 10
4073-
40744043
def test_put_snapshot_includes_widgets_and_queries(self) -> None:
40754044
with self.feature("organizations:dashboards-revisions"):
40764045
self.do_request("put", self.url(self.dashboard.id), data={"title": "New Title"})

tests/sentry/models/test_dashboard.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from sentry.models.dashboard import Dashboard, DashboardFavoriteUser
1+
from sentry.models.dashboard import Dashboard, DashboardFavoriteUser, DashboardRevision
22
from sentry.models.organization import Organization
33
from sentry.testutils.cases import TestCase
44
from sentry.users.models.user import User
@@ -187,3 +187,57 @@ def test_deletes_and_increments_existing_positions(self) -> None:
187187
assert unfavorited.position is None
188188
assert dashboard is not None
189189
assert dashboard.position == 0
190+
191+
192+
class DashboardRevisionTest(TestCase):
193+
def setUp(self) -> None:
194+
super().setUp()
195+
self.dashboard = self.create_dashboard(title="My Dashboard", organization=self.organization)
196+
197+
def test_create_for_dashboard_stores_correct_fields(self) -> None:
198+
snapshot = {"title": "My Dashboard", "widgets": []}
199+
revision = DashboardRevision.create_for_dashboard(self.dashboard, self.user, snapshot)
200+
201+
assert revision.dashboard == self.dashboard
202+
assert revision.title == self.dashboard.title
203+
assert revision.snapshot == snapshot
204+
assert revision.snapshot_schema_version == DashboardRevision.SNAPSHOT_SCHEMA_VERSION
205+
assert revision.created_by_id == self.user.id
206+
207+
def test_create_for_dashboard_prunes_beyond_retention_limit(self) -> None:
208+
for i in range(DashboardRevision.RETENTION_LIMIT):
209+
DashboardRevision.objects.create(
210+
dashboard=self.dashboard,
211+
created_by_id=self.user.id,
212+
title=f"Revision {i}",
213+
snapshot={},
214+
snapshot_schema_version=DashboardRevision.SNAPSHOT_SCHEMA_VERSION,
215+
)
216+
217+
DashboardRevision.create_for_dashboard(self.dashboard, self.user, {})
218+
219+
assert (
220+
DashboardRevision.objects.filter(dashboard=self.dashboard).count()
221+
== DashboardRevision.RETENTION_LIMIT
222+
)
223+
224+
def test_create_for_dashboard_retains_newest_revisions(self) -> None:
225+
titles = [f"Revision {i}" for i in range(DashboardRevision.RETENTION_LIMIT)]
226+
for title in titles:
227+
DashboardRevision.objects.create(
228+
dashboard=self.dashboard,
229+
created_by_id=self.user.id,
230+
title=title,
231+
snapshot={},
232+
snapshot_schema_version=DashboardRevision.SNAPSHOT_SCHEMA_VERSION,
233+
)
234+
235+
new_revision = DashboardRevision.create_for_dashboard(self.dashboard, self.user, {})
236+
237+
surviving_ids = set(
238+
DashboardRevision.objects.filter(dashboard=self.dashboard).values_list("id", flat=True)
239+
)
240+
# The oldest revision was pruned; the new one and the 9 most recent survive
241+
oldest = DashboardRevision.objects.filter(title="Revision 0").first()
242+
assert oldest is None
243+
assert new_revision.id in surviving_ids

0 commit comments

Comments
 (0)