<s> from django . test import TestCase <EOL> from tastypie . contrib . contenttypes . fields import GenericForeignKeyField <EOL> from content_gfk . models import Note , Quote , Definition <EOL> from content_gfk . api . resources import NoteResource , QuoteResource <EOL> class ContentTypeFieldTestCase ( TestCase ) : <EOL> def test_init ( self ) : <EOL> with self . assertRaises ( ValueError ) : <EOL> GenericForeignKeyField ( ( ( Note , NoteResource ) ) , '<STR_LIT>' ) <EOL> with self . assertRaises ( ValueError ) : <EOL> GenericForeignKeyField ( { } , '<STR_LIT>' ) <EOL> with self . assertRaises ( ValueError ) : <EOL> GenericForeignKeyField ( { NoteResource : Note } , '<STR_LIT>' ) <EOL> def test_get_related_resource ( self ) : <EOL> gfk_field = GenericForeignKeyField ( { <EOL> Note : NoteResource , <EOL> Quote : QuoteResource <EOL> } , '<STR_LIT>' ) <EOL> definition_1 = Definition . objects . create ( <EOL> word = '<STR_LIT>' , <EOL> content = "<STR_LIT>" <EOL> ) <EOL> with self . assertRaises ( TypeError ) : <EOL> gfk_field . get_related_resource ( definition_1 ) <EOL> note_1 = Note . objects . create ( <EOL> title = '<STR_LIT>' , <EOL> content = '<STR_LIT>' <EOL> ) <EOL> self . assertTrue ( isinstance ( gfk_field . get_related_resource ( note_1 ) , NoteResource ) ) <EOL> def test_resource_from_uri ( self ) : <EOL> note_2 = Note . objects . create ( <EOL> title = '<STR_LIT>' , <EOL> content = '<STR_LIT>' <EOL> ) <EOL> gfk_field = GenericForeignKeyField ( { <EOL> Note : NoteResource , <EOL> Quote : QuoteResource <EOL> } , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> gfk_field . resource_from_uri ( <EOL> gfk_field . to_class ( ) , <EOL> '<STR_LIT>' % note_2 . pk <EOL> ) . obj , <EOL> note_2 <EOL> ) <EOL> def test_build_related_resource ( self ) : <EOL> gfk_field = GenericForeignKeyField ( { <EOL> Note : NoteResource , <EOL> Quote : QuoteResource <EOL> } , '<STR_LIT>' ) <EOL> quote_1 = Quote . objects . create ( <EOL> byline = '<STR_LIT>' , <EOL> content = '<STR_LIT>' <EOL> ) <EOL> qr = QuoteResource ( ) <EOL> qr . build_bundle ( obj = quote_1 ) <EOL> bundle = gfk_field . build_related_resource ( <EOL> '<STR_LIT>' % quote_1 . pk <EOL> ) <EOL> self . assertEqual ( bundle . obj , quote_1 ) </s>
