Skip to content

Commit

Permalink
test: add unit tests for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Jan 15, 2025
1 parent a0a744b commit 757454b
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions course_discovery/apps/tagging/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""
Test the admin interface for the tagging app.
"""

from django.contrib.admin.sites import site
from django.test import RequestFactory, TestCase

from course_discovery.apps.course_metadata.tests.factories import CourseFactory
from course_discovery.apps.tagging.admin import CourseVerticalFiltersAdmin
from course_discovery.apps.tagging.models import CourseVerticalFilter, SubVerticalFilter, VerticalFilter
from course_discovery.apps.tagging.tests.factories import SubVerticalFilterFactory, VerticalFilterFactory


class AdminTests(TestCase):
def setUp(self):
self.factory = RequestFactory()
self.active_vertical = VerticalFilterFactory(name="Technology", is_active=True)
self.inactive_vertical = VerticalFilterFactory(
name="Inactive Vertical", is_active=False
)

self.active_sub_vertical = SubVerticalFilterFactory(
name="Software Engineering",
vertical_filters=self.active_vertical,
is_active=True,
)
self.inactive_sub_vertical = SubVerticalFilterFactory(
name="Inactive SubVertical",
vertical_filters=self.active_vertical,
is_active=False,
)

self.non_draft_course = CourseFactory(title="Try Course", draft=False)
self.draft_course = CourseFactory(title="Draft Try Course", draft=True)

def test_vertical_filter_admin_registration(self):
self.assertIn(VerticalFilter, site._registry)

def test_sub_vertical_filter_admin_registration(self):
self.assertIn(SubVerticalFilter, site._registry)

def test_course_vertical_filter_admin_registration(self):
self.assertIn(CourseVerticalFilter, site._registry)

def test_formfield_for_foreignkey_filters_active_verticals(self):
admin_instance = CourseVerticalFiltersAdmin(CourseVerticalFilter, site)
request = self.factory.get("/admin/")

formfield = admin_instance.formfield_for_foreignkey(
db_field=CourseVerticalFilter._meta.get_field("vertical"), request=request
)
queryset = formfield.queryset

self.assertIn(self.active_vertical, queryset)
self.assertNotIn(self.inactive_vertical, queryset)

def test_formfield_for_foreignkey_filters_active_sub_verticals(self):
admin_instance = CourseVerticalFiltersAdmin(CourseVerticalFilter, site)
request = self.factory.get("/admin/")

formfield = admin_instance.formfield_for_foreignkey(
db_field=CourseVerticalFilter._meta.get_field("sub_vertical"),
request=request,
)
queryset = formfield.queryset

self.assertIn(self.active_sub_vertical, queryset)
self.assertNotIn(self.inactive_sub_vertical, queryset)

def test_formfield_for_foreignkey_filters_non_draft_courses(self):
admin_instance = CourseVerticalFiltersAdmin(CourseVerticalFilter, site)
request = self.factory.get("/admin/")

formfield = admin_instance.formfield_for_foreignkey(
db_field=CourseVerticalFilter._meta.get_field("course"), request=request
)
queryset = formfield.queryset

self.assertIn(self.non_draft_course, queryset)
self.assertNotIn(self.draft_course, queryset)

0 comments on commit 757454b

Please sign in to comment.