<s> import datetime <EOL> from django . conf import settings <EOL> from django . db import models <EOL> from django . test import TestCase <EOL> from generic_aggregation import generic_annotate as _generic_annotate , generic_aggregate as _generic_aggregate , generic_filter as _generic_filter <EOL> from generic_aggregation . utils import fallback_generic_annotate , fallback_generic_aggregate , fallback_generic_filter <EOL> from generic_aggregation_tests . models import ( <EOL> Food , Rating , CharFieldGFK <EOL> ) <EOL> class SimpleTest ( TestCase ) : <EOL> PAST_DATE = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def setUp ( self ) : <EOL> self . apple = Food . objects . create ( name = '<STR_LIT>' ) <EOL> self . orange = Food . objects . create ( name = '<STR_LIT>' ) <EOL> self . peach = Food . objects . create ( name = '<STR_LIT>' ) <EOL> Rating . objects . create ( content_object = self . apple , rating = <NUM_LIT:5> ) <EOL> Rating . objects . create ( content_object = self . apple , rating = <NUM_LIT:3> ) <EOL> Rating . objects . create ( content_object = self . apple , rating = <NUM_LIT:1> , <EOL> created = self . PAST_DATE ) <EOL> Rating . objects . create ( content_object = self . apple , rating = <NUM_LIT:3> , <EOL> created = self . PAST_DATE ) <EOL> Rating . objects . create ( content_object = self . orange , rating = <NUM_LIT:4> ) <EOL> Rating . objects . create ( content_object = self . orange , rating = <NUM_LIT:3> ) <EOL> Rating . objects . create ( content_object = self . orange , rating = <NUM_LIT:8> , <EOL> created = self . PAST_DATE ) <EOL> def generic_annotate ( self , * args , ** kwargs ) : <EOL> return _generic_annotate ( * args , ** kwargs ) <EOL> def generic_aggregate ( self , * args , ** kwargs ) : <EOL> return _generic_aggregate ( * args , ** kwargs ) <EOL> def generic_filter ( self , * args , ** kwargs ) : <EOL> return _generic_filter ( * args , ** kwargs ) <EOL> def test_annotation ( self ) : <EOL> annotated_qs = self . generic_annotate ( <EOL> Food . objects . all ( ) , <EOL> Rating . objects . all ( ) , <EOL> models . Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' ) <EOL> self . assertEqual ( annotated_qs . count ( ) , <NUM_LIT:3> ) <EOL> food_a , food_b , food_c = annotated_qs <EOL> self . assertEqual ( food_a . score , <NUM_LIT:4> ) <EOL> self . assertEqual ( food_a . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_b . score , <NUM_LIT:3> ) <EOL> self . assertEqual ( food_b . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_c . score , <NUM_LIT:0> ) <EOL> self . assertEqual ( food_c . name , '<STR_LIT>' ) <EOL> annotated_qs = self . generic_annotate ( Food . objects . all ( ) , Rating , models . Sum ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( annotated_qs . count ( ) , <NUM_LIT:3> ) <EOL> if settings . NULLS_ASC_SORT_FIRST : <EOL> food_b , food_a , food_c = annotated_qs . order_by ( '<STR_LIT>' ) <EOL> else : <EOL> food_c , food_b , food_a = annotated_qs . order_by ( '<STR_LIT>' ) <EOL> self . assertEqual ( food_b . score , <NUM_LIT:15> ) <EOL> self . assertEqual ( food_b . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_a . score , <NUM_LIT:12> ) <EOL> self . assertEqual ( food_a . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_c . score , None ) <EOL> self . assertEqual ( food_c . name , '<STR_LIT>' ) <EOL> annotated_qs = self . generic_annotate ( Food , Rating , models . Avg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( annotated_qs . count ( ) , <NUM_LIT:3> ) <EOL> if settings . NULLS_ASC_SORT_FIRST : <EOL> food_b , food_a , food_c = annotated_qs . order_by ( '<STR_LIT>' ) <EOL> else : <EOL> food_c , food_b , food_a = annotated_qs . order_by ( '<STR_LIT>' ) <EOL> self . assertEqual ( food_b . score , <NUM_LIT:5> ) <EOL> self . assertEqual ( food_b . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_a . score , <NUM_LIT:3> ) <EOL> self . assertEqual ( food_a . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_c . score , None ) <EOL> self . assertEqual ( food_c . name , '<STR_LIT>' ) <EOL> def test_aggregation ( self ) : <EOL> aggregated = self . generic_aggregate ( Food , Rating , models . Count ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( aggregated , <NUM_LIT:7> ) <EOL> aggregated = self . generic_aggregate ( Food . objects . all ( ) , Rating , models . Sum ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( aggregated , <NUM_LIT> ) <EOL> aggregated = self . generic_aggregate ( Food . objects . filter ( name = '<STR_LIT>' ) , Rating , models . Count ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( aggregated , <NUM_LIT:4> ) <EOL> aggregated = self . generic_aggregate ( Food . objects . filter ( name = '<STR_LIT>' ) , Rating , models . Count ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( aggregated , <NUM_LIT:3> ) <EOL> aggregated = self . generic_aggregate ( Food . objects . filter ( name = '<STR_LIT>' ) , Rating , models . Avg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( aggregated , <NUM_LIT:3> ) <EOL> aggregated = self . generic_aggregate ( Food . objects . filter ( name = '<STR_LIT>' ) , Rating , models . Avg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( aggregated , <NUM_LIT:5> ) <EOL> def test_subset_annotation ( self ) : <EOL> todays_ratings = Rating . objects . filter ( created__gte = datetime . date . today ( ) ) <EOL> annotated_qs = self . generic_annotate ( Food . objects . all ( ) , todays_ratings , models . Sum ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( annotated_qs . count ( ) , <NUM_LIT:3> ) <EOL> if settings . NULLS_ASC_SORT_FIRST : <EOL> food_a , food_b , food_c = annotated_qs . order_by ( '<STR_LIT>' ) <EOL> else : <EOL> food_c , food_a , food_b = annotated_qs . order_by ( '<STR_LIT>' ) <EOL> self . assertEqual ( food_a . score , <NUM_LIT:8> ) <EOL> self . assertEqual ( food_a . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_b . score , <NUM_LIT:7> ) <EOL> self . assertEqual ( food_b . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_c . score , None ) <EOL> self . assertEqual ( food_c . name , '<STR_LIT>' ) <EOL> persimmon = Food . objects . create ( name = '<STR_LIT>' ) <EOL> Rating . objects . create ( content_object = persimmon , rating = <NUM_LIT:1> , <EOL> created = self . PAST_DATE ) <EOL> self . assertEqual ( annotated_qs . count ( ) , <NUM_LIT:4> ) <EOL> self . assertEqual ( <EOL> { '<STR_LIT>' : <NUM_LIT:8> , '<STR_LIT>' : <NUM_LIT:7> , '<STR_LIT>' : None , '<STR_LIT>' : None } , <EOL> { food . name : food . score for food in annotated_qs } ) <EOL> def test_subset_aggregation ( self ) : <EOL> todays_ratings = Rating . objects . filter ( created__gte = datetime . date . today ( ) ) <EOL> aggregated = self . generic_aggregate ( Food . objects . all ( ) , todays_ratings , models . Sum ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( aggregated , <NUM_LIT:15> ) <EOL> aggregated = self . generic_aggregate ( Food . objects . all ( ) , todays_ratings , models . Count ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( aggregated , <NUM_LIT:4> ) <EOL> def test_charfield_pks ( self ) : <EOL> a1 = CharFieldGFK . objects . create ( name = '<STR_LIT>' , content_object = self . apple ) <EOL> a2 = CharFieldGFK . objects . create ( name = '<STR_LIT>' , content_object = self . apple ) <EOL> o1 = CharFieldGFK . objects . create ( name = '<STR_LIT>' , content_object = self . orange ) <EOL> annotated_qs = self . generic_annotate ( Food . objects . all ( ) , CharFieldGFK , models . Count ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( annotated_qs . count ( ) , <NUM_LIT:3> ) <EOL> food_a , food_b , food_c = annotated_qs . order_by ( '<STR_LIT>' ) <EOL> self . assertEqual ( food_b . score , <NUM_LIT:1> ) <EOL> self . assertEqual ( food_b . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_a . score , <NUM_LIT:2> ) <EOL> self . assertEqual ( food_a . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_c . score , <NUM_LIT:0> ) <EOL> self . assertEqual ( food_c . name , '<STR_LIT>' ) <EOL> aggregated = self . generic_aggregate ( Food . objects . all ( ) , CharFieldGFK , models . Count ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( aggregated , <NUM_LIT:3> ) <EOL> def test_custom_alias ( self ) : <EOL> annotated_qs = self . generic_annotate ( Food , Rating , models . Count ( '<STR_LIT>' ) , alias = '<STR_LIT:count>' ) <EOL> food_a , food_b , food_c = annotated_qs . order_by ( '<STR_LIT>' ) <EOL> self . assertEqual ( food_a . count , <NUM_LIT:4> ) <EOL> self . assertEqual ( food_a . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_b . count , <NUM_LIT:3> ) <EOL> self . assertEqual ( food_b . name , '<STR_LIT>' ) <EOL> self . assertEqual ( food_c . count , <NUM_LIT:0> ) <EOL> self . assertEqual ( food_c . name , '<STR_LIT>' ) <EOL> def test_filter ( self ) : <EOL> ratings = self . generic_filter ( Rating . objects . all ( ) , Food . objects . filter ( name = '<STR_LIT>' ) ) <EOL> self . assertEqual ( len ( ratings ) , <NUM_LIT:3> ) <EOL> for obj in ratings : <EOL> self . assertEqual ( obj . content_object . name , '<STR_LIT>' ) <EOL> def test_filter_cast ( self ) : <EOL> a1 = CharFieldGFK . objects . create ( name = '<STR_LIT>' , content_object = self . apple ) <EOL> a2 = CharFieldGFK . objects . create ( name = '<STR_LIT>' , content_object = self . apple ) <EOL> o1 = CharFieldGFK . objects . create ( name = '<STR_LIT>' , content_object = self . orange ) <EOL> qs = self . generic_filter ( CharFieldGFK . objects . all ( ) , Food . objects . filter ( name = '<STR_LIT>' ) ) <EOL> self . assertEqual ( len ( qs ) , <NUM_LIT:2> ) <EOL> for obj in qs : <EOL> self . assertEqual ( obj . content_object . name , '<STR_LIT>' ) <EOL> class FallbackTestCase ( SimpleTest ) : <EOL> def generic_annotate ( self , * args , ** kwargs ) : <EOL> return fallback_generic_annotate ( * args , ** kwargs ) <EOL> def generic_aggregate ( self , * args , ** kwargs ) : <EOL> return fallback_generic_aggregate ( * args , ** kwargs ) <EOL> def generic_filter ( self , * args , ** kwargs ) : <EOL> return fallback_generic_filter ( * args , ** kwargs ) </s>
