<s> from __future__ import absolute_import , with_statement <EOL> from django . test import TestCase <EOL> from . models import Article , Publication <EOL> class ManyToManyTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . p1 = Publication . objects . create ( id = None , title = '<STR_LIT>' ) <EOL> self . p2 = Publication . objects . create ( id = None , title = '<STR_LIT>' ) <EOL> self . p3 = Publication . objects . create ( id = None , title = '<STR_LIT>' ) <EOL> self . p4 = Publication . objects . create ( title = '<STR_LIT>' ) <EOL> self . a1 = Article . objects . create ( id = None , headline = '<STR_LIT>' ) <EOL> self . a1 . publications . add ( self . p1 ) <EOL> self . a2 = Article . objects . create ( id = None , headline = '<STR_LIT>' ) <EOL> self . a2 . publications . add ( self . p1 , self . p2 , self . p3 , self . p4 ) <EOL> self . a3 = Article . objects . create ( headline = '<STR_LIT>' ) <EOL> self . a3 . publications . add ( self . p2 ) <EOL> self . a4 = Article . objects . create ( headline = '<STR_LIT>' ) <EOL> self . a4 . publications . add ( self . p2 ) <EOL> def test_add ( self ) : <EOL> a5 = Article ( id = None , headline = '<STR_LIT>' ) <EOL> self . assertRaises ( ValueError , getattr , a5 , '<STR_LIT>' ) <EOL> a5 . save ( ) <EOL> a5 . publications . add ( self . p1 ) <EOL> self . assertQuerysetEqual ( a5 . publications . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> a6 = Article ( id = None , headline = '<STR_LIT>' ) <EOL> a6 . save ( ) <EOL> a6 . publications . add ( self . p1 , self . p2 ) <EOL> a6 . publications . add ( self . p3 ) <EOL> a6 . publications . add ( self . p3 ) <EOL> self . assertQuerysetEqual ( a6 . publications . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> with self . assertRaisesRegexp ( TypeError , "<STR_LIT>" ) : <EOL> a6 . publications . add ( a5 ) <EOL> p4 = a6 . publications . create ( title = '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( a6 . publications . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def test_reverse_add ( self ) : <EOL> a5 = Article ( headline = '<STR_LIT>' ) <EOL> a5 . save ( ) <EOL> self . p2 . article_set . add ( a5 ) <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( a5 . publications . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> new_article = self . p2 . article_set . create ( headline = '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> a6 = self . p2 . article_set . all ( ) [ <NUM_LIT:3> ] <EOL> self . assertQuerysetEqual ( a6 . publications . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def test_related_sets ( self ) : <EOL> self . assertQuerysetEqual ( self . a1 . publications . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( self . a2 . publications . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . p1 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( Publication . objects . get ( id = self . p4 . id ) . article_set . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_selects ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( publications__id__exact = self . p1 . id ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( publications__pk = self . p1 . id ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( publications = self . p1 . id ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( publications = self . p1 ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( publications__title__startswith = "<STR_LIT>" ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( publications__title__startswith = "<STR_LIT>" ) . distinct ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertEqual ( Article . objects . filter ( publications__title__startswith = "<STR_LIT>" ) . count ( ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( Article . objects . filter ( publications__title__startswith = "<STR_LIT>" ) . distinct ( ) . count ( ) , <NUM_LIT:3> ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( publications__in = [ self . p1 . id , self . p2 . id ] ) . distinct ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( publications__in = [ self . p1 . id , self . p2 ] ) . distinct ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( publications__in = [ self . p1 , self . p2 ] ) . distinct ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( Article . objects . exclude ( publications = self . p2 ) , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_reverse_selects ( self ) : <EOL> self . assertQuerysetEqual ( Publication . objects . filter ( id__exact = self . p1 . id ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( Publication . objects . filter ( pk = self . p1 . id ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( <EOL> Publication . objects . filter ( article__headline__startswith = "<STR_LIT>" ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( Publication . objects . filter ( article__id__exact = self . a1 . id ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( Publication . objects . filter ( article__pk = self . a1 . id ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( Publication . objects . filter ( article = self . a1 . id ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( Publication . objects . filter ( article = self . a1 ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( <EOL> Publication . objects . filter ( article__in = [ self . a1 . id , self . a2 . id ] ) . distinct ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Publication . objects . filter ( article__in = [ self . a1 . id , self . a2 ] ) . distinct ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Publication . objects . filter ( article__in = [ self . a1 , self . a2 ] ) . distinct ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def test_delete ( self ) : <EOL> self . p1 . delete ( ) <EOL> self . assertQuerysetEqual ( Publication . objects . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . a1 . publications . all ( ) , [ ] ) <EOL> self . a2 . delete ( ) <EOL> self . assertQuerysetEqual ( Article . objects . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def test_bulk_delete ( self ) : <EOL> Publication . objects . filter ( title__startswith = '<STR_LIT>' ) . delete ( ) <EOL> self . assertQuerysetEqual ( Publication . objects . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( Article . objects . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . a2 . publications . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> q = Article . objects . filter ( headline__startswith = '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( q , [ '<STR_LIT>' ] ) <EOL> q . delete ( ) <EOL> self . assertQuerysetEqual ( q , [ ] ) <EOL> self . assertQuerysetEqual ( self . p1 . article_set . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_remove ( self ) : <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . a4 . publications . remove ( self . p2 ) <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . a4 . publications . all ( ) , [ ] ) <EOL> self . p2 . article_set . remove ( self . a3 ) <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . a3 . publications . all ( ) , [ ] ) <EOL> def test_assign ( self ) : <EOL> self . p2 . article_set = [ self . a4 , self . a3 ] <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . a4 . publications . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . a4 . publications = [ self . p3 . id ] <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( self . a4 . publications . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . p2 . article_set = [ ] <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , [ ] ) <EOL> self . a4 . publications = [ ] <EOL> self . assertQuerysetEqual ( self . a4 . publications . all ( ) , [ ] ) <EOL> def test_assign_ids ( self ) : <EOL> self . p2 . article_set = [ self . a4 . id , self . a3 . id ] <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . a4 . publications . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . a4 . publications = [ self . p3 . id ] <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( self . a4 . publications . all ( ) , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_clear ( self ) : <EOL> self . p2 . article_set . clear ( ) <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , [ ] ) <EOL> self . assertQuerysetEqual ( self . a4 . publications . all ( ) , [ ] ) <EOL> self . p2 . article_set . add ( self . a3 , self . a4 ) <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . a4 . publications . all ( ) , <EOL> [ <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> self . a4 . publications . clear ( ) <EOL> self . assertQuerysetEqual ( self . a4 . publications . all ( ) , [ ] ) <EOL> self . assertQuerysetEqual ( self . p2 . article_set . all ( ) , <EOL> [ '<STR_LIT>' ] ) </s>
