<s> from django . conf import settings <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . test import TestCase , Client <EOL> from django . contrib . auth . models import User <EOL> from django . db import models <EOL> from softdelete . test_softdelete_app . models import TestModelOne , TestModelTwo , TestModelThree , TestModelThrough <EOL> from softdelete . models import * <EOL> from softdelete . signals import * <EOL> import logging <EOL> class BaseTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . tmo1 = TestModelOne . objects . create ( extra_bool = True ) <EOL> self . tmo2 = TestModelOne . objects . create ( extra_bool = False ) <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> if x % <NUM_LIT:2> : <EOL> parent = self . tmo1 <EOL> else : <EOL> parent = self . tmo2 <EOL> TestModelTwo . objects . create ( extra_int = x , tmo = parent ) <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> if x % <NUM_LIT:2> : <EOL> left_side = self . tmo1 <EOL> else : <EOL> left_side = self . tmo2 <EOL> for x in range ( <NUM_LIT:10> ) : <EOL> t3 = TestModelThree . objects . create ( ) <EOL> tmt = TestModelThrough . objects . create ( tmo1 = left_side , tmo3 = t3 ) <EOL> self . user = User . objects . create_user ( username = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , <EOL> email = '<STR_LIT>' ) <EOL> gr = create_group ( ) <EOL> if USE_SOFTDELETE_GROUP : <EOL> gr = Group . objects . get ( name = "<STR_LIT>" ) <EOL> self . user . groups . add ( gr ) <EOL> self . user . save ( ) <EOL> gr . save ( ) <EOL> else : <EOL> assign_permissions ( self . user ) <EOL> self . user . save ( ) <EOL> self . unauthorized = User . objects . create_user ( username = '<STR_LIT>' , <EOL> password = '<STR_LIT>' , <EOL> email = '<STR_LIT>' ) <EOL> class InitialTest ( BaseTest ) : <EOL> def test_simple_delete ( self ) : <EOL> self . assertEquals ( <NUM_LIT:2> , TestModelOne . objects . count ( ) ) <EOL> self . assertEquals ( <NUM_LIT:10> , TestModelTwo . objects . count ( ) ) <EOL> self . assertEquals ( <NUM_LIT:2> , <EOL> TestModelOne . objects . all_with_deleted ( ) . count ( ) ) <EOL> self . assertEquals ( <NUM_LIT:10> , <EOL> TestModelTwo . objects . all_with_deleted ( ) . count ( ) ) <EOL> self . tmo1 . delete ( ) <EOL> self . assertEquals ( <NUM_LIT:1> , TestModelOne . objects . count ( ) ) <EOL> self . assertEquals ( <NUM_LIT:5> , TestModelTwo . objects . count ( ) ) <EOL> self . assertEquals ( <NUM_LIT:2> , <EOL> TestModelOne . objects . all_with_deleted ( ) . count ( ) ) <EOL> self . assertEquals ( <NUM_LIT:10> , <EOL> TestModelTwo . objects . all_with_deleted ( ) . count ( ) ) <EOL> class DeleteTest ( BaseTest ) : <EOL> def pre_delete ( self , * args , ** kwargs ) : <EOL> self . pre_delete_called = True <EOL> def post_delete ( self , * args , ** kwargs ) : <EOL> self . post_delete_called = True <EOL> def pre_soft_delete ( self , * args , ** kwargs ) : <EOL> self . pre_soft_delete_called = True <EOL> def post_soft_delete ( self , * args , ** kwargs ) : <EOL> self . post_soft_delete_called = True <EOL> def _pretest ( self ) : <EOL> self . pre_delete_called = False <EOL> self . post_delete_called = False <EOL> self . pre_soft_delete_called = False <EOL> self . post_soft_delete_called = False <EOL> models . signals . pre_delete . connect ( self . pre_delete ) <EOL> models . signals . post_delete . connect ( self . post_delete ) <EOL> pre_soft_delete . connect ( self . pre_soft_delete ) <EOL> post_soft_delete . connect ( self . post_soft_delete ) <EOL> self . assertEquals ( <NUM_LIT:2> , TestModelOne . objects . count ( ) ) <EOL> self . assertEquals ( <NUM_LIT:10> , TestModelTwo . objects . count ( ) ) <EOL> self . assertFalse ( self . tmo1 . deleted ) <EOL> self . assertFalse ( self . pre_delete_called ) <EOL> self . assertFalse ( self . post_delete_called ) <EOL> self . assertFalse ( self . pre_soft_delete_called ) <EOL> self . assertFalse ( self . post_soft_delete_called ) <EOL> self . cs_count = ChangeSet . objects . count ( ) <EOL> self . rs_count = SoftDeleteRecord . objects . count ( ) <EOL> def _posttest ( self ) : <EOL> self . tmo1 = TestModelOne . objects . get ( pk = self . tmo1 . pk ) <EOL> self . tmo2 = TestModelOne . objects . get ( pk = self . tmo2 . pk ) <EOL> self . assertTrue ( self . tmo1 . deleted ) <EOL> self . assertFalse ( self . tmo2 . deleted ) <EOL> self . assertTrue ( self . pre_delete_called ) <EOL> self . assertTrue ( self . post_delete_called ) <EOL> self . assertTrue ( self . pre_soft_delete_called ) <EOL> self . assertTrue ( self . post_soft_delete_called ) <EOL> self . tmo1 . undelete ( ) <EOL> def test_delete ( self ) : <EOL> self . _pretest ( ) <EOL> self . tmo1 . delete ( ) <EOL> self . assertEquals ( self . cs_count + <NUM_LIT:1> , ChangeSet . objects . count ( ) ) <EOL> self . assertEquals ( self . rs_count + <NUM_LIT> , SoftDeleteRecord . objects . count ( ) ) <EOL> self . _posttest ( ) <EOL> def test_hard_delete ( self ) : <EOL> self . _pretest ( ) <EOL> tmo_tmp = TestModelOne . objects . create ( extra_bool = True ) <EOL> tmo_tmp . delete ( ) <EOL> self . assertEquals ( self . cs_count + <NUM_LIT:1> , ChangeSet . objects . count ( ) ) <EOL> self . assertEquals ( self . rs_count + <NUM_LIT:1> , SoftDeleteRecord . objects . count ( ) ) <EOL> tmo_tmp . delete ( ) <EOL> self . assertEquals ( self . cs_count , ChangeSet . objects . count ( ) ) <EOL> self . assertEquals ( self . rs_count , SoftDeleteRecord . objects . count ( ) ) <EOL> self . assertRaises ( TestModelOne . DoesNotExist , <EOL> TestModelOne . objects . get , <EOL> pk = tmo_tmp . pk ) <EOL> def test_filter_delete ( self ) : <EOL> self . _pretest ( ) <EOL> TestModelOne . objects . filter ( pk = <NUM_LIT:1> ) . delete ( ) <EOL> self . assertEquals ( self . cs_count + <NUM_LIT:1> , ChangeSet . objects . count ( ) ) <EOL> self . assertEquals ( self . rs_count + <NUM_LIT> , SoftDeleteRecord . objects . count ( ) ) <EOL> self . _posttest ( ) <EOL> class AdminTest ( BaseTest ) : <EOL> def test_admin ( self ) : <EOL> client = Client ( ) <EOL> u = User . objects . create_user ( username = '<STR_LIT>' , password = '<STR_LIT:test>' , <EOL> email = '<STR_LIT>' ) <EOL> u . is_staff = True <EOL> u . is_superuser = True <EOL> u . save ( ) <EOL> self . assertFalse ( self . tmo1 . deleted ) <EOL> client . login ( username = '<STR_LIT>' , password = '<STR_LIT:test>' ) <EOL> tmo = client . get ( '<STR_LIT>' ) <EOL> self . assertEquals ( tmo . status_code , <NUM_LIT:200> ) <EOL> tmo = client . post ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : '<STR_LIT:1>' , '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> self . assertEquals ( tmo . status_code , <NUM_LIT> ) <EOL> self . tmo1 = TestModelOne . objects . get ( pk = self . tmo1 . pk ) <EOL> self . assertTrue ( self . tmo1 . deleted ) <EOL> class AuthorizationTest ( BaseTest ) : <EOL> def test_permission_needed ( self ) : <EOL> cl = Client ( ) <EOL> cl . login ( username = '<STR_LIT>' , <EOL> password = '<STR_LIT>' ) <EOL> rv = cl . get ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEquals ( rv . status_code , <NUM_LIT> ) <EOL> rv = cl . get ( reverse ( '<STR_LIT>' , args = ( <NUM_LIT:1> , ) ) ) <EOL> self . assertEquals ( rv . status_code , <NUM_LIT> ) <EOL> rv = cl . get ( reverse ( '<STR_LIT>' , args = ( <NUM_LIT:1> , ) ) ) <EOL> self . assertEquals ( rv . status_code , <NUM_LIT> ) <EOL> class UndeleteTest ( BaseTest ) : <EOL> def pre_undelete ( self , * args , ** kwargs ) : <EOL> self . pre_undelete_called = True <EOL> def post_undelete ( self , * args , ** kwargs ) : <EOL> self . post_undelete_called = True <EOL> def test_undelete ( self ) : <EOL> self . pre_undelete_called = False <EOL> self . post_undelete_called = False <EOL> pre_undelete . connect ( self . pre_undelete ) <EOL> post_undelete . connect ( self . post_undelete ) <EOL> self . assertFalse ( self . pre_undelete_called ) <EOL> self . assertFalse ( self . post_undelete_called ) <EOL> self . cs_count = ChangeSet . objects . count ( ) <EOL> self . rs_count = SoftDeleteRecord . objects . count ( ) <EOL> self . tmo1 . delete ( ) <EOL> self . assertEquals ( self . cs_count + <NUM_LIT:1> , ChangeSet . objects . count ( ) ) <EOL> self . assertEquals ( self . rs_count + <NUM_LIT> , SoftDeleteRecord . objects . count ( ) ) <EOL> self . tmo1 = TestModelOne . objects . get ( pk = self . tmo1 . pk ) <EOL> self . tmo2 = TestModelOne . objects . get ( pk = self . tmo2 . pk ) <EOL> self . assertTrue ( self . tmo1 . deleted ) <EOL> self . assertFalse ( self . tmo2 . deleted ) <EOL> self . tmo1 . undelete ( ) <EOL> self . assertEquals ( <NUM_LIT:0> , ChangeSet . objects . count ( ) ) <EOL> self . assertEquals ( <NUM_LIT:0> , SoftDeleteRecord . objects . count ( ) ) <EOL> self . tmo1 = TestModelOne . objects . get ( pk = self . tmo1 . pk ) <EOL> self . tmo2 = TestModelOne . objects . get ( pk = self . tmo2 . pk ) <EOL> self . assertFalse ( self . tmo1 . deleted ) <EOL> self . assertFalse ( self . tmo2 . deleted ) <EOL> self . assertTrue ( self . pre_undelete_called ) <EOL> self . assertTrue ( self . post_undelete_called ) <EOL> self . tmo1 . delete ( ) <EOL> self . assertEquals ( self . cs_count + <NUM_LIT:1> , ChangeSet . objects . count ( ) ) <EOL> self . assertEquals ( self . rs_count + <NUM_LIT> , SoftDeleteRecord . objects . count ( ) ) <EOL> self . tmo1 = TestModelOne . objects . get ( pk = self . tmo1 . pk ) <EOL> self . tmo2 = TestModelOne . objects . get ( pk = self . tmo2 . pk ) <EOL> self . assertTrue ( self . tmo1 . deleted ) <EOL> self . assertFalse ( self . tmo2 . deleted ) <EOL> TestModelOne . objects . deleted_set ( ) . undelete ( ) <EOL> self . assertEquals ( <NUM_LIT:0> , ChangeSet . objects . count ( ) ) <EOL> self . assertEquals ( <NUM_LIT:0> , SoftDeleteRecord . objects . count ( ) ) <EOL> self . tmo1 = TestModelOne . objects . get ( pk = self . tmo1 . pk ) <EOL> self . tmo2 = TestModelOne . objects . get ( pk = self . tmo2 . pk ) <EOL> self . assertFalse ( self . tmo1 . deleted ) <EOL> self . assertFalse ( self . tmo2 . deleted ) <EOL> self . assertTrue ( self . pre_undelete_called ) <EOL> self . assertTrue ( self . post_undelete_called ) <EOL> class M2MTests ( BaseTest ) : <EOL> def test_m2mdelete ( self ) : <EOL> t3 = TestModelThree . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertFalse ( t3 . deleted ) <EOL> for x in t3 . tmos . all ( ) : <EOL> self . assertFalse ( x . deleted ) <EOL> t3 . delete ( ) <EOL> for x in t3 . tmos . all ( ) : <EOL> self . assertFalse ( x . deleted ) <EOL> class SoftDeleteRelatedFieldLookupsTests ( BaseTest ) : <EOL> def test_related_foreign_key ( self ) : <EOL> tmt1 = TestModelTwo . objects . create ( extra_int = <NUM_LIT:100> , tmo = self . tmo1 ) <EOL> tmt2 = TestModelTwo . objects . create ( extra_int = <NUM_LIT:100> , tmo = self . tmo2 ) <EOL> self . assertEquals ( self . tmo1 . tmts . filter ( extra_int = <NUM_LIT:100> ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( self . tmo1 . tmts . filter ( extra_int = <NUM_LIT:100> ) [ <NUM_LIT:0> ] . pk , tmt1 . pk ) <EOL> self . assertEquals ( self . tmo2 . tmts . filter ( extra_int = <NUM_LIT:100> ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( self . tmo2 . tmts . filter ( extra_int = <NUM_LIT:100> ) [ <NUM_LIT:0> ] . pk , tmt2 . pk ) <EOL> self . assertEquals ( self . tmo1 . tmts . get ( extra_int = <NUM_LIT:100> ) , tmt1 ) <EOL> self . assertEquals ( self . tmo2 . tmts . get ( extra_int = <NUM_LIT:100> ) , tmt2 ) <EOL> tmt1 . delete ( ) <EOL> self . assertEquals ( self . tmo1 . tmts . filter ( extra_int = <NUM_LIT:100> ) . count ( ) , <NUM_LIT:0> ) <EOL> tmt1 . undelete ( ) <EOL> self . assertEquals ( self . tmo1 . tmts . filter ( extra_int = <NUM_LIT:100> ) . count ( ) , <NUM_LIT:1> ) <EOL> tmt1 . delete ( ) <EOL> tmt1 . delete ( ) <EOL> self . assertRaises ( TestModelTwo . DoesNotExist , <EOL> self . tmo1 . tmts . get , extra_int = <NUM_LIT:100> ) <EOL> def test_related_m2m ( self ) : <EOL> t31 = TestModelThree . objects . create ( extra_int = <NUM_LIT:100> ) <EOL> TestModelThrough . objects . create ( tmo1 = self . tmo1 , tmo3 = t31 ) <EOL> t32 = TestModelThree . objects . create ( extra_int = <NUM_LIT:100> ) <EOL> TestModelThrough . objects . create ( tmo1 = self . tmo2 , tmo3 = t32 ) <EOL> self . assertEquals ( self . tmo1 . testmodelthree_set . filter ( extra_int = <NUM_LIT:100> ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( self . tmo1 . testmodelthree_set . filter ( extra_int = <NUM_LIT:100> ) [ <NUM_LIT:0> ] . pk , t31 . pk ) <EOL> self . assertEquals ( self . tmo2 . testmodelthree_set . filter ( extra_int = <NUM_LIT:100> ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEquals ( self . tmo2 . testmodelthree_set . filter ( extra_int = <NUM_LIT:100> ) [ <NUM_LIT:0> ] . pk , t32 . pk ) <EOL> self . assertEquals ( self . tmo1 . testmodelthree_set . get ( extra_int = <NUM_LIT:100> ) , t31 ) <EOL> self . assertEquals ( self . tmo2 . testmodelthree_set . get ( extra_int = <NUM_LIT:100> ) , t32 ) <EOL> t31 . delete ( ) <EOL> self . assertEquals ( self . tmo1 . testmodelthree_set . filter ( extra_int = <NUM_LIT:100> ) . count ( ) , <NUM_LIT:0> ) <EOL> t31 . undelete ( ) <EOL> self . assertEquals ( self . tmo1 . testmodelthree_set . filter ( extra_int = <NUM_LIT:100> ) . count ( ) , <NUM_LIT:1> ) <EOL> t31 . delete ( ) <EOL> t31 . delete ( ) <EOL> self . assertRaises ( TestModelThree . DoesNotExist , <EOL> self . tmo1 . testmodelthree_set . get , extra_int = <NUM_LIT:100> ) </s>
