From 62944580e1b38cc0be25f9289912abcbb3eb4b49 Mon Sep 17 00:00:00 2001 From: Brent Moran Date: Fri, 19 Jul 2024 18:12:46 +0800 Subject: [PATCH] add test for each leaf filter function --- db/sql/00_msar.sql | 2 +- db/sql/test_00_msar.sql | 190 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+), 1 deletion(-) diff --git a/db/sql/00_msar.sql b/db/sql/00_msar.sql index 2fe8b624ff..ec9703d74e 100644 --- a/db/sql/00_msar.sql +++ b/db/sql/00_msar.sql @@ -3309,7 +3309,7 @@ SELECT val; $$ LANGUAGE SQL RETURNS NULL ON NULL INPUT; -DROP TABLE msar.filter_templates; +DROP TABLE IF EXISTS msar.filter_templates; CREATE TABLE msar.filter_templates (filter_key text PRIMARY KEY, filter_template text); INSERT INTO msar.filter_templates VALUES -- basic composition operators diff --git a/db/sql/test_00_msar.sql b/db/sql/test_00_msar.sql index 0b1c9a4dd3..7b14574e07 100644 --- a/db/sql/test_00_msar.sql +++ b/db/sql/test_00_msar.sql @@ -2975,3 +2975,193 @@ BEGIN RETURN NEXT is(msar.build_order_by_expr(rel_id, null), null); END; $$ LANGUAGE plpgsql; + + +-- msar.build_filter_expr -------------------------------------------------------------------------- + +CREATE OR REPLACE FUNCTION test_build_filter_expr() RETURNS SETOF TEXT AS $$ +DECLARE + rel_id oid; +BEGIN + PERFORM __setup_list_records_table(); + rel_id := 'atable'::regclass::oid; + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'equal', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'col1 = ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'lesser', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'col1 < ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'greater', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'col1 > ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'lesser_or_equal', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'col1 <= ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'greater_or_equal', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'col1 >= ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'null', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2)))), + 'col1 IS NULL' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'not_null', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2)))), + 'col1 IS NOT NULL' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'contains_case_insensitive', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 'ABc')))), + 'col1 ILIKE ''%'' || ''ABc'' || ''%''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'starts_with_case_insensitive', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 'a''bc')))), + 'col1 ILIKE ''a''''bc'' || ''%''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'json_array_length_equals', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'jsonb_array_length(col1::jsonb) = ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'json_array_length_greater_than', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'jsonb_array_length(col1::jsonb) > ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'json_array_length_greater_or_equal', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'jsonb_array_length(col1::jsonb) >= ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'json_array_length_less_than', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'jsonb_array_length(col1::jsonb) < ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'json_array_length_less_or_equal', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'jsonb_array_length(col1::jsonb) <= ''500''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'json_array_not_empty', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 500)))), + 'jsonb_array_length(col1::jsonb) > 0' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'json_array_contains', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', '"500"')))), + 'col1 @> ''"500"''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'uri_scheme_equals', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 'https')))), + 'mathesar_types.uri_scheme(col1) = ''https''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'uri_authority_contains', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 'google')))), + 'mathesar_types.uri_authority(col1) LIKE ''%'' || ''google'' || ''%''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'email_domain_equals', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 'gmail.com')))), + 'mathesar_types.email_domain_name(col1) = ''gmail.com''' + ); + RETURN NEXT is( + msar.build_filter_expr( + rel_id, + jsonb_build_object( + 'type', 'email_domain_contains', 'args', jsonb_build_array( + jsonb_build_object('type', 'column_id', 'value', 2), + jsonb_build_object('type', 'literal', 'value', 'mail')))), + 'mathesar_types.email_domain_name(col1) LIKE ''%'' || ''mail'' || ''%''' + ); +END; +$$ LANGUAGE plpgsql;