mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-25 18:55:31 -06:00
Add Reverse Engineered SQL tests for Rules. Fixes #4600
This commit is contained in:
parent
41e2a674e3
commit
094e5577e8
@ -13,6 +13,7 @@ New features
|
|||||||
Housekeeping
|
Housekeeping
|
||||||
************
|
************
|
||||||
|
|
||||||
|
| `Issue #4600 <https://redmine.postgresql.org/issues/4600>`_ - Add Reverse Engineered SQL tests for Rules.
|
||||||
|
|
||||||
Bug fixes
|
Bug fixes
|
||||||
*********
|
*********
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
-- Rule: "test_delete_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule
|
||||||
|
|
||||||
|
-- DROP Rule "test_delete_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule;
|
||||||
|
|
||||||
|
CREATE OR REPLACE RULE "test_delete_rule1_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON DELETE TO public.test_emp_rule
|
||||||
|
DO INSTEAD
|
||||||
|
DELETE FROM test_emp_rule
|
||||||
|
WHERE test_emp_rule.name = old.name;
|
||||||
|
|
||||||
|
COMMENT ON RULE "test_delete_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule IS 'This is a delete rule';
|
@ -0,0 +1,9 @@
|
|||||||
|
ALTER RULE "test_delete_rule_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule RENAME TO "test_delete_rule1_$%{}[]()&*^!@""'`\/#";
|
||||||
|
|
||||||
|
CREATE OR REPLACE RULE "test_delete_rule1_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON DELETE TO public.test_emp_rule
|
||||||
|
DO INSTEAD
|
||||||
|
DELETE FROM test_emp_rule
|
||||||
|
WHERE test_emp_rule.name = old.name;
|
||||||
|
|
||||||
|
COMMENT ON RULE "test_delete_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule IS 'This is a delete rule';
|
@ -0,0 +1,12 @@
|
|||||||
|
-- Rule: "test_insert_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule
|
||||||
|
|
||||||
|
-- DROP Rule "test_insert_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule;
|
||||||
|
|
||||||
|
CREATE OR REPLACE RULE "test_insert_rule1_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON INSERT TO public.test_emp_rule
|
||||||
|
WHERE new.salary > 8000
|
||||||
|
DO INSTEAD
|
||||||
|
UPDATE test_emp_rule SET salary = 8000
|
||||||
|
WHERE test_emp_rule.emp_id = new.emp_id;
|
||||||
|
|
||||||
|
COMMENT ON RULE "test_insert_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule IS 'This is a insert rule';
|
@ -0,0 +1,10 @@
|
|||||||
|
ALTER RULE "test_insert_rule_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule RENAME TO "test_insert_rule1_$%{}[]()&*^!@""'`\/#";
|
||||||
|
|
||||||
|
CREATE OR REPLACE RULE "test_insert_rule1_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON INSERT TO public.test_emp_rule
|
||||||
|
WHERE new.salary > 8000
|
||||||
|
DO INSTEAD
|
||||||
|
UPDATE test_emp_rule SET salary = 8000
|
||||||
|
WHERE test_emp_rule.emp_id = new.emp_id;
|
||||||
|
|
||||||
|
COMMENT ON RULE "test_insert_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule IS 'This is a insert rule';
|
@ -0,0 +1,12 @@
|
|||||||
|
-- Rule: "test_update_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule
|
||||||
|
|
||||||
|
-- DROP Rule "test_update_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule;
|
||||||
|
|
||||||
|
CREATE OR REPLACE RULE "test_update_rule1_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON UPDATE TO public.test_emp_rule
|
||||||
|
WHERE old.name = 'Sam'::text
|
||||||
|
DO INSTEAD
|
||||||
|
UPDATE test_emp_rule SET salary = new.salary
|
||||||
|
WHERE test_emp_rule.name = 'Joe'::text;
|
||||||
|
|
||||||
|
COMMENT ON RULE "test_update_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule IS 'This is a update rule';
|
@ -0,0 +1,10 @@
|
|||||||
|
ALTER RULE "test_update_rule_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule RENAME TO "test_update_rule1_$%{}[]()&*^!@""'`\/#";
|
||||||
|
|
||||||
|
CREATE OR REPLACE RULE "test_update_rule1_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON UPDATE TO public.test_emp_rule
|
||||||
|
WHERE old.name = 'Sam'
|
||||||
|
DO INSTEAD
|
||||||
|
UPDATE test_emp_rule SET salary = new.salary
|
||||||
|
WHERE test_emp_rule.name = 'Joe';
|
||||||
|
|
||||||
|
COMMENT ON RULE "test_update_rule1_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule IS 'This is a update rule';
|
@ -0,0 +1,8 @@
|
|||||||
|
-- Rule: "test_delete_rule_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule
|
||||||
|
|
||||||
|
-- DROP Rule "test_delete_rule_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule;
|
||||||
|
|
||||||
|
CREATE OR REPLACE RULE "test_delete_rule_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON DELETE TO public.test_emp_rule
|
||||||
|
DO
|
||||||
|
NOTHING;
|
@ -0,0 +1,4 @@
|
|||||||
|
CREATE OR REPLACE RULE "test_delete_rule_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON DELETE TO public.test_emp_rule
|
||||||
|
DO
|
||||||
|
NOTHING;
|
@ -0,0 +1,10 @@
|
|||||||
|
-- Rule: "test_insert_rule_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule
|
||||||
|
|
||||||
|
-- DROP Rule "test_insert_rule_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule;
|
||||||
|
|
||||||
|
CREATE OR REPLACE RULE "test_insert_rule_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON INSERT TO public.test_emp_rule
|
||||||
|
WHERE new.salary > 5000
|
||||||
|
DO
|
||||||
|
UPDATE test_emp_rule SET salary = 5000
|
||||||
|
WHERE test_emp_rule.emp_id = new.emp_id;
|
@ -0,0 +1,6 @@
|
|||||||
|
CREATE OR REPLACE RULE "test_insert_rule_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON INSERT TO public.test_emp_rule
|
||||||
|
WHERE new.salary > 5000
|
||||||
|
DO
|
||||||
|
UPDATE test_emp_rule SET salary = 5000
|
||||||
|
WHERE test_emp_rule.emp_id = new.emp_id;
|
@ -0,0 +1,10 @@
|
|||||||
|
-- Rule: "test_update_rule_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule
|
||||||
|
|
||||||
|
-- DROP Rule "test_update_rule_$%{}[]()&*^!@""'`\/#" ON public.test_emp_rule;
|
||||||
|
|
||||||
|
CREATE OR REPLACE RULE "test_update_rule_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON UPDATE TO public.test_emp_rule
|
||||||
|
WHERE old.name = 'Joe'::text
|
||||||
|
DO
|
||||||
|
UPDATE test_emp_rule SET salary = new.salary
|
||||||
|
WHERE test_emp_rule.name = 'Sam'::text;
|
@ -0,0 +1,6 @@
|
|||||||
|
CREATE OR REPLACE RULE "test_update_rule_$%{}[]()&*^!@""'`\/#" AS
|
||||||
|
ON UPDATE TO public.test_emp_rule
|
||||||
|
WHERE old.name = 'Joe'
|
||||||
|
DO
|
||||||
|
UPDATE test_emp_rule SET salary = new.salary
|
||||||
|
WHERE test_emp_rule.name = 'Sam';
|
@ -0,0 +1,148 @@
|
|||||||
|
{
|
||||||
|
"scenarios": [
|
||||||
|
{
|
||||||
|
"type": "create",
|
||||||
|
"name": "Create Table For Rule",
|
||||||
|
"endpoint": "NODE-table.obj",
|
||||||
|
"sql_endpoint": "NODE-table.sql_id",
|
||||||
|
"data": {
|
||||||
|
"name": "test_emp_rule",
|
||||||
|
"columns": [{
|
||||||
|
"name": "emp_id",
|
||||||
|
"cltype": "integer",
|
||||||
|
"is_primary_key": true
|
||||||
|
}, {
|
||||||
|
"name": "name",
|
||||||
|
"cltype": "text"
|
||||||
|
}, {
|
||||||
|
"name": "salary",
|
||||||
|
"cltype": "bigint"
|
||||||
|
}],
|
||||||
|
"is_partitioned": false,
|
||||||
|
"schema": "public",
|
||||||
|
"spcname": "pg_default"
|
||||||
|
},
|
||||||
|
"store_table_id": true
|
||||||
|
}, {
|
||||||
|
"type": "create",
|
||||||
|
"name": "Create Rule for insert event",
|
||||||
|
"endpoint": "NODE-rule.obj",
|
||||||
|
"sql_endpoint": "NODE-rule.sql_id",
|
||||||
|
"msql_endpoint": "NODE-rule.msql",
|
||||||
|
"data": {
|
||||||
|
"name": "test_insert_rule_$%{}[]()&*^!@\"'`\\/#",
|
||||||
|
"schema": "public",
|
||||||
|
"view": "test_emp_rule",
|
||||||
|
"event": "Insert",
|
||||||
|
"condition": "new.salary > 5000",
|
||||||
|
"statements": "UPDATE test_emp_rule SET salary = 5000\n WHERE test_emp_rule.emp_id = new.emp_id"
|
||||||
|
},
|
||||||
|
"expected_sql_file": "create_insert_event_rule.sql",
|
||||||
|
"expected_msql_file": "create_insert_event_rule_msql.sql"
|
||||||
|
}, {
|
||||||
|
"type": "alter",
|
||||||
|
"name": "Alter Rule for insert event",
|
||||||
|
"endpoint": "NODE-rule.obj_id",
|
||||||
|
"sql_endpoint": "NODE-rule.sql_id",
|
||||||
|
"msql_endpoint": "NODE-rule.msql_id",
|
||||||
|
"data": {
|
||||||
|
"name": "test_insert_rule1_$%{}[]()&*^!@\"'`\\/#",
|
||||||
|
"schema": "public",
|
||||||
|
"view": "test_emp_rule",
|
||||||
|
"event": "Insert",
|
||||||
|
"do_instead": true,
|
||||||
|
"comment": "This is a insert rule",
|
||||||
|
"condition": "new.salary > 8000",
|
||||||
|
"statements": "UPDATE test_emp_rule SET salary = 8000\n\tWHERE test_emp_rule.emp_id = new.emp_id"
|
||||||
|
},
|
||||||
|
"expected_sql_file": "alter_insert_event_rule.sql",
|
||||||
|
"expected_msql_file": "alter_insert_event_rule_msql.sql"
|
||||||
|
}, {
|
||||||
|
"type": "delete",
|
||||||
|
"name": "Drop Rule",
|
||||||
|
"endpoint": "NODE-rule.delete_id",
|
||||||
|
"data": {
|
||||||
|
"name": "test_insert_rule1_$%{}[]()&*^!@\"'`\\/#"
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
"type": "create",
|
||||||
|
"name": "Create Rule for update event",
|
||||||
|
"endpoint": "NODE-rule.obj",
|
||||||
|
"sql_endpoint": "NODE-rule.sql_id",
|
||||||
|
"msql_endpoint": "NODE-rule.msql",
|
||||||
|
"data": {
|
||||||
|
"name": "test_update_rule_$%{}[]()&*^!@\"'`\\/#",
|
||||||
|
"schema": "public",
|
||||||
|
"view": "test_emp_rule",
|
||||||
|
"event": "Update",
|
||||||
|
"condition": "old.name = 'Joe'",
|
||||||
|
"statements": "UPDATE test_emp_rule SET salary = new.salary\n WHERE test_emp_rule.name = 'Sam'"
|
||||||
|
},
|
||||||
|
"expected_sql_file": "create_update_event_rule.sql",
|
||||||
|
"expected_msql_file": "create_update_event_rule_msql.sql"
|
||||||
|
}, {
|
||||||
|
"type": "alter",
|
||||||
|
"name": "Alter Rule for update event",
|
||||||
|
"endpoint": "NODE-rule.obj_id",
|
||||||
|
"sql_endpoint": "NODE-rule.sql_id",
|
||||||
|
"msql_endpoint": "NODE-rule.msql_id",
|
||||||
|
"data": {
|
||||||
|
"name": "test_update_rule1_$%{}[]()&*^!@\"'`\\/#",
|
||||||
|
"schema": "public",
|
||||||
|
"view": "test_emp_rule",
|
||||||
|
"event": "Update",
|
||||||
|
"do_instead": true,
|
||||||
|
"comment": "This is a update rule",
|
||||||
|
"condition": "old.name = 'Sam'",
|
||||||
|
"statements": "UPDATE test_emp_rule SET salary = new.salary\n\tWHERE test_emp_rule.name = 'Joe'"
|
||||||
|
},
|
||||||
|
"expected_sql_file": "alter_update_event_rule.sql",
|
||||||
|
"expected_msql_file": "alter_update_event_rule_msql.sql"
|
||||||
|
}, {
|
||||||
|
"type": "delete",
|
||||||
|
"name": "Drop Rule",
|
||||||
|
"endpoint": "NODE-rule.delete_id",
|
||||||
|
"data": {
|
||||||
|
"name": "test_update_rule1_$%{}[]()&*^!@\"'`\\/#"
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
"type": "create",
|
||||||
|
"name": "Create Rule for delete event",
|
||||||
|
"endpoint": "NODE-rule.obj",
|
||||||
|
"sql_endpoint": "NODE-rule.sql_id",
|
||||||
|
"msql_endpoint": "NODE-rule.msql",
|
||||||
|
"data": {
|
||||||
|
"name": "test_delete_rule_$%{}[]()&*^!@\"'`\\/#",
|
||||||
|
"schema": "public",
|
||||||
|
"view": "test_emp_rule",
|
||||||
|
"event": "Delete"
|
||||||
|
},
|
||||||
|
"expected_sql_file": "create_delete_event_rule.sql",
|
||||||
|
"expected_msql_file": "create_delete_event_rule_msql.sql"
|
||||||
|
}, {
|
||||||
|
"type": "alter",
|
||||||
|
"name": "Alter Rule for delete event",
|
||||||
|
"endpoint": "NODE-rule.obj_id",
|
||||||
|
"sql_endpoint": "NODE-rule.sql_id",
|
||||||
|
"msql_endpoint": "NODE-rule.msql_id",
|
||||||
|
"data": {
|
||||||
|
"name": "test_delete_rule1_$%{}[]()&*^!@\"'`\\/#",
|
||||||
|
"schema": "public",
|
||||||
|
"view": "test_emp_rule",
|
||||||
|
"event": "Delete",
|
||||||
|
"do_instead": true,
|
||||||
|
"comment": "This is a delete rule",
|
||||||
|
"statements": "DELETE FROM test_emp_rule\n WHERE test_emp_rule.name = old.name;"
|
||||||
|
},
|
||||||
|
"expected_sql_file": "alter_delete_event_rule.sql",
|
||||||
|
"expected_msql_file": "alter_delete_event_rule_msql.sql"
|
||||||
|
}, {
|
||||||
|
"type": "delete",
|
||||||
|
"name": "Drop Rule",
|
||||||
|
"endpoint": "NODE-rule.delete_id",
|
||||||
|
"data": {
|
||||||
|
"name": "test_delete_rule1_$%{}[]()&*^!@\"'`\\/#"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -5,7 +5,8 @@
|
|||||||
{% set rule_name = o_data.name %}
|
{% set rule_name = o_data.name %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if data.name and data.name != o_data.name %}
|
{% if data.name and data.name != o_data.name %}
|
||||||
ALTER RULE {{ conn|qtIdent(o_data.name) }} ON {{ conn|qtIdent(o_data.schema, o_data.view) }} RENAME TO {{ conn|qtIdent(data.name) }};{{ '\r\r' }}
|
ALTER RULE {{ conn|qtIdent(o_data.name) }} ON {{ conn|qtIdent(o_data.schema, o_data.view) }} RENAME TO {{ conn|qtIdent(data.name) }};
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if data.event or data.do_instead is defined or data.condition or data.statements %}
|
{% if data.event or data.do_instead is defined or data.condition or data.statements %}
|
||||||
CREATE OR REPLACE RULE {{ conn|qtIdent(rule_name) }} AS
|
CREATE OR REPLACE RULE {{ conn|qtIdent(rule_name) }} AS
|
||||||
|
@ -194,6 +194,18 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator):
|
|||||||
"... skipped (pre-condition SQL not satisfied)")
|
"... skipped (pre-condition SQL not satisfied)")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# If msql_endpoint exists then validate the modified sql
|
||||||
|
if 'msql_endpoint' in scenario\
|
||||||
|
and scenario['msql_endpoint']:
|
||||||
|
if not self.check_msql(scenario, object_id):
|
||||||
|
print_msg = scenario['name']
|
||||||
|
if 'expected_msql_file' in scenario:
|
||||||
|
print_msg += " Expected MSQL File:" + scenario[
|
||||||
|
'expected_msql_file']
|
||||||
|
print_msg = print_msg + "... FAIL"
|
||||||
|
print(print_msg)
|
||||||
|
continue
|
||||||
|
|
||||||
if 'type' in scenario and scenario['type'] == 'create':
|
if 'type' in scenario and scenario['type'] == 'create':
|
||||||
# Get the url and create the specific node.
|
# Get the url and create the specific node.
|
||||||
|
|
||||||
@ -253,18 +265,6 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator):
|
|||||||
elif 'type' in scenario and scenario['type'] == 'alter':
|
elif 'type' in scenario and scenario['type'] == 'alter':
|
||||||
# Get the url and create the specific node.
|
# Get the url and create the specific node.
|
||||||
|
|
||||||
# If msql_endpoint exists then validate the modified sql
|
|
||||||
if 'msql_endpoint' in scenario\
|
|
||||||
and scenario['msql_endpoint']:
|
|
||||||
if not self.check_msql(scenario, object_id):
|
|
||||||
print_msg = scenario['name']
|
|
||||||
if 'expected_msql_file' in scenario:
|
|
||||||
print_msg += " Expected MSQL File:" + scenario[
|
|
||||||
'expected_msql_file']
|
|
||||||
print_msg = print_msg + "... FAIL"
|
|
||||||
print(print_msg)
|
|
||||||
continue
|
|
||||||
|
|
||||||
alter_url = self.get_url(scenario['endpoint'], object_id)
|
alter_url = self.get_url(scenario['endpoint'], object_id)
|
||||||
response = self.tester.put(alter_url,
|
response = self.tester.put(alter_url,
|
||||||
data=json.dumps(scenario['data']),
|
data=json.dumps(scenario['data']),
|
||||||
@ -397,7 +397,7 @@ class ReverseEngineeredSQLTestCases(BaseTestGenerator):
|
|||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
self.assertFalse("Expected SQL File not found")
|
self.assertFalse("Expected Modified SQL File not found")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.final_test_status = False
|
self.final_test_status = False
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
Loading…
Reference in New Issue
Block a user