<s> from __future__ import absolute_import <EOL> from django . test import TestCase <EOL> from . models import Reporter , Article <EOL> class ManyToOneNullTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . r = Reporter ( name = '<STR_LIT>' ) <EOL> self . r . save ( ) <EOL> self . a = Article ( headline = "<STR_LIT>" , reporter = self . r ) <EOL> self . a . save ( ) <EOL> self . a2 = self . r . article_set . create ( headline = "<STR_LIT>" ) <EOL> self . a3 = Article ( headline = "<STR_LIT>" , reporter = None ) <EOL> self . a3 . save ( ) <EOL> self . r2 = Reporter ( name = '<STR_LIT>' ) <EOL> self . r2 . save ( ) <EOL> self . a4 = self . r2 . article_set . create ( headline = '<STR_LIT>' ) <EOL> def test_get_related ( self ) : <EOL> self . assertEqual ( self . a . reporter . id , self . r . id ) <EOL> r = self . a . reporter <EOL> self . assertEqual ( r . id , self . r . id ) <EOL> def test_created_via_related_set ( self ) : <EOL> self . assertEqual ( self . a2 . reporter . id , self . r . id ) <EOL> def test_related_set ( self ) : <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( self . r . article_set . filter ( headline__startswith = '<STR_LIT>' ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( self . r . article_set . count ( ) , <NUM_LIT:2> ) <EOL> def test_created_without_related ( self ) : <EOL> self . assertEqual ( self . a3 . reporter , None ) <EOL> a3 = Article . objects . get ( pk = self . a3 . pk ) <EOL> self . assertRaises ( AttributeError , getattr , a3 . reporter , '<STR_LIT:id>' ) <EOL> self . assertEqual ( a3 . reporter , None ) <EOL> self . assertQuerysetEqual ( Article . objects . filter ( reporter__isnull = True ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( Article . objects . filter ( reporter = None ) , <EOL> [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . r . article_set . add ( a3 ) <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . r . article_set . remove ( a3 ) <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( Article . objects . filter ( reporter__isnull = True ) , <EOL> [ '<STR_LIT>' ] ) <EOL> def test_remove_from_wrong_set ( self ) : <EOL> self . assertQuerysetEqual ( self . r2 . article_set . all ( ) , [ '<STR_LIT>' ] ) <EOL> self . assertRaises ( Reporter . DoesNotExist , self . r . article_set . remove , self . a4 ) <EOL> self . assertQuerysetEqual ( self . r2 . article_set . all ( ) , [ '<STR_LIT>' ] ) <EOL> def test_assign_clear_related_set ( self ) : <EOL> self . r2 . article_set = [ self . a2 , self . a3 ] <EOL> self . assertQuerysetEqual ( self . r2 . article_set . all ( ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . r . article_set . clear ( ) <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , [ ] ) <EOL> self . assertQuerysetEqual ( Article . objects . filter ( reporter__isnull = True ) , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def test_clear_efficiency ( self ) : <EOL> r = Reporter . objects . create ( ) <EOL> for _ in range ( <NUM_LIT:3> ) : <EOL> r . article_set . create ( ) <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> r . article_set . clear ( ) <EOL> self . assertEqual ( r . article_set . count ( ) , <NUM_LIT:0> ) </s>
