Unit-testing Custom Admin Action, requires request and querysets parameters.
Hi, I would like to unit-test the activate_apps( ) and deactivate_apps( ) functions below, as low-level as possible. How could I do this without going through Selenium (FT) route? In my admin.py: from django.contrib import admin from my_app_listing.models import App class AppAdmin(admin.ModelAdmin): actions = ['activate_apps', 'deactivate_apps'] list_display = ('name', 'is_active', 'frequency') fieldsets = [ (None, {'fields': ['name']}), (None, {'fields': ['is_active']}), (None, {'fields': ['frequency']}), ('Description', {'fields': ['description'], 'classes': ['collapse' ]}), ] def activate_apps(self, request, queryset): queryset.update(is_active=True) activate_apps.short_description = "Activate selected Apps" def deactivate_apps(self, request, queryset): queryset.update(is_active=False) deactivate_apps.short_description = "Deactivate selected Apps" admin.site.register(App, AppAdmin) Thanks in advance -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bea4fcab-8d7c-4613-9454-f5a70181ce5a%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Unit-testing Custom Admin Action, requires request and querysets parameters.
Hello, Would it work to import AppAdmin, instantiate it, and then call the methods in a unit test? Collin -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9338e316-a58c-4ec1-a67a-47c52ffba1bc%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Unit-testing Custom Admin Action, requires request and querysets parameters.
Hi Collin, I apologise for the late update. Didnt get any notifications from your reply. I have solved this issue as per your suggestion. Pasting the solution for others' reference. Thanks ! from django.test import TestCase from django.contrib.admin.sites import AdminSite from batch_apps.models import App from batch_apps.admin import AppAdmin class MockRequest(object): pass request = MockRequest() class AppAdminTest(TestCase): def setUp(self): self.app_admin = AppAdmin(App, AdminSite()) def test_activate_apps_should_activate_inactive_app(self): app1 = App.objects.create(name='Inactive App 001', is_active=False) queryset = App.objects.filter(pk=1) self.app_admin.activate_apps(request, queryset) self.assertTrue(App.objects.get(pk=1).is_active) def test_deactivate_apps_should_deactivate_active_app(self): app1 = App.objects.create(name='Active App 001', is_active=True) queryset = App.objects.filter(pk=1) self.app_admin.deactivate_apps(request, queryset) self.assertFalse(App.objects.get(pk=1).is_active) On Thursday, 6 November 2014 01:05:13 UTC+8, Collin Anderson wrote: > > Hello, > > Would it work to import AppAdmin, instantiate it, and then call the > methods in a unit test? > > Collin > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2f79f583-99a9-4d1c-b384-9733bbf98ec0%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.