From 9ab3ca8db7cdcafc83a33bf5fafce12587594acc Mon Sep 17 00:00:00 2001 From: Jan-Jelle Kester Date: Mon, 21 Oct 2013 22:41:21 +0200 Subject: [PATCH] Write first two tests --- src/testapp/tests.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/testapp/tests.py b/src/testapp/tests.py index acbf4ac..fb29ca8 100644 --- a/src/testapp/tests.py +++ b/src/testapp/tests.py @@ -1,4 +1,5 @@ from django.test import TestCase +from auditlog.models import LogEntry from testapp.models import SimpleModel @@ -6,11 +7,34 @@ class ModelTest(TestCase): def setUp(self): self.obj_simple = SimpleModel.objects.create(text='I am not difficult.') - def test_simple_create(self): - """Mutations on simple models are logged correctly.""" - # Create the object to work with + def test_create(self): + """Creation is logged correctly.""" + # Get the object to work with obj = self.obj_simple + + # 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 - self.assertTrue(obj.history) + 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")