2013-10-23 15:10:59 +00:00
import datetime
2013-10-21 19:46:31 +00:00
from django . test import TestCase
2013-10-21 20:41:21 +00:00
from auditlog . models import LogEntry
2013-10-23 15:10:59 +00:00
from testapp . models import SimpleModel , AltPrimaryKeyModel , ProxyModel
2013-10-21 19:46:31 +00:00
2013-10-23 15:10:59 +00:00
class SimpleModelTest ( TestCase ) :
2013-10-21 19:46:31 +00:00
def setUp ( self ) :
2013-10-23 15:10:59 +00:00
self . obj = SimpleModel . objects . create ( text = ' I am not difficult. ' )
2013-10-21 19:46:31 +00:00
2013-10-21 20:41:21 +00:00
def test_create ( self ) :
""" Creation is logged correctly. """
# Get the object to work with
2013-10-23 15:10:59 +00:00
obj = self . obj
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 ( )
2013-10-23 15:10:59 +00:00
except obj . history . DoesNotExist :
2013-10-21 20:41:21 +00:00
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
2013-10-23 15:10:59 +00:00
obj = self . obj
2013-10-21 20:41:21 +00:00
# Change something
obj . boolean = True
2013-10-21 19:46:31 +00:00
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 " )
2013-10-23 15:10:59 +00:00
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. ' )