<s> from django . contrib . postgres . aggregates import ( <EOL> ArrayAgg , BitAnd , BitOr , BoolAnd , BoolOr , Corr , CovarPop , RegrAvgX , <EOL> RegrAvgY , RegrCount , RegrIntercept , RegrR2 , RegrSlope , RegrSXX , RegrSXY , <EOL> RegrSYY , StatAggregate , StringAgg , <EOL> ) <EOL> from django . db . models . expressions import F , Value <EOL> from django . test . utils import Approximate <EOL> from . import PostgreSQLTestCase <EOL> from . models import AggregateTestModel , StatTestModel <EOL> class TestGeneralAggregate ( PostgreSQLTestCase ) : <EOL> @ classmethod <EOL> def setUpTestData ( cls ) : <EOL> AggregateTestModel . objects . create ( boolean_field = True , char_field = '<STR_LIT>' , integer_field = <NUM_LIT:0> ) <EOL> AggregateTestModel . objects . create ( boolean_field = False , char_field = '<STR_LIT>' , integer_field = <NUM_LIT:1> ) <EOL> AggregateTestModel . objects . create ( boolean_field = False , char_field = '<STR_LIT>' , integer_field = <NUM_LIT:2> ) <EOL> AggregateTestModel . objects . create ( boolean_field = True , char_field = '<STR_LIT>' , integer_field = <NUM_LIT:0> ) <EOL> def test_array_agg_charfield ( self ) : <EOL> values = AggregateTestModel . objects . aggregate ( arrayagg = ArrayAgg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] } ) <EOL> def test_array_agg_integerfield ( self ) : <EOL> values = AggregateTestModel . objects . aggregate ( arrayagg = ArrayAgg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ] } ) <EOL> def test_array_agg_booleanfield ( self ) : <EOL> values = AggregateTestModel . objects . aggregate ( arrayagg = ArrayAgg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : [ True , False , False , True ] } ) <EOL> def test_array_agg_empty_result ( self ) : <EOL> AggregateTestModel . objects . all ( ) . delete ( ) <EOL> values = AggregateTestModel . objects . aggregate ( arrayagg = ArrayAgg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : [ ] } ) <EOL> values = AggregateTestModel . objects . aggregate ( arrayagg = ArrayAgg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : [ ] } ) <EOL> values = AggregateTestModel . objects . aggregate ( arrayagg = ArrayAgg ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : [ ] } ) <EOL> def test_bit_and_general ( self ) : <EOL> values = AggregateTestModel . objects . filter ( <EOL> integer_field__in = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) . aggregate ( bitand = BitAnd ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> def test_bit_and_on_only_true_values ( self ) : <EOL> values = AggregateTestModel . objects . filter ( <EOL> integer_field = <NUM_LIT:1> ) . aggregate ( bitand = BitAnd ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> def test_bit_and_on_only_false_values ( self ) : <EOL> values = AggregateTestModel . objects . filter ( <EOL> integer_field = <NUM_LIT:0> ) . aggregate ( bitand = BitAnd ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> def test_bit_and_empty_result ( self ) : <EOL> AggregateTestModel . objects . all ( ) . delete ( ) <EOL> values = AggregateTestModel . objects . aggregate ( bitand = BitAnd ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_bit_or_general ( self ) : <EOL> values = AggregateTestModel . objects . filter ( <EOL> integer_field__in = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) . aggregate ( bitor = BitOr ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> def test_bit_or_on_only_true_values ( self ) : <EOL> values = AggregateTestModel . objects . filter ( <EOL> integer_field = <NUM_LIT:1> ) . aggregate ( bitor = BitOr ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> def test_bit_or_on_only_false_values ( self ) : <EOL> values = AggregateTestModel . objects . filter ( <EOL> integer_field = <NUM_LIT:0> ) . aggregate ( bitor = BitOr ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> def test_bit_or_empty_result ( self ) : <EOL> AggregateTestModel . objects . all ( ) . delete ( ) <EOL> values = AggregateTestModel . objects . aggregate ( bitor = BitOr ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_bool_and_general ( self ) : <EOL> values = AggregateTestModel . objects . aggregate ( booland = BoolAnd ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : False } ) <EOL> def test_bool_and_empty_result ( self ) : <EOL> AggregateTestModel . objects . all ( ) . delete ( ) <EOL> values = AggregateTestModel . objects . aggregate ( booland = BoolAnd ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_bool_or_general ( self ) : <EOL> values = AggregateTestModel . objects . aggregate ( boolor = BoolOr ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : True } ) <EOL> def test_bool_or_empty_result ( self ) : <EOL> AggregateTestModel . objects . all ( ) . delete ( ) <EOL> values = AggregateTestModel . objects . aggregate ( boolor = BoolOr ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_string_agg_requires_delimiter ( self ) : <EOL> with self . assertRaises ( TypeError ) : <EOL> AggregateTestModel . objects . aggregate ( stringagg = StringAgg ( '<STR_LIT>' ) ) <EOL> def test_string_agg_charfield ( self ) : <EOL> values = AggregateTestModel . objects . aggregate ( stringagg = StringAgg ( '<STR_LIT>' , delimiter = '<STR_LIT:;>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> def test_string_agg_empty_result ( self ) : <EOL> AggregateTestModel . objects . all ( ) . delete ( ) <EOL> values = AggregateTestModel . objects . aggregate ( stringagg = StringAgg ( '<STR_LIT>' , delimiter = '<STR_LIT:;>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> class TestStatisticsAggregate ( PostgreSQLTestCase ) : <EOL> @ classmethod <EOL> def setUpTestData ( cls ) : <EOL> StatTestModel . objects . create ( <EOL> int1 = <NUM_LIT:1> , <EOL> int2 = <NUM_LIT:3> , <EOL> related_field = AggregateTestModel . objects . create ( integer_field = <NUM_LIT:0> ) , <EOL> ) <EOL> StatTestModel . objects . create ( <EOL> int1 = <NUM_LIT:2> , <EOL> int2 = <NUM_LIT:2> , <EOL> related_field = AggregateTestModel . objects . create ( integer_field = <NUM_LIT:1> ) , <EOL> ) <EOL> StatTestModel . objects . create ( <EOL> int1 = <NUM_LIT:3> , <EOL> int2 = <NUM_LIT:1> , <EOL> related_field = AggregateTestModel . objects . create ( integer_field = <NUM_LIT:2> ) , <EOL> ) <EOL> def test_missing_arguments_raises_exception ( self ) : <EOL> with self . assertRaisesMessage ( ValueError , '<STR_LIT>' ) : <EOL> StatAggregate ( x = None , y = None ) <EOL> def test_correct_source_expressions ( self ) : <EOL> func = StatAggregate ( x = '<STR_LIT:test>' , y = <NUM_LIT> ) <EOL> self . assertIsInstance ( func . source_expressions [ <NUM_LIT:0> ] , Value ) <EOL> self . assertIsInstance ( func . source_expressions [ <NUM_LIT:1> ] , F ) <EOL> def test_alias_is_required ( self ) : <EOL> class SomeFunc ( StatAggregate ) : <EOL> function = '<STR_LIT>' <EOL> with self . assertRaisesMessage ( TypeError , '<STR_LIT>' ) : <EOL> StatTestModel . objects . aggregate ( SomeFunc ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> def test_corr_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( corr = Corr ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : - <NUM_LIT:1.0> } ) <EOL> def test_corr_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( corr = Corr ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_covar_pop_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( covarpop = CovarPop ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : Approximate ( - <NUM_LIT> , places = <NUM_LIT:1> ) } ) <EOL> def test_covar_pop_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( covarpop = CovarPop ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_covar_pop_sample ( self ) : <EOL> values = StatTestModel . objects . aggregate ( covarpop = CovarPop ( y = '<STR_LIT>' , x = '<STR_LIT>' , sample = True ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : - <NUM_LIT:1.0> } ) <EOL> def test_covar_pop_sample_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( covarpop = CovarPop ( y = '<STR_LIT>' , x = '<STR_LIT>' , sample = True ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_regr_avgx_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( regravgx = RegrAvgX ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> def test_regr_avgx_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( regravgx = RegrAvgX ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_regr_avgy_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( regravgy = RegrAvgY ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> def test_regr_avgy_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( regravgy = RegrAvgY ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_regr_count_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( regrcount = RegrCount ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:3> } ) <EOL> def test_regr_count_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( regrcount = RegrCount ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> def test_regr_intercept_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( regrintercept = RegrIntercept ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:4> } ) <EOL> def test_regr_intercept_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( regrintercept = RegrIntercept ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_regr_r2_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( regrr2 = RegrR2 ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:1> } ) <EOL> def test_regr_r2_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( regrr2 = RegrR2 ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_regr_slope_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( regrslope = RegrSlope ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : - <NUM_LIT:1> } ) <EOL> def test_regr_slope_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( regrslope = RegrSlope ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_regr_sxx_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( regrsxx = RegrSXX ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> def test_regr_sxx_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( regrsxx = RegrSXX ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_regr_sxy_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( regrsxy = RegrSXY ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : - <NUM_LIT> } ) <EOL> def test_regr_sxy_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( regrsxy = RegrSXY ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_regr_syy_general ( self ) : <EOL> values = StatTestModel . objects . aggregate ( regrsyy = RegrSYY ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT> } ) <EOL> def test_regr_syy_empty_result ( self ) : <EOL> StatTestModel . objects . all ( ) . delete ( ) <EOL> values = StatTestModel . objects . aggregate ( regrsyy = RegrSYY ( y = '<STR_LIT>' , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : None } ) <EOL> def test_regr_avgx_with_related_obj_and_number_as_argument ( self ) : <EOL> """<STR_LIT>""" <EOL> values = StatTestModel . objects . aggregate ( complex_regravgx = RegrAvgX ( y = <NUM_LIT:5> , x = '<STR_LIT>' ) ) <EOL> self . assertEqual ( values , { '<STR_LIT>' : <NUM_LIT:1.0> } ) </s>
