<s> from __future__ import absolute_import <EOL> import datetime <EOL> from decimal import Decimal <EOL> from django . db . models import Avg , Sum , Count , Max , Min <EOL> from django . test import TestCase , Approximate <EOL> from . models import Author , Publisher , Book , Store <EOL> class BaseAggregateTestCase ( TestCase ) : <EOL> fixtures = [ "<STR_LIT>" ] <EOL> def test_empty_aggregate ( self ) : <EOL> self . assertEqual ( Author . objects . all ( ) . aggregate ( ) , { } ) <EOL> def test_single_aggregate ( self ) : <EOL> vals = Author . objects . aggregate ( Avg ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( vals , { "<STR_LIT>" : Approximate ( <NUM_LIT> , places = <NUM_LIT:1> ) } ) <EOL> def test_multiple_aggregates ( self ) : <EOL> vals = Author . objects . aggregate ( Sum ( "<STR_LIT>" ) , Avg ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( vals , { "<STR_LIT>" : <NUM_LIT> , "<STR_LIT>" : Approximate ( <NUM_LIT> , places = <NUM_LIT:1> ) } ) <EOL> def test_filter_aggregate ( self ) : <EOL> vals = Author . objects . filter ( age__gt = <NUM_LIT> ) . aggregate ( Sum ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( vals [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> def test_related_aggregate ( self ) : <EOL> vals = Author . objects . aggregate ( Avg ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( vals [ "<STR_LIT>" ] , <NUM_LIT> , places = <NUM_LIT:2> ) <EOL> vals = Book . objects . filter ( rating__lt = <NUM_LIT> ) . aggregate ( Avg ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( vals [ "<STR_LIT>" ] , <NUM_LIT> , places = <NUM_LIT:2> ) <EOL> vals = Author . objects . all ( ) . filter ( name__contains = "<STR_LIT:a>" ) . aggregate ( Avg ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( vals [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> vals = Book . objects . aggregate ( Sum ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( vals [ "<STR_LIT>" ] , <NUM_LIT:30> ) <EOL> vals = Publisher . objects . aggregate ( Sum ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( vals [ "<STR_LIT>" ] , Decimal ( "<STR_LIT>" ) ) <EOL> def test_aggregate_multi_join ( self ) : <EOL> vals = Store . objects . aggregate ( Max ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( vals [ "<STR_LIT>" ] , <NUM_LIT> ) <EOL> vals = Author . objects . aggregate ( Min ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( vals [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> def test_aggregate_alias ( self ) : <EOL> vals = Store . objects . filter ( name = "<STR_LIT>" ) . aggregate ( amazon_mean = Avg ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( len ( vals ) , <NUM_LIT:1> ) <EOL> self . assertAlmostEqual ( vals [ "<STR_LIT>" ] , <NUM_LIT> , places = <NUM_LIT:2> ) <EOL> def test_annotate_basic ( self ) : <EOL> self . assertQuerysetEqual ( <EOL> Book . objects . annotate ( ) . order_by ( '<STR_LIT>' ) , [ <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> books = Book . objects . annotate ( mean_age = Avg ( "<STR_LIT>" ) ) <EOL> b = books . get ( pk = <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> b . name , <EOL> '<STR_LIT>' <EOL> ) <EOL> self . assertEqual ( b . mean_age , <NUM_LIT> ) <EOL> def test_annotate_m2m ( self ) : <EOL> books = Book . objects . filter ( rating__lt = <NUM_LIT> ) . annotate ( Avg ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT:name>" ) <EOL> self . assertQuerysetEqual ( <EOL> books , [ <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , Approximate ( <NUM_LIT> , places = <NUM_LIT:1> ) ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) <EOL> ] , <EOL> lambda b : ( b . name , b . authors__age__avg ) , <EOL> ) <EOL> books = Book . objects . annotate ( num_authors = Count ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT:name>" ) <EOL> self . assertQuerysetEqual ( <EOL> books , [ <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> lambda b : ( b . name , b . num_authors ) <EOL> ) <EOL> def test_backwards_m2m_annotate ( self ) : <EOL> authors = Author . objects . filter ( name__contains = "<STR_LIT:a>" ) . annotate ( Avg ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT:name>" ) <EOL> self . assertQuerysetEqual ( <EOL> authors , [ <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) <EOL> ] , <EOL> lambda a : ( a . name , a . book__rating__avg ) <EOL> ) <EOL> authors = Author . objects . annotate ( num_books = Count ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT:name>" ) <EOL> self . assertQuerysetEqual ( <EOL> authors , [ <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:2> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> ] , <EOL> lambda a : ( a . name , a . num_books ) <EOL> ) <EOL> def test_reverse_fkey_annotate ( self ) : <EOL> books = Book . objects . annotate ( Sum ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT:name>" ) <EOL> self . assertQuerysetEqual ( <EOL> books , [ <EOL> ( '<STR_LIT>' , <NUM_LIT:7> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:9> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:3> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:7> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:3> ) <EOL> ] , <EOL> lambda b : ( b . name , b . publisher__num_awards__sum ) <EOL> ) <EOL> publishers = Publisher . objects . annotate ( Sum ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT:name>" ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> ( '<STR_LIT>' , Decimal ( "<STR_LIT>" ) ) , <EOL> ( "<STR_LIT>" , None ) , <EOL> ( '<STR_LIT>' , Decimal ( "<STR_LIT>" ) ) , <EOL> ( '<STR_LIT>' , Decimal ( "<STR_LIT>" ) ) , <EOL> ( '<STR_LIT>' , Decimal ( "<STR_LIT>" ) ) <EOL> ] , <EOL> lambda p : ( p . name , p . book__price__sum ) <EOL> ) <EOL> def test_annotate_values ( self ) : <EOL> books = list ( Book . objects . filter ( pk = <NUM_LIT:1> ) . annotate ( mean_age = Avg ( "<STR_LIT>" ) ) . values ( ) ) <EOL> self . assertEqual ( <EOL> books , [ <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT:id>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : Approximate ( Decimal ( "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : datetime . date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:6> ) , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } <EOL> ] <EOL> ) <EOL> books = Book . objects . filter ( pk = <NUM_LIT:1> ) . annotate ( mean_age = Avg ( '<STR_LIT>' ) ) . values ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } <EOL> ] <EOL> ) <EOL> books = Book . objects . filter ( pk = <NUM_LIT:1> ) . annotate ( mean_age = Avg ( "<STR_LIT>" ) ) . values ( "<STR_LIT:name>" ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> { <EOL> "<STR_LIT:name>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> ) <EOL> books = Book . objects . filter ( pk = <NUM_LIT:1> ) . values ( ) . annotate ( mean_age = Avg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT:id>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : Approximate ( Decimal ( "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" : datetime . date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:6> ) , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } <EOL> ] <EOL> ) <EOL> books = Book . objects . values ( "<STR_LIT>" ) . annotate ( n_authors = Count ( "<STR_LIT>" ) , mean_age = Avg ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:6> , <EOL> "<STR_LIT>" : Approximate ( <NUM_LIT> , places = <NUM_LIT:1> ) <EOL> } , <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:2> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } <EOL> ] <EOL> ) <EOL> authors = Author . objects . annotate ( Avg ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT:name>" ) <EOL> self . assertEqual ( len ( authors ) , <NUM_LIT:9> ) <EOL> self . assertQuerysetEqual ( <EOL> authors , [ <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , None ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , Approximate ( <NUM_LIT> , places = <NUM_LIT:1> ) ) <EOL> ] , <EOL> lambda a : ( a . name , a . friends__age__avg ) <EOL> ) <EOL> def test_count ( self ) : <EOL> vals = Book . objects . aggregate ( Count ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( vals , { "<STR_LIT>" : <NUM_LIT:6> } ) <EOL> vals = Book . objects . aggregate ( Count ( "<STR_LIT>" , distinct = True ) ) <EOL> self . assertEqual ( vals , { "<STR_LIT>" : <NUM_LIT:4> } ) <EOL> def test_fkey_aggregate ( self ) : <EOL> explicit = list ( Author . objects . annotate ( Count ( '<STR_LIT>' ) ) ) <EOL> implicit = list ( Author . objects . annotate ( Count ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( explicit , implicit ) <EOL> def test_annotate_ordering ( self ) : <EOL> books = Book . objects . values ( '<STR_LIT>' ) . annotate ( oldest = Max ( '<STR_LIT>' ) ) . order_by ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> <EOL> } , <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } <EOL> ] <EOL> ) <EOL> books = Book . objects . values ( "<STR_LIT>" ) . annotate ( oldest = Max ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } , <EOL> { <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> } <EOL> ] <EOL> ) <EOL> def test_aggregate_annotation ( self ) : <EOL> vals = Book . objects . annotate ( num_authors = Count ( "<STR_LIT>" ) ) . aggregate ( Avg ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( vals , { "<STR_LIT>" : Approximate ( <NUM_LIT> , places = <NUM_LIT:1> ) } ) <EOL> def test_filtering ( self ) : <EOL> p = Publisher . objects . create ( name = '<STR_LIT>' , num_awards = <NUM_LIT:0> ) <EOL> Book . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> pages = <NUM_LIT:1> , <EOL> isbn = '<STR_LIT>' , <EOL> rating = <NUM_LIT> , <EOL> price = Decimal ( "<STR_LIT>" ) , <EOL> publisher = p , <EOL> contact_id = <NUM_LIT:1> , <EOL> pubdate = datetime . date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:1> ) <EOL> ) <EOL> Book . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> pages = <NUM_LIT:1> , <EOL> isbn = '<STR_LIT>' , <EOL> rating = <NUM_LIT> , <EOL> price = Decimal ( "<STR_LIT>" ) , <EOL> publisher = p , <EOL> contact_id = <NUM_LIT:1> , <EOL> pubdate = datetime . date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:2> ) <EOL> ) <EOL> Book . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> pages = <NUM_LIT:1> , <EOL> isbn = '<STR_LIT>' , <EOL> rating = <NUM_LIT> , <EOL> price = Decimal ( "<STR_LIT>" ) , <EOL> publisher = p , <EOL> contact_id = <NUM_LIT:1> , <EOL> pubdate = datetime . date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:3> ) <EOL> ) <EOL> publishers = Publisher . objects . annotate ( num_books = Count ( "<STR_LIT>" ) ) . filter ( num_books__gt = <NUM_LIT:1> ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda p : p . name , <EOL> ) <EOL> publishers = Publisher . objects . filter ( book__price__lt = Decimal ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda p : p . name <EOL> ) <EOL> publishers = Publisher . objects . annotate ( num_books = Count ( "<STR_LIT>" ) ) . filter ( num_books__gt = <NUM_LIT:1> , book__price__lt = Decimal ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda p : p . name , <EOL> ) <EOL> publishers = Publisher . objects . filter ( book__price__lt = Decimal ( "<STR_LIT>" ) ) . annotate ( num_books = Count ( "<STR_LIT>" ) ) . filter ( num_books__gt = <NUM_LIT:1> ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda p : p . name <EOL> ) <EOL> publishers = Publisher . objects . annotate ( num_books = Count ( "<STR_LIT>" ) ) . filter ( num_books__range = [ <NUM_LIT:1> , <NUM_LIT:3> ] ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda p : p . name <EOL> ) <EOL> publishers = Publisher . objects . annotate ( num_books = Count ( "<STR_LIT>" ) ) . filter ( num_books__range = [ <NUM_LIT:1> , <NUM_LIT:2> ] ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda p : p . name <EOL> ) <EOL> publishers = Publisher . objects . annotate ( num_books = Count ( "<STR_LIT>" ) ) . filter ( num_books__in = [ <NUM_LIT:1> , <NUM_LIT:3> ] ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda p : p . name , <EOL> ) <EOL> publishers = Publisher . objects . annotate ( num_books = Count ( "<STR_LIT>" ) ) . filter ( num_books__isnull = True ) <EOL> self . assertEqual ( len ( publishers ) , <NUM_LIT:0> ) <EOL> def test_annotation ( self ) : <EOL> vals = Author . objects . filter ( pk = <NUM_LIT:1> ) . aggregate ( Count ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( vals , { "<STR_LIT>" : <NUM_LIT:2> } ) <EOL> books = Book . objects . annotate ( num_authors = Count ( "<STR_LIT>" ) ) . filter ( num_authors__ge = <NUM_LIT:2> ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> books , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda b : b . name <EOL> ) <EOL> authors = Author . objects . annotate ( num_friends = Count ( "<STR_LIT>" , distinct = True ) ) . filter ( num_friends = <NUM_LIT:0> ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> authors , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda a : a . name <EOL> ) <EOL> publishers = Publisher . objects . annotate ( num_books = Count ( "<STR_LIT>" ) ) . filter ( num_books__gt = <NUM_LIT:1> ) . order_by ( "<STR_LIT>" ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda p : p . name <EOL> ) <EOL> publishers = Publisher . objects . filter ( book__price__lt = Decimal ( "<STR_LIT>" ) ) . annotate ( num_books = Count ( "<STR_LIT>" ) ) . filter ( num_books__gt = <NUM_LIT:1> ) <EOL> self . assertQuerysetEqual ( <EOL> publishers , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda p : p . name <EOL> ) <EOL> books = Book . objects . annotate ( num_authors = Count ( "<STR_LIT>" ) ) . filter ( authors__name__contains = "<STR_LIT>" , num_authors__gt = <NUM_LIT:1> ) <EOL> self . assertQuerysetEqual ( <EOL> books , [ <EOL> "<STR_LIT>" , <EOL> ] , <EOL> lambda b : b . name <EOL> ) <EOL> def test_more_aggregation ( self ) : <EOL> a = Author . objects . get ( name__contains = '<STR_LIT>' ) <EOL> b = Book . objects . get ( name__contains = '<STR_LIT>' ) <EOL> b . authors . add ( a ) <EOL> b . save ( ) <EOL> vals = Book . objects . annotate ( num_authors = Count ( "<STR_LIT>" ) ) . filter ( authors__name__contains = "<STR_LIT>" , num_authors__gt = <NUM_LIT:1> ) . aggregate ( Avg ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( vals , { "<STR_LIT>" : <NUM_LIT> } ) <EOL> def test_even_more_aggregate ( self ) : <EOL> publishers = Publisher . objects . annotate ( earliest_book = Min ( "<STR_LIT>" ) ) . exclude ( earliest_book = None ) . order_by ( "<STR_LIT>" ) . values ( ) <EOL> self . assertEqual ( <EOL> list ( publishers ) , [ <EOL> { <EOL> '<STR_LIT>' : datetime . date ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT:15> ) , <EOL> '<STR_LIT>' : <NUM_LIT:9> , <EOL> '<STR_LIT:id>' : <NUM_LIT:4> , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT>' : datetime . date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:15> ) , <EOL> '<STR_LIT>' : <NUM_LIT:7> , <EOL> '<STR_LIT:id>' : <NUM_LIT:3> , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT>' : datetime . date ( <NUM_LIT> , <NUM_LIT:12> , <NUM_LIT:6> ) , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT:id>' : <NUM_LIT:1> , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } , <EOL> { <EOL> '<STR_LIT>' : datetime . date ( <NUM_LIT> , <NUM_LIT:3> , <NUM_LIT:3> ) , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:id>' : <NUM_LIT:2> , <EOL> '<STR_LIT:name>' : '<STR_LIT>' <EOL> } <EOL> ] <EOL> ) <EOL> vals = Store . objects . aggregate ( Max ( "<STR_LIT>" ) , Min ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( <EOL> vals , <EOL> { <EOL> "<STR_LIT>" : datetime . time ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) , <EOL> "<STR_LIT>" : datetime . datetime ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT> , <NUM_LIT:16> , <NUM_LIT> , <NUM_LIT> ) , <EOL> } <EOL> ) <EOL> def test_annotate_values_list ( self ) : <EOL> books = Book . objects . filter ( pk = <NUM_LIT:1> ) . annotate ( mean_age = Avg ( "<STR_LIT>" ) ) . values_list ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> ( <NUM_LIT:1> , "<STR_LIT>" , <NUM_LIT> ) , <EOL> ] <EOL> ) <EOL> books = Book . objects . filter ( pk = <NUM_LIT:1> ) . annotate ( mean_age = Avg ( "<STR_LIT>" ) ) . values_list ( "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> ( '<STR_LIT>' , ) <EOL> ] <EOL> ) <EOL> books = Book . objects . filter ( pk = <NUM_LIT:1> ) . annotate ( mean_age = Avg ( "<STR_LIT>" ) ) . values_list ( "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> ( <NUM_LIT> , ) <EOL> ] <EOL> ) <EOL> books = Book . objects . filter ( pk = <NUM_LIT:1> ) . annotate ( mean_age = Avg ( "<STR_LIT>" ) ) . values_list ( "<STR_LIT>" , flat = True ) <EOL> self . assertEqual ( list ( books ) , [ <NUM_LIT> ] ) <EOL> books = Book . objects . values_list ( "<STR_LIT>" ) . annotate ( count = Count ( "<STR_LIT>" ) ) . order_by ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . assertEqual ( <EOL> list ( books ) , [ <EOL> ( Decimal ( "<STR_LIT>" ) , <NUM_LIT:2> ) , <EOL> ( Decimal ( '<STR_LIT>' ) , <NUM_LIT:1> ) , <EOL> ( Decimal ( '<STR_LIT>' ) , <NUM_LIT:1> ) , <EOL> ( Decimal ( '<STR_LIT>' ) , <NUM_LIT:1> ) , <EOL> ( Decimal ( '<STR_LIT>' ) , <NUM_LIT:1> ) , <EOL> ] <EOL> ) <EOL> def test_dates_with_aggregation ( self ) : <EOL> """<STR_LIT>""" <EOL> dates = Book . objects . annotate ( num_authors = Count ( "<STR_LIT>" ) ) . dates ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertQuerysetEqual ( <EOL> dates , [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> ) </s>
