<s> from django . db import connection <EOL> from django . db . models . fields import FieldDoesNotExist <EOL> from django . test import TestCase <EOL> from django . test . utils import override_settings <EOL> from django . utils import six <EOL> from sortedm2m . compat import get_field <EOL> from . models import ( <EOL> Book , Shelf , DoItYourselfShelf , Store , MessyStore , SelfReference ) <EOL> str_ = six . text_type <EOL> class TestSortedManyToManyField ( TestCase ) : <EOL> model = Shelf <EOL> def setUp ( self ) : <EOL> self . books = [ Book . objects . create ( name = c ) for c in '<STR_LIT>' ] <EOL> def test_adding_items ( self ) : <EOL> shelf = self . model . objects . create ( ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ self . books [ <NUM_LIT:2> ] ] ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:5> ] ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:2> ] , <EOL> self . books [ <NUM_LIT:5> ] , <EOL> self . books [ <NUM_LIT:1> ] ] ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:2> ] , <EOL> self . books [ <NUM_LIT:5> ] , <EOL> self . books [ <NUM_LIT:1> ] ] ) <EOL> shelf . books . clear ( ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:3> ] , self . books [ <NUM_LIT:1> ] , self . books [ <NUM_LIT:2> ] ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:3> ] , <EOL> self . books [ <NUM_LIT:1> ] , <EOL> self . books [ <NUM_LIT:2> ] ] ) <EOL> def test_adding_items_by_pk ( self ) : <EOL> shelf = self . model . objects . create ( ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:2> ] . pk ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ self . books [ <NUM_LIT:2> ] ] ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:5> ] . pk , str_ ( self . books [ <NUM_LIT:1> ] . pk ) ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:2> ] , <EOL> self . books [ <NUM_LIT:5> ] , <EOL> self . books [ <NUM_LIT:1> ] ] ) <EOL> shelf . books . clear ( ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:3> ] . pk , self . books [ <NUM_LIT:1> ] , str_ ( self . books [ <NUM_LIT:2> ] . pk ) ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:3> ] , <EOL> self . books [ <NUM_LIT:1> ] , <EOL> self . books [ <NUM_LIT:2> ] ] ) <EOL> def test_set_items ( self ) : <EOL> shelf = self . model . objects . create ( ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> books = self . books [ <NUM_LIT:5> : <NUM_LIT:2> : - <NUM_LIT:1> ] <EOL> shelf . books = books <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , books ) <EOL> books . reverse ( ) <EOL> shelf . books = books <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , books ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:8> ] ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , books + [ self . books [ <NUM_LIT:8> ] ] ) <EOL> shelf . books = [ ] <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> shelf . books = [ self . books [ <NUM_LIT:9> ] ] <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:9> ] ] ) <EOL> shelf . books = [ ] <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> def test_set_items_by_pk ( self ) : <EOL> shelf = self . model . objects . create ( ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> books = self . books [ <NUM_LIT:5> : <NUM_LIT:2> : - <NUM_LIT:1> ] <EOL> shelf . books = [ b . pk for b in books ] <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , books ) <EOL> shelf . books = [ self . books [ <NUM_LIT:5> ] . pk , self . books [ <NUM_LIT:2> ] ] <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:5> ] , <EOL> self . books [ <NUM_LIT:2> ] ] ) <EOL> shelf . books = [ str_ ( self . books [ <NUM_LIT:8> ] . pk ) ] <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ self . books [ <NUM_LIT:8> ] ] ) <EOL> def test_remove_items ( self ) : <EOL> shelf = self . model . objects . create ( ) <EOL> shelf . books = self . books [ <NUM_LIT:2> : <NUM_LIT:5> ] <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:2> ] , <EOL> self . books [ <NUM_LIT:3> ] , <EOL> self . books [ <NUM_LIT:4> ] ] ) <EOL> shelf . books . remove ( self . books [ <NUM_LIT:3> ] ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:2> ] , <EOL> self . books [ <NUM_LIT:4> ] ] ) <EOL> shelf . books . remove ( self . books [ <NUM_LIT:2> ] , self . books [ <NUM_LIT:4> ] ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> def test_remove_items_by_pk ( self ) : <EOL> shelf = self . model . objects . create ( ) <EOL> shelf . books = self . books [ <NUM_LIT:2> : <NUM_LIT:5> ] <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:2> ] , <EOL> self . books [ <NUM_LIT:3> ] , <EOL> self . books [ <NUM_LIT:4> ] ] ) <EOL> shelf . books . remove ( self . books [ <NUM_LIT:3> ] . pk ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ <EOL> self . books [ <NUM_LIT:2> ] , <EOL> self . books [ <NUM_LIT:4> ] ] ) <EOL> shelf . books . remove ( self . books [ <NUM_LIT:2> ] , str_ ( self . books [ <NUM_LIT:4> ] . pk ) ) <EOL> self . assertEqual ( list ( shelf . books . all ( ) ) , [ ] ) <EOL> @ override_settings ( DEBUG = True ) <EOL> def test_prefetch_related_queries_num ( self ) : <EOL> shelf = self . model . objects . create ( ) <EOL> shelf . books . add ( self . books [ <NUM_LIT:0> ] ) <EOL> shelf = self . model . objects . filter ( pk = shelf . pk ) . prefetch_related ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> queries_num = len ( connection . queries ) <EOL> name = shelf . books . all ( ) [ <NUM_LIT:0> ] . name <EOL> self . assertEqual ( queries_num , len ( connection . queries ) ) <EOL> def test_prefetch_related_sorting ( self ) : <EOL> shelf = self . model . objects . create ( ) <EOL> books = [ self . books [ <NUM_LIT:0> ] , self . books [ <NUM_LIT:2> ] , self . books [ <NUM_LIT:1> ] ] <EOL> shelf . books = books <EOL> shelf = self . model . objects . filter ( pk = shelf . pk ) . prefetch_related ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> def get_ids ( queryset ) : <EOL> return [ obj . id for obj in queryset ] <EOL> self . assertEqual ( get_ids ( shelf . books . all ( ) ) , get_ids ( books ) ) <EOL> class TestStringReference ( TestSortedManyToManyField ) : <EOL> '''<STR_LIT>''' <EOL> model = Store <EOL> class TestStringReference ( TestSortedManyToManyField ) : <EOL> '''<STR_LIT>''' <EOL> model = DoItYourselfShelf <EOL> def test_custom_sort_value_field_name ( self ) : <EOL> from sortedm2m . fields import SORT_VALUE_FIELD_NAME <EOL> self . assertEqual ( len ( self . model . _meta . many_to_many ) , <NUM_LIT:1> ) <EOL> sortedm2m = self . model . _meta . many_to_many [ <NUM_LIT:0> ] <EOL> intermediate_model = sortedm2m . rel . through <EOL> self . assertRaises ( <EOL> FieldDoesNotExist , <EOL> get_field , <EOL> intermediate_model , SORT_VALUE_FIELD_NAME ) <EOL> field = get_field ( intermediate_model , '<STR_LIT>' ) <EOL> self . assertTrue ( field ) <EOL> class TestSelfReference ( TestCase ) : <EOL> def test_self_adding ( self ) : <EOL> s1 = SelfReference . objects . create ( ) <EOL> s2 = SelfReference . objects . create ( ) <EOL> s3 = SelfReference . objects . create ( ) <EOL> s4 = SelfReference . objects . create ( ) <EOL> s1 . me . add ( s3 ) <EOL> s1 . me . add ( s4 , s2 ) <EOL> self . assertEqual ( list ( s1 . me . all ( ) ) , [ s3 , s4 , s2 ] ) </s>
