<s> from __future__ import absolute_import , unicode_literals <EOL> import datetime <EOL> import pickle <EOL> from decimal import Decimal <EOL> from operator import attrgetter <EOL> from django . core . exceptions import FieldError <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . db . models import Count , Max , Avg , Sum , StdDev , Variance , F , Q <EOL> from django . test import TestCase , Approximate , skipUnlessDBFeature <EOL> from django . utils import six <EOL> from django . utils . unittest import expectedFailure <EOL> from . models import ( <EOL> Author , Book , Publisher , Clues , Entries , HardbackBook , ItemTag , <EOL> WithManualPK , Alfa , Bravo , Charlie ) <EOL> class AggregationTests ( TestCase ) : <EOL> fixtures = [ "<STR_LIT>" ] <EOL> def assertObjectAttrs ( self , obj , ** kwargs ) : <EOL> for attr , value in six . iteritems ( kwargs ) : <EOL> self . assertEqual ( getattr ( obj , attr ) , value ) <EOL> def test_aggregates_in_where_clause ( self ) : <EOL> """<STR_LIT>""" <EOL> qs = Book . objects . values ( '<STR_LIT>' ) . annotate ( Max ( '<STR_LIT:id>' ) ) <EOL> qs = qs . order_by ( '<STR_LIT>' ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> books = Book . objects . order_by ( '<STR_LIT:id>' ) <EOL> qs1 = books . filter ( id__in = qs ) <EOL> qs2 = books . filter ( id__in = list ( qs ) ) <EOL> self . assertEqual ( list ( qs1 ) , list ( qs2 ) ) <EOL> def test_aggregates_in_where_clause_pre_eval ( self ) : <EOL> """<STR_LIT>""" <EOL> qs = Book . objects . values ( '<STR_LIT>' ) . annotate ( Max ( '<STR_LIT:id>' ) ) <EOL> qs = qs . order_by ( '<STR_LIT>' ) . values_list ( '<STR_LIT>' , flat = True ) <EOL> list ( qs ) <EOL> books = Book . objects . order_by ( '<STR_LIT:id>' ) <EOL> qs1 = books . filter ( id__in = qs ) <EOL> qs2 = books . filter ( id__in = list ( qs ) ) <EOL> self . assertEqual ( list ( qs1 ) , list ( qs2 ) ) <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> def test_annotate_with_extra ( self ) : <EOL> """<STR_LIT>""" <EOL> shortest_book_sql = """<STR_LIT>""" <EOL> qs = Publisher . objects . extra ( select = { <EOL> '<STR_LIT>' : shortest_book_sql , <EOL> } ) . annotate ( total_books = Count ( '<STR_LIT>' ) ) <EOL> list ( qs ) <EOL> def test_aggregate ( self ) : <EOL> self . assertEqual ( <EOL> Author . objects . order_by ( "<STR_LIT:name>" ) . aggregate ( Avg ( "<STR_LIT>" ) ) , <EOL> { "<STR_LIT>" : Approximate ( <NUM_LIT> , places = <NUM_LIT:1> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( Sum ( "<STR_LIT>" ) ) , <EOL> { "<STR_LIT>" : <NUM_LIT> } , <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( Sum ( '<STR_LIT>' ) , Avg ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : Approximate ( <NUM_LIT> , places = <NUM_LIT:2> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . values ( ) . aggregate ( Sum ( '<STR_LIT>' ) , Avg ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : Approximate ( <NUM_LIT> , places = <NUM_LIT:2> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } ) . aggregate ( Sum ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : <NUM_LIT> } <EOL> ) <EOL> def test_annotation ( self ) : <EOL> obj = Book . objects . annotate ( mean_auth_age = Avg ( "<STR_LIT>" ) ) . extra ( select = { "<STR_LIT>" : "<STR_LIT>" } ) . get ( pk = <NUM_LIT:2> ) <EOL> self . assertObjectAttrs ( obj , <EOL> contact_id = <NUM_LIT:3> , <EOL> id = <NUM_LIT:2> , <EOL> isbn = '<STR_LIT>' , <EOL> mean_auth_age = <NUM_LIT> , <EOL> name = '<STR_LIT>' , <EOL> pages = <NUM_LIT> , <EOL> price = Decimal ( "<STR_LIT>" ) , <EOL> pubdate = datetime . date ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:3> ) , <EOL> publisher_id = <NUM_LIT:2> , <EOL> rating = <NUM_LIT> <EOL> ) <EOL> self . assertTrue ( obj . manufacture_cost == <NUM_LIT> or obj . manufacture_cost == Decimal ( '<STR_LIT>' ) ) <EOL> obj = Book . objects . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } ) . annotate ( mean_auth_age = Avg ( '<STR_LIT>' ) ) . get ( pk = <NUM_LIT:2> ) <EOL> self . assertObjectAttrs ( obj , <EOL> contact_id = <NUM_LIT:3> , <EOL> id = <NUM_LIT:2> , <EOL> isbn = '<STR_LIT>' , <EOL> mean_auth_age = <NUM_LIT> , <EOL> name = '<STR_LIT>' , <EOL> pages = <NUM_LIT> , <EOL> price = Decimal ( "<STR_LIT>" ) , <EOL> pubdate = datetime . date ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:3> ) , <EOL> publisher_id = <NUM_LIT:2> , <EOL> rating = <NUM_LIT> <EOL> ) <EOL> self . assertTrue ( obj . manufacture_cost == <NUM_LIT> or obj . manufacture_cost == Decimal ( '<STR_LIT>' ) ) <EOL> obj = Book . objects . annotate ( mean_auth_age = Avg ( '<STR_LIT>' ) ) . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } ) . values ( ) . get ( pk = <NUM_LIT:2> ) <EOL> manufacture_cost = obj [ '<STR_LIT>' ] <EOL> self . assertTrue ( manufacture_cost == <NUM_LIT> or manufacture_cost == Decimal ( '<STR_LIT>' ) ) <EOL> del obj [ '<STR_LIT>' ] <EOL> self . assertEqual ( obj , { <EOL> "<STR_LIT>" : <NUM_LIT:3> , <EOL> "<STR_LIT:id>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : Decimal ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : datetime . date ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:3> ) , <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } ) <EOL> obj = Book . objects . values ( ) . annotate ( mean_auth_age = Avg ( '<STR_LIT>' ) ) . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } ) . get ( pk = <NUM_LIT:2> ) <EOL> manufacture_cost = obj [ '<STR_LIT>' ] <EOL> self . assertTrue ( manufacture_cost == <NUM_LIT> or manufacture_cost == Decimal ( '<STR_LIT>' ) ) <EOL> del obj [ '<STR_LIT>' ] <EOL> self . assertEqual ( obj , { <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT:id>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : Decimal ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : datetime . date ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:3> ) , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT> <EOL> } ) <EOL> obj = Book . objects . annotate ( mean_auth_age = Avg ( '<STR_LIT>' ) ) . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } ) . values ( '<STR_LIT:name>' ) . get ( pk = <NUM_LIT:1> ) <EOL> self . assertEqual ( obj , { <EOL> "<STR_LIT:name>" : '<STR_LIT>' , <EOL> } ) <EOL> obj = Book . objects . annotate ( mean_auth_age = Avg ( '<STR_LIT>' ) ) . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } ) . values ( '<STR_LIT:name>' , '<STR_LIT>' ) . get ( pk = <NUM_LIT:1> ) <EOL> self . assertEqual ( obj , { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> } ) <EOL> qs = Book . objects . annotate ( n_authors = Count ( '<STR_LIT>' ) ) . values ( '<STR_LIT:name>' ) . filter ( n_authors__gt = <NUM_LIT:2> ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { "<STR_LIT:name>" : '<STR_LIT>' } <EOL> ] , <EOL> lambda b : b , <EOL> ) <EOL> obj = Book . objects . values ( '<STR_LIT:name>' ) . annotate ( mean_auth_age = Avg ( '<STR_LIT>' ) ) . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } ) . get ( pk = <NUM_LIT:1> ) <EOL> self . assertEqual ( obj , { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> } ) <EOL> self . assertEqual ( <EOL> len ( Author . objects . annotate ( Avg ( '<STR_LIT>' ) ) . values ( ) ) , <EOL> <NUM_LIT:9> <EOL> ) <EOL> qs = Book . objects . values ( '<STR_LIT>' ) . annotate ( oldest = Max ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) . annotate ( Max ( '<STR_LIT>' ) ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : Decimal ( "<STR_LIT>" ) , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:3> } , <EOL> { '<STR_LIT>' : Decimal ( "<STR_LIT>" ) , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:7> } , <EOL> { '<STR_LIT>' : Decimal ( "<STR_LIT>" ) , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : Decimal ( "<STR_LIT>" ) , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:9> } , <EOL> { '<STR_LIT>' : Decimal ( "<STR_LIT>" ) , '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:7> } <EOL> ] , <EOL> lambda b : b , <EOL> ) <EOL> def test_aggrate_annotation ( self ) : <EOL> vals = Book . objects . all ( ) . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . aggregate ( Max ( '<STR_LIT>' ) , Max ( '<STR_LIT>' ) , Sum ( '<STR_LIT>' ) , Avg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( vals , { <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : Approximate ( <NUM_LIT> , places = <NUM_LIT:2> ) , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : Decimal ( "<STR_LIT>" ) <EOL> } ) <EOL> def test_field_error ( self ) : <EOL> self . assertRaises ( <EOL> FieldError , <EOL> lambda : Book . objects . all ( ) . aggregate ( num_authors = Count ( '<STR_LIT:foo>' ) ) <EOL> ) <EOL> self . assertRaises ( <EOL> FieldError , <EOL> lambda : Book . objects . all ( ) . annotate ( num_authors = Count ( '<STR_LIT:foo>' ) ) <EOL> ) <EOL> self . assertRaises ( <EOL> FieldError , <EOL> lambda : Book . objects . all ( ) . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . aggregate ( Max ( '<STR_LIT:foo>' ) ) <EOL> ) <EOL> def test_more ( self ) : <EOL> self . assertEqual ( <EOL> Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . count ( ) , <EOL> <NUM_LIT:6> <EOL> ) <EOL> vals = Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . aggregate ( Max ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <EOL> vals , <EOL> { '<STR_LIT>' : <NUM_LIT:3> } <EOL> ) <EOL> vals = Publisher . objects . annotate ( avg_price = Avg ( '<STR_LIT>' ) ) . aggregate ( Max ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <EOL> vals , <EOL> { '<STR_LIT>' : <NUM_LIT> } <EOL> ) <EOL> vals = Book . objects . aggregate ( number = Max ( '<STR_LIT>' ) , select = Max ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <EOL> vals , <EOL> { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } <EOL> ) <EOL> obj = Book . objects . select_related ( '<STR_LIT>' ) . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . values ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( obj , { <EOL> '<STR_LIT>' : <NUM_LIT:8> , <EOL> '<STR_LIT:id>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : Decimal ( "<STR_LIT>" ) , <EOL> '<STR_LIT>' : datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:15> ) , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } ) <EOL> self . assertEqual ( <EOL> len ( Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) ) , <EOL> <NUM_LIT:6> <EOL> ) <EOL> self . assertEqual ( <EOL> len ( Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . filter ( num_authors__gt = <NUM_LIT:2> ) ) , <EOL> <NUM_LIT:1> <EOL> ) <EOL> self . assertEqual ( <EOL> len ( Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . exclude ( num_authors__gt = <NUM_LIT:2> ) ) , <EOL> <NUM_LIT:5> <EOL> ) <EOL> self . assertEqual ( <EOL> len ( Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . filter ( num_authors__lt = <NUM_LIT:3> ) . exclude ( num_authors__lt = <NUM_LIT:2> ) ) , <EOL> <NUM_LIT:2> <EOL> ) <EOL> self . assertEqual ( <EOL> len ( Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . exclude ( num_authors__lt = <NUM_LIT:2> ) . filter ( num_authors__lt = <NUM_LIT:3> ) ) , <EOL> <NUM_LIT:2> <EOL> ) <EOL> def test_aggregate_fexpr ( self ) : <EOL> qs = Publisher . objects . annotate ( num_books = Count ( '<STR_LIT>' ) ) . filter ( num_books__lt = F ( '<STR_LIT>' ) / <NUM_LIT:2> ) . order_by ( '<STR_LIT:name>' ) . values ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:9> } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:7> } <EOL> ] , <EOL> lambda p : p , <EOL> ) <EOL> qs = Publisher . objects . annotate ( num_books = Count ( '<STR_LIT>' ) ) . exclude ( num_books__lt = F ( '<STR_LIT>' ) / <NUM_LIT:2> ) . order_by ( '<STR_LIT:name>' ) . values ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:3> } , <EOL> { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:name>' : "<STR_LIT>" , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } <EOL> ] , <EOL> lambda p : p , <EOL> ) <EOL> qs = Publisher . objects . annotate ( num_books = Count ( '<STR_LIT>' ) ) . filter ( num_awards__gt = <NUM_LIT:2> * F ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT:name>' ) . values ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:9> } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:7> } <EOL> ] , <EOL> lambda p : p , <EOL> ) <EOL> qs = Publisher . objects . annotate ( num_books = Count ( '<STR_LIT>' ) ) . exclude ( num_books__lt = F ( '<STR_LIT>' ) / <NUM_LIT:2> ) . order_by ( '<STR_LIT:name>' ) . values ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:3> } , <EOL> { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT:name>' : "<STR_LIT>" , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } <EOL> ] , <EOL> lambda p : p , <EOL> ) <EOL> def test_db_col_table ( self ) : <EOL> qs = Clues . objects . values ( '<STR_LIT>' ) . annotate ( Appearances = Count ( '<STR_LIT>' ) , Distinct_Clues = Count ( '<STR_LIT>' , distinct = True ) ) <EOL> self . assertQuerysetEqual ( qs , [ ] ) <EOL> qs = Entries . objects . annotate ( clue_count = Count ( '<STR_LIT>' ) ) <EOL> self . assertQuerysetEqual ( qs , [ ] ) <EOL> def test_boolean_conversion ( self ) : <EOL> e = Entries . objects . create ( Entry = '<STR_LIT:foo>' ) <EOL> c = Clues . objects . create ( EntryID = e , Clue = '<STR_LIT:bar>' ) <EOL> qs = Clues . objects . select_related ( '<STR_LIT>' ) . annotate ( Count ( '<STR_LIT>' ) ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ c ] , lambda x : x ) <EOL> self . assertEqual ( qs [ <NUM_LIT:0> ] . EntryID , e ) <EOL> self . assertIs ( qs [ <NUM_LIT:0> ] . EntryID . Exclude , False ) <EOL> def test_empty ( self ) : <EOL> self . assertEqual ( <EOL> Book . objects . filter ( id__in = [ ] ) . count ( ) , <EOL> <NUM_LIT:0> <EOL> ) <EOL> vals = Book . objects . filter ( id__in = [ ] ) . aggregate ( num_authors = Count ( '<STR_LIT>' ) , avg_authors = Avg ( '<STR_LIT>' ) , max_authors = Max ( '<STR_LIT>' ) , max_price = Max ( '<STR_LIT>' ) , max_rating = Max ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <EOL> vals , <EOL> { '<STR_LIT>' : None , '<STR_LIT>' : None , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : None , '<STR_LIT>' : None } <EOL> ) <EOL> qs = Publisher . objects . filter ( pk = <NUM_LIT:5> ) . annotate ( num_authors = Count ( '<STR_LIT>' ) , avg_authors = Avg ( '<STR_LIT>' ) , max_authors = Max ( '<STR_LIT>' ) , max_price = Max ( '<STR_LIT>' ) , max_rating = Max ( '<STR_LIT>' ) ) . values ( ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : None , '<STR_LIT:name>' : "<STR_LIT>" , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : None , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : None , '<STR_LIT:id>' : <NUM_LIT:5> , '<STR_LIT>' : None } <EOL> ] , <EOL> lambda p : p <EOL> ) <EOL> def test_more_more ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' , '<STR_LIT:name>' ) , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda b : b . name <EOL> ) <EOL> qs = Book . objects . filter ( rating__lt = <NUM_LIT> ) . select_related ( ) . annotate ( Avg ( '<STR_LIT>' ) ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , Approximate ( <NUM_LIT> , places = <NUM_LIT:2> ) , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> , '<STR_LIT>' , '<STR_LIT>' ) <EOL> ] , <EOL> lambda b : ( b . name , b . authors__age__avg , b . publisher . name , b . contact . name ) <EOL> ) <EOL> qs = Book . objects . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } ) . values ( '<STR_LIT>' ) . annotate ( Count ( '<STR_LIT:id>' ) ) . order_by ( '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:2> } , <EOL> { '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> ] , <EOL> lambda b : b <EOL> ) <EOL> qs = Book . objects . extra ( select = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:foo>' : '<STR_LIT>' } ) . values ( '<STR_LIT>' ) . annotate ( Count ( '<STR_LIT:id>' ) ) . order_by ( '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:2> } , <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:1> } , <EOL> { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:2> } , <EOL> { '<STR_LIT>' : <NUM_LIT:4> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> ] , <EOL> lambda b : b <EOL> ) <EOL> ids = Book . objects . filter ( pages__gt = <NUM_LIT:100> ) . annotate ( n_authors = Count ( '<STR_LIT>' ) ) . filter ( n_authors__gt = <NUM_LIT:2> ) . order_by ( '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> Book . objects . filter ( id__in = ids ) , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda b : b . name <EOL> ) <EOL> qs = Book . objects . values ( '<STR_LIT>' ) . annotate ( max_pages = Max ( '<STR_LIT>' ) ) . order_by ( ) <EOL> grouping , gb_params = qs . query . get_compiler ( qs . db ) . get_grouping ( [ ] , [ ] ) <EOL> self . assertEqual ( len ( grouping ) , <NUM_LIT:1> ) <EOL> def test_duplicate_alias ( self ) : <EOL> self . assertRaises ( ValueError , Book . objects . all ( ) . annotate , Avg ( '<STR_LIT>' ) , authors__age__avg = Avg ( '<STR_LIT>' ) ) <EOL> def test_field_name_conflict ( self ) : <EOL> self . assertRaises ( ValueError , Author . objects . annotate , age = Avg ( '<STR_LIT>' ) ) <EOL> def test_m2m_name_conflict ( self ) : <EOL> self . assertRaises ( ValueError , Author . objects . annotate , friends = Count ( '<STR_LIT>' ) ) <EOL> def test_values_queryset_non_conflict ( self ) : <EOL> results = Author . objects . values ( '<STR_LIT:name>' ) . annotate ( age = Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertEqual ( len ( results ) , <NUM_LIT:9> ) <EOL> self . assertEqual ( results [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( results [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> results = Author . objects . values ( '<STR_LIT:name>' ) . annotate ( age = Avg ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertEqual ( len ( results ) , <NUM_LIT:9> ) <EOL> self . assertEqual ( results [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( results [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <NUM_LIT> ) <EOL> results = Author . objects . values ( '<STR_LIT:name>' ) . annotate ( friends = Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertEqual ( len ( results ) , <NUM_LIT:9> ) <EOL> self . assertEqual ( results [ <NUM_LIT:0> ] [ '<STR_LIT:name>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( results [ <NUM_LIT:0> ] [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> def test_reverse_relation_name_conflict ( self ) : <EOL> self . assertRaises ( ValueError , Author . objects . annotate , book_contact_set = Avg ( '<STR_LIT>' ) ) <EOL> def test_pickle ( self ) : <EOL> qs = Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) <EOL> pickle . dumps ( qs ) <EOL> query = qs . query . get_compiler ( qs . db ) . as_sql ( ) [ <NUM_LIT:0> ] <EOL> qs2 = pickle . loads ( pickle . dumps ( qs ) ) <EOL> self . assertEqual ( <EOL> qs2 . query . get_compiler ( qs2 . db ) . as_sql ( ) [ <NUM_LIT:0> ] , <EOL> query , <EOL> ) <EOL> def test_more_more_more ( self ) : <EOL> books = Book . objects . all ( ) <EOL> books . aggregate ( Avg ( "<STR_LIT>" ) ) <EOL> self . assertQuerysetEqual ( <EOL> books . all ( ) , [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] , <EOL> lambda b : b . name <EOL> ) <EOL> qs = Book . objects . annotate ( num_authors = Count ( '<STR_LIT>' ) ) . filter ( num_authors = <NUM_LIT:2> ) . dates ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:15> ) , <EOL> datetime . date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:6> ) , <EOL> ] , <EOL> lambda b : b <EOL> ) <EOL> qs = Book . objects . annotate ( mean_auth_age = Avg ( '<STR_LIT>' ) ) . extra ( select = { '<STR_LIT>' : '<STR_LIT>' } , select_params = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) . order_by ( '<STR_LIT>' ) . values ( '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> <EOL> ] , <EOL> lambda b : int ( b [ "<STR_LIT>" ] ) <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . values ( '<STR_LIT>' ) . annotate ( Count ( '<STR_LIT>' ) ) . count ( ) , <EOL> <NUM_LIT:4> <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . annotate ( Count ( '<STR_LIT>' ) ) . values ( '<STR_LIT>' ) . count ( ) , <EOL> <NUM_LIT:6> <EOL> ) <EOL> publishers = Publisher . objects . filter ( id__in = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> self . assertEqual ( <EOL> sorted ( p . name for p in publishers ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> ) <EOL> publishers = publishers . annotate ( n_books = Count ( "<STR_LIT>" ) ) <EOL> sorted_publishers = sorted ( publishers , key = lambda x : x . name ) <EOL> self . assertEqual ( <EOL> sorted_publishers [ <NUM_LIT:0> ] . n_books , <EOL> <NUM_LIT:2> <EOL> ) <EOL> self . assertEqual ( <EOL> sorted_publishers [ <NUM_LIT:1> ] . n_books , <EOL> <NUM_LIT:1> <EOL> ) <EOL> self . assertEqual ( <EOL> sorted ( p . name for p in publishers ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> ) <EOL> books = Book . objects . filter ( publisher__in = publishers ) <EOL> self . assertQuerysetEqual ( <EOL> books , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda b : b . name <EOL> ) <EOL> self . assertEqual ( <EOL> sorted ( p . name for p in publishers ) , <EOL> [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> ) <EOL> self . assertEqual ( <EOL> HardbackBook . objects . aggregate ( n_pages = Sum ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : <NUM_LIT> } <EOL> ) <EOL> self . assertEqual ( <EOL> HardbackBook . objects . aggregate ( n_pages = Sum ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : <NUM_LIT> } , <EOL> ) <EOL> qs = HardbackBook . objects . annotate ( n_authors = Count ( '<STR_LIT>' ) ) . values ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' } <EOL> ] , <EOL> lambda h : h <EOL> ) <EOL> qs = HardbackBook . objects . annotate ( n_authors = Count ( '<STR_LIT>' ) ) . values ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> { '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT:name>' : '<STR_LIT>' } , <EOL> { '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT:name>' : '<STR_LIT>' } <EOL> ] , <EOL> lambda h : h , <EOL> ) <EOL> self . assertRaises ( <EOL> FieldError , <EOL> lambda : Book . objects . annotate ( mean_age = Avg ( '<STR_LIT>' ) ) . annotate ( Avg ( '<STR_LIT>' ) ) <EOL> ) <EOL> def test_empty_filter_count ( self ) : <EOL> self . assertEqual ( <EOL> Author . objects . filter ( id__in = [ ] ) . annotate ( Count ( "<STR_LIT>" ) ) . count ( ) , <EOL> <NUM_LIT:0> <EOL> ) <EOL> def test_empty_filter_aggregate ( self ) : <EOL> self . assertEqual ( <EOL> Author . objects . filter ( id__in = [ ] ) . annotate ( Count ( "<STR_LIT>" ) ) . aggregate ( Count ( "<STR_LIT>" ) ) , <EOL> { "<STR_LIT>" : None } <EOL> ) <EOL> def test_none_call_before_aggregate ( self ) : <EOL> self . assertEqual ( <EOL> Author . objects . none ( ) . aggregate ( Avg ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : None } <EOL> ) <EOL> def test_annotate_and_join ( self ) : <EOL> self . assertEqual ( <EOL> Author . objects . annotate ( c = Count ( "<STR_LIT>" ) ) . exclude ( friends__name = "<STR_LIT>" ) . count ( ) , <EOL> Author . objects . count ( ) <EOL> ) <EOL> def test_f_expression_annotation ( self ) : <EOL> qs = Book . objects . values ( "<STR_LIT:name>" ) . annotate ( <EOL> n_authors = Count ( "<STR_LIT>" ) <EOL> ) . filter ( <EOL> pages__lt = F ( "<STR_LIT>" ) * <NUM_LIT:200> <EOL> ) . values_list ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> Book . objects . filter ( pk__in = qs ) , [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> def test_values_annotate_values ( self ) : <EOL> qs = Book . objects . values ( "<STR_LIT:name>" ) . annotate ( <EOL> n_authors = Count ( "<STR_LIT>" ) <EOL> ) . values_list ( "<STR_LIT>" , flat = True ) <EOL> self . assertEqual ( list ( qs ) , list ( Book . objects . values_list ( "<STR_LIT>" , flat = True ) ) ) <EOL> def test_having_group_by ( self ) : <EOL> qs = Book . objects . values_list ( "<STR_LIT:name>" ) . annotate ( <EOL> n_authors = Count ( "<STR_LIT>" ) <EOL> ) . filter ( <EOL> pages__gt = F ( "<STR_LIT>" ) <EOL> ) . values_list ( "<STR_LIT:name>" , flat = True ) <EOL> self . assertEqual ( <EOL> list ( qs ) , list ( Book . objects . values_list ( "<STR_LIT:name>" , flat = True ) ) <EOL> ) <EOL> def test_annotation_disjunction ( self ) : <EOL> qs = Book . objects . annotate ( n_authors = Count ( "<STR_LIT>" ) ) . filter ( <EOL> Q ( n_authors = <NUM_LIT:2> ) | Q ( name = "<STR_LIT>" ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> qs = Book . objects . annotate ( n_authors = Count ( "<STR_LIT>" ) ) . filter ( <EOL> Q ( name = "<STR_LIT>" ) | ( Q ( name = "<STR_LIT>" ) & Q ( n_authors = <NUM_LIT:3> ) ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> qs = Publisher . objects . annotate ( <EOL> rating_sum = Sum ( "<STR_LIT>" ) , <EOL> book_count = Count ( "<STR_LIT>" ) <EOL> ) . filter ( <EOL> Q ( rating_sum__gt = <NUM_LIT> ) | Q ( rating_sum__isnull = True ) <EOL> ) . order_by ( '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> qs = Publisher . objects . annotate ( <EOL> rating_sum = Sum ( "<STR_LIT>" ) , <EOL> book_count = Count ( "<STR_LIT>" ) <EOL> ) . filter ( <EOL> Q ( pk__lt = F ( "<STR_LIT>" ) ) | Q ( rating_sum = None ) <EOL> ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> attrgetter ( "<STR_LIT:name>" ) <EOL> ) <EOL> def test_quoting_aggregate_order_by ( self ) : <EOL> qs = Book . objects . filter ( <EOL> name = "<STR_LIT>" <EOL> ) . annotate ( <EOL> authorCount = Count ( "<STR_LIT>" ) <EOL> ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ <EOL> ( "<STR_LIT>" , <NUM_LIT:3> ) , <EOL> ] , <EOL> lambda b : ( b . name , b . authorCount ) <EOL> ) <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> def test_stddev ( self ) : <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( StdDev ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:1> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( StdDev ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:1> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( StdDev ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:2> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( StdDev ( '<STR_LIT>' , sample = True ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:2> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( StdDev ( '<STR_LIT>' , sample = True ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:2> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( StdDev ( '<STR_LIT>' , sample = True ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:1> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( Variance ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:1> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( Variance ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:1> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( Variance ( '<STR_LIT>' ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:1> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( Variance ( '<STR_LIT>' , sample = True ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:1> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( Variance ( '<STR_LIT>' , sample = True ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:2> ) } <EOL> ) <EOL> self . assertEqual ( <EOL> Book . objects . aggregate ( Variance ( '<STR_LIT>' , sample = True ) ) , <EOL> { '<STR_LIT>' : Approximate ( <NUM_LIT> , <NUM_LIT:2> ) } <EOL> ) <EOL> def test_filtering_by_annotation_name ( self ) : <EOL> qs = Author . objects . annotate ( book_cnt = Count ( '<STR_LIT>' ) ) . filter ( book_cnt = <NUM_LIT:2> ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , <EOL> [ '<STR_LIT>' ] , <EOL> lambda b : b . name <EOL> ) <EOL> qs = Author . objects . annotate ( book_count = Count ( '<STR_LIT>' ) ) . filter ( book_count = <NUM_LIT:2> ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , <EOL> [ '<STR_LIT>' ] , <EOL> lambda b : b . name <EOL> ) <EOL> qs = Author . objects . annotate ( Count ( '<STR_LIT>' ) ) . filter ( book__count = <NUM_LIT:2> ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , <EOL> [ '<STR_LIT>' ] , <EOL> lambda b : b . name <EOL> ) <EOL> def test_type_conversion ( self ) : <EOL> from django . db . models import CharField <EOL> from django . db import connection <EOL> testData = '<STR_LIT>' <EOL> testField = CharField ( ) <EOL> self . assertEqual ( <EOL> connection . ops . convert_values ( testData , testField ) , <EOL> testData <EOL> ) <EOL> def test_annotate_joins ( self ) : <EOL> """<STR_LIT>""" <EOL> qs = Book . objects . annotate ( n = Count ( '<STR_LIT>' ) ) <EOL> self . assertIs ( qs . query . alias_map [ '<STR_LIT>' ] . join_type , None ) <EOL> self . assertEqual ( len ( qs . exclude ( publisher = - <NUM_LIT:1> ) ) , <NUM_LIT:6> ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" ) <EOL> def test_aggregate_duplicate_columns ( self ) : <EOL> results = Author . objects . annotate ( num_contacts = Count ( '<STR_LIT>' ) ) <EOL> grouping , gb_params = results . query . get_compiler ( using = '<STR_LIT:default>' ) . get_grouping ( [ ] , [ ] ) <EOL> self . assertEqual ( len ( grouping ) , <NUM_LIT:1> ) <EOL> assert '<STR_LIT:id>' in grouping [ <NUM_LIT:0> ] <EOL> assert '<STR_LIT:name>' not in grouping [ <NUM_LIT:0> ] <EOL> assert '<STR_LIT>' not in grouping [ <NUM_LIT:0> ] <EOL> self . assertEqual ( results . query . group_by , [ ( '<STR_LIT>' , '<STR_LIT:id>' ) ] ) <EOL> self . assertEqual ( <EOL> [ ( a . name , a . num_contacts ) for a in results . order_by ( '<STR_LIT:name>' ) ] , <EOL> [ <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ] <EOL> ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" ) <EOL> def test_aggregate_duplicate_columns_only ( self ) : <EOL> results = Author . objects . only ( '<STR_LIT:id>' , '<STR_LIT:name>' ) . annotate ( num_contacts = Count ( '<STR_LIT>' ) ) <EOL> grouping , gb_params = results . query . get_compiler ( using = '<STR_LIT:default>' ) . get_grouping ( [ ] , [ ] ) <EOL> self . assertEqual ( len ( grouping ) , <NUM_LIT:1> ) <EOL> assert '<STR_LIT:id>' in grouping [ <NUM_LIT:0> ] <EOL> assert '<STR_LIT:name>' not in grouping [ <NUM_LIT:0> ] <EOL> assert '<STR_LIT>' not in grouping [ <NUM_LIT:0> ] <EOL> self . assertEqual ( results . query . group_by , [ ( '<STR_LIT>' , '<STR_LIT:id>' ) ] ) <EOL> self . assertEqual ( <EOL> [ ( a . name , a . num_contacts ) for a in results . order_by ( '<STR_LIT:name>' ) ] , <EOL> [ <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ] <EOL> ) <EOL> @ skipUnlessDBFeature ( "<STR_LIT>" ) <EOL> def test_aggregate_duplicate_columns_select_related ( self ) : <EOL> results = Book . objects . select_related ( '<STR_LIT>' ) . annotate ( <EOL> num_authors = Count ( '<STR_LIT>' ) ) <EOL> grouping , gb_params = results . query . get_compiler ( using = '<STR_LIT:default>' ) . get_grouping ( [ ] , [ ] ) <EOL> self . assertEqual ( len ( grouping ) , <NUM_LIT:1> ) <EOL> assert '<STR_LIT:id>' in grouping [ <NUM_LIT:0> ] <EOL> assert '<STR_LIT:name>' not in grouping [ <NUM_LIT:0> ] <EOL> assert '<STR_LIT>' not in grouping [ <NUM_LIT:0> ] <EOL> self . assertEqual ( results . query . group_by , [ ( '<STR_LIT>' , '<STR_LIT:id>' ) ] ) <EOL> self . assertEqual ( <EOL> [ ( b . name , b . num_authors ) for b in results . order_by ( '<STR_LIT:name>' ) ] , <EOL> [ <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:3> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ) <EOL> ] <EOL> ) <EOL> def test_reverse_join_trimming ( self ) : <EOL> qs = Author . objects . annotate ( Count ( '<STR_LIT>' ) ) <EOL> self . assertIn ( '<STR_LIT>' , str ( qs . query ) ) <EOL> def test_aggregation_with_generic_reverse_relation ( self ) : <EOL> """<STR_LIT>""" <EOL> b = Book . objects . get ( name = '<STR_LIT>' ) <EOL> ItemTag . objects . create ( object_id = b . id , tag = '<STR_LIT>' , <EOL> content_type = ContentType . objects . get_for_model ( b ) ) <EOL> ItemTag . objects . create ( object_id = b . id , tag = '<STR_LIT>' , <EOL> content_type = ContentType . objects . get_for_model ( b ) ) <EOL> wmpk = WithManualPK . objects . create ( id = b . pk ) <EOL> ItemTag . objects . create ( object_id = wmpk . id , tag = '<STR_LIT>' , <EOL> content_type = ContentType . objects . get_for_model ( wmpk ) ) <EOL> b = Book . objects . get ( name__startswith = '<STR_LIT>' ) <EOL> ItemTag . objects . create ( object_id = b . id , tag = '<STR_LIT>' , <EOL> content_type = ContentType . objects . get_for_model ( b ) ) <EOL> self . assertEqual ( Book . objects . aggregate ( Count ( '<STR_LIT>' ) ) , { '<STR_LIT>' : <NUM_LIT:3> } ) <EOL> results = Book . objects . annotate ( Count ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' , '<STR_LIT:name>' ) <EOL> self . assertEqual ( <EOL> [ ( b . name , b . tags__count ) for b in results ] , <EOL> [ <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> ] <EOL> ) <EOL> def test_negated_aggregation ( self ) : <EOL> expected_results = Author . objects . exclude ( <EOL> pk__in = Author . objects . annotate ( book_cnt = Count ( '<STR_LIT>' ) ) . filter ( book_cnt = <NUM_LIT:2> ) <EOL> ) . order_by ( '<STR_LIT:name>' ) <EOL> expected_results = [ a . name for a in expected_results ] <EOL> qs = Author . objects . annotate ( book_cnt = Count ( '<STR_LIT>' ) ) . exclude ( <EOL> Q ( book_cnt = <NUM_LIT:2> ) , Q ( book_cnt = <NUM_LIT:2> ) ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , <EOL> expected_results , <EOL> lambda b : b . name <EOL> ) <EOL> expected_results = Author . objects . exclude ( <EOL> pk__in = Author . objects . annotate ( book_cnt = Count ( '<STR_LIT>' ) ) . filter ( book_cnt = <NUM_LIT:2> ) <EOL> ) . order_by ( '<STR_LIT:name>' ) <EOL> expected_results = [ a . name for a in expected_results ] <EOL> qs = Author . objects . annotate ( book_cnt = Count ( '<STR_LIT>' ) ) . exclude ( Q ( book_cnt = <NUM_LIT:2> ) | Q ( book_cnt = <NUM_LIT:2> ) ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , <EOL> expected_results , <EOL> lambda b : b . name <EOL> ) <EOL> def test_name_filters ( self ) : <EOL> qs = Author . objects . annotate ( Count ( '<STR_LIT>' ) ) . filter ( <EOL> Q ( book__count__exact = <NUM_LIT:2> ) | Q ( name = '<STR_LIT>' ) <EOL> ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> lambda b : b . name <EOL> ) <EOL> def test_name_expressions ( self ) : <EOL> qs = Author . objects . annotate ( Count ( '<STR_LIT>' ) ) . filter ( <EOL> Q ( name = '<STR_LIT>' ) | Q ( age = F ( '<STR_LIT>' ) + <NUM_LIT> ) <EOL> ) . order_by ( '<STR_LIT:name>' ) <EOL> self . assertQuerysetEqual ( <EOL> qs , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> lambda b : b . name <EOL> ) <EOL> def test_ticket_11293 ( self ) : <EOL> q1 = Q ( price__gt = <NUM_LIT:50> ) <EOL> q2 = Q ( authors__count__gt = <NUM_LIT:1> ) <EOL> query = Book . objects . annotate ( Count ( '<STR_LIT>' ) ) . filter ( <EOL> q1 | q2 ) . order_by ( '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> query , [ <NUM_LIT:1> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ] , <EOL> lambda b : b . pk ) <EOL> def test_ticket_11293_q_immutable ( self ) : <EOL> """<STR_LIT>""" <EOL> q1 = Q ( isbn = '<STR_LIT>' ) <EOL> q2 = Q ( authors__count__gt = <NUM_LIT:1> ) <EOL> query = Book . objects . annotate ( Count ( '<STR_LIT>' ) ) <EOL> query . filter ( q1 | q2 ) <EOL> self . assertEqual ( len ( q2 . children ) , <NUM_LIT:1> ) <EOL> def test_fobj_group_by ( self ) : <EOL> """<STR_LIT>""" <EOL> qs = Book . objects . annotate ( <EOL> acount = Count ( '<STR_LIT>' ) <EOL> ) . filter ( <EOL> acount = F ( '<STR_LIT>' ) <EOL> ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ '<STR_LIT>' ] , <EOL> lambda b : b . name ) <EOL> def test_ticket_21150 ( self ) : <EOL> b = Bravo . objects . create ( ) <EOL> c = Charlie . objects . create ( bravo = b ) <EOL> qs = Charlie . objects . select_related ( '<STR_LIT>' ) . annotate ( Count ( '<STR_LIT>' ) ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ c ] , lambda x : x ) <EOL> self . assertIs ( qs [ <NUM_LIT:0> ] . alfa , None ) <EOL> a = Alfa . objects . create ( ) <EOL> c . alfa = a <EOL> c . save ( ) <EOL> qs = qs . all ( ) <EOL> self . assertQuerysetEqual ( <EOL> qs , [ c ] , lambda x : x ) <EOL> self . assertEqual ( qs [ <NUM_LIT:0> ] . alfa , a ) </s>
