<s> from __future__ import unicode_literals <EOL> from django . contrib . auth . models import User <EOL> from django . core import management <EOL> from django . test import TestCase <EOL> from django . utils . six import StringIO <EOL> from . models import ( <EOL> Car , CarDriver , Driver , Group , Membership , Person , UserMembership , <EOL> ) <EOL> class M2MThroughTestCase ( TestCase ) : <EOL> @ classmethod <EOL> def setUpTestData ( cls ) : <EOL> cls . bob = Person . objects . create ( name = "<STR_LIT>" ) <EOL> cls . jim = Person . objects . create ( name = "<STR_LIT>" ) <EOL> cls . rock = Group . objects . create ( name = "<STR_LIT>" ) <EOL> cls . roll = Group . objects . create ( name = "<STR_LIT>" ) <EOL> cls . frank = User . objects . create_user ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" ) <EOL> cls . jane = User . objects . create_user ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:password>" ) <EOL> cls . bob_rock = Membership . objects . create ( person = cls . bob , group = cls . rock ) <EOL> cls . bob_roll = Membership . objects . create ( person = cls . bob , group = cls . roll , price = <NUM_LIT:50> ) <EOL> cls . jim_rock = Membership . objects . create ( person = cls . jim , group = cls . rock , price = <NUM_LIT:50> ) <EOL> cls . frank_rock = UserMembership . objects . create ( user = cls . frank , group = cls . rock ) <EOL> cls . frank_roll = UserMembership . objects . create ( user = cls . frank , group = cls . roll ) <EOL> cls . jane_rock = UserMembership . objects . create ( user = cls . jane , group = cls . rock ) <EOL> def test_retrieve_reverse_m2m_items ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . bob . group_set . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> ordered = False <EOL> ) <EOL> def test_retrieve_forward_m2m_items ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . roll . members . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> ] <EOL> ) <EOL> def test_cannot_use_setattr_on_reverse_m2m_with_intermediary_model ( self ) : <EOL> msg = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> with self . assertRaisesMessage ( AttributeError , msg ) : <EOL> self . bob . group_set . set ( [ ] ) <EOL> def test_cannot_use_setattr_on_forward_m2m_with_intermediary_model ( self ) : <EOL> msg = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> with self . assertRaisesMessage ( AttributeError , msg ) : <EOL> self . roll . members . set ( [ ] ) <EOL> def test_cannot_use_create_on_m2m_with_intermediary_model ( self ) : <EOL> with self . assertRaises ( AttributeError ) : <EOL> self . rock . members . create ( name = "<STR_LIT>" ) <EOL> def test_cannot_use_create_on_reverse_m2m_with_intermediary_model ( self ) : <EOL> with self . assertRaises ( AttributeError ) : <EOL> self . bob . group_set . create ( name = "<STR_LIT>" ) <EOL> def test_retrieve_reverse_m2m_items_via_custom_id_intermediary ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . frank . group_set . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> ordered = False <EOL> ) <EOL> def test_retrieve_forward_m2m_items_via_custom_id_intermediary ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . roll . user_members . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> ] <EOL> ) <EOL> def test_join_trimming_forwards ( self ) : <EOL> "<STR_LIT>" <EOL> self . assertQuerysetEqual ( <EOL> self . rock . members . filter ( membership__price = <NUM_LIT:50> ) , [ <EOL> "<STR_LIT>" , <EOL> ] <EOL> ) <EOL> def test_join_trimming_reverse ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . bob . group_set . filter ( membership__price = <NUM_LIT:50> ) , [ <EOL> "<STR_LIT>" , <EOL> ] <EOL> ) <EOL> class M2MThroughSerializationTestCase ( TestCase ) : <EOL> @ classmethod <EOL> def setUpTestData ( cls ) : <EOL> cls . bob = Person . objects . create ( name = "<STR_LIT>" ) <EOL> cls . roll = Group . objects . create ( name = "<STR_LIT>" ) <EOL> cls . bob_roll = Membership . objects . create ( person = cls . bob , group = cls . roll ) <EOL> def test_serialization ( self ) : <EOL> "<STR_LIT>" <EOL> pks = { "<STR_LIT>" : self . bob . pk , "<STR_LIT>" : self . roll . pk , "<STR_LIT>" : self . bob_roll . pk } <EOL> out = StringIO ( ) <EOL> management . call_command ( "<STR_LIT>" , "<STR_LIT>" , format = "<STR_LIT>" , stdout = out ) <EOL> self . assertJSONEqual ( <EOL> out . getvalue ( ) . strip ( ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % pks <EOL> ) <EOL> out = StringIO ( ) <EOL> management . call_command ( "<STR_LIT>" , "<STR_LIT>" , format = "<STR_LIT>" , indent = <NUM_LIT:2> , stdout = out ) <EOL> self . assertXMLEqual ( out . getvalue ( ) . strip ( ) , """<STR_LIT>""" . strip ( ) % pks ) <EOL> class ToFieldThroughTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . car = Car . objects . create ( make = "<STR_LIT>" ) <EOL> self . driver = Driver . objects . create ( name = "<STR_LIT>" ) <EOL> CarDriver . objects . create ( car = self . car , driver = self . driver ) <EOL> self . unused_driver = Driver . objects . create ( name = "<STR_LIT>" ) <EOL> self . unused_car1 = Car . objects . create ( make = "<STR_LIT>" ) <EOL> self . unused_car2 = Car . objects . create ( make = "<STR_LIT>" ) <EOL> def test_to_field ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . car . drivers . all ( ) , <EOL> [ "<STR_LIT>" ] <EOL> ) <EOL> def test_to_field_reverse ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . driver . car_set . all ( ) , <EOL> [ "<STR_LIT>" ] <EOL> ) <EOL> def test_to_field_clear_reverse ( self ) : <EOL> self . driver . car_set . clear ( ) <EOL> self . assertQuerysetEqual ( <EOL> self . driver . car_set . all ( ) , [ ] ) <EOL> def test_to_field_clear ( self ) : <EOL> self . car . drivers . clear ( ) <EOL> self . assertQuerysetEqual ( <EOL> self . car . drivers . all ( ) , [ ] ) <EOL> def test_add ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . car . drivers . all ( ) , <EOL> [ "<STR_LIT>" ] <EOL> ) <EOL> self . car . drivers . _add_items ( '<STR_LIT>' , '<STR_LIT>' , self . unused_driver ) <EOL> self . assertQuerysetEqual ( <EOL> self . car . drivers . all ( ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> ) <EOL> def test_add_null ( self ) : <EOL> nullcar = Car . objects . create ( make = None ) <EOL> with self . assertRaises ( ValueError ) : <EOL> nullcar . drivers . _add_items ( '<STR_LIT>' , '<STR_LIT>' , self . unused_driver ) <EOL> def test_add_related_null ( self ) : <EOL> nulldriver = Driver . objects . create ( name = None ) <EOL> with self . assertRaises ( ValueError ) : <EOL> self . car . drivers . _add_items ( '<STR_LIT>' , '<STR_LIT>' , nulldriver ) <EOL> def test_add_reverse ( self ) : <EOL> car2 = Car . objects . create ( make = "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> self . driver . car_set . all ( ) , <EOL> [ "<STR_LIT>" ] <EOL> ) <EOL> self . driver . car_set . _add_items ( '<STR_LIT>' , '<STR_LIT>' , car2 ) <EOL> self . assertQuerysetEqual ( <EOL> self . driver . car_set . all ( ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ordered = False <EOL> ) <EOL> def test_add_null_reverse ( self ) : <EOL> nullcar = Car . objects . create ( make = None ) <EOL> with self . assertRaises ( ValueError ) : <EOL> self . driver . car_set . _add_items ( '<STR_LIT>' , '<STR_LIT>' , nullcar ) <EOL> def test_add_null_reverse_related ( self ) : <EOL> nulldriver = Driver . objects . create ( name = None ) <EOL> with self . assertRaises ( ValueError ) : <EOL> nulldriver . car_set . _add_items ( '<STR_LIT>' , '<STR_LIT>' , self . car ) <EOL> def test_remove ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . car . drivers . all ( ) , <EOL> [ "<STR_LIT>" ] <EOL> ) <EOL> self . car . drivers . _remove_items ( '<STR_LIT>' , '<STR_LIT>' , self . driver ) <EOL> self . assertQuerysetEqual ( <EOL> self . car . drivers . all ( ) , [ ] ) <EOL> def test_remove_reverse ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> self . driver . car_set . all ( ) , <EOL> [ "<STR_LIT>" ] <EOL> ) <EOL> self . driver . car_set . _remove_items ( '<STR_LIT>' , '<STR_LIT>' , self . car ) <EOL> self . assertQuerysetEqual ( <EOL> self . driver . car_set . all ( ) , [ ] ) <EOL> class ThroughLoadDataTestCase ( TestCase ) : <EOL> fixtures = [ "<STR_LIT>" ] <EOL> def test_sequence_creation ( self ) : <EOL> """<STR_LIT>""" <EOL> out = StringIO ( ) <EOL> management . call_command ( "<STR_LIT>" , "<STR_LIT>" , format = "<STR_LIT>" , stdout = out ) <EOL> self . assertJSONEqual ( <EOL> out . getvalue ( ) . strip ( ) , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) </s>
