From cfae9c2eaf0020be8d8acbe104cb693e0fee0796 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 26 Aug 2015 15:15:45 -0400 Subject: - Added support for the SQL-standard function :class:`.array_agg`, which automatically returns an :class:`.Array` of the correct type and supports index / slice operations. As arrays are only supported on Postgresql at the moment, only actually works on Postgresql. fixes #3132 --- test/dialect/postgresql/test_types.py | 27 +++++++++++++++++++++++++++ test/sql/test_functions.py | 12 ++++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index 2d5c2aaa1..da45c2f2a 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -916,6 +916,33 @@ class ArrayRoundTripTest(fixtures.TablesTest, AssertsExecutionResults): assert isinstance(tbl.c.intarr.type.item_type, Integer) assert isinstance(tbl.c.strarr.type.item_type, String) + @testing.provide_metadata + def test_array_agg(self): + values_table = Table('values', self.metadata, Column('value', Integer)) + self.metadata.create_all(testing.db) + testing.db.execute( + values_table.insert(), + [{'value': i} for i in range(1, 10)] + ) + + stmt = select([func.array_agg(values_table.c.value)]) + eq_( + testing.db.execute(stmt).scalar(), + list(range(1, 10)) + ) + + stmt = select([func.array_agg(values_table.c.value)[3]]) + eq_( + testing.db.execute(stmt).scalar(), + 3 + ) + + stmt = select([func.array_agg(values_table.c.value)[2:4]]) + eq_( + testing.db.execute(stmt).scalar(), + [2, 3, 4] + ) + def test_array_index_slice_exprs(self): """test a variety of expressions that sometimes need parenthesizing""" diff --git a/test/sql/test_functions.py b/test/sql/test_functions.py index ccc9b2dcd..f080046ff 100644 --- a/test/sql/test_functions.py +++ b/test/sql/test_functions.py @@ -1,8 +1,8 @@ -from sqlalchemy.testing import eq_ +from sqlalchemy.testing import eq_, is_ import datetime from sqlalchemy import func, select, Integer, literal, DateTime, Table, \ Column, Sequence, MetaData, extract, Date, String, bindparam, \ - literal_column + literal_column, Array from sqlalchemy.sql import table, column from sqlalchemy import sql, util from sqlalchemy.sql.compiler import BIND_TEMPLATES @@ -489,6 +489,14 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): MissingType().compile ) + def test_array_agg(self): + m = MetaData() + t = Table('t', m, Column('data', Integer)) + expr = func.array_agg(t.c.data) + is_(expr.type._type_affinity, Array) + is_(expr.type.item_type._type_affinity, Integer) + + class ExecuteTest(fixtures.TestBase): -- cgit v1.2.1