<s> from __future__ import absolute_import <EOL> from django . db import models <EOL> from django . test import TestCase <EOL> from . models import First , Second , Third , Parent , Child , Category , Record , Relation <EOL> class ManyToOneRegressionTests ( TestCase ) : <EOL> def test_object_creation ( self ) : <EOL> Third . objects . create ( id = '<STR_LIT:3>' , name = '<STR_LIT>' ) <EOL> parent = Parent ( name = '<STR_LIT>' ) <EOL> parent . save ( ) <EOL> Child . objects . create ( name = '<STR_LIT>' , parent = parent ) <EOL> def test_fk_assignment_and_related_object_cache ( self ) : <EOL> p = Parent . objects . create ( name = "<STR_LIT>" ) <EOL> c = Child . objects . create ( name = "<STR_LIT>" , parent = p ) <EOL> c = Child . objects . get ( name = "<STR_LIT>" ) <EOL> p = c . parent <EOL> self . assertTrue ( c . parent is p ) <EOL> del c . _parent_cache <EOL> self . assertFalse ( c . parent is p ) <EOL> p2 = Parent . objects . create ( name = "<STR_LIT>" ) <EOL> c . parent = p2 <EOL> self . assertTrue ( c . parent is p2 ) <EOL> p . bestchild = None <EOL> self . assertTrue ( p . bestchild is None ) <EOL> p . save ( ) <EOL> self . assertTrue ( p . bestchild is None ) <EOL> p = Parent . objects . get ( name = "<STR_LIT>" ) <EOL> self . assertTrue ( p . bestchild is None ) <EOL> self . assertRaises ( ValueError , setattr , c , "<STR_LIT>" , None ) <EOL> self . assertRaises ( ValueError , setattr , c , "<STR_LIT>" , First ( id = <NUM_LIT:1> , second = <NUM_LIT:1> ) ) <EOL> self . assertRaises ( ValueError , Child , name = '<STR_LIT>' , parent = None ) <EOL> self . assertRaises ( ValueError , Child . objects . create , name = '<STR_LIT>' , parent = None ) <EOL> p = Parent . objects . get ( name = "<STR_LIT>" ) <EOL> c = Child ( parent = p ) <EOL> self . assertTrue ( c . parent is p ) <EOL> p = Parent ( ) <EOL> c = Child ( parent = p ) <EOL> self . assertTrue ( c . parent is p ) <EOL> p = Parent . objects . get ( name = "<STR_LIT>" ) <EOL> c = Child ( parent_id = p . id ) <EOL> self . assertFalse ( c . parent is p ) <EOL> self . assertEqual ( c . parent , p ) <EOL> def test_multiple_foreignkeys ( self ) : <EOL> c1 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> c2 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> c3 = Category . objects . create ( name = '<STR_LIT>' ) <EOL> r1 = Record . objects . create ( category = c1 ) <EOL> r2 = Record . objects . create ( category = c1 ) <EOL> r3 = Record . objects . create ( category = c2 ) <EOL> r4 = Record . objects . create ( category = c2 ) <EOL> r5 = Record . objects . create ( category = c3 ) <EOL> r = Relation . objects . create ( left = r1 , right = r2 ) <EOL> r = Relation . objects . create ( left = r3 , right = r4 ) <EOL> r = Relation . objects . create ( left = r1 , right = r3 ) <EOL> r = Relation . objects . create ( left = r5 , right = r2 ) <EOL> r = Relation . objects . create ( left = r3 , right = r2 ) <EOL> q1 = Relation . objects . filter ( left__category__name__in = [ '<STR_LIT>' ] , right__category__name__in = [ '<STR_LIT>' ] ) <EOL> self . assertQuerysetEqual ( q1 , [ "<STR_LIT>" ] ) <EOL> q2 = Category . objects . filter ( record__left_set__right__category__name = '<STR_LIT>' ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertQuerysetEqual ( q2 , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> p = Parent . objects . create ( name = "<STR_LIT>" ) <EOL> c = Child . objects . create ( name = "<STR_LIT>" , parent = p ) <EOL> self . assertRaises ( ValueError , Child . objects . create , name = "<STR_LIT>" , parent = c ) <EOL> def test_fk_instantiation_outside_model ( self ) : <EOL> cat = models . ForeignKey ( Category ) <EOL> self . assertEqual ( '<STR_LIT:id>' , cat . rel . get_related_field ( ) . name ) </s>
