<s> from __future__ import absolute_import <EOL> from django import forms <EOL> from django . contrib . contenttypes . generic import generic_inlineformset_factory <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . test import TestCase <EOL> from . models import ( TaggedItem , ValuableTaggedItem , Comparison , Animal , <EOL> Vegetable , Mineral , Gecko ) <EOL> class GenericRelationsTests ( TestCase ) : <EOL> def test_generic_relations ( self ) : <EOL> lion = Animal . objects . create ( common_name = "<STR_LIT>" , latin_name = "<STR_LIT>" ) <EOL> platypus = Animal . objects . create ( <EOL> common_name = "<STR_LIT>" , latin_name = "<STR_LIT>" <EOL> ) <EOL> eggplant = Vegetable . objects . create ( name = "<STR_LIT>" , is_yucky = True ) <EOL> bacon = Vegetable . objects . create ( name = "<STR_LIT>" , is_yucky = False ) <EOL> quartz = Mineral . objects . create ( name = "<STR_LIT>" , hardness = <NUM_LIT:7> ) <EOL> bacon . tags . create ( tag = "<STR_LIT>" ) <EOL> bacon . tags . create ( tag = "<STR_LIT>" ) <EOL> lion . tags . create ( tag = "<STR_LIT>" ) <EOL> lion . tags . create ( tag = "<STR_LIT>" ) <EOL> platypus . tags . create ( tag = "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( lion . tags . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> self . assertQuerysetEqual ( bacon . tags . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> t = TaggedItem . objects . get ( tag = "<STR_LIT>" ) <EOL> self . assertEqual ( t . content_object , bacon ) <EOL> tag1 = TaggedItem . objects . create ( content_object = quartz , tag = "<STR_LIT>" ) <EOL> tag2 = TaggedItem . objects . create ( content_object = quartz , tag = "<STR_LIT>" ) <EOL> ctype = ContentType . objects . get_for_model ( quartz ) <EOL> q = TaggedItem . objects . filter ( <EOL> content_type__pk = ctype . id , object_id = quartz . id <EOL> ) <EOL> self . assertQuerysetEqual ( q , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> tag1 . content_object = platypus <EOL> tag1 . save ( ) <EOL> self . assertQuerysetEqual ( platypus . tags . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> q = TaggedItem . objects . filter ( <EOL> content_type__pk = ctype . id , object_id = quartz . id <EOL> ) <EOL> self . assertQuerysetEqual ( q , [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( Animal . objects . order_by ( '<STR_LIT>' ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> self . assertQuerysetEqual ( Animal . objects . filter ( tags__tag = '<STR_LIT>' ) , [ <EOL> "<STR_LIT>" <EOL> ] ) <EOL> self . assertQuerysetEqual ( Animal . objects . exclude ( tags__tag = '<STR_LIT>' ) , [ <EOL> "<STR_LIT>" <EOL> ] ) <EOL> comp_func = lambda obj : ( <EOL> obj . tag , obj . content_type . model_class ( ) , obj . object_id <EOL> ) <EOL> self . assertQuerysetEqual ( TaggedItem . objects . all ( ) , [ <EOL> ( u'<STR_LIT>' , Mineral , quartz . pk ) , <EOL> ( u'<STR_LIT>' , Animal , platypus . pk ) , <EOL> ( u'<STR_LIT>' , Vegetable , bacon . pk ) , <EOL> ( u'<STR_LIT>' , Animal , lion . pk ) , <EOL> ( u'<STR_LIT>' , Vegetable , bacon . pk ) , <EOL> ( u'<STR_LIT>' , Animal , platypus . pk ) , <EOL> ( u'<STR_LIT>' , Animal , lion . pk ) <EOL> ] , <EOL> comp_func <EOL> ) <EOL> lion . delete ( ) <EOL> self . assertQuerysetEqual ( TaggedItem . objects . all ( ) , [ <EOL> ( u'<STR_LIT>' , Mineral , quartz . pk ) , <EOL> ( u'<STR_LIT>' , Animal , platypus . pk ) , <EOL> ( u'<STR_LIT>' , Vegetable , bacon . pk ) , <EOL> ( u'<STR_LIT>' , Vegetable , bacon . pk ) , <EOL> ( u'<STR_LIT>' , Animal , platypus . pk ) <EOL> ] , <EOL> comp_func <EOL> ) <EOL> quartz_pk = quartz . pk <EOL> quartz . delete ( ) <EOL> self . assertQuerysetEqual ( TaggedItem . objects . all ( ) , [ <EOL> ( u'<STR_LIT>' , Mineral , quartz_pk ) , <EOL> ( u'<STR_LIT>' , Animal , platypus . pk ) , <EOL> ( u'<STR_LIT>' , Vegetable , bacon . pk ) , <EOL> ( u'<STR_LIT>' , Vegetable , bacon . pk ) , <EOL> ( u'<STR_LIT>' , Animal , platypus . pk ) <EOL> ] , <EOL> comp_func <EOL> ) <EOL> tag = TaggedItem . objects . order_by ( "<STR_LIT:id>" ) [ <NUM_LIT:0> ] <EOL> tag . delete ( ) <EOL> self . assertQuerysetEqual ( bacon . tags . all ( ) , [ "<STR_LIT>" ] ) <EOL> self . assertQuerysetEqual ( TaggedItem . objects . all ( ) , [ <EOL> ( u'<STR_LIT>' , Mineral , quartz_pk ) , <EOL> ( u'<STR_LIT>' , Animal , platypus . pk ) , <EOL> ( u'<STR_LIT>' , Vegetable , bacon . pk ) , <EOL> ( u'<STR_LIT>' , Animal , platypus . pk ) <EOL> ] , <EOL> comp_func <EOL> ) <EOL> TaggedItem . objects . filter ( tag = '<STR_LIT>' ) . delete ( ) <EOL> ctype = ContentType . objects . get_for_model ( lion ) <EOL> self . assertQuerysetEqual ( Animal . objects . filter ( tags__content_type = ctype ) , [ <EOL> "<STR_LIT>" <EOL> ] ) <EOL> def test_multiple_gfk ( self ) : <EOL> tiger = Animal . objects . create ( common_name = "<STR_LIT>" ) <EOL> cheetah = Animal . objects . create ( common_name = "<STR_LIT>" ) <EOL> bear = Animal . objects . create ( common_name = "<STR_LIT>" ) <EOL> Comparison . objects . create ( <EOL> first_obj = cheetah , other_obj = tiger , comparative = "<STR_LIT>" <EOL> ) <EOL> Comparison . objects . create ( <EOL> first_obj = tiger , other_obj = cheetah , comparative = "<STR_LIT>" <EOL> ) <EOL> tiger . comparisons . create ( other_obj = bear , comparative = "<STR_LIT>" ) <EOL> tiger . comparisons . create ( other_obj = cheetah , comparative = "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( cheetah . comparisons . all ( ) , [ <EOL> "<STR_LIT>" <EOL> ] ) <EOL> self . assertQuerysetEqual ( tiger . comparisons . filter ( comparative = "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> subjective = [ "<STR_LIT>" ] <EOL> tiger . comparisons . filter ( comparative__in = subjective ) . delete ( ) <EOL> self . assertQuerysetEqual ( Comparison . objects . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] ) <EOL> cheetah . delete ( ) <EOL> self . assertQuerysetEqual ( Comparison . objects . all ( ) , [ <EOL> "<STR_LIT>" <EOL> ] ) <EOL> def test_gfk_subclasses ( self ) : <EOL> quartz = Mineral . objects . create ( name = "<STR_LIT>" , hardness = <NUM_LIT:7> ) <EOL> valuedtag = ValuableTaggedItem . objects . create ( <EOL> content_object = quartz , tag = "<STR_LIT>" , value = <NUM_LIT:10> <EOL> ) <EOL> self . assertEqual ( valuedtag . content_object , quartz ) <EOL> def test_generic_inline_formsets ( self ) : <EOL> GenericFormSet = generic_inlineformset_factory ( TaggedItem , extra = <NUM_LIT:1> ) <EOL> formset = GenericFormSet ( ) <EOL> self . assertHTMLEqual ( u'<STR_LIT>' . join ( form . as_p ( ) for form in formset . forms ) , u"""<STR_LIT>""" ) <EOL> formset = GenericFormSet ( instance = Animal ( ) ) <EOL> self . assertHTMLEqual ( u'<STR_LIT>' . join ( form . as_p ( ) for form in formset . forms ) , u"""<STR_LIT>""" ) <EOL> platypus = Animal . objects . create ( <EOL> common_name = "<STR_LIT>" , latin_name = "<STR_LIT>" <EOL> ) <EOL> platypus . tags . create ( tag = "<STR_LIT>" ) <EOL> GenericFormSet = generic_inlineformset_factory ( TaggedItem , extra = <NUM_LIT:1> ) <EOL> formset = GenericFormSet ( instance = platypus ) <EOL> tagged_item_id = TaggedItem . objects . get ( <EOL> tag = '<STR_LIT>' , object_id = platypus . id <EOL> ) . id <EOL> self . assertHTMLEqual ( u'<STR_LIT>' . join ( form . as_p ( ) for form in formset . forms ) , u"""<STR_LIT>""" % tagged_item_id ) <EOL> lion = Animal . objects . create ( common_name = "<STR_LIT>" , latin_name = "<STR_LIT>" ) <EOL> formset = GenericFormSet ( instance = lion , prefix = '<STR_LIT:x>' ) <EOL> self . assertHTMLEqual ( u'<STR_LIT>' . join ( form . as_p ( ) for form in formset . forms ) , u"""<STR_LIT>""" ) <EOL> def test_gfk_manager ( self ) : <EOL> tailless = Gecko . objects . create ( has_tail = False ) <EOL> tag = TaggedItem . objects . create ( content_object = tailless , tag = "<STR_LIT>" ) <EOL> self . assertEqual ( tag . content_object , tailless ) <EOL> class CustomWidget ( forms . CharField ) : <EOL> pass <EOL> class TaggedItemForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = TaggedItem <EOL> widgets = { '<STR_LIT>' : CustomWidget } <EOL> class GenericInlineFormsetTest ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_generic_inlineformset_factory ( self ) : <EOL> Formset = generic_inlineformset_factory ( TaggedItem , TaggedItemForm ) <EOL> form = Formset ( ) . forms [ <NUM_LIT:0> ] <EOL> self . assertTrue ( isinstance ( form [ '<STR_LIT>' ] . field . widget , CustomWidget ) ) </s>
