@@ -93,6 +93,54 @@ def test_aggregation_query_with_alias(aggregation_query_client, nested_query):
9393 assert r .value > 0
9494
9595
96+ def test_sum_query_default (aggregation_query_client , nested_query ):
97+ query = nested_query
98+
99+ aggregation_query = aggregation_query_client .aggregation_query (query )
100+ aggregation_query .sum ("person" )
101+ result = _do_fetch (aggregation_query )
102+ assert len (result ) == 1
103+ for r in result [0 ]:
104+ assert r .alias == "property_1"
105+ assert r .value == 8
106+
107+
108+ def test_sum_query_with_alias (aggregation_query_client , nested_query ):
109+ query = nested_query
110+
111+ aggregation_query = aggregation_query_client .aggregation_query (query )
112+ aggregation_query .sum ("person" , alias = "sum_person" )
113+ result = _do_fetch (aggregation_query )
114+ assert len (result ) == 1
115+ for r in result [0 ]:
116+ assert r .alias == "sum_person"
117+ assert r .value > 0
118+
119+
120+ def test_avg_query_default (aggregation_query_client , nested_query ):
121+ query = nested_query
122+
123+ aggregation_query = aggregation_query_client .aggregation_query (query )
124+ aggregation_query .avg ("person" )
125+ result = _do_fetch (aggregation_query )
126+ assert len (result ) == 1
127+ for r in result [0 ]:
128+ assert r .alias == "property_1"
129+ assert r .value == 8
130+
131+
132+ def test_avg_query_with_alias (aggregation_query_client , nested_query ):
133+ query = nested_query
134+
135+ aggregation_query = aggregation_query_client .aggregation_query (query )
136+ aggregation_query .avg ("person" , alias = "avg_person" )
137+ result = _do_fetch (aggregation_query )
138+ assert len (result ) == 1
139+ for r in result [0 ]:
140+ assert r .alias == "avg_person"
141+ assert r .value > 0
142+
143+
96144def test_aggregation_query_with_limit (aggregation_query_client , nested_query ):
97145 query = nested_query
98146
@@ -121,41 +169,60 @@ def test_aggregation_query_multiple_aggregations(
121169 aggregation_query = aggregation_query_client .aggregation_query (query )
122170 aggregation_query .count (alias = "total" )
123171 aggregation_query .count (alias = "all" )
172+ aggregation_query .sum ("person" , alias = "sum_person" )
173+ aggregation_query .avg ("person" , alias = "avg_person" )
124174 result = _do_fetch (aggregation_query )
125175 assert len (result ) == 1
126176 for r in result [0 ]:
127- assert r .alias in ["all" , "total" ]
177+ assert r .alias in ["all" , "total" , "sum_person" , "avg_person" ]
128178 assert r .value > 0
129179
130180
131181def test_aggregation_query_add_aggregation (aggregation_query_client , nested_query ):
132182 from google .cloud .datastore .aggregation import CountAggregation
183+ from google .cloud .datastore .aggregation import SumAggregation
184+ from google .cloud .datastore .aggregation import AvgAggregation
133185
134186 query = nested_query
135187
136188 aggregation_query = aggregation_query_client .aggregation_query (query )
137189 count_aggregation = CountAggregation (alias = "total" )
138190 aggregation_query .add_aggregation (count_aggregation )
191+
192+ sum_aggregation = SumAggregation ("person" , alias = "sum_person" )
193+ aggregation_query .add_aggregation (sum_aggregation )
194+
195+ avg_aggregation = AvgAggregation ("person" , alias = "avg_person" )
196+ aggregation_query .add_aggregation (avg_aggregation )
197+
139198 result = _do_fetch (aggregation_query )
140199 assert len (result ) == 1
141200 for r in result [0 ]:
142- assert r .alias == "total"
201+ assert r .alias in [ "total" , "sum_person" , "avg_person" ]
143202 assert r .value > 0
144203
145204
146205def test_aggregation_query_add_aggregations (aggregation_query_client , nested_query ):
147- from google .cloud .datastore .aggregation import CountAggregation
206+ from google .cloud .datastore .aggregation import (
207+ CountAggregation ,
208+ SumAggregation ,
209+ AvgAggregation ,
210+ )
148211
149212 query = nested_query
150213
151214 aggregation_query = aggregation_query_client .aggregation_query (query )
152215 count_aggregation_1 = CountAggregation (alias = "total" )
153216 count_aggregation_2 = CountAggregation (alias = "all" )
154- aggregation_query .add_aggregations ([count_aggregation_1 , count_aggregation_2 ])
217+ sum_aggregation = SumAggregation ("person" , alias = "sum_person" )
218+ avg_aggregation = AvgAggregation ("person" , alias = "avg_person" )
219+ aggregation_query .add_aggregations (
220+ [count_aggregation_1 , count_aggregation_2 , sum_aggregation , avg_aggregation ]
221+ )
155222 result = _do_fetch (aggregation_query )
156223 assert len (result ) == 1
157224 for r in result [0 ]:
158- assert r .alias in ["total" , "all" ]
225+ assert r .alias in ["total" , "all" , "sum_person" , "avg_person" ]
159226 assert r .value > 0
160227
161228
@@ -202,11 +269,13 @@ def test_aggregation_query_with_nested_query_filtered(
202269
203270 aggregation_query = aggregation_query_client .aggregation_query (query )
204271 aggregation_query .count (alias = "total" )
272+ aggregation_query .sum ("person" , alias = "sum_person" )
273+ aggregation_query .avg ("person" , alias = "avg_person" )
205274 result = _do_fetch (aggregation_query )
206275 assert len (result ) == 1
207276
208277 for r in result [0 ]:
209- assert r .alias == "total"
278+ assert r .alias in [ "total" , "sum_person" , "avg_person" ]
210279 assert r .value == 6
211280
212281
@@ -226,9 +295,11 @@ def test_aggregation_query_with_nested_query_multiple_filters(
226295
227296 aggregation_query = aggregation_query_client .aggregation_query (query )
228297 aggregation_query .count (alias = "total" )
298+ aggregation_query .sum ("person" , alias = "sum_person" )
299+ aggregation_query .avg ("person" , alias = "avg_person" )
229300 result = _do_fetch (aggregation_query )
230301 assert len (result ) == 1
231302
232303 for r in result [0 ]:
233- assert r .alias == "total"
304+ assert r .alias in [ "total" , "sum_person" , "avg_person" ]
234305 assert r .value == 4
0 commit comments