Skip to content

Add more test cases for json_query function #25650

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -439,4 +439,50 @@ SELECT json_query(
VALUES cast('[[true,{"c":{"c":null}}],{"c":null},null]'AS varchar)
""");
}

@Test
public void testDescendantMemberWithoutArrayWrapperAccessor()
{
// Test `json_query` to read the result of a non-descendant 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 read the result of 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 read the result of multiple descendant members in the JSON.
// `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.
// `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)
""");
}
}