<s> from __future__ import absolute_import <EOL> import copy <EOL> from django import forms <EOL> from django . test import TestCase <EOL> from widgy . models . links import ( <EOL> link_registry , get_link_field_from_model , LinkFormMixin , LinkFormField , <EOL> get_composite_key , convert_linkable_to_choice , LinkRegistry , LinkField <EOL> ) <EOL> from . . models import ( <EOL> LinkableThing , ThingWithLink , AnotherLinkableThing , LinkableThing3 , <EOL> Bucket , VersionPageThrough , ChildThingWithLink <EOL> ) <EOL> class TestLinkField ( TestCase ) : <EOL> def test_inheritance ( self ) : <EOL> self . assertFalse ( any ( <EOL> i . name == '<STR_LIT>' for i in ChildThingWithLink . _meta . local_fields <EOL> ) ) <EOL> def test_copy ( self ) : <EOL> registry = LinkRegistry ( ) <EOL> f1 = LinkField ( link_registry = registry ) <EOL> f2 = copy . deepcopy ( f1 ) <EOL> self . assertIs ( f1 . _link_registry , f2 . _link_registry ) <EOL> class TestLinkRelations ( TestCase ) : <EOL> def test_get_all_linkable_classes ( self ) : <EOL> self . assertIn ( LinkableThing , link_registry ) <EOL> self . assertIn ( AnotherLinkableThing , link_registry ) <EOL> def test_get_all_linker_classes ( self ) : <EOL> self . assertIn ( ThingWithLink , link_registry . get_all_linker_classes ( ) ) <EOL> self . assertNotIn ( Bucket , link_registry . get_all_linker_classes ( ) ) <EOL> self . assertNotIn ( VersionPageThrough , link_registry . get_all_linker_classes ( ) ) <EOL> def test_get_all_links_for_obj ( self ) : <EOL> linkable = LinkableThing . objects . create ( ) <EOL> self . assertEqual ( len ( list ( link_registry . get_links ( linkable ) ) ) , <NUM_LIT:0> ) <EOL> thing = ThingWithLink . objects . create ( <EOL> link = linkable , <EOL> ) <EOL> self . assertEqual ( list ( link_registry . get_links ( linkable ) ) , [ thing ] ) <EOL> linkable2 = AnotherLinkableThing . objects . create ( ) <EOL> thing2 = ThingWithLink . objects . create ( <EOL> link = linkable2 , <EOL> ) <EOL> self . assertEqual ( list ( link_registry . get_links ( linkable2 ) ) , [ thing2 ] ) <EOL> def test_get_all_possible_linkables ( self ) : <EOL> l1 = LinkableThing . objects . create ( ) <EOL> l2 = LinkableThing . objects . create ( ) <EOL> l3 = AnotherLinkableThing . objects . create ( ) <EOL> choices = get_link_field_from_model ( ThingWithLink , '<STR_LIT>' ) . get_choices ( ) <EOL> keyfn = get_composite_key <EOL> self . assertEqual ( sorted ( list ( choices ) , key = keyfn ) , sorted ( [ l3 , l1 , l2 ] , key = keyfn ) ) <EOL> class LinkForm ( LinkFormMixin , forms . ModelForm ) : <EOL> link = LinkFormField ( ) <EOL> class Meta : <EOL> model = ThingWithLink <EOL> fields = ( '<STR_LIT>' , ) <EOL> class TestLinkForm ( TestCase ) : <EOL> def test_save_and_create ( self ) : <EOL> page = LinkableThing . objects . create ( ) <EOL> form = LinkForm ( ) <EOL> choice = get_composite_key ( page ) <EOL> form = LinkForm ( { <EOL> '<STR_LIT>' : choice , <EOL> } ) <EOL> self . assertTrue ( form . is_valid ( ) ) <EOL> instance = form . save ( commit = False ) <EOL> self . assertEqual ( instance . link , page ) <EOL> form2 = LinkForm ( { <EOL> '<STR_LIT>' : choice , <EOL> } ) <EOL> form2 . save ( commit = False ) <EOL> def test_choices ( self ) : <EOL> page1 = LinkableThing . objects . create ( name = '<STR_LIT>' ) <EOL> page2 = LinkableThing . objects . create ( name = '<STR_LIT:a>' ) <EOL> page3 = AnotherLinkableThing . objects . create ( ) <EOL> page4 = LinkableThing3 . objects . create ( ) <EOL> form = LinkForm ( ) <EOL> self . assertEqual ( form . fields [ '<STR_LIT>' ] . choices , [ <EOL> ( '<STR_LIT>' , [ <EOL> convert_linkable_to_choice ( page3 ) , <EOL> ] ) , <EOL> ( '<STR_LIT>' , [ <EOL> convert_linkable_to_choice ( page2 ) , <EOL> convert_linkable_to_choice ( page1 ) , <EOL> ] ) , <EOL> ( '<STR_LIT>' , [ <EOL> convert_linkable_to_choice ( page4 ) , <EOL> ] ) , <EOL> ] ) </s>
