mirror of
https://github.com/jazzband/django-auditlog.git
synced 2026-03-16 22:20:26 +00:00
Write some more tests
This commit is contained in:
parent
ecf20dfe33
commit
6dd93496b6
2 changed files with 43 additions and 8 deletions
|
|
@ -22,9 +22,13 @@ class AltPrimaryKeyModel(models.Model):
|
|||
"""
|
||||
|
||||
key = models.CharField(max_length=100, primary_key=True)
|
||||
value = models.DecimalField(decimal_places=4, max_digits=12)
|
||||
|
||||
history = AuditLogHistoryField()
|
||||
text = models.TextField(blank=True)
|
||||
boolean = models.BooleanField(default=False)
|
||||
integer = models.IntegerField(blank=True, null=True)
|
||||
datetime = models.DateTimeField(auto_now=True)
|
||||
|
||||
history = AuditLogHistoryField(pk_indexable=False)
|
||||
|
||||
|
||||
class ProxyModel(SimpleModel):
|
||||
|
|
@ -57,3 +61,5 @@ class ManyRelatedModel(models.Model):
|
|||
|
||||
|
||||
auditlog.register(SimpleModel)
|
||||
auditlog.register(AltPrimaryKeyModel)
|
||||
auditlog.register(ProxyModel)
|
||||
|
|
|
|||
|
|
@ -1,23 +1,24 @@
|
|||
import datetime
|
||||
from django.test import TestCase
|
||||
from auditlog.models import LogEntry
|
||||
from testapp.models import SimpleModel
|
||||
from testapp.models import SimpleModel, AltPrimaryKeyModel, ProxyModel
|
||||
|
||||
|
||||
class ModelTest(TestCase):
|
||||
class SimpleModelTest(TestCase):
|
||||
def setUp(self):
|
||||
self.obj_simple = SimpleModel.objects.create(text='I am not difficult.')
|
||||
self.obj = SimpleModel.objects.create(text='I am not difficult.')
|
||||
|
||||
def test_create(self):
|
||||
"""Creation is logged correctly."""
|
||||
# Get the object to work with
|
||||
obj = self.obj_simple
|
||||
obj = self.obj
|
||||
|
||||
# Check for log entries
|
||||
self.assertTrue(obj.history.count() == 1, msg="There is one log entry")
|
||||
|
||||
try:
|
||||
history = obj.history.get()
|
||||
except LogEntry.DoesNotExist:
|
||||
except obj.history.DoesNotExist:
|
||||
self.assertTrue(False, "Log entry exists")
|
||||
else:
|
||||
self.assertEqual(history.action, LogEntry.Action.CREATE, msg="Action is 'CREATE'")
|
||||
|
|
@ -26,7 +27,7 @@ class ModelTest(TestCase):
|
|||
def test_update(self):
|
||||
"""Updates are logged correctly."""
|
||||
# Get the object to work with
|
||||
obj = self.obj_simple
|
||||
obj = self.obj
|
||||
|
||||
# Change something
|
||||
obj.boolean = True
|
||||
|
|
@ -38,3 +39,31 @@ class ModelTest(TestCase):
|
|||
history = obj.history.get(action=LogEntry.Action.UPDATE)
|
||||
|
||||
self.assertJSONEqual(history.changes, '{"boolean": ["False", "True"]}', msg="The change is correctly logged")
|
||||
|
||||
def test_delete(self):
|
||||
"""Deletion is logged correctly."""
|
||||
# Get the object to work with
|
||||
obj = self.obj
|
||||
|
||||
history = obj.history.latest()
|
||||
|
||||
# Delete the object
|
||||
obj.delete()
|
||||
|
||||
# Check for log entries
|
||||
self.assertTrue(LogEntry.objects.filter(content_type=history.content_type, object_pk=history.object_pk, action=LogEntry.Action.DELETE).count() == 1, msg="There is one log entry for 'DELETE'")
|
||||
|
||||
def test_recreate(self):
|
||||
SimpleModel.objects.all().delete()
|
||||
self.setUp()
|
||||
self.test_create()
|
||||
|
||||
|
||||
class AltPrimaryKeyModelTest(SimpleModelTest):
|
||||
def setUp(self):
|
||||
self.obj = AltPrimaryKeyModel.objects.create(key=str(datetime.datetime.now()), text='I am strange.')
|
||||
|
||||
|
||||
class ProxyModelTest(SimpleModelTest):
|
||||
def setUp(self):
|
||||
self.obj = ProxyModel.objects.create(text='I am not what you think.')
|
||||
|
|
|
|||
Loading…
Reference in a new issue