@@ -37,6 +37,7 @@ import (
37
37
"google.golang.org/grpc"
38
38
"google.golang.org/grpc/codes"
39
39
"google.golang.org/grpc/status"
40
+ "google.golang.org/protobuf/types/known/structpb"
40
41
)
41
42
42
43
// TODO(djd): Make test entity clean up more robust: some test entities may
@@ -52,6 +53,7 @@ var suffix string
52
53
const (
53
54
replayFilename = "datastore.replay"
54
55
envDatabases = "GCLOUD_TESTS_GOLANG_DATASTORE_DATABASES"
56
+ keyPrefix = "TestIntegration_"
55
57
)
56
58
57
59
type replayInfo struct {
@@ -471,6 +473,8 @@ func TestIntegration_NilKey(t *testing.T) {
471
473
type SQChild struct {
472
474
I , J int
473
475
T , U int64
476
+ V float64
477
+ W string
474
478
}
475
479
476
480
type SQTestCase struct {
@@ -701,17 +705,17 @@ func TestIntegration_AggregationQueries(t *testing.T) {
701
705
client := newTestClient (ctx , t )
702
706
defer client .Close ()
703
707
704
- parent := NameKey ("SQParent" , "TestIntegration_Filters "+ suffix , nil )
708
+ parent := NameKey ("SQParent" , keyPrefix + "AggregationQueries "+ suffix , nil )
705
709
now := timeNow .Truncate (time .Millisecond ).Unix ()
706
710
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" },
715
719
}
716
720
717
721
keys := make ([]* Key , len (children ))
@@ -729,7 +733,6 @@ func TestIntegration_AggregationQueries(t *testing.T) {
729
733
}
730
734
}()
731
735
732
- baseQuery := NewQuery ("SQChild" ).Ancestor (parent )
733
736
testCases := []struct {
734
737
desc string
735
738
aggQuery * AggregationQuery
@@ -738,21 +741,91 @@ func TestIntegration_AggregationQueries(t *testing.T) {
738
741
wantAggResult AggregationResult
739
742
}{
740
743
{
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" ,
746
750
},
747
751
{
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" ) ,
752
756
wantAggResult : map [string ]interface {}{
753
757
"count" : & pb.Value {ValueType : & pb.Value_IntegerValue {IntegerValue : 5 }},
754
758
},
755
759
},
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
+ },
756
829
}
757
830
758
831
for _ , testCase := range testCases {
0 commit comments