summaryrefslogtreecommitdiff
path: root/test/dialect/postgresql
diff options
context:
space:
mode:
authorFederico Caselli <cfederico87@gmail.com>2022-09-17 13:12:35 +0200
committerFederico Caselli <cfederico87@gmail.com>2022-09-17 13:12:35 +0200
commit02fe382d6bfc5e8ccab6e2024a5241379a02b7e0 (patch)
tree71be66e11aec5224f77f8e4e4e62ad9bd3fb6491 /test/dialect/postgresql
parentf582618afe1a5b112a1a22ddd0cbfcc8b97c8f09 (diff)
downloadsqlalchemy-02fe382d6bfc5e8ccab6e2024a5241379a02b7e0.tar.gz
Improve array_agg and Array processing
The :class:`_functions.array_agg` will now set the array dimensions to 1. Improved :class:`_types.ARRAY` processing to accept ``None`` values as value of a multi-array. Fixes: #7083 Change-Id: Iafec4f77fde9719ccc7c8535bf6235dbfbc62102
Diffstat (limited to 'test/dialect/postgresql')
-rw-r--r--test/dialect/postgresql/test_types.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py
index 92fcfbcab..b5c20bd8d 100644
--- a/test/dialect/postgresql/test_types.py
+++ b/test/dialect/postgresql/test_types.py
@@ -36,6 +36,7 @@ from sqlalchemy import types
from sqlalchemy import Unicode
from sqlalchemy import util
from sqlalchemy.dialects import postgresql
+from sqlalchemy.dialects.postgresql import aggregate_order_by
from sqlalchemy.dialects.postgresql import array
from sqlalchemy.dialects.postgresql import DATEMULTIRANGE
from sqlalchemy.dialects.postgresql import DATERANGE
@@ -1901,6 +1902,48 @@ class ArrayRoundTripTest:
stmt = select(func.array_agg(values_table.c.value)[2:4])
eq_(connection.execute(stmt).scalar(), [2, 3, 4])
+ def test_array_agg_json(self, metadata, connection):
+ table = Table(
+ "values", metadata, Column("id", Integer), Column("bar", JSON)
+ )
+ metadata.create_all(connection)
+ connection.execute(
+ table.insert(),
+ [{"id": 1, "bar": [{"buz": 1}]}, {"id": 2, "bar": None}],
+ )
+
+ arg = aggregate_order_by(table.c.bar, table.c.id)
+ stmt = select(sa.func.array_agg(arg))
+ eq_(connection.execute(stmt).scalar(), [[{"buz": 1}], None])
+
+ arg = aggregate_order_by(table.c.bar, table.c.id.desc())
+ stmt = select(sa.func.array_agg(arg))
+ eq_(connection.execute(stmt).scalar(), [None, [{"buz": 1}]])
+
+ @testing.combinations(ARRAY, postgresql.ARRAY, argnames="cls")
+ def test_array_none(self, connection, metadata, cls):
+ table = Table(
+ "values", metadata, Column("id", Integer), Column("bar", cls(JSON))
+ )
+ metadata.create_all(connection)
+ connection.execute(
+ table.insert().values(
+ [
+ {
+ "id": 1,
+ "bar": sa.text("""array['[{"x": 1}]'::json, null]"""),
+ },
+ {"id": 2, "bar": None},
+ ]
+ )
+ )
+
+ stmt = select(table.c.bar).order_by(table.c.id)
+ eq_(connection.scalars(stmt).all(), [[[{"x": 1}], None], None])
+
+ stmt = select(table.c.bar).order_by(table.c.id.desc())
+ eq_(connection.scalars(stmt).all(), [None, [[{"x": 1}], None]])
+
def test_array_index_slice_exprs(self, connection):
"""test a variety of expressions that sometimes need parenthesizing"""