python - Unit Test Cases for Django Admin.py operations -
this first django project. in admin.py file have defined fields , search_fields. wondering how can write test cases validate these.
code snippet:
class categoryadmin(admin.modeladmin): fields = ['name'] search_fields = ['name']
is required have unit test cases these things?
i mess names in search_fields, list_display, etc. put following test case first attempt validate. ran across question when searching stack overflow better or more complete solutions , figured might drop code here since question didn't have answers.
model_classnames = ['mymodelx', 'mymodely', 'mymodelz'] admin_fieldname_lists = ['list_display', 'search_fields'] class testadminconfig(testcase): def test_admin_fieldname_lists(self): model_classname in model_classnames: model_class = locate("tasks.models.%s" % model_classname) admin_class = locate("tasks.admin.%sadmin" % model_classname) #print("classes: %s, %s" % (model_class.__name__, admin_class.__name__)) list_name in admin_fieldname_lists: #print(" list: %s" % list_name) fieldname in getattr(admin_class, list_name): fieldname = fieldname.replace("^", "") #print(" field: %s" % fieldname) if fieldname in dir(model_class): continue if fieldname in dir(admin_class): continue model_class.objects.filter(**{fieldname:none})
in nutshell, cycles through model / fieldname combos , decides given "fieldname" ok if can found on model class, corresponding modeladmin class, or more complicated string "foo__auth_user__first_name" works filter. code removes "^" characters prefix search_fields.
since question old, did find own solution? if so, it?
Comments
Post a Comment