Skip to content

Commit

Permalink
add test for nested arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Oct 20, 2024
1 parent 976f3ef commit fe2ce3d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions piccolo/query/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from .aggregate import Avg, Count, Max, Min, Sum
from .datetime import Day, Extract, Hour, Month, Second, Strftime, Year
from .json import Arrow
from .math import Abs, Ceil, Floor, Round
from .string import Concat, Length, Lower, Ltrim, Reverse, Rtrim, Upper
from .type_conversion import Cast

__all__ = (
"Abs",
"Arrow",
"Avg",
"Cast",
"Ceil",
Expand Down
7 changes: 6 additions & 1 deletion piccolo/query/functions/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ class Arrow(QueryString):
``.output(load_json=True)``.
"""

def __init__(self, column: JSONB, key: str, alias: t.Optional[str] = None):
def __init__(
self,
column: JSONB | QueryString,
key: str,
alias: t.Optional[str] = None,
):
super().__init__("{} -> {}", column, key, alias=alias)

def clean_value(self, value: t.Any):
Expand Down
25 changes: 25 additions & 0 deletions tests/query/functions/test_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from piccolo.columns import JSONB
from piccolo.query.functions.json import Arrow
from piccolo.table import Table
from piccolo.testing.test_case import AsyncTableTest


class RecordingStudio(Table):
facilities = JSONB(null=True)


class TestArrow(AsyncTableTest):

tables = [RecordingStudio]

async def test_nested(self):
await RecordingStudio(
{RecordingStudio.facilities: {"a": {"b": {"c": 1}}}}
).save()

response = await RecordingStudio.select(
Arrow(Arrow(RecordingStudio.facilities, "a"), "b").as_alias(
"b_value"
)
)
self.assertListEqual(response, [{"b_value": '{"c": 1}'}])

0 comments on commit fe2ce3d

Please sign in to comment.