Skip to content

Add more test cases for json_query function #25783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ public void testNullInput()
}

@Test
public void testDescendantMemberAccessor()
public void testDescendantMemberAccessorWithArrayWrapper()
{
assertThat(assertions.query(
"""
Expand All @@ -434,9 +434,58 @@ SELECT json_query(
'lax $..c'
WITH ARRAY WRAPPER)
"""))
.matches("""
VALUES cast('[[true,{"c":{"c":null}}],{"c":null},null]'AS varchar)
""");

// `json_query` must return a single JSON item.
// `WITH ARRAY WRAPPER` wraps `Diana` and `John` into a single JSON array, which can then be successfully returned.
assertThat(assertions.query(
"""
SELECT json_query(
'{"author":"Diana","id":{"value":1, "author":"John"},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
'lax $..author'
WITH ARRAY WRAPPER)
"""))
.matches(
"""
VALUES cast('[[true,{"c":{"c":null}}],{"c":null},null]'AS varchar)
VALUES cast('["Diana","John"]'AS varchar)
""");
}

@Test
public void testDescendantMemberAccessorWithoutArrayWrapper()
{
// Test `json_query` to find a non-existent member in the JSON.
assertThat(assertions.query(
"""
select json_query(
'{"id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
'lax $..author'
omit quotes)
"""))
.matches("VALUES cast(NULL as varchar)");

// Test `json_query` to find a single descendant member in the JSON.
assertThat(assertions.query(
"""
select json_query(
'{"author":"Diana","id":{"value":1},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
'lax $..author'
omit quotes)
"""))
.matches("VALUES cast('Diana' as varchar)");

// Test `json_query` to find multiple descendant members in the JSON.
// `json_query` must return a single JSON item.
// `WITHOUT ARRAY WRAPPER` returns `NULL`, which is the default result in case of an error.
assertThat(assertions.query(
"""
select json_query(
'{"author":"Diana","id":{"value":1, "author":"John"},"notes":[{"type":1,"comment":"foo"},{"type":2,"comment":null}],"comment":["bar","baz"]}',
'lax $..author'
omit quotes)
"""))
.matches("VALUES cast(NULL as varchar)");
}
}