|
22 | 22 |
|
23 | 23 | from .conftest import setup_table |
24 | 24 | from .conftest import sqlalchemy_1_4_or_higher, sqlalchemy_before_1_4 |
| 25 | +from sqlalchemy.sql.functions import rollup, cube, grouping_sets |
25 | 26 |
|
26 | 27 |
|
27 | 28 | def test_constraints_are_ignored(faux_conn, metadata): |
@@ -278,3 +279,66 @@ def test_no_implicit_join_for_inner_unnest_no_table2_column(faux_conn, metadata) |
278 | 279 | ) |
279 | 280 | found_outer_sql = q.compile(faux_conn).string |
280 | 281 | assert found_outer_sql == expected_outer_sql |
| 282 | + |
| 283 | + |
| 284 | +def test_grouping_sets(faux_conn, metadata): |
| 285 | + table = setup_table( |
| 286 | + faux_conn, |
| 287 | + "table1", |
| 288 | + metadata, |
| 289 | + sqlalchemy.Column("foo", sqlalchemy.Integer), |
| 290 | + sqlalchemy.Column("bar", sqlalchemy.Integer), |
| 291 | + ) |
| 292 | + |
| 293 | + q = sqlalchemy.select(table.c.foo, table.c.bar).group_by( |
| 294 | + grouping_sets(table.c.foo, table.c.bar) |
| 295 | + ) |
| 296 | + |
| 297 | + expected_sql = ( |
| 298 | + "SELECT `table1`.`foo`, `table1`.`bar` \n" |
| 299 | + "FROM `table1` GROUP BY GROUPING SETS ((`table1`.`foo`), (`table1`.`bar`))" |
| 300 | + ) |
| 301 | + found_sql = q.compile(faux_conn).string |
| 302 | + assert found_sql == expected_sql |
| 303 | + |
| 304 | + |
| 305 | +def test_rollup(faux_conn, metadata): |
| 306 | + table = setup_table( |
| 307 | + faux_conn, |
| 308 | + "table1", |
| 309 | + metadata, |
| 310 | + sqlalchemy.Column("foo", sqlalchemy.Integer), |
| 311 | + sqlalchemy.Column("bar", sqlalchemy.Integer), |
| 312 | + ) |
| 313 | + |
| 314 | + q = sqlalchemy.select(table.c.foo, table.c.bar).group_by( |
| 315 | + rollup(table.c.foo, table.c.bar) |
| 316 | + ) |
| 317 | + |
| 318 | + expected_sql = ( |
| 319 | + "SELECT `table1`.`foo`, `table1`.`bar` \n" |
| 320 | + "FROM `table1` GROUP BY ROLLUP(`table1`.`foo`, `table1`.`bar`)" |
| 321 | + ) |
| 322 | + found_sql = q.compile(faux_conn).string |
| 323 | + assert found_sql == expected_sql |
| 324 | + |
| 325 | + |
| 326 | +def test_cube(faux_conn, metadata): |
| 327 | + table = setup_table( |
| 328 | + faux_conn, |
| 329 | + "table1", |
| 330 | + metadata, |
| 331 | + sqlalchemy.Column("foo", sqlalchemy.Integer), |
| 332 | + sqlalchemy.Column("bar", sqlalchemy.Integer), |
| 333 | + ) |
| 334 | + |
| 335 | + q = sqlalchemy.select(table.c.foo, table.c.bar).group_by( |
| 336 | + cube(table.c.foo, table.c.bar) |
| 337 | + ) |
| 338 | + |
| 339 | + expected_sql = ( |
| 340 | + "SELECT `table1`.`foo`, `table1`.`bar` \n" |
| 341 | + "FROM `table1` GROUP BY CUBE(`table1`.`foo`, `table1`.`bar`)" |
| 342 | + ) |
| 343 | + found_sql = q.compile(faux_conn).string |
| 344 | + assert found_sql == expected_sql |
0 commit comments