Skip to content

Commit cf09123

Browse files
committed
Add test coverage for filter preview
1 parent c23af9e commit cf09123

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

tests/htmx/plannedmaintenance/test_views.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from argus.auth.factories import AdminUserFactory, PersonUserFactory
88
from argus.filter.factories import FilterFactory
9+
from argus.htmx.plannedmaintenance.views import FILTER_PREVIEW_LIMIT
10+
from argus.incident.factories import StatefulIncidentFactory, SourceSystemFactory
911
from argus.plannedmaintenance.factories import PlannedMaintenanceFactory
1012
from argus.plannedmaintenance.models import MODIFICATION_WINDOW_PM, PlannedMaintenanceTask
1113
from argus.util.datetime_utils import LOCAL_INFINITY
@@ -284,3 +286,114 @@ def test_search_filters_sorts_current_user_filters_first(self):
284286
response = self.client.get(reverse("htmx:search-filters"))
285287
data = response.json()
286288
self.assertEqual(data["results"][0]["id"], self.user_filter.pk)
289+
290+
291+
@tag("integration")
292+
class FilterPreviewViewTests(TestCase):
293+
def setUp(self):
294+
self.user = PersonUserFactory()
295+
296+
def test_filter_preview_requires_login(self):
297+
response = self.client.get(reverse("htmx:plannedmaintenance-filter-preview"))
298+
self.assertEqual(response.status_code, 302)
299+
300+
def test_filter_preview_without_filters_shows_no_filters(self):
301+
self.client.force_login(self.user)
302+
response = self.client.get(reverse("htmx:plannedmaintenance-filter-preview"))
303+
self.assertTrue(response.context["no_filters"])
304+
305+
def test_filter_preview_with_filters_shows_counts(self):
306+
self.client.force_login(self.user)
307+
# Create an open incident
308+
incident = StatefulIncidentFactory()
309+
310+
# Create a filter that matches by source
311+
filter_with_match = FilterFactory(
312+
user=self.user,
313+
filter={"sourceSystemIds": [incident.source.pk]},
314+
)
315+
316+
response = self.client.get(
317+
reverse("htmx:plannedmaintenance-filter-preview"),
318+
{"filters": [filter_with_match.pk]},
319+
)
320+
321+
self.assertIn("matching_count", response.context)
322+
self.assertIn("matching_percent", response.context)
323+
self.assertIn("total_open", response.context)
324+
self.assertEqual(response.context["matching_count"], 1)
325+
326+
def test_filter_preview_with_no_matching_incidents(self):
327+
self.client.force_login(self.user)
328+
# Create an open incident
329+
StatefulIncidentFactory()
330+
331+
# Create a filter that matches nothing (non-existent source ID)
332+
filter_no_match = FilterFactory(
333+
user=self.user,
334+
filter={"sourceSystemIds": [99999]},
335+
)
336+
337+
response = self.client.get(
338+
reverse("htmx:plannedmaintenance-filter-preview"),
339+
{"filters": [filter_no_match.pk]},
340+
)
341+
342+
self.assertEqual(response.context["matching_count"], 0)
343+
344+
def test_filter_preview_with_no_open_incidents(self):
345+
self.client.force_login(self.user)
346+
# Don't create any incidents - tests division by zero guard
347+
filter_obj = FilterFactory(user=self.user)
348+
349+
response = self.client.get(
350+
reverse("htmx:plannedmaintenance-filter-preview"),
351+
{"filters": [filter_obj.pk]},
352+
)
353+
354+
self.assertEqual(response.context["matching_count"], 0)
355+
self.assertEqual(response.context["matching_percent"], 0)
356+
357+
def test_filter_preview_intersects_multiple_filters(self):
358+
self.client.force_login(self.user)
359+
# Create two incidents with different sources
360+
incident1 = StatefulIncidentFactory()
361+
StatefulIncidentFactory()
362+
363+
# Create two filters: one matching incident1's source, one matching a non-existent source
364+
filter1 = FilterFactory(
365+
user=self.user,
366+
filter={"sourceSystemIds": [incident1.source.pk]},
367+
)
368+
filter2 = FilterFactory(
369+
user=self.user,
370+
filter={"sourceSystemIds": [99999]},
371+
)
372+
373+
response = self.client.get(
374+
reverse("htmx:plannedmaintenance-filter-preview"),
375+
{"filters": [filter1.pk, filter2.pk]},
376+
)
377+
378+
# AND logic: no incident matches both filters
379+
self.assertEqual(response.context["matching_count"], 0)
380+
381+
def test_filter_preview_limits_incident_list(self):
382+
self.client.force_login(self.user)
383+
source = SourceSystemFactory()
384+
# Create more incidents than the limit
385+
for _ in range(7):
386+
StatefulIncidentFactory(source=source)
387+
388+
filter_obj = FilterFactory(
389+
user=self.user,
390+
filter={"sourceSystemIds": [source.pk]},
391+
)
392+
393+
response = self.client.get(
394+
reverse("htmx:plannedmaintenance-filter-preview"),
395+
{"filters": [filter_obj.pk]},
396+
)
397+
398+
self.assertEqual(response.context["matching_count"], 7)
399+
self.assertEqual(len(response.context["incident_list"]), FILTER_PREVIEW_LIMIT)

0 commit comments

Comments
 (0)