-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0a744b
commit 757454b
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |