@@ -37,6 +37,7 @@ import (
3737 "google.golang.org/grpc"
3838 "google.golang.org/grpc/codes"
3939 "google.golang.org/grpc/status"
40+ "google.golang.org/protobuf/types/known/structpb"
4041)
4142
4243// TODO(djd): Make test entity clean up more robust: some test entities may
@@ -52,6 +53,7 @@ var suffix string
5253const (
5354 replayFilename = "datastore.replay"
5455 envDatabases = "GCLOUD_TESTS_GOLANG_DATASTORE_DATABASES"
56+ keyPrefix = "TestIntegration_"
5557)
5658
5759type replayInfo struct {
@@ -471,6 +473,8 @@ func TestIntegration_NilKey(t *testing.T) {
471473type SQChild struct {
472474 I , J int
473475 T , U int64
476+ V float64
477+ W string
474478}
475479
476480type SQTestCase struct {
@@ -701,17 +705,17 @@ func TestIntegration_AggregationQueries(t *testing.T) {
701705 client := newTestClient (ctx , t )
702706 defer client .Close ()
703707
704- parent := NameKey ("SQParent" , "TestIntegration_Filters "+ suffix , nil )
708+ parent := NameKey ("SQParent" , keyPrefix + "AggregationQueries "+ suffix , nil )
705709 now := timeNow .Truncate (time .Millisecond ).Unix ()
706710 children := []* SQChild {
707- {I : 0 , T : now , U : now },
708- {I : 1 , T : now , U : now },
709- {I : 2 , T : now , U : now },
710- {I : 3 , T : now , U : now },
711- {I : 4 , T : now , U : now },
712- {I : 5 , T : now , U : now },
713- {I : 6 , T : now , U : now },
714- {I : 7 , T : now , U : now },
711+ {I : 0 , T : now , U : now , V : 1.5 , W : "str" },
712+ {I : 1 , T : now , U : now , V : 1.5 , W : "str" },
713+ {I : 2 , T : now , U : now , V : 1.5 , W : "str" },
714+ {I : 3 , T : now , U : now , V : 1.5 , W : "str" },
715+ {I : 4 , T : now , U : now , V : 1.5 , W : "str" },
716+ {I : 5 , T : now , U : now , V : 1.5 , W : "str" },
717+ {I : 6 , T : now , U : now , V : 1.5 , W : "str" },
718+ {I : 7 , T : now , U : now , V : 1.5 , W : "str" },
715719 }
716720
717721 keys := make ([]* Key , len (children ))
@@ -729,7 +733,6 @@ func TestIntegration_AggregationQueries(t *testing.T) {
729733 }
730734 }()
731735
732- baseQuery := NewQuery ("SQChild" ).Ancestor (parent )
733736 testCases := []struct {
734737 desc string
735738 aggQuery * AggregationQuery
@@ -738,21 +741,91 @@ func TestIntegration_AggregationQueries(t *testing.T) {
738741 wantAggResult AggregationResult
739742 }{
740743 {
741- desc : "Count Failure - Missing index" ,
742- aggQuery : baseQuery .Filter ("T>=" , now ).NewAggregationQuery ().WithCount ("count" ),
743- wantFailure : true ,
744- wantErrMsg : "no matching index found" ,
745- wantAggResult : nil ,
744+ desc : "Count Failure - Missing index" ,
745+ aggQuery : NewQuery ("SQChild" ).Ancestor (parent ).Filter ("T>=" , now ).
746+ NewAggregationQuery ().
747+ WithCount ("count" ),
748+ wantFailure : true ,
749+ wantErrMsg : "no matching index found" ,
746750 },
747751 {
748- desc : "Count Success" ,
749- aggQuery : baseQuery . Filter ("T=" , now ).Filter ("I>=" , 3 ).NewAggregationQuery (). WithCount ( "count" ),
750- wantFailure : false ,
751- wantErrMsg : "" ,
752+ desc : "Count Success" ,
753+ aggQuery : NewQuery ( "SQChild" ). Ancestor ( parent ). Filter ("T=" , now ).Filter ("I>=" , 3 ).
754+ NewAggregationQuery ().
755+ WithCount ( "count" ) ,
752756 wantAggResult : map [string ]interface {}{
753757 "count" : & pb.Value {ValueType : & pb.Value_IntegerValue {IntegerValue : 5 }},
754758 },
755759 },
760+ {
761+ desc : "Multiple aggregations" ,
762+ aggQuery : NewQuery ("SQChild" ).Ancestor (parent ).Filter ("T=" , now ).
763+ NewAggregationQuery ().
764+ WithSum ("I" , "i_sum" ).
765+ WithAvg ("I" , "avg" ).
766+ WithSum ("V" , "v_sum" ),
767+ wantAggResult : map [string ]interface {}{
768+ "i_sum" : & pb.Value {ValueType : & pb.Value_IntegerValue {IntegerValue : 28 }},
769+ "v_sum" : & pb.Value {ValueType : & pb.Value_DoubleValue {DoubleValue : 12 }},
770+ "avg" : & pb.Value {ValueType : & pb.Value_DoubleValue {DoubleValue : 3.5 }},
771+ },
772+ },
773+ {
774+ desc : "Multiple aggregations with limit " ,
775+ aggQuery : NewQuery ("SQChild" ).Ancestor (parent ).Filter ("T=" , now ).Limit (2 ).
776+ NewAggregationQuery ().
777+ WithSum ("I" , "sum" ).
778+ WithAvg ("I" , "avg" ),
779+ wantAggResult : map [string ]interface {}{
780+ "sum" : & pb.Value {ValueType : & pb.Value_IntegerValue {IntegerValue : 1 }},
781+ "avg" : & pb.Value {ValueType : & pb.Value_DoubleValue {DoubleValue : 0.5 }},
782+ },
783+ },
784+ {
785+ desc : "Multiple aggregations on non-numeric field" ,
786+ aggQuery : NewQuery ("SQChild" ).Ancestor (parent ).Filter ("T=" , now ).Limit (2 ).
787+ NewAggregationQuery ().
788+ WithSum ("W" , "sum" ).
789+ WithAvg ("W" , "avg" ),
790+ wantAggResult : map [string ]interface {}{
791+ "sum" : & pb.Value {ValueType : & pb.Value_IntegerValue {IntegerValue : int64 (0 )}},
792+ "avg" : & pb.Value {ValueType : & pb.Value_NullValue {NullValue : structpb .NullValue_NULL_VALUE }},
793+ },
794+ },
795+ {
796+ desc : "Sum aggregation without alias" ,
797+ aggQuery : NewQuery ("SQChild" ).Ancestor (parent ).Filter ("T=" , now ).
798+ NewAggregationQuery ().
799+ WithSum ("I" , "" ),
800+ wantAggResult : map [string ]interface {}{
801+ "property_1" : & pb.Value {ValueType : & pb.Value_IntegerValue {IntegerValue : 28 }},
802+ },
803+ },
804+ {
805+ desc : "Average aggregation without alias" ,
806+ aggQuery : NewQuery ("SQChild" ).Ancestor (parent ).Filter ("T=" , now ).
807+ NewAggregationQuery ().
808+ WithAvg ("I" , "" ),
809+ wantAggResult : map [string ]interface {}{
810+ "property_1" : & pb.Value {ValueType : & pb.Value_DoubleValue {DoubleValue : 3.5 }},
811+ },
812+ },
813+ {
814+ desc : "Sum aggregation on '__key__'" ,
815+ aggQuery : NewQuery ("SQChild" ).Ancestor (parent ).Filter ("T=" , now ).
816+ NewAggregationQuery ().
817+ WithSum ("__key__" , "" ),
818+ wantFailure : true ,
819+ wantErrMsg : "Aggregations are not supported for the property" ,
820+ },
821+ {
822+ desc : "Average aggregation on '__key__'" ,
823+ aggQuery : NewQuery ("SQChild" ).Ancestor (parent ).Filter ("T=" , now ).
824+ NewAggregationQuery ().
825+ WithAvg ("__key__" , "" ),
826+ wantFailure : true ,
827+ wantErrMsg : "Aggregations are not supported for the property" ,
828+ },
756829 }
757830
758831 for _ , testCase := range testCases {
0 commit comments