-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_cursor_aggregate.py
More file actions
329 lines (261 loc) · 11 KB
/
test_cursor_aggregate.py
File metadata and controls
329 lines (261 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# -*- coding: utf-8 -*-
import json
from pymongosql.result_set import ResultSet
class TestCursorAggregate:
"""Test aggregate function execution with real MongoDB data"""
def test_aggregate_qualified_basic_execution(self, conn):
"""Test executing qualified aggregate call: collection.aggregate('pipeline', 'options')"""
sql = """
SELECT *
FROM users.aggregate('[{"$match": {"age": {"$gt": 25}}}]', '{}')
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
assert isinstance(cursor.result_set, ResultSet)
rows = cursor.result_set.fetchall()
assert len(rows) > 0 # Should have users over 25
assert len(rows) == 19 # Expected count from test data
def test_aggregate_unqualified_group_execution(self, conn):
"""Test executing unqualified aggregate: aggregate('pipeline', 'options')"""
# This requires specifying collection at execution time or in a different way
# For now, test the qualified version which is more practical
pass
def test_aggregate_with_projection(self, conn):
"""Test aggregate with SELECT projection - should project specified fields"""
sql = """
SELECT name, age
FROM users.aggregate('[{"$match": {"active": true}}]', '{}')
LIMIT 5
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
assert isinstance(cursor.result_set, ResultSet)
# Check description has correct columns
col_names = [desc[0] for desc in cursor.result_set.description]
assert "name" in col_names
assert "age" in col_names
rows = cursor.result_set.fetchall()
assert len(rows) > 0
assert len(rows[0]) == 2 # Should have 2 columns (name, age)
def test_aggregate_with_nested_projection(self, conn):
"""Test aggregate with $project stage to validate nested structure projection (e.g., address.city)"""
pipeline = json.dumps(
[{"$match": {"active": True}}, {"$project": {"name": 1, "city": "$address.city", "age": 1}}]
)
sql = f"""
SELECT *
FROM users.aggregate('{pipeline}', '{{}}')
LIMIT 5
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
assert isinstance(cursor.result_set, ResultSet)
# Check description has correct columns including projected nested field
col_names = [desc[0] for desc in cursor.result_set.description]
assert "name" in col_names
assert "city" in col_names, "city field should be projected from address.city"
assert "age" in col_names
rows = cursor.result_set.fetchall()
assert len(rows) > 0
# Verify that nested city values are correctly returned
city_idx = col_names.index("city")
name_idx = col_names.index("name")
age_idx = col_names.index("age")
for row in rows:
city_value = row[city_idx]
# City should be a string value extracted from the nested address object
assert city_value is not None
assert isinstance(city_value, str)
assert len(city_value) > 0
# Verify other fields are also present
assert row[name_idx] is not None
assert row[age_idx] is not None
def test_aggregate_with_where_clause(self, conn):
"""Test aggregate pipeline combined with WHERE clause for additional filtering"""
sql = """
SELECT name, email, age
FROM users.aggregate('[{"$match": {"active": true}}]', '{}')
WHERE age > 30
LIMIT 10
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
assert isinstance(cursor.result_set, ResultSet)
rows = cursor.result_set.fetchall()
assert len(rows) > 0
# All returned rows should have age > 30
col_names = [desc[0] for desc in cursor.result_set.description]
age_idx = col_names.index("age")
for row in rows:
assert row[age_idx] > 30
def test_aggregate_with_sort_and_limit(self, conn):
"""Test aggregate with ORDER BY and LIMIT"""
sql = """
SELECT name, age
FROM users.aggregate('[{"$match": {"active": true}}]', '{}')
ORDER BY age DESC
LIMIT 5
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
rows = cursor.result_set.fetchall()
assert len(rows) == 5
# Verify ordering - each row should have age >= next row
col_names = [desc[0] for desc in cursor.result_set.description]
age_idx = col_names.index("age")
ages = [row[age_idx] for row in rows]
assert ages == sorted(ages, reverse=True)
def test_aggregate_products_group_by(self, conn):
"""Test aggregate with $group stage to group products"""
pipeline = json.dumps([{"$group": {"_id": "$category", "count": {"$sum": 1}, "avg_price": {"$avg": "$price"}}}])
sql = f"""
SELECT *
FROM products.aggregate('{pipeline}', '{{}}')
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
rows = cursor.result_set.fetchall()
# Should have results grouped by category
assert len(rows) > 0
def test_aggregate_orders_sum_amount(self, conn):
"""Test aggregate with $group to sum order amounts"""
pipeline = json.dumps(
[{"$group": {"_id": "$status", "total_amount": {"$sum": "$total"}, "order_count": {"$sum": 1}}}]
)
sql = f"""
SELECT *
FROM orders.aggregate('{pipeline}', '{{}}')
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
rows = cursor.result_set.fetchall()
# Should have grouped results by order status
assert len(rows) > 0
def test_aggregate_with_fetchone(self, conn):
"""Test aggregate query using fetchone instead of fetchall"""
sql = """
SELECT name, age
FROM users.aggregate('[{"$match": {"age": {"$gte": 20}}}]', '{}')
ORDER BY age DESC
"""
cursor = conn.cursor()
cursor.execute(sql)
# Get first row with fetchone
first_row = cursor.fetchone()
assert first_row is not None
assert len(first_row) == 2
# Should be oldest user
col_names = [desc[0] for desc in cursor.result_set.description]
age_idx = col_names.index("age")
first_age = first_row[age_idx]
# Get next few rows and verify age is descending
next_rows = cursor.fetchmany(3)
for row in next_rows:
assert row[age_idx] <= first_age
def test_aggregate_with_skip(self, conn):
"""Test aggregate with OFFSET (SKIP)"""
sql = """
SELECT name, email
FROM users.aggregate('[{"$match": {"active": true}}]', '{}')
ORDER BY name ASC
LIMIT 10 OFFSET 5
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
rows = cursor.result_set.fetchall()
# Should have some results (skipped first 5, limited to 10)
assert len(rows) > 0
assert len(rows) <= 10
def test_aggregate_cursor_rowcount(self, conn):
"""Test that cursor.rowcount reflects aggregate query results"""
sql = """
SELECT *
FROM users.aggregate('[{"$match": {"age": {"$gt": 25}}}]', '{}')
"""
cursor = conn.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
# rowcount should match the number of rows fetched
assert cursor.rowcount == len(rows)
def test_aggregate_with_field_alias(self, conn):
"""Test aggregate query with field aliases in projection"""
sql = """
SELECT name AS user_name, age AS user_age
FROM users.aggregate('[{"$match": {"active": true}}]', '{}')
LIMIT 3
"""
cursor = conn.cursor()
cursor.execute(sql)
# Check that aliases appear in description
col_names = [desc[0] for desc in cursor.result_set.description]
assert "user_name" in col_names
assert "user_age" in col_names
assert "name" not in col_names
assert "age" not in col_names
rows = cursor.result_set.fetchall()
assert len(rows) == 3
assert len(rows[0]) == 2
def test_aggregate_description_type_info(self, conn):
"""Test that cursor.description has proper DB API 2.0 format for aggregate queries"""
sql = """
SELECT name, age, email
FROM users.aggregate('[{"$match": {"active": true}}]', '{}')
LIMIT 1
"""
cursor = conn.cursor()
cursor.execute(sql)
# Verify description format
desc = cursor.description
assert isinstance(desc, list)
assert len(desc) == 3 # 3 columns
assert all(isinstance(d, tuple) and len(d) == 7 for d in desc)
assert all(isinstance(d[0], str) for d in desc) # Column names are strings
def test_aggregate_empty_result(self, conn):
"""Test aggregate query that returns no results"""
sql = """
SELECT *
FROM users.aggregate('[{"$match": {"age": {"$gt": 200}}}]', '{}')
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
rows = cursor.result_set.fetchall()
assert len(rows) == 0
def test_aggregate_multiple_stages(self, conn):
"""Test aggregate with multiple pipeline stages"""
pipeline = json.dumps(
[
{"$match": {"active": True}},
{"$group": {"_id": None, "avg_age": {"$avg": "$age"}, "count": {"$sum": 1}}},
{"$project": {"_id": 0, "average_age": "$avg_age", "total_users": "$count"}},
]
)
sql = f"""
SELECT *
FROM users.aggregate('{pipeline}', '{{}}')
"""
cursor = conn.cursor()
result = cursor.execute(sql)
assert result == cursor
rows = cursor.result_set.fetchall()
# Should have one row with aggregated stats
assert len(rows) == 1
row = rows[0]
# Verify projections defined in pipeline appear in the result
col_names = [desc[0] for desc in cursor.result_set.description]
assert "average_age" in col_names, "average_age should be in result columns"
assert "total_users" in col_names, "total_users should be in result columns"
assert "_id" not in col_names, "_id should be excluded from result columns"
# Verify the values are present and valid
avg_age_idx = col_names.index("average_age")
total_users_idx = col_names.index("total_users")
assert row[avg_age_idx] is not None and isinstance(row[avg_age_idx], (int, float))
assert row[total_users_idx] is not None and isinstance(row[total_users_idx], (int, float))