Skip to content

Commit

Permalink
add basic operator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dantownsend committed Oct 23, 2024
1 parent 0b760e2 commit 87f8411
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions tests/query/operators/test_json.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from unittest import TestCase

from piccolo.columns import JSONB
from piccolo.query.operators.json import GetChildElement
from piccolo.query.operators.json import GetChildElement, GetElementFromPath
from piccolo.table import Table
from piccolo.testing.test_case import AsyncTableTest
from tests.base import engines_skip


Expand All @@ -10,18 +11,42 @@ class RecordingStudio(Table):


@engines_skip("sqlite")
class TestArrow(AsyncTableTest):
class TestGetChildElement(TestCase):

def test_query(self):
"""
Make sure the generated SQL looks correct.
"""
querystring = GetChildElement(
GetChildElement(RecordingStudio.facilities, "a"), "b"
)

sql, query_args = querystring.compile_string()

self.assertEqual(
sql,
'"recording_studio"."facilities" -> $1 -> $2',
)

self.assertListEqual(query_args, ["a", "b"])


@engines_skip("sqlite")
class TestGetElementFromPath(TestCase):

tables = [RecordingStudio]
def test_query(self):
"""
Make sure the generated SQL looks correct.
"""
querystring = GetElementFromPath(
RecordingStudio.facilities, ["a", "b"]
)

async def test_nested(self):
await RecordingStudio(
{RecordingStudio.facilities: {"a": {"b": {"c": 1}}}}
).save()
sql, query_args = querystring.compile_string()

response = await RecordingStudio.select(
GetChildElement(
GetChildElement(RecordingStudio.facilities, "a"), "b"
).as_alias("b_value")
self.assertEqual(
sql,
'"recording_studio"."facilities" #> $1',
)
self.assertListEqual(response, [{"b_value": '{"c": 1}'}])

self.assertListEqual(query_args, [["a", "b"]])

0 comments on commit 87f8411

Please sign in to comment.