0

I created a custom functions in django admin.py and im trying to test the using coverage :

class Responsible(admin.ModelAdmin):

   """
    Inherits of admin class
  """

   list_display = ('user', 'Name', 'last_name', 'process',)
   search_fields = ('p__name', 'user__username')

   def User_Name(self, obj):
       return obj.user.first_name

   def User_Last_Name(self, obj):
       return obj.user.last_name

Responsible model has a django user model foreign key...So far i tried a lot of ways to test:

class AdminTestCase(TestCase):

    fixtures = ["initial_data.json"]


    def test_first_name(self):
        rsf = Responsible.objects.get(id = 1)
        User_Name(rsf)

    def test_first_name2(self):
        self.obj = Responsible.objects.get(id = 1)

But nothing works....any help please ?

Thanks in advance !!

jsanchezs
  • 1,992
  • 3
  • 25
  • 51

4 Answers4

1

You will have to use django client and open list page for Responsible model in django admin.

https://docs.djangoproject.com/en/1.10/topics/testing/tools/#overview-and-a-quick-example

By opening admin list page your custom functions will be called and hence will covered under tests.

So basically something like below needs to be done:

from django.test import Client, TestCase

class BaseTestCase(TestCase):
    """Base TestCase with utilites to create user and login client."""

    def setUp(self):
        """Class setup."""
        self.client = Client()
        self.index_url = '/'
        self.login()

    def create_user(self):
        """Create user and returns username, password tuple."""
        username, password = 'admin', 'test'
        user = User.objects.get_or_create(
            username=username,
            email='admin@test.com',
            is_superuser=True
        )[0]
        user.set_password(password)
        user.save()
        self.user = user
        return (username, password)

    def login(self):
        """Log in client session."""
        username, password = self.create_user()
        self.client.login(username=username, password=password)


class AdminTestCase(BaseTestCase):

    def test_responsible_list(self):
        response = self.client.get('/admin/responsilbe_list/')
        # assertions....
Falloutcoder
  • 991
  • 6
  • 19
  • Thanks for your answer, never done that...could you give me an example in your answer for the first function called "User_Name" ? – jsanchezs Nov 21 '16 at 16:24
  • Updated answer. – Falloutcoder Nov 21 '16 at 16:37
  • I appreciate your help, but i think that's not exactly what i need, i already tested login and logout...what i need is to test those functions i posted – jsanchezs Nov 21 '16 at 16:46
  • `def test_responsible_list(self):` will test it. By opening the list page in admin for your model `Responsible`, done by `self.client.get('/admin/responsilbe_list/')` will eventually call your admin custom methods. – Falloutcoder Nov 21 '16 at 16:48
  • I found another way, i voted you up anyway for your help and guidance. Thanks !! – jsanchezs Nov 21 '16 at 20:05
  • too long way for such easy task. Just import admin class, then in TestCase test its custom functions as plain and simple as any function. – Alex V Feb 12 '19 at 15:04
0

Actually i found it, and it was quite simple if someone ever need it :

def test_first_name_admin(self):
    rsf = ResponsibleStateFlow.objects.get(id = 1)
    ResponsibleStateFlowAdmin.User_Name(self, rsf)
    ResponsibleStateFlowAdmin.User_Last_Name(self, rsf)
jsanchezs
  • 1,992
  • 3
  • 25
  • 51
0

Complete, fast and easy:

from [your-app].admin.py import Responsible  # better name it ResponsibleAdmin

class AdminTestCase(TestCase):

fixtures = ["initial_data.json"]


def test_first_name(self):
    r = Responsible.objects.get(id = 1)
    admin_function_result = Responsible.User_Name(Responsible, obj=r)
    self.assertEquals(admin_function_result, r.user.first_name)  # alternatively, pass first_name as string 

No need to use admin logins. Test plain functions as plain functions.

Alex V
  • 95
  • 2
  • 10
0

The custom function needs an object derived from the queryset of the view. This should work.

from django.contrib import admin

from myapp.admin import ResponsibleAdmin  # renamed to distinguish from model class

class AdminTestCase(TestCase):

    fixtures = ["initial_data.json"]  # as in the question

    def test_first_name(self):
        r = Responsible.objects.get(id=1)
        r_admin = ResponsibleAdmin(Responsible, admin.site)
        obj = r_admin.get_object(None, r.id)
        admin_first_name = r_admin.User_Name(obj)
        self.assertEquals(admin_first_name, r.user.first_name) 
omushpapa
  • 1,663
  • 20
  • 25