<s> from __future__ import absolute_import , with_statement <EOL> from copy import deepcopy <EOL> from datetime import datetime <EOL> from django . core . exceptions import MultipleObjectsReturned <EOL> from django . test import TestCase <EOL> from django . utils . translation import ugettext_lazy <EOL> from . models import Article , Reporter <EOL> class ManyToOneTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . r = Reporter ( first_name = '<STR_LIT>' , last_name = '<STR_LIT>' , email = '<STR_LIT>' ) <EOL> self . r . save ( ) <EOL> self . r2 = Reporter ( first_name = '<STR_LIT>' , last_name = '<STR_LIT>' , email = '<STR_LIT>' ) <EOL> self . r2 . save ( ) <EOL> self . a = Article ( id = None , headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) , reporter = self . r ) <EOL> self . a . save ( ) <EOL> def test_get ( self ) : <EOL> r = self . a . reporter <EOL> self . assertEqual ( r . id , self . r . id ) <EOL> self . assertEqual ( ( r . first_name , self . r . last_name ) , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def test_create ( self ) : <EOL> a3 = Article ( id = None , headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) , reporter_id = self . r . id ) <EOL> a3 . save ( ) <EOL> self . assertEqual ( a3 . reporter . id , self . r . id ) <EOL> a4 = Article ( id = None , headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) , reporter_id = str ( self . r . id ) ) <EOL> a4 . save ( ) <EOL> self . assertEqual ( repr ( a4 . reporter ) , "<STR_LIT>" ) <EOL> def test_add ( self ) : <EOL> new_article = self . r . article_set . create ( headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) ) <EOL> self . assertEqual ( repr ( new_article ) , "<STR_LIT>" ) <EOL> self . assertEqual ( new_article . reporter . id , self . r . id ) <EOL> new_article2 = Article ( headline = "<STR_LIT>" , pub_date = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> self . r . article_set . add ( new_article2 ) <EOL> self . assertEqual ( new_article2 . reporter . id , self . r . id ) <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . r2 . article_set . add ( new_article2 ) <EOL> self . assertEqual ( new_article2 . reporter . id , self . r2 . id ) <EOL> self . assertQuerysetEqual ( self . r2 . article_set . all ( ) , [ "<STR_LIT>" ] ) <EOL> with self . assertRaisesRegexp ( TypeError , "<STR_LIT>" ) : <EOL> self . r . article_set . add ( self . r2 ) <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> def test_assign ( self ) : <EOL> new_article = self . r . article_set . create ( headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) ) <EOL> new_article2 = self . r2 . article_set . create ( headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> new_article2 . reporter = self . r <EOL> new_article2 . save ( ) <EOL> self . assertEqual ( repr ( new_article2 . reporter ) , "<STR_LIT>" ) <EOL> self . assertEqual ( new_article2 . reporter . id , self . r . id ) <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . r2 . article_set . all ( ) , [ ] ) <EOL> self . r2 . article_set = [ new_article , new_article2 ] <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( self . r2 . article_set . all ( ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . r . article_set = [ new_article ] <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( self . r2 . article_set . all ( ) , [ "<STR_LIT>" ] ) <EOL> self . assertFalse ( hasattr ( self . r2 . article_set , '<STR_LIT>' ) ) <EOL> self . assertFalse ( hasattr ( self . r2 . article_set , '<STR_LIT>' ) ) <EOL> def test_selects ( self ) : <EOL> new_article = self . r . article_set . create ( headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) ) <EOL> new_article2 = self . r2 . article_set . create ( headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> self . assertQuerysetEqual ( self . r . article_set . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <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> self . assertEqual ( self . r2 . article_set . count ( ) , <NUM_LIT:1> ) <EOL> self . assertQuerysetEqual ( Article . objects . filter ( id__exact = self . a . id ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Article . objects . filter ( pk = self . a . id ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Article . objects . filter ( headline__startswith = '<STR_LIT>' ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Article . objects . filter ( reporter__first_name__exact = '<STR_LIT>' ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( Article . objects . filter ( reporter__first_name = '<STR_LIT>' ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter__first_name__exact = '<STR_LIT>' , <EOL> reporter__last_name__exact = '<STR_LIT>' ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> queryset = Article . objects . filter ( reporter__first_name__exact = '<STR_LIT>' , <EOL> reporter__last_name__exact = '<STR_LIT>' ) <EOL> self . assertNumQueries ( <NUM_LIT:1> , list , queryset ) <EOL> self . assertEqual ( queryset . query . get_compiler ( queryset . db ) . as_sql ( ) [ <NUM_LIT:0> ] . count ( '<STR_LIT>' ) , <NUM_LIT:1> ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter__first_name__exact = '<STR_LIT>' ) . extra ( <EOL> where = [ "<STR_LIT>" ] ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter__first_name__exact = '<STR_LIT>' <EOL> ) . extra ( where = [ "<STR_LIT>" % u'<STR_LIT>' ] ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter__id__exact = self . r . id ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter__pk = self . r . id ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter = self . r . id ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter = self . r ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter__in = [ self . r . id , self . r2 . id ] ) . distinct ( ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter__in = [ self . r , self . r2 ] ) . distinct ( ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( <EOL> reporter__in = Reporter . objects . filter ( first_name = '<STR_LIT>' ) . values ( '<STR_LIT>' ) . query <EOL> ) . distinct ( ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> def test_reverse_selects ( self ) : <EOL> a3 = Article . objects . create ( id = None , headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) , reporter_id = self . r . id ) <EOL> a4 = Article . objects . create ( id = None , headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) , reporter_id = str ( self . r . id ) ) <EOL> self . assertQuerysetEqual ( Reporter . objects . filter ( id__exact = self . r . id ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Reporter . objects . filter ( pk = self . r . id ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Reporter . objects . filter ( first_name__startswith = '<STR_LIT>' ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Reporter . objects . filter ( article__id__exact = self . a . id ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Reporter . objects . filter ( article__pk = self . a . id ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Reporter . objects . filter ( article = self . a . id ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Reporter . objects . filter ( article = self . a ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( <EOL> Reporter . objects . filter ( article__in = [ self . a . id , a3 . id ] ) . distinct ( ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( <EOL> Reporter . objects . filter ( article__in = [ self . a . id , a3 ] ) . distinct ( ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( <EOL> Reporter . objects . filter ( article__in = [ self . a , a3 ] ) . distinct ( ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( <EOL> Reporter . objects . filter ( article__headline__startswith = '<STR_LIT:T>' ) , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( <EOL> Reporter . objects . filter ( article__headline__startswith = '<STR_LIT:T>' ) . distinct ( ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertEqual ( <EOL> Reporter . objects . filter ( article__headline__startswith = '<STR_LIT:T>' ) . count ( ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> Reporter . objects . filter ( article__headline__startswith = '<STR_LIT:T>' ) . distinct ( ) . count ( ) , <NUM_LIT:1> ) <EOL> self . assertQuerysetEqual ( <EOL> Reporter . objects . filter ( article__reporter__first_name__startswith = '<STR_LIT>' ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( <EOL> Reporter . objects . filter ( article__reporter__first_name__startswith = '<STR_LIT>' ) . distinct ( ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( <EOL> Reporter . objects . filter ( article__reporter__exact = self . r ) . distinct ( ) , <EOL> [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( <EOL> Reporter . objects . filter ( article__reporter = self . r ) . distinct ( ) , <EOL> [ "<STR_LIT>" ] ) <EOL> d = { '<STR_LIT>' : u'<STR_LIT>' , '<STR_LIT>' : u'<STR_LIT>' } <EOL> self . assertEqual ( [ d ] , <EOL> list ( Article . objects . filter ( reporter = self . r ) . distinct ( ) . order_by ( ) <EOL> . values ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> def test_select_related ( self ) : <EOL> r1 = Reporter . objects . create ( first_name = '<STR_LIT>' , last_name = '<STR_LIT>' , email = '<STR_LIT>' ) <EOL> r2 = Reporter . objects . create ( first_name = '<STR_LIT>' , last_name = '<STR_LIT>' , email = '<STR_LIT>' ) <EOL> a1 = Article . objects . create ( headline = '<STR_LIT>' , pub_date = datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) , reporter = r1 ) <EOL> a2 = Article . objects . create ( headline = '<STR_LIT>' , pub_date = datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> ) , reporter = r2 ) <EOL> self . assertEqual ( list ( Article . objects . select_related ( ) . dates ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> [ <EOL> datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ] ) <EOL> self . assertEqual ( list ( Article . objects . select_related ( ) . dates ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> [ <EOL> datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ] ) <EOL> self . assertEqual ( list ( Article . objects . select_related ( ) . dates ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> [ <EOL> datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> ] ) <EOL> def test_delete ( self ) : <EOL> new_article = self . r . article_set . create ( headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) ) <EOL> new_article2 = self . r2 . article_set . create ( headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> a3 = Article . objects . create ( id = None , headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) , reporter_id = self . r . id ) <EOL> a4 = Article . objects . create ( id = None , headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:7> , <NUM_LIT> ) , reporter_id = str ( self . r . id ) ) <EOL> self . assertQuerysetEqual ( Article . objects . all ( ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( Reporter . objects . order_by ( '<STR_LIT>' ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . r2 . delete ( ) <EOL> self . assertQuerysetEqual ( Article . objects . all ( ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> self . assertQuerysetEqual ( Reporter . objects . order_by ( '<STR_LIT>' ) , <EOL> [ "<STR_LIT>" ] ) <EOL> Reporter . objects . filter ( article__headline__startswith = '<STR_LIT>' ) . delete ( ) <EOL> self . assertQuerysetEqual ( Reporter . objects . all ( ) , [ ] ) <EOL> self . assertQuerysetEqual ( Article . objects . all ( ) , [ ] ) <EOL> def test_regression_12876 ( self ) : <EOL> self . r . cached_query = Article . objects . filter ( reporter = self . r ) <EOL> self . assertEqual ( repr ( deepcopy ( self . r ) ) , "<STR_LIT>" ) <EOL> def test_explicit_fk ( self ) : <EOL> a2 , created = Article . objects . get_or_create ( id = None , <EOL> headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:7> ) , <EOL> reporter_id = self . r . id ) <EOL> self . assertTrue ( created ) <EOL> self . assertEqual ( a2 . reporter . id , self . r . id ) <EOL> self . assertQuerysetEqual ( <EOL> Article . objects . filter ( reporter_id__exact = self . r . id ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] ) <EOL> a3 = Article . objects . create ( id = None , headline = "<STR_LIT>" , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:7> ) , <EOL> reporter_id = self . r2 . id ) <EOL> self . assertEqual ( a3 . reporter . id , self . r2 . id ) <EOL> self . assertRaises ( MultipleObjectsReturned , <EOL> Article . objects . get , reporter_id = self . r . id ) <EOL> self . assertEqual ( repr ( a3 ) , <EOL> repr ( Article . objects . get ( reporter_id = self . r2 . id , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:7> ) ) ) ) <EOL> def test_manager_class_caching ( self ) : <EOL> r1 = Reporter . objects . create ( first_name = '<STR_LIT>' ) <EOL> r2 = Reporter . objects . create ( first_name = '<STR_LIT>' ) <EOL> self . assertTrue ( r1 . article_set . __class__ is r1 . article_set . __class__ ) <EOL> self . assertTrue ( r1 . article_set . __class__ is r2 . article_set . __class__ ) <EOL> def test_create_relation_with_ugettext_lazy ( self ) : <EOL> reporter = Reporter . objects . create ( first_name = '<STR_LIT>' , <EOL> last_name = '<STR_LIT>' , <EOL> email = '<STR_LIT>' ) <EOL> lazy = ugettext_lazy ( u'<STR_LIT:test>' ) <EOL> reporter . article_set . create ( headline = lazy , <EOL> pub_date = datetime ( <NUM_LIT> , <NUM_LIT:6> , <NUM_LIT:10> ) ) <EOL> notlazy = unicode ( lazy ) <EOL> article = reporter . article_set . get ( ) <EOL> self . assertEqual ( article . headline , notlazy ) </s>
