From 87f84110ba15ab8a93c04fdd95f69904ba9f3273 Mon Sep 17 00:00:00 2001 From: Daniel Townsend Date: Wed, 23 Oct 2024 20:46:51 +0100 Subject: [PATCH] add basic operator tests --- tests/query/operators/test_json.py | 51 ++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/tests/query/operators/test_json.py b/tests/query/operators/test_json.py index a6bb9a6c..d7840ef9 100644 --- a/tests/query/operators/test_json.py +++ b/tests/query/operators/test_json.py @@ -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 @@ -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"]])