Commit 676934d8 by Dudás Ádám

firewall: tests for admin.HostAdmin

parent 2517c6f5
...@@ -20,12 +20,11 @@ class HostAdmin(admin.ModelAdmin): ...@@ -20,12 +20,11 @@ class HostAdmin(admin.ModelAdmin):
filter_horizontal = ('groups', ) filter_horizontal = ('groups', )
inlines = (RuleInline, RecordInline) inlines = (RuleInline, RecordInline)
def groups_l(self, instance): @staticmethod
def groups_l(instance):
"""Returns instance's groups' names as a comma-separated list.""" """Returns instance's groups' names as a comma-separated list."""
retval = [] names = [group.name for group in instance.groups.all()]
for group in instance.groups.all(): return u', '.join(names)
retval.append(group.name)
return u', '.join(retval)
class HostInline(contrib.admin.TabularInline): class HostInline(contrib.admin.TabularInline):
model = Host model = Host
......
""" from django.test import TestCase
This file demonstrates writing tests using the unittest module. These will from admin import HostAdmin
pass when you run "manage.py test".
Replace this with more appropriate tests for your application. class MockInstance:
""" def __init__(self, groups):
self.groups = MockGroups(groups)
from django.test import TestCase class MockGroup:
def __init__(self, name):
self.name = name
class MockGroups:
def __init__(self, groups):
self.groups = groups
def all(self):
return self.groups
class HostAdminNoGroupTestCase(TestCase):
def runTest(self):
instance = MockInstance([])
l = HostAdmin.groups_l(instance)
self.assertEqual(l, "")
class HostAdminSingleGroupTestCase(TestCase):
def runTest(self):
instance = MockInstance([MockGroup("alma")])
l = HostAdmin.groups_l(instance)
self.assertEqual(l, "alma")
class SimpleTest(TestCase): class HostAdminMultipleGroupsTestCase(TestCase):
def test_basic_addition(self): def runTest(self):
""" instance = MockInstance([MockGroup("alma"),
Tests that 1 + 1 always equals 2. MockGroup("korte"), MockGroup("szilva")])
""" l = HostAdmin.groups_l(instance)
self.assertEqual(1 + 1, 2) self.assertEqual(l, "alma, korte, szilva")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment