django-auditlog/src/testapp/tests.py

41 lines
1.4 KiB
Python
Raw Normal View History

from django.test import TestCase
2013-10-21 20:41:21 +00:00
from auditlog.models import LogEntry
from testapp.models import SimpleModel
class ModelTest(TestCase):
def setUp(self):
self.obj_simple = SimpleModel.objects.create(text='I am not difficult.')
2013-10-21 20:41:21 +00:00
def test_create(self):
"""Creation is logged correctly."""
# Get the object to work with
obj = self.obj_simple
2013-10-21 20:41:21 +00:00
# Check for log entries
self.assertTrue(obj.history.count() == 1, msg="There is one log entry")
try:
history = obj.history.get()
except LogEntry.DoesNotExist:
self.assertTrue(False, "Log entry exists")
else:
self.assertEqual(history.action, LogEntry.Action.CREATE, msg="Action is 'CREATE'")
self.assertEqual(history.object_repr, str(obj), msg="Representation is equal")
def test_update(self):
"""Updates are logged correctly."""
# Get the object to work with
obj = self.obj_simple
# Change something
obj.boolean = True
obj.save()
# Check for log entries
2013-10-21 20:41:21 +00:00
self.assertTrue(obj.history.filter(action=LogEntry.Action.UPDATE).count() == 1, msg="There is one log entry for 'UPDATE'")
history = obj.history.get(action=LogEntry.Action.UPDATE)
self.assertJSONEqual(history.changes, '{"boolean": ["False", "True"]}', msg="The change is correctly logged")