<s> from distutils . version import LooseVersion <EOL> import django <EOL> from django . conf . urls import patterns , include <EOL> from django . core . exceptions import ValidationError <EOL> from django . contrib import admin <EOL> from django . contrib . admin . views . main import ChangeList <EOL> from django . contrib . admin . sites import AdminSite <EOL> from django . contrib . auth . models import User <EOL> from django . db import models <EOL> from django . test import TestCase , RequestFactory <EOL> from . admin import SafeDeleteAdmin , highlight_deleted <EOL> from . models import ( safedelete_mixin_factory , HARD_DELETE , <EOL> HARD_DELETE_NOCASCADE , SOFT_DELETE , NO_DELETE , <EOL> DELETED_VISIBLE_BY_PK ) <EOL> if LooseVersion ( django . get_version ( ) ) < LooseVersion ( '<STR_LIT>' ) : <EOL> from . models import SoftDeleteMixin <EOL> else : <EOL> from . shortcuts import SoftDeleteMixin <EOL> class Author ( safedelete_mixin_factory ( HARD_DELETE_NOCASCADE ) ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:200> ) <EOL> class Category ( safedelete_mixin_factory ( SOFT_DELETE , visibility = DELETED_VISIBLE_BY_PK ) ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:200> , unique = True ) <EOL> class Article ( safedelete_mixin_factory ( HARD_DELETE ) ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:200> ) <EOL> author = models . ForeignKey ( Author ) <EOL> category = models . ForeignKey ( Category , null = True , default = None ) <EOL> def __unicode__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . pk , self . name ) <EOL> class Order ( SoftDeleteMixin ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> articles = models . ManyToManyField ( Article ) <EOL> class VeryImportant ( safedelete_mixin_factory ( NO_DELETE ) ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:200> ) <EOL> class CustomQueryset ( models . query . QuerySet ) : <EOL> def best ( self ) : <EOL> return self . filter ( color = '<STR_LIT>' ) <EOL> class CustomManager ( models . Manager ) : <EOL> def best ( self ) : <EOL> return self . get_queryset ( ) . best ( ) <EOL> class HasCustomQueryset ( safedelete_mixin_factory ( <EOL> policy = SOFT_DELETE , manager_superclass = CustomManager , queryset_superclass = CustomQueryset ) <EOL> ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:200> ) <EOL> color = models . CharField ( max_length = <NUM_LIT:5> , choices = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> class CategoryAdmin ( SafeDeleteAdmin ) : <EOL> list_display = ( highlight_deleted , ) + SafeDeleteAdmin . list_display <EOL> admin . site . register ( Category , CategoryAdmin ) <EOL> urlpatterns = patterns ( <EOL> '<STR_LIT>' , <EOL> ( r'<STR_LIT>' , include ( admin . site . urls ) ) , <EOL> ) <EOL> class SimpleTest ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . authors = ( <EOL> Author . objects . create ( name = '<STR_LIT>' ) , <EOL> Author . objects . create ( name = '<STR_LIT>' ) , <EOL> Author . objects . create ( name = '<STR_LIT>' ) , <EOL> ) <EOL> self . categories = ( <EOL> Category . objects . create ( name = '<STR_LIT>' ) , <EOL> Category . objects . create ( name = '<STR_LIT>' ) , <EOL> Category . objects . create ( name = '<STR_LIT>' ) , <EOL> ) <EOL> self . articles = ( <EOL> Article . objects . create ( name = '<STR_LIT>' , author = self . authors [ <NUM_LIT:1> ] ) , <EOL> Article . objects . create ( name = '<STR_LIT>' , author = self . authors [ <NUM_LIT:1> ] , category = self . categories [ <NUM_LIT:1> ] ) , <EOL> Article . objects . create ( name = '<STR_LIT>' , author = self . authors [ <NUM_LIT:2> ] , category = self . categories [ <NUM_LIT:2> ] ) , <EOL> ) <EOL> self . order = Order . objects . create ( name = '<STR_LIT>' ) <EOL> self . order . articles . add ( self . articles [ <NUM_LIT:0> ] , self . articles [ <NUM_LIT:1> ] ) <EOL> def test_softdelete ( self ) : <EOL> self . assertEqual ( Order . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . order . delete ( ) <EOL> self . assertEqual ( Order . objects . count ( ) , <NUM_LIT:0> ) <EOL> self . assertEqual ( Order . objects . all_with_deleted ( ) . count ( ) , <NUM_LIT:1> ) <EOL> self . order . save ( ) <EOL> self . assertEqual ( Order . objects . count ( ) , <NUM_LIT:1> ) <EOL> def test_hard_delete ( self ) : <EOL> self . assertEqual ( Article . objects . count ( ) , <NUM_LIT:3> ) <EOL> self . articles [ <NUM_LIT:0> ] . delete ( ) <EOL> self . assertEqual ( Article . objects . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( Article . objects . all_with_deleted ( ) . count ( ) , <NUM_LIT:2> ) <EOL> self . articles [ <NUM_LIT:1> ] . delete ( force_policy = SOFT_DELETE ) <EOL> self . assertEqual ( Article . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Article . objects . all_with_deleted ( ) . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( Article . objects . filter ( author = self . authors [ <NUM_LIT:2> ] ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_hard_delete_nocascade ( self ) : <EOL> self . assertEqual ( Author . objects . count ( ) , <NUM_LIT:3> ) <EOL> self . authors [ <NUM_LIT:0> ] . delete ( ) <EOL> self . assertEqual ( Author . objects . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( Author . objects . all_with_deleted ( ) . count ( ) , <NUM_LIT:2> ) <EOL> self . authors [ <NUM_LIT:1> ] . delete ( ) <EOL> self . assertEqual ( Author . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( Author . objects . all_with_deleted ( ) . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( Article . objects . count ( ) , <NUM_LIT:3> ) <EOL> def test_no_delete ( self ) : <EOL> obj = VeryImportant . objects . create ( name = "<STR_LIT>" ) <EOL> obj . delete ( ) <EOL> self . assertEqual ( obj . deleted , False ) <EOL> obj = VeryImportant . objects . get ( pk = obj . pk ) <EOL> self . assertEqual ( obj . deleted , False ) <EOL> def test_no_delete_manager ( self ) : <EOL> obj = VeryImportant . objects . create ( name = "<STR_LIT>" ) <EOL> VeryImportant . objects . all ( ) . delete ( ) <EOL> obj = VeryImportant . objects . get ( pk = obj . pk ) <EOL> self . assertEqual ( obj . deleted , False ) <EOL> def test_save ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( Order . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . order . delete ( ) <EOL> self . assertEqual ( Order . objects . count ( ) , <NUM_LIT:0> ) <EOL> self . order . save ( ) <EOL> self . assertEqual ( Order . objects . count ( ) , <NUM_LIT:1> ) <EOL> def test_undelete ( self ) : <EOL> self . assertEqual ( Order . objects . count ( ) , <NUM_LIT:1> ) <EOL> self . order . delete ( ) <EOL> self . assertEqual ( Order . objects . count ( ) , <NUM_LIT:0> ) <EOL> self . order . undelete ( ) <EOL> self . assertEqual ( Order . objects . count ( ) , <NUM_LIT:1> ) <EOL> def test_access_by_pk ( self ) : <EOL> """<STR_LIT>""" <EOL> pk = self . categories [ <NUM_LIT:1> ] . id <EOL> self . categories [ <NUM_LIT:1> ] . delete ( ) <EOL> self . assertRaises ( Category . DoesNotExist , Category . objects . get , name = self . categories [ <NUM_LIT:1> ] . name ) <EOL> self . assertEqual ( self . categories [ <NUM_LIT:1> ] , Category . objects . get ( pk = pk ) ) <EOL> cat = Category . objects . filter ( pk = pk ) <EOL> self . assertEqual ( len ( cat ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( self . categories [ <NUM_LIT:1> ] , cat [ <NUM_LIT:0> ] ) <EOL> def test_no_access_by_pk ( self ) : <EOL> """<STR_LIT>""" <EOL> self . order . delete ( ) <EOL> self . assertRaises ( Order . DoesNotExist , Order . objects . get , pk = self . order . id ) <EOL> def test_queryset ( self ) : <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:3> ) <EOL> Category . objects . all ( ) . delete ( ) <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:0> ) <EOL> Category . objects . all ( ) . undelete ( ) <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:0> ) <EOL> Category . objects . deleted_only ( ) . undelete ( ) <EOL> self . assertEqual ( Category . objects . count ( ) , <NUM_LIT:3> ) <EOL> def test_custom_queryset_original_behavior ( self ) : <EOL> HasCustomQueryset . objects . create ( name = '<STR_LIT>' , color = '<STR_LIT>' ) <EOL> HasCustomQueryset . objects . create ( name = '<STR_LIT>' , color = '<STR_LIT>' ) <EOL> self . assertEqual ( HasCustomQueryset . objects . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( HasCustomQueryset . objects . best ( ) . count ( ) , <NUM_LIT:1> ) <EOL> def test_related_manager ( self ) : <EOL> order = Order . objects . create ( name = '<STR_LIT>' ) <EOL> Order . objects . create ( name = '<STR_LIT>' ) <EOL> order . articles . add ( self . articles [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( self . articles [ <NUM_LIT:0> ] . order_set . all ( ) . count ( ) , <NUM_LIT:2> ) <EOL> order . delete ( ) <EOL> self . assertEqual ( self . articles [ <NUM_LIT:0> ] . order_set . all ( ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> self . articles [ <NUM_LIT:0> ] . order_set . all_with_deleted ( ) . count ( ) , <NUM_LIT:2> <EOL> ) <EOL> def test_prefetch_related ( self ) : <EOL> """<STR_LIT>""" <EOL> authors = Author . objects . all ( ) . prefetch_related ( '<STR_LIT>' ) <EOL> for author in authors : <EOL> self . assertQuerysetEqual ( <EOL> author . article_set . all ( ) . order_by ( '<STR_LIT>' ) , <EOL> [ repr ( a ) for a in Author . objects . get ( pk = author . pk ) . article_set . all ( ) . order_by ( '<STR_LIT>' ) ] <EOL> ) <EOL> def test_validate_unique ( self ) : <EOL> """<STR_LIT>""" <EOL> Category . objects . create ( name = '<STR_LIT:test>' ) . delete ( ) <EOL> with self . assertRaises ( ValidationError ) : <EOL> Category ( name = '<STR_LIT:test>' ) . validate_unique ( ) <EOL> class AdminTestCase ( TestCase ) : <EOL> urls = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . author = Author . objects . create ( name = '<STR_LIT>' ) <EOL> self . categories = ( <EOL> Category . objects . create ( name = '<STR_LIT>' ) , <EOL> Category . objects . create ( name = '<STR_LIT>' ) , <EOL> Category . objects . create ( name = '<STR_LIT>' ) , <EOL> ) <EOL> self . articles = ( <EOL> Article ( name = '<STR_LIT>' , author = self . author ) , <EOL> Article ( name = '<STR_LIT>' , author = self . author , category = self . categories [ <NUM_LIT:1> ] ) , <EOL> Article ( name = '<STR_LIT>' , author = self . author , category = self . categories [ <NUM_LIT:2> ] ) , <EOL> ) <EOL> self . categories [ <NUM_LIT:1> ] . delete ( ) <EOL> self . request_factory = RequestFactory ( ) <EOL> self . request = self . request_factory . get ( '<STR_LIT:/>' , { } ) <EOL> self . modeladmin_default = admin . ModelAdmin ( Category , AdminSite ( ) ) <EOL> self . modeladmin = CategoryAdmin ( Category , AdminSite ( ) ) <EOL> User . objects . create_superuser ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . client . login ( username = "<STR_LIT>" , password = "<STR_LIT>" ) <EOL> def tearDown ( self ) : <EOL> self . client . logout ( ) <EOL> def get_changelist ( self , request , model , modeladmin ) : <EOL> return ChangeList ( <EOL> request , model , modeladmin . list_display , <EOL> modeladmin . list_display_links , modeladmin . list_filter , <EOL> modeladmin . date_hierarchy , modeladmin . search_fields , <EOL> modeladmin . list_select_related , modeladmin . list_per_page , <EOL> modeladmin . list_max_show_all , modeladmin . list_editable , <EOL> modeladmin <EOL> ) <EOL> def test_admin_model ( self ) : <EOL> changelist_default = self . get_changelist ( self . request , Category , self . modeladmin_default ) <EOL> changelist = self . get_changelist ( self . request , Category , self . modeladmin ) <EOL> if django . VERSION [ <NUM_LIT:1> ] == <NUM_LIT:4> or django . VERSION [ <NUM_LIT:1> ] == <NUM_LIT:5> : <EOL> self . assertEqual ( changelist . get_filters ( self . request ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . title , "<STR_LIT>" ) <EOL> self . assertEqual ( changelist . get_query_set ( self . request ) . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( changelist_default . get_query_set ( self . request ) . count ( ) , <NUM_LIT:2> ) <EOL> else : <EOL> self . assertEqual ( changelist . get_filters ( self . request ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . title , "<STR_LIT>" ) <EOL> self . assertEqual ( changelist . queryset . count ( ) , <NUM_LIT:3> ) <EOL> self . assertEqual ( changelist_default . queryset . count ( ) , <NUM_LIT:2> ) <EOL> def test_admin_listing ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . client . get ( '<STR_LIT>' ) <EOL> line = '<STR_LIT>' . format ( self . categories [ <NUM_LIT:1> ] ) <EOL> self . assertContains ( resp , line ) <EOL> def test_admin_undelete_action ( self ) : <EOL> """<STR_LIT>""" <EOL> resp = self . client . post ( '<STR_LIT>' , data = { <EOL> '<STR_LIT:index>' : <NUM_LIT:0> , <EOL> '<STR_LIT:action>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ self . categories [ <NUM_LIT:1> ] . pk ] , <EOL> } ) <EOL> self . assertTemplateUsed ( resp , '<STR_LIT>' ) <EOL> category = Category . objects . get ( pk = self . categories [ <NUM_LIT:1> ] . pk ) <EOL> self . assertTrue ( self . categories [ <NUM_LIT:1> ] . deleted ) <EOL> resp = self . client . post ( '<STR_LIT>' , data = { <EOL> '<STR_LIT:index>' : <NUM_LIT:0> , <EOL> '<STR_LIT:action>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : [ self . categories [ <NUM_LIT:1> ] . pk ] , <EOL> } ) <EOL> category = Category . objects . get ( pk = self . categories [ <NUM_LIT:1> ] . pk ) <EOL> self . assertFalse ( category . deleted ) </s>
